Refactor TmfTrace and dependencies - remove getTrace()
[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.FileNotFoundException;
17 import java.io.InputStream;
18
19 import org.eclipse.core.commands.AbstractHandler;
20 import org.eclipse.core.commands.ExecutionEvent;
21 import org.eclipse.core.commands.ExecutionException;
22 import org.eclipse.core.resources.IFile;
23 import org.eclipse.core.resources.IFolder;
24 import org.eclipse.core.resources.IResource;
25 import org.eclipse.core.runtime.CoreException;
26 import org.eclipse.jface.viewers.ISelection;
27 import org.eclipse.jface.viewers.ISelectionProvider;
28 import org.eclipse.jface.viewers.TreeSelection;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
30 import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment;
31 import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal;
32 import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager;
33 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
34 import org.eclipse.linuxtools.tmf.core.trace.TmfTrace;
35 import org.eclipse.linuxtools.tmf.ui.editors.EventsViewEditor;
36 import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
37 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
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;
49 import org.eclipse.ui.ide.IDE;
50 import org.eclipse.ui.part.FileEditorInput;
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
59 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
60
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 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
76 if (window == null)
77 return false;
78
79 // Get the selection
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)
84 return false;
85 final ISelection selection = selectionProvider.getSelection();
86
87 // Make sure there is only one selection and that it is a trace
88 fTrace = null;
89 if (selection instanceof TreeSelection) {
90 final TreeSelection sel = (TreeSelection) selection;
91 // There should be only one item selected as per the plugin.xml
92 final Object element = sel.getFirstElement();
93 if (element instanceof TmfTraceElement)
94 fTrace = (TmfTraceElement) element;
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(final ExecutionEvent event) throws ExecutionException {
108
109 // Check if we are closing down
110 final 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 // If trace is under an experiment, use the original trace from the traces folder
119 if (fTrace.getParent() instanceof TmfExperimentElement)
120 for (final TmfTraceElement trace : fTrace.getProject().getTracesFolder().getTraces())
121 if (trace.getName().equals(fTrace.getName())) {
122 fTrace = trace;
123 break;
124 }
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 if (usesEditor)
140 trace.indexTrace(false);
141 } catch (final FileNotFoundException e) {
142 displayErrorMsg(Messages.OpenTraceHandler_NoTrace);
143 return null;
144 }
145
146 final IResource resource = fTrace.getResource();
147 IFile file = null;
148 if (resource instanceof IFile)
149 file = (IFile) resource;
150 else if (resource instanceof IFolder)
151 try {
152 final IFile bookmarksFile = fTrace.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
153 if (!bookmarksFile.exists()) {
154 final InputStream source = new ByteArrayInputStream(new byte[0]);
155 bookmarksFile.create(source, true, null);
156 }
157 bookmarksFile.setHidden(true);
158
159 final IFolder folder = (IFolder) resource;
160 file = folder.getFile(fTrace.getName() + '_');
161 if (!file.exists())
162 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
163 file.setHidden(true);
164 if (usesEditor)
165 file.setPersistentProperty(TmfTraceElement.TRACETYPE, fTrace.getTraceType());
166 else
167 file.setPersistentProperty(TmfTraceElement.TRACETYPE, TmfTrace.class.getCanonicalName());
168 } catch (final CoreException e) {
169 e.printStackTrace();
170 }
171
172 if (usesEditor)
173 try {
174 final IEditorInput editorInput = new TmfEditorInput(file, trace);
175 final IWorkbench wb = PlatformUI.getWorkbench();
176 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
177
178 final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
179 if ((editor != null) && (editor instanceof IReusableEditor)) {
180 activePage.reuseEditor((IReusableEditor) editor, editorInput);
181 activePage.activate(editor);
182 } else {
183 activePage.openEditor(editorInput, editorId);
184 if (resource instanceof IFile)
185 IDE.setDefaultEditor((IFile) resource, editorId);
186 }
187 } catch (final PartInitException e) {
188 e.printStackTrace();
189 }
190 else {
191 // Create the experiment
192 final ITmfTrace[] traces = new ITmfTrace[] { trace };
193 final TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getIndexPageSize());
194 experiment.setBookmarksFile(file);
195
196 TmfExperiment.setCurrentExperiment(experiment);
197 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
198 IDE.setDefaultEditor(file, EventsViewEditor.ID);
199 }
200 return null;
201 }
202
203 private void displayErrorMsg(final String errorMsg) {
204 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
205 mb.setText(Messages.OpenTraceHandler_Title);
206 mb.setMessage(errorMsg);
207 mb.open();
208 }
209
210 }
This page took 0.035825 seconds and 5 git commands to generate.