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