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