Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 Ericsson | |
3 | * | |
4 | * All rights reserved. This program and the accompanying materials are | |
5 | * made available under the terms of the Eclipse Public License v1.0 which | |
6 | * accompanies this distribution, and is available at | |
7 | * http://www.eclipse.org/legal/epl-v10.html | |
8 | * | |
9 | * Contributors: | |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 | 14 | |
828e5592 | 15 | import java.io.ByteArrayInputStream; |
12c155f5 | 16 | import java.io.FileNotFoundException; |
828e5592 | 17 | import java.io.InputStream; |
12c155f5 FC |
18 | import java.util.List; |
19 | ||
20 | import org.eclipse.core.commands.AbstractHandler; | |
21 | import org.eclipse.core.commands.ExecutionEvent; | |
22 | import org.eclipse.core.commands.ExecutionException; | |
828e5592 PT |
23 | import org.eclipse.core.resources.IFile; |
24 | import org.eclipse.core.resources.IResource; | |
25 | import org.eclipse.core.runtime.CoreException; | |
12c155f5 | 26 | import org.eclipse.jface.viewers.ISelection; |
1595249b | 27 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 28 | import org.eclipse.jface.viewers.TreeSelection; |
6c13869b FC |
29 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
30 | import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment; | |
09d11238 PT |
31 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal; |
32 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; | |
6c13869b | 33 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
09d11238 | 34 | import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor; |
828e5592 PT |
35 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
36 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; | |
12c155f5 FC |
37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
39 | import org.eclipse.swt.widgets.MessageBox; | |
828e5592 PT |
40 | import org.eclipse.ui.IEditorInput; |
41 | import org.eclipse.ui.IEditorPart; | |
42 | import org.eclipse.ui.IReusableEditor; | |
43 | import org.eclipse.ui.IWorkbench; | |
12c155f5 FC |
44 | import org.eclipse.ui.IWorkbenchPage; |
45 | import org.eclipse.ui.IWorkbenchPart; | |
46 | import org.eclipse.ui.IWorkbenchWindow; | |
47 | import org.eclipse.ui.PlatformUI; | |
828e5592 | 48 | import org.eclipse.ui.ide.IDE; |
a1091415 | 49 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
50 | |
51 | /** | |
52 | * <b><u>OpenExperimentHandler</u></b> | |
53 | * <p> | |
12c155f5 FC |
54 | */ |
55 | public class OpenExperimentHandler extends AbstractHandler { | |
56 | ||
828e5592 PT |
57 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
58 | ||
12c155f5 FC |
59 | private TmfExperimentElement fExperiment = null; |
60 | ||
61 | // ------------------------------------------------------------------------ | |
62 | // Validation | |
63 | // ------------------------------------------------------------------------ | |
64 | ||
65 | @Override | |
66 | public boolean isEnabled() { | |
67 | ||
68 | // Check if we are closing down | |
69 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
70 | if (window == null) | |
71 | return false; | |
72 | ||
73 | // Get the selection | |
74 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
75 | IWorkbenchPart part = page.getActivePart(); | |
1595249b FC |
76 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
77 | if (selectionProvider == null) | |
78 | return false; | |
79 | ISelection selection = selectionProvider.getSelection(); | |
12c155f5 FC |
80 | |
81 | // Make sure there is only one selection and that it is an experiment | |
82 | fExperiment = null; | |
83 | if (selection instanceof TreeSelection) { | |
84 | TreeSelection sel = (TreeSelection) selection; | |
85 | // There should be only one item selected as per the plugin.xml | |
86 | Object element = sel.getFirstElement(); | |
87 | if (element instanceof TmfExperimentElement) { | |
88 | fExperiment = (TmfExperimentElement) element; | |
89 | } | |
90 | } | |
91 | ||
92 | // We only enable opening from the Traces folder for now | |
93 | return (fExperiment != null); | |
94 | } | |
95 | ||
96 | // ------------------------------------------------------------------------ | |
97 | // Execution | |
98 | // ------------------------------------------------------------------------ | |
99 | ||
100 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
101 | @Override | |
102 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
103 | ||
104 | // Check if we are closing down | |
105 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
106 | if (window == null) | |
107 | return false; | |
108 | ||
828e5592 PT |
109 | try { |
110 | IFile bookmarksFile = fExperiment.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); | |
111 | if (!bookmarksFile.exists()) { | |
112 | InputStream source = new ByteArrayInputStream(new byte[0]); | |
113 | bookmarksFile.create(source, true, null); | |
114 | } | |
115 | bookmarksFile.setHidden(true); | |
12c155f5 | 116 | |
09d11238 | 117 | IFile file = fExperiment.getResource().getFile(fExperiment.getName() + '_'); |
828e5592 PT |
118 | if (!file.exists()) { |
119 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); | |
120 | } | |
121 | file.setHidden(true); | |
122 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, TmfExperiment.class.getCanonicalName()); | |
123 | ||
124 | // Instantiate the experiment's traces | |
125 | List<TmfTraceElement> traceEntries = fExperiment.getTraces(); | |
126 | int nbTraces = traceEntries.size(); | |
127 | int cacheSize = Integer.MAX_VALUE; | |
09d11238 PT |
128 | boolean useEditor = true; |
129 | String experimentEditorId = null; | |
828e5592 PT |
130 | ITmfTrace<?>[] traces = new ITmfTrace[nbTraces]; |
131 | for (int i = 0; i < nbTraces; i++) { | |
132 | TmfTraceElement element = traceEntries.get(i); | |
133 | ITmfTrace trace = element.instantiateTrace(); | |
134 | TmfEvent traceEvent = element.instantiateEvent(); | |
135 | if (trace == null || traceEvent == null) { | |
136 | displayErrorMsg(Messages.OpenExperimentHandler_NoTraceType); | |
137 | for (int j = 0; j < i; j++) { | |
138 | traces[j].dispose(); | |
139 | } | |
140 | return null; | |
141 | } | |
142 | try { | |
6da85396 | 143 | trace.initTrace(element.getName(), element.getLocation().getPath(), traceEvent.getClass(), false); |
828e5592 PT |
144 | } catch (FileNotFoundException e) { |
145 | displayErrorMsg(""); //$NON-NLS-1$ | |
146 | } | |
a1091415 | 147 | trace.setResource(element.getResource()); |
828e5592 | 148 | cacheSize = Math.min(cacheSize, trace.getCacheSize()); |
09d11238 PT |
149 | String editorId = element.getEditorId(); |
150 | if (editorId == null) { | |
151 | useEditor = false; | |
152 | experimentEditorId = null; | |
153 | } else if (useEditor) { | |
154 | if (experimentEditorId == null) { | |
155 | experimentEditorId = editorId; | |
156 | } else if (!editorId.equals(experimentEditorId)) { | |
157 | useEditor = false; | |
158 | } | |
159 | } | |
828e5592 | 160 | traces[i] = trace; |
12c155f5 | 161 | } |
12c155f5 | 162 | |
09d11238 | 163 | // Create the experiment |
828e5592 | 164 | TmfExperiment experiment = new TmfExperiment(TmfEvent.class, fExperiment.getName(), traces, cacheSize); |
a1091415 | 165 | experiment.setBookmarksFile(file); |
828e5592 | 166 | |
09d11238 PT |
167 | if (useEditor) { |
168 | IEditorInput editorInput = new TmfEditorInput(file, experiment); | |
169 | IWorkbench wb = PlatformUI.getWorkbench(); | |
170 | IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
171 | ||
172 | String editorId = TmfEventsEditor.ID; | |
a1091415 | 173 | IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); |
09d11238 PT |
174 | if (editor != null && editor instanceof IReusableEditor) { |
175 | activePage.reuseEditor((IReusableEditor) editor, editorInput); | |
176 | activePage.activate(editor); | |
177 | } else { | |
5a5c2fc7 | 178 | activePage.openEditor(editorInput, editorId); |
09d11238 | 179 | } |
0710697b | 180 | experiment.initTrace(null, null, null, true); |
828e5592 | 181 | IDE.setDefaultEditor(file, editorId); |
09d11238 PT |
182 | // editor should dispose the experiment on close |
183 | } else { | |
184 | TmfExperiment.setCurrentExperiment(experiment); | |
185 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
186 | IDE.setDefaultEditor(file, EventsViewEditor.ID); | |
828e5592 PT |
187 | } |
188 | } catch (CoreException e) { | |
189 | displayErrorMsg(e.getMessage()); | |
190 | } | |
12c155f5 FC |
191 | |
192 | return null; | |
193 | } | |
194 | ||
195 | private void displayErrorMsg(String errorMsg) { | |
196 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
828e5592 | 197 | mb.setText(Messages.OpenExperimentHandler_Title); |
12c155f5 FC |
198 | mb.setMessage(errorMsg); |
199 | mb.open(); | |
200 | } | |
201 | ||
202 | } |