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