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 | ||
13 | package org.eclipse.linuxtools.tmf.ui.project.handlers; | |
14 | ||
828e5592 | 15 | import java.io.ByteArrayInputStream; |
12c155f5 | 16 | import java.io.FileNotFoundException; |
828e5592 | 17 | import java.io.InputStream; |
12c155f5 FC |
18 | |
19 | import org.eclipse.core.commands.AbstractHandler; | |
20 | import org.eclipse.core.commands.ExecutionEvent; | |
21 | import org.eclipse.core.commands.ExecutionException; | |
4ba871ae | 22 | import org.eclipse.core.resources.IFile; |
828e5592 | 23 | import org.eclipse.core.resources.IFolder; |
12c155f5 | 24 | import org.eclipse.core.resources.IResource; |
828e5592 | 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; |
828e5592 | 34 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
09d11238 | 35 | import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor; |
12c155f5 | 36 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
828e5592 | 37 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement; |
12c155f5 FC |
38 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
39 | import org.eclipse.swt.widgets.MessageBox; | |
40 | import org.eclipse.ui.IEditorInput; | |
41 | import org.eclipse.ui.IEditorPart; | |
42 | import org.eclipse.ui.IReusableEditor; | |
43 | import org.eclipse.ui.IWorkbench; | |
44 | import org.eclipse.ui.IWorkbenchPage; | |
45 | import org.eclipse.ui.IWorkbenchPart; | |
46 | import org.eclipse.ui.IWorkbenchWindow; | |
47 | import org.eclipse.ui.PartInitException; | |
48 | import org.eclipse.ui.PlatformUI; | |
4ba871ae | 49 | import org.eclipse.ui.ide.IDE; |
12c155f5 FC |
50 | |
51 | /** | |
52 | * <b><u>OpenTraceHandler</u></b> | |
53 | * <p> | |
54 | * TODO: Add support for multiple trace selection | |
55 | */ | |
56 | public class OpenTraceHandler extends AbstractHandler { | |
57 | ||
828e5592 PT |
58 | private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$ |
59 | ||
12c155f5 FC |
60 | // ------------------------------------------------------------------------ |
61 | // Attributes | |
62 | // ------------------------------------------------------------------------ | |
63 | ||
64 | private TmfTraceElement fTrace = null; | |
65 | ||
66 | // ------------------------------------------------------------------------ | |
67 | // Validation | |
68 | // ------------------------------------------------------------------------ | |
69 | ||
70 | @Override | |
71 | public boolean isEnabled() { | |
72 | ||
73 | // Check if we are closing down | |
74 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
75 | if (window == null) | |
76 | return false; | |
77 | ||
78 | // Get the selection | |
79 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
80 | IWorkbenchPart part = page.getActivePart(); | |
1595249b FC |
81 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
82 | if (selectionProvider == null) | |
83 | return false; | |
84 | ISelection selection = selectionProvider.getSelection(); | |
12c155f5 FC |
85 | |
86 | // Make sure there is only one selection and that it is a trace | |
87 | fTrace = null; | |
88 | if (selection instanceof TreeSelection) { | |
89 | TreeSelection sel = (TreeSelection) selection; | |
90 | // There should be only one item selected as per the plugin.xml | |
91 | Object element = sel.getFirstElement(); | |
92 | if (element instanceof TmfTraceElement) { | |
93 | fTrace = (TmfTraceElement) element; | |
94 | } | |
95 | } | |
96 | ||
97 | // We only enable opening from the Traces folder for now | |
98 | return (fTrace != null); | |
99 | } | |
100 | ||
101 | // ------------------------------------------------------------------------ | |
102 | // Execution | |
103 | // ------------------------------------------------------------------------ | |
104 | ||
105 | @Override | |
106 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
107 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
108 | ||
109 | // Check if we are closing down | |
110 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
111 | if (window == null) | |
112 | return null; | |
113 | ||
114 | // Check that the trace is valid | |
115 | if (fTrace == null) { | |
116 | return null; | |
117 | } | |
118 | ||
828e5592 PT |
119 | // If trace is under an experiment, use the original trace from the traces folder |
120 | if (fTrace.getParent() instanceof TmfExperimentElement) { | |
121 | for (TmfTraceElement trace : fTrace.getProject().getTracesFolder().getTraces()) { | |
122 | if (trace.getName().equals(fTrace.getName())) { | |
123 | fTrace = trace; | |
124 | break; | |
125 | } | |
126 | } | |
127 | } | |
128 | ||
12c155f5 | 129 | ITmfTrace trace = fTrace.instantiateTrace(); |
828e5592 PT |
130 | TmfEvent traceEvent = fTrace.instantiateEvent(); |
131 | if (trace == null || traceEvent == null) { | |
12c155f5 FC |
132 | displayErrorMsg(Messages.OpenTraceHandler_NoTraceType); |
133 | return null; | |
134 | } | |
828e5592 PT |
135 | if (trace instanceof TmfTrace) { |
136 | ((TmfTrace) trace).setResource(fTrace.getResource()); | |
137 | } | |
12c155f5 FC |
138 | |
139 | // Get the editor_id from the extension point | |
140 | String editorId = fTrace.getEditorId(); | |
141 | boolean usesEditor = editorId != null && editorId.length() > 0; | |
142 | ||
143 | try { | |
144 | trace.initTrace(fTrace.getLocation().getPath(), traceEvent.getClass(), usesEditor); | |
145 | } catch (FileNotFoundException e) { | |
146 | displayErrorMsg(Messages.OpenTraceHandler_NoTrace); | |
147 | return null; | |
148 | } | |
149 | ||
09d11238 PT |
150 | IResource resource = fTrace.getResource(); |
151 | IFile file = null; | |
152 | if (resource instanceof IFile) { | |
153 | file = (IFile) resource; | |
154 | } else if (resource instanceof IFolder){ | |
155 | try { | |
156 | IFile bookmarksFile = fTrace.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); | |
157 | if (!bookmarksFile.exists()) { | |
158 | InputStream source = new ByteArrayInputStream(new byte[0]); | |
159 | bookmarksFile.create(source, true, null); | |
160 | } | |
161 | bookmarksFile.setHidden(true); | |
162 | ||
163 | IFolder folder = (IFolder) resource; | |
164 | file = folder.getFile(fTrace.getName() + '_'); | |
165 | if (!file.exists()) { | |
166 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); | |
167 | } | |
168 | file.setHidden(true); | |
169 | if (usesEditor) { | |
170 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, fTrace.getTraceType()); | |
171 | } else { | |
172 | file.setPersistentProperty(TmfTraceElement.TRACETYPE, TmfTrace.class.getCanonicalName()); | |
173 | } | |
174 | } catch (CoreException e) { | |
175 | e.printStackTrace(); | |
176 | } | |
177 | } | |
178 | ||
12c155f5 | 179 | if (usesEditor) { |
09d11238 PT |
180 | if (trace instanceof TmfTrace) { |
181 | ((TmfTrace) trace).setResource(file); | |
182 | } | |
12c155f5 | 183 | try { |
12c155f5 FC |
184 | IEditorInput editorInput = new TmfEditorInput(resource, trace); |
185 | IWorkbench wb = PlatformUI.getWorkbench(); | |
186 | IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
187 | ||
188 | IEditorPart editor = activePage.findEditor(editorInput); | |
189 | if (editor != null && editor instanceof IReusableEditor) { | |
190 | activePage.reuseEditor((IReusableEditor) editor, editorInput); | |
191 | activePage.activate(editor); | |
192 | } else { | |
193 | editor = activePage.openEditor(editorInput, editorId); | |
4ba871ae PT |
194 | if (resource instanceof IFile) { |
195 | IDE.setDefaultEditor((IFile) resource, editorId); | |
196 | } | |
12c155f5 FC |
197 | } |
198 | } catch (PartInitException e) { | |
199 | e.printStackTrace(); | |
200 | } | |
09d11238 PT |
201 | } else { |
202 | // Create the experiment | |
12c155f5 | 203 | ITmfTrace[] traces = new ITmfTrace[] { trace }; |
c5b45b39 | 204 | TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getCacheSize()); |
828e5592 | 205 | experiment.setResource(file); |
09d11238 PT |
206 | |
207 | TmfExperiment.setCurrentExperiment(experiment); | |
208 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
209 | IDE.setDefaultEditor(file, EventsViewEditor.ID); | |
12c155f5 FC |
210 | } |
211 | return null; | |
212 | } | |
213 | ||
214 | private void displayErrorMsg(String errorMsg) { | |
215 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
216 | mb.setText(Messages.OpenTraceHandler_Title); | |
217 | mb.setMessage(errorMsg); | |
218 | mb.open(); | |
219 | } | |
220 | ||
221 | } |