TMF: Add supplementary folder to experiments
[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.util.List;
16
17 import org.eclipse.core.commands.AbstractHandler;
18 import org.eclipse.core.commands.ExecutionEvent;
19 import org.eclipse.core.commands.ExecutionException;
20 import org.eclipse.core.resources.IFile;
21 import org.eclipse.core.runtime.CoreException;
22 import org.eclipse.jface.viewers.ISelection;
23 import org.eclipse.jface.viewers.ISelectionProvider;
24 import org.eclipse.jface.viewers.TreeSelection;
25 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
26 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
27 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
28 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
29 import org.eclipse.linuxtools.tmf.core.trace.TmfExperiment;
30 import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
31 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
32 import org.eclipse.linuxtools.tmf.ui.project.model.TmfExperimentElement;
33 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
34 import org.eclipse.swt.widgets.Display;
35 import org.eclipse.swt.widgets.MessageBox;
36 import org.eclipse.ui.IEditorInput;
37 import org.eclipse.ui.IEditorPart;
38 import org.eclipse.ui.IReusableEditor;
39 import org.eclipse.ui.IWorkbench;
40 import org.eclipse.ui.IWorkbenchPage;
41 import org.eclipse.ui.IWorkbenchPart;
42 import org.eclipse.ui.IWorkbenchWindow;
43 import org.eclipse.ui.PlatformUI;
44 import org.eclipse.ui.ide.IDE;
45 import org.eclipse.ui.part.FileEditorInput;
46
47 /**
48 * <b><u>OpenExperimentHandler</u></b>
49 * <p>
50 */
51 public class OpenExperimentHandler extends AbstractHandler {
52
53 private TmfExperimentElement fExperiment = null;
54
55 // ------------------------------------------------------------------------
56 // Validation
57 // ------------------------------------------------------------------------
58
59 @Override
60 public boolean isEnabled() {
61
62 // Check if we are closing down
63 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
64 if (window == null) {
65 return false;
66 }
67
68 // Get the selection
69 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
70 final IWorkbenchPart part = page.getActivePart();
71 if (part == null) {
72 return false;
73 }
74 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
75 if (selectionProvider == null) {
76 return false;
77 }
78 final ISelection selection = selectionProvider.getSelection();
79
80 // Make sure there is only one selection and that it is an experiment
81 fExperiment = null;
82 if (selection instanceof TreeSelection) {
83 final TreeSelection sel = (TreeSelection) selection;
84 // There should be only one item selected as per the plugin.xml
85 final Object element = sel.getFirstElement();
86 if (element instanceof TmfExperimentElement) {
87 fExperiment = (TmfExperimentElement) element;
88 }
89 }
90
91 // We only enable opening from the Traces folder for now
92 return (fExperiment != null);
93 }
94
95 // ------------------------------------------------------------------------
96 // Execution
97 // ------------------------------------------------------------------------
98
99 @Override
100 public Object execute(final ExecutionEvent event) throws ExecutionException {
101
102 // Check if we are closing down
103 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
104 if (window == null) {
105 return false;
106 }
107
108 final TmfExperimentElement experimentElement = fExperiment;
109
110 Thread thread = new Thread() {
111 @Override
112 public void run() {
113
114 final IFile file;
115 try {
116 file = experimentElement.createBookmarksFile();
117 } catch (final CoreException e) {
118 Activator.getDefault().logError("Error opening experiment " + experimentElement.getName(), e); //$NON-NLS-1$
119 displayErrorMsg(Messages.OpenExperimentHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
120 return;
121 }
122
123 /* Unlike traces, there is no instanceExperiment, so we call this function
124 * here alone. Maybe it would be better to do this on experiment's element
125 * constructor?
126 */
127 experimentElement.refreshSupplementaryFolder();
128
129 // Instantiate the experiment's traces
130 final List<TmfTraceElement> traceEntries = experimentElement.getTraces();
131 final int nbTraces = traceEntries.size();
132 int cacheSize = Integer.MAX_VALUE;
133 String commonEditorId = null;
134 final ITmfTrace[] traces = new ITmfTrace[nbTraces];
135 for (int i = 0; i < nbTraces; i++) {
136 TmfTraceElement element = traceEntries.get(i);
137
138 // Since trace is under an experiment, use the original trace from the traces folder
139 element = element.getElementUnderTraceFolder();
140
141 final ITmfTrace trace = element.instantiateTrace();
142 final ITmfEvent traceEvent = element.instantiateEvent();
143 if ((trace == null) || (traceEvent == null)) {
144 displayErrorMsg(Messages.OpenExperimentHandler_NoTraceType);
145 for (int j = 0; j < i; j++) {
146 traces[j].dispose();
147 }
148 if (trace != null) {
149 trace.dispose();
150 }
151 return;
152 }
153 try {
154 trace.initTrace(element.getResource(), element.getLocation().getPath(), traceEvent.getClass());
155 } catch (final TmfTraceException e) {
156 displayErrorMsg(Messages.OpenTraceHandler_InitError + "\n\n" + e); //$NON-NLS-1$
157 for (int j = 0; j < i; j++) {
158 traces[j].dispose();
159 }
160 trace.dispose();
161 return;
162 }
163 cacheSize = Math.min(cacheSize, trace.getCacheSize());
164
165 // If all traces use the same editorId, use it, otherwise use the default
166 final String editorId = element.getEditorId();
167 if (commonEditorId == null) {
168 commonEditorId = (editorId != null) ? editorId : TmfEventsEditor.ID;
169 } else if (!commonEditorId.equals(editorId)) {
170 commonEditorId = TmfEventsEditor.ID;
171 }
172 traces[i] = trace;
173 }
174
175 // Create the experiment
176 final TmfExperiment experiment = new TmfExperiment(ITmfEvent.class, experimentElement.getName(), traces, cacheSize, experimentElement.getResource());
177 experiment.setBookmarksFile(file);
178
179 final String editorId = commonEditorId;
180 Display.getDefault().asyncExec(new Runnable() {
181 @Override
182 public void run() {
183 try {
184 final IEditorInput editorInput = new TmfEditorInput(file, experiment);
185 final IWorkbench wb = PlatformUI.getWorkbench();
186 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
187
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 IDE.setDefaultEditor(file, editorId);
196 // editor should dispose the experiment on close
197 } catch (final CoreException e) {
198 Activator.getDefault().logError("Error opening experiment " + experimentElement.getName(), e); //$NON-NLS-1$
199 displayErrorMsg(Messages.OpenExperimentHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$
200 experiment.dispose();
201 return;
202 }
203 }
204 });
205 }
206 };
207
208 thread.start();
209
210 return null;
211 }
212
213 private static void displayErrorMsg(final String errorMsg) {
214 Display.getDefault().asyncExec(new Runnable() {
215 @Override
216 public void run() {
217 final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
218 mb.setText(Messages.OpenExperimentHandler_Title);
219 mb.setMessage(errorMsg);
220 mb.open();
221 }
222 });
223 }
224 }
This page took 0.036712 seconds and 5 git commands to generate.