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 | |
18 | import org.eclipse.core.commands.AbstractHandler; | |
19 | import org.eclipse.core.commands.ExecutionEvent; | |
20 | import org.eclipse.core.commands.ExecutionException; | |
4ba871ae | 21 | import org.eclipse.core.resources.IFile; |
828e5592 | 22 | import org.eclipse.core.resources.IFolder; |
12c155f5 | 23 | import org.eclipse.core.resources.IResource; |
828e5592 | 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; |
b4f71e4a | 30 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 31 | import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment; |
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; |
828e5592 | 35 | import org.eclipse.linuxtools.tmf.core.trace.TmfTrace; |
09d11238 | 36 | import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor; |
12c155f5 FC |
37 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
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 | |
25e48683 FC |
75 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
76 | if (window == null) | |
12c155f5 FC |
77 | return false; |
78 | ||
79 | // Get the selection | |
25e48683 FC |
80 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
81 | final IWorkbenchPart part = page.getActivePart(); | |
82 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
83 | if (selectionProvider == null) | |
1595249b | 84 | return false; |
25e48683 | 85 | final ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
86 | |
87 | // Make sure there is only one selection and that it is a trace | |
88 | fTrace = null; | |
89 | if (selection instanceof TreeSelection) { | |
25e48683 | 90 | final TreeSelection sel = (TreeSelection) selection; |
12c155f5 | 91 | // There should be only one item selected as per the plugin.xml |
25e48683 FC |
92 | final Object element = sel.getFirstElement(); |
93 | if (element instanceof TmfTraceElement) | |
12c155f5 | 94 | fTrace = (TmfTraceElement) element; |
12c155f5 FC |
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" }) | |
25e48683 | 107 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
12c155f5 FC |
108 | |
109 | // Check if we are closing down | |
25e48683 FC |
110 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
111 | if (window == null) | |
12c155f5 FC |
112 | return null; |
113 | ||
114 | // Check that the trace is valid | |
25e48683 | 115 | if (fTrace == null) |
12c155f5 | 116 | return null; |
12c155f5 | 117 | |
828e5592 | 118 | // If trace is under an experiment, use the original trace from the traces folder |
e12ecd30 | 119 | fTrace = fTrace.getElementUnderTraceFolder(); |
828e5592 | 120 | |
25e48683 FC |
121 | final ITmfTrace trace = fTrace.instantiateTrace(); |
122 | final ITmfEvent traceEvent = fTrace.instantiateEvent(); | |
ce2388e0 | 123 | if ((trace == null) || (traceEvent == null)) { |
12c155f5 FC |
124 | displayErrorMsg(Messages.OpenTraceHandler_NoTraceType); |
125 | return null; | |
126 | } | |
127 | ||
128 | // Get the editor_id from the extension point | |
25e48683 FC |
129 | final String editorId = fTrace.getEditorId(); |
130 | final boolean usesEditor = (editorId != null) && (editorId.length() > 0); | |
12c155f5 FC |
131 | |
132 | try { | |
25e48683 | 133 | trace.initTrace(fTrace.getResource(), fTrace.getLocation().getPath(), traceEvent.getClass()); |
b4f71e4a | 134 | } catch (final TmfTraceException e) { |
e12ecd30 | 135 | displayErrorMsg(Messages.OpenTraceHandler_NoTrace + "\n\n" + e); //$NON-NLS-1$ |
12c155f5 FC |
136 | return null; |
137 | } | |
138 | ||
25e48683 | 139 | final IResource resource = fTrace.getResource(); |
09d11238 | 140 | IFile file = null; |
25e48683 | 141 | if (resource instanceof IFile) |
09d11238 | 142 | file = (IFile) resource; |
25e48683 | 143 | else if (resource instanceof IFolder) |
09d11238 | 144 | try { |
25e48683 | 145 | final IFile bookmarksFile = fTrace.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE); |
09d11238 | 146 | if (!bookmarksFile.exists()) { |
25e48683 | 147 | final InputStream source = new ByteArrayInputStream(new byte[0]); |
09d11238 PT |
148 | bookmarksFile.create(source, true, null); |
149 | } | |
150 | bookmarksFile.setHidden(true); | |
151 | ||
25e48683 | 152 | final IFolder folder = (IFolder) resource; |
09d11238 | 153 | file = folder.getFile(fTrace.getName() + '_'); |
25e48683 | 154 | if (!file.exists()) |
09d11238 | 155 | file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null); |
09d11238 | 156 | file.setHidden(true); |
25e48683 | 157 | if (usesEditor) |
e12ecd30 | 158 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, fTrace.getTraceType()); |
25e48683 | 159 | else |
e12ecd30 | 160 | file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfTrace.class.getCanonicalName()); |
25e48683 | 161 | } catch (final CoreException e) { |
09d11238 PT |
162 | e.printStackTrace(); |
163 | } | |
09d11238 | 164 | |
25e48683 | 165 | if (usesEditor) |
12c155f5 | 166 | try { |
25e48683 FC |
167 | final IEditorInput editorInput = new TmfEditorInput(file, trace); |
168 | final IWorkbench wb = PlatformUI.getWorkbench(); | |
169 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
12c155f5 | 170 | |
25e48683 | 171 | final IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); |
ce2388e0 | 172 | if ((editor != null) && (editor instanceof IReusableEditor)) { |
12c155f5 FC |
173 | activePage.reuseEditor((IReusableEditor) editor, editorInput); |
174 | activePage.activate(editor); | |
175 | } else { | |
5a5c2fc7 | 176 | activePage.openEditor(editorInput, editorId); |
25e48683 | 177 | if (resource instanceof IFile) |
4ba871ae | 178 | IDE.setDefaultEditor((IFile) resource, editorId); |
12c155f5 | 179 | } |
25e48683 | 180 | } catch (final PartInitException e) { |
12c155f5 FC |
181 | e.printStackTrace(); |
182 | } | |
25e48683 | 183 | else { |
09d11238 | 184 | // Create the experiment |
25e48683 | 185 | final ITmfTrace[] traces = new ITmfTrace[] { trace }; |
20658947 | 186 | final TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getCacheSize()); |
a1091415 | 187 | experiment.setBookmarksFile(file); |
ce2388e0 | 188 | |
09d11238 PT |
189 | TmfExperiment.setCurrentExperiment(experiment); |
190 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
191 | IDE.setDefaultEditor(file, EventsViewEditor.ID); | |
12c155f5 FC |
192 | } |
193 | return null; | |
194 | } | |
195 | ||
25e48683 FC |
196 | private void displayErrorMsg(final String errorMsg) { |
197 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
12c155f5 FC |
198 | mb.setText(Messages.OpenTraceHandler_Title); |
199 | mb.setMessage(errorMsg); | |
200 | mb.open(); | |
201 | } | |
202 | ||
203 | } |