Commit | Line | Data |
---|---|---|
12c155f5 FC |
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.tmf.ui.project.handlers; | |
14 | ||
15 | import java.io.FileNotFoundException; | |
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.IResource; | |
21 | import org.eclipse.jface.viewers.ISelection; | |
1595249b | 22 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 23 | import org.eclipse.jface.viewers.TreeSelection; |
6c13869b FC |
24 | import org.eclipse.linuxtools.tmf.core.event.TmfEvent; |
25 | import org.eclipse.linuxtools.tmf.core.experiment.TmfExperiment; | |
26 | import org.eclipse.linuxtools.tmf.core.signal.TmfExperimentSelectedSignal; | |
27 | import org.eclipse.linuxtools.tmf.core.signal.TmfSignalManager; | |
28 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; | |
12c155f5 FC |
29 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
30 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; | |
31 | import org.eclipse.swt.widgets.MessageBox; | |
32 | import org.eclipse.ui.IEditorInput; | |
33 | import org.eclipse.ui.IEditorPart; | |
34 | import org.eclipse.ui.IReusableEditor; | |
35 | import org.eclipse.ui.IWorkbench; | |
36 | import org.eclipse.ui.IWorkbenchPage; | |
37 | import org.eclipse.ui.IWorkbenchPart; | |
38 | import org.eclipse.ui.IWorkbenchWindow; | |
39 | import org.eclipse.ui.PartInitException; | |
40 | import org.eclipse.ui.PlatformUI; | |
41 | ||
42 | /** | |
43 | * <b><u>OpenTraceHandler</u></b> | |
44 | * <p> | |
45 | * TODO: Add support for multiple trace selection | |
46 | */ | |
47 | public class OpenTraceHandler extends AbstractHandler { | |
48 | ||
49 | // ------------------------------------------------------------------------ | |
50 | // Attributes | |
51 | // ------------------------------------------------------------------------ | |
52 | ||
53 | private TmfTraceElement fTrace = null; | |
54 | ||
55 | // ------------------------------------------------------------------------ | |
56 | // Validation | |
57 | // ------------------------------------------------------------------------ | |
58 | ||
59 | @Override | |
60 | public boolean isEnabled() { | |
61 | ||
62 | // Check if we are closing down | |
63 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
64 | if (window == null) | |
65 | return false; | |
66 | ||
67 | // Get the selection | |
68 | IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); | |
69 | IWorkbenchPart part = page.getActivePart(); | |
1595249b FC |
70 | ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); |
71 | if (selectionProvider == null) | |
72 | return false; | |
73 | ISelection selection = selectionProvider.getSelection(); | |
12c155f5 FC |
74 | |
75 | // Make sure there is only one selection and that it is a trace | |
76 | fTrace = null; | |
77 | if (selection instanceof TreeSelection) { | |
78 | TreeSelection sel = (TreeSelection) selection; | |
79 | // There should be only one item selected as per the plugin.xml | |
80 | Object element = sel.getFirstElement(); | |
81 | if (element instanceof TmfTraceElement) { | |
82 | fTrace = (TmfTraceElement) element; | |
83 | } | |
84 | } | |
85 | ||
86 | // We only enable opening from the Traces folder for now | |
87 | return (fTrace != null); | |
88 | } | |
89 | ||
90 | // ------------------------------------------------------------------------ | |
91 | // Execution | |
92 | // ------------------------------------------------------------------------ | |
93 | ||
94 | @Override | |
95 | @SuppressWarnings({ "rawtypes", "unchecked" }) | |
96 | public Object execute(ExecutionEvent event) throws ExecutionException { | |
97 | ||
98 | // Check if we are closing down | |
99 | IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); | |
100 | if (window == null) | |
101 | return null; | |
102 | ||
103 | // Check that the trace is valid | |
104 | if (fTrace == null) { | |
105 | return null; | |
106 | } | |
107 | ||
108 | TmfEvent traceEvent = fTrace.instantiateEvent(); | |
109 | ITmfTrace trace = fTrace.instantiateTrace(); | |
110 | if (trace == null) { | |
111 | displayErrorMsg(Messages.OpenTraceHandler_NoTraceType); | |
112 | return null; | |
113 | } | |
114 | ||
115 | // Get the editor_id from the extension point | |
116 | String editorId = fTrace.getEditorId(); | |
117 | boolean usesEditor = editorId != null && editorId.length() > 0; | |
118 | ||
119 | try { | |
120 | trace.initTrace(fTrace.getLocation().getPath(), traceEvent.getClass(), usesEditor); | |
121 | } catch (FileNotFoundException e) { | |
122 | displayErrorMsg(Messages.OpenTraceHandler_NoTrace); | |
123 | return null; | |
124 | } | |
125 | ||
126 | if (usesEditor) { | |
127 | try { | |
128 | IResource resource = fTrace.getResource(); | |
129 | IEditorInput editorInput = new TmfEditorInput(resource, trace); | |
130 | IWorkbench wb = PlatformUI.getWorkbench(); | |
131 | IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
132 | ||
133 | IEditorPart editor = activePage.findEditor(editorInput); | |
134 | if (editor != null && editor instanceof IReusableEditor) { | |
135 | activePage.reuseEditor((IReusableEditor) editor, editorInput); | |
136 | activePage.activate(editor); | |
137 | } else { | |
138 | editor = activePage.openEditor(editorInput, editorId); | |
139 | } | |
140 | } catch (PartInitException e) { | |
141 | e.printStackTrace(); | |
142 | } | |
143 | ||
144 | } else { | |
475743b7 FC |
145 | // Close the current experiment, if any |
146 | TmfExperiment<?> currentExperiment = TmfExperiment.getCurrentExperiment(); | |
147 | if (currentExperiment != null) { | |
148 | currentExperiment.dispose(); | |
149 | } | |
150 | ||
151 | // Open the new experiment and broadcast | |
12c155f5 | 152 | ITmfTrace[] traces = new ITmfTrace[] { trace }; |
c5b45b39 | 153 | TmfExperiment experiment = new TmfExperiment(traceEvent.getClass(), fTrace.getName(), traces, trace.getCacheSize()); |
12c155f5 FC |
154 | TmfExperiment.setCurrentExperiment(experiment); |
155 | TmfSignalManager.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment)); | |
156 | } | |
157 | return null; | |
158 | } | |
159 | ||
160 | private void displayErrorMsg(String errorMsg) { | |
161 | MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
162 | mb.setText(Messages.OpenTraceHandler_Title); | |
163 | mb.setMessage(errorMsg); | |
164 | mb.open(); | |
165 | } | |
166 | ||
167 | } |