tmf: Simple warning fixes in tmf.core and tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / OpenExperimentHandler.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 import java.util.List;
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.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.tmf.core.TmfCommonConstants;
29 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
30 import org.eclipse.linuxtools.tmf.core.event.TmfTimeRange;
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.ui.editors.EventsViewEditor;
37 import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
38 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
39 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
40 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
41 import org.eclipse.swt.widgets.MessageBox;
42 import org.eclipse.ui.IEditorInput;
43 import org.eclipse.ui.IEditorPart;
44 import org.eclipse.ui.IReusableEditor;
45 import org.eclipse.ui.IWorkbench;
46 import org.eclipse.ui.IWorkbenchPage;
47 import org.eclipse.ui.IWorkbenchPart;
48 import org.eclipse.ui.IWorkbenchWindow;
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>OpenExperimentHandler</u></b>
55 * <p>
56 */
57 public class OpenExperimentHandler extends AbstractHandler {
58
59 private static final String BOOKMARKS_HIDDEN_FILE = ".bookmarks"; //$NON-NLS-1$
60
61 private TmfExperimentElement fExperiment = null;
62
63 // ------------------------------------------------------------------------
64 // Validation
65 // ------------------------------------------------------------------------
66
67 @Override
68 public boolean isEnabled() {
69
70 // Check if we are closing down
71 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
72 if (window == null)
73 return false;
74
75 // Get the selection
76 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
77 final IWorkbenchPart part = page.getActivePart();
78 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
79 if (selectionProvider == null)
80 return false;
81 final ISelection selection = selectionProvider.getSelection();
82
83 // Make sure there is only one selection and that it is an experiment
84 fExperiment = null;
85 if (selection instanceof TreeSelection) {
86 final TreeSelection sel = (TreeSelection) selection;
87 // There should be only one item selected as per the plugin.xml
88 final Object element = sel.getFirstElement();
89 if (element instanceof TmfExperimentElement)
90 fExperiment = (TmfExperimentElement) element;
91 }
92
93 // We only enable opening from the Traces folder for now
94 return (fExperiment != null);
95 }
96
97 // ------------------------------------------------------------------------
98 // Execution
99 // ------------------------------------------------------------------------
100
101 @SuppressWarnings({ "rawtypes", "unchecked" })
102 @Override
103 public Object execute(final ExecutionEvent event) throws ExecutionException {
104
105 // Check if we are closing down
106 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
107 if (window == null)
108 return false;
109
110 try {
111 final IFile bookmarksFile = fExperiment.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE);
112 if (!bookmarksFile.exists()) {
113 final InputStream source = new ByteArrayInputStream(new byte[0]);
114 bookmarksFile.create(source, true, null);
115 }
116 bookmarksFile.setHidden(true);
117
118 final IFile file = fExperiment.getResource().getFile(fExperiment.getName() + '_');
119 if (!file.exists()) {
120 file.createLink(bookmarksFile.getLocation(), IResource.REPLACE, null);
121 }
122 file.setHidden(true);
123 file.setPersistentProperty(TmfCommonConstants.TRACETYPE, TmfExperiment.class.getCanonicalName());
124
125 // Instantiate the experiment's traces
126 final List<TmfTraceElement> traceEntries = fExperiment.getTraces();
127 final int nbTraces = traceEntries.size();
128 int cacheSize = Integer.MAX_VALUE;
129 boolean useEditor = true;
130 String experimentEditorId = null;
131 final ITmfTrace<?>[] traces = new ITmfTrace[nbTraces];
132 for (int i = 0; i < nbTraces; i++) {
133 TmfTraceElement element = traceEntries.get(i);
134
135 // Since trace is under an experiment, use the original trace from the traces folder
136 element = element.getElementUnderTraceFolder();
137
138 final ITmfTrace trace = element.instantiateTrace();
139 final ITmfEvent traceEvent = element.instantiateEvent();
140 if ((trace == null) || (traceEvent == null)) {
141 displayErrorMsg(Messages.OpenExperimentHandler_NoTraceType);
142 for (int j = 0; j < i; j++) {
143 traces[j].dispose();
144 }
145 return null;
146 }
147 try {
148 trace.initTrace(element.getResource(), element.getLocation().getPath(), traceEvent.getClass());
149 } catch (final TmfTraceException e) {
150 displayErrorMsg(""); //$NON-NLS-1$
151 }
152 cacheSize = Math.min(cacheSize, trace.getCacheSize());
153 final String editorId = element.getEditorId();
154 if (editorId == null) {
155 useEditor = false;
156 experimentEditorId = null;
157 } else if (useEditor) {
158 if (experimentEditorId == null) {
159 experimentEditorId = editorId;
160 } else if (!editorId.equals(experimentEditorId)) {
161 useEditor = false;
162 }
163 }
164 traces[i] = trace;
165 }
166
167 // Create the experiment
168 TmfExperiment experiment;
169 if (useEditor) {
170 experiment = new TmfExperiment(ITmfEvent.class, fExperiment.getName(), traces, cacheSize) {
171 @Override
172 public void initTrace(IResource resource, String path, Class type) {
173 super.initTrace(resource, path, type);
174 getIndexer().buildIndex(getNbEvents(), TmfTimeRange.ETERNITY, false);
175 }
176 };
177 } else {
178 experiment = new TmfExperiment(ITmfEvent.class, fExperiment.getName(), traces, cacheSize);
179 }
180 experiment.setBookmarksFile(file);
181
182 if (useEditor) {
183 final IEditorInput editorInput = new TmfEditorInput(file, experiment);
184 final IWorkbench wb = PlatformUI.getWorkbench();
185 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
186
187 final String editorId = TmfEventsEditor.ID;
188 final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
189 if ((editor != null) && (editor instanceof IReusableEditor)) {
190 activePage.reuseEditor((IReusableEditor) editor, editorInput);
191 activePage.activate(editor);
192 } else {
193 activePage.openEditor(editorInput, editorId);
194 }
195 experiment.initTrace(null, null, null);
196 IDE.setDefaultEditor(file, editorId);
197 // editor should dispose the experiment on close
198 } else {
199 TmfExperiment.setCurrentExperiment(experiment);
200 TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment));
201 IDE.setDefaultEditor(file, EventsViewEditor.ID);
202 }
203 } catch (final CoreException e) {
204 displayErrorMsg(e.getMessage());
205 }
206
207 return null;
208 }
209
210 private void displayErrorMsg(final String errorMsg) {
211 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
212 mb.setText(Messages.OpenExperimentHandler_Title);
213 mb.setMessage(errorMsg);
214 mb.open();
215 }
216
217 }
This page took 0.036893 seconds and 5 git commands to generate.