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