Commit | Line | Data |
---|---|---|
12c155f5 FC |
1 | /******************************************************************************* |
2 | * Copyright (c) 2009, 2010, 2011 Ericsson | |
ce2388e0 | 3 | * |
12c155f5 FC |
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 | |
ce2388e0 | 8 | * |
12c155f5 FC |
9 | * Contributors: |
10 | * Francois Chouinard - Initial API and implementation | |
11 | *******************************************************************************/ | |
12 | ||
d34665f9 | 13 | package org.eclipse.linuxtools.internal.tmf.ui.project.handlers; |
12c155f5 | 14 | |
12c155f5 FC |
15 | import org.eclipse.core.commands.AbstractHandler; |
16 | import org.eclipse.core.commands.ExecutionEvent; | |
17 | import org.eclipse.core.commands.ExecutionException; | |
4ba871ae | 18 | import org.eclipse.core.resources.IFile; |
828e5592 | 19 | import org.eclipse.core.runtime.CoreException; |
12c155f5 | 20 | import org.eclipse.jface.viewers.ISelection; |
1595249b | 21 | import org.eclipse.jface.viewers.ISelectionProvider; |
12c155f5 | 22 | import org.eclipse.jface.viewers.TreeSelection; |
8fd82db5 | 23 | import org.eclipse.linuxtools.internal.tmf.ui.Activator; |
ce2388e0 | 24 | import org.eclipse.linuxtools.tmf.core.event.ITmfEvent; |
b4f71e4a | 25 | import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException; |
6c13869b | 26 | import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace; |
12c155f5 | 27 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput; |
faa38350 | 28 | import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor; |
12c155f5 | 29 | import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement; |
9c8e0546 | 30 | import org.eclipse.swt.widgets.Display; |
12c155f5 FC |
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; | |
4ba871ae | 41 | import org.eclipse.ui.ide.IDE; |
a1091415 | 42 | import org.eclipse.ui.part.FileEditorInput; |
12c155f5 FC |
43 | |
44 | /** | |
45 | * <b><u>OpenTraceHandler</u></b> | |
46 | * <p> | |
47 | * TODO: Add support for multiple trace selection | |
48 | */ | |
49 | public class OpenTraceHandler extends AbstractHandler { | |
50 | ||
51 | // ------------------------------------------------------------------------ | |
52 | // Attributes | |
53 | // ------------------------------------------------------------------------ | |
54 | ||
55 | private TmfTraceElement fTrace = null; | |
56 | ||
57 | // ------------------------------------------------------------------------ | |
58 | // Validation | |
59 | // ------------------------------------------------------------------------ | |
60 | ||
61 | @Override | |
62 | public boolean isEnabled() { | |
63 | ||
64 | // Check if we are closing down | |
25e48683 | 65 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
abbdd66a | 66 | if (window == null) { |
12c155f5 | 67 | return false; |
abbdd66a | 68 | } |
12c155f5 FC |
69 | |
70 | // Get the selection | |
25e48683 FC |
71 | final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); |
72 | final IWorkbenchPart part = page.getActivePart(); | |
73 | final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider(); | |
abbdd66a | 74 | if (selectionProvider == null) { |
1595249b | 75 | return false; |
abbdd66a | 76 | } |
25e48683 | 77 | final ISelection selection = selectionProvider.getSelection(); |
12c155f5 FC |
78 | |
79 | // Make sure there is only one selection and that it is a trace | |
80 | fTrace = null; | |
81 | if (selection instanceof TreeSelection) { | |
25e48683 | 82 | final TreeSelection sel = (TreeSelection) selection; |
12c155f5 | 83 | // There should be only one item selected as per the plugin.xml |
25e48683 | 84 | final Object element = sel.getFirstElement(); |
abbdd66a | 85 | if (element instanceof TmfTraceElement) { |
12c155f5 | 86 | fTrace = (TmfTraceElement) element; |
abbdd66a | 87 | } |
12c155f5 FC |
88 | } |
89 | ||
90 | // We only enable opening from the Traces folder for now | |
91 | return (fTrace != null); | |
92 | } | |
93 | ||
94 | // ------------------------------------------------------------------------ | |
95 | // Execution | |
96 | // ------------------------------------------------------------------------ | |
97 | ||
98 | @Override | |
25e48683 | 99 | public Object execute(final ExecutionEvent event) throws ExecutionException { |
12c155f5 FC |
100 | |
101 | // Check if we are closing down | |
25e48683 | 102 | final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow(); |
abbdd66a | 103 | if (window == null) { |
12c155f5 | 104 | return null; |
abbdd66a | 105 | } |
12c155f5 FC |
106 | |
107 | // Check that the trace is valid | |
abbdd66a | 108 | if (fTrace == null) { |
12c155f5 | 109 | return null; |
abbdd66a | 110 | } |
12c155f5 | 111 | |
828e5592 | 112 | // If trace is under an experiment, use the original trace from the traces folder |
9c8e0546 BH |
113 | final TmfTraceElement traceElement = fTrace.getElementUnderTraceFolder(); |
114 | ||
115 | Thread thread = new Thread() { | |
116 | @Override | |
117 | public void run() { | |
118 | ||
119 | final ITmfTrace trace = traceElement.instantiateTrace(); | |
120 | final ITmfEvent traceEvent = traceElement.instantiateEvent(); | |
121 | if ((trace == null) || (traceEvent == null)) { | |
122 | displayErrorMsg(Messages.OpenTraceHandler_NoTraceType); | |
123 | if (trace != null) { | |
124 | trace.dispose(); | |
125 | } | |
126 | return; | |
09d11238 | 127 | } |
09d11238 | 128 | |
9c8e0546 BH |
129 | // Get the editor_id from the extension point |
130 | String traceEditorId = traceElement.getEditorId(); | |
131 | final String editorId = (traceEditorId != null) ? traceEditorId : TmfEventsEditor.ID; | |
132 | ||
133 | try { | |
134 | trace.initTrace(traceElement.getResource(), traceElement.getLocation().getPath(), traceEvent.getClass()); | |
135 | } catch (final TmfTraceException e) { | |
136 | displayErrorMsg(Messages.OpenTraceHandler_InitError + "\n\n" + e); //$NON-NLS-1$ | |
137 | trace.dispose(); | |
138 | return; | |
abbdd66a | 139 | } |
09d11238 | 140 | |
c4c81d91 PT |
141 | final IFile file; |
142 | try { | |
143 | file = traceElement.getBookmarksFile(); | |
144 | } catch (final CoreException e) { | |
145 | Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$ | |
146 | displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$ | |
147 | trace.dispose(); | |
148 | return; | |
12c155f5 | 149 | } |
9c8e0546 | 150 | |
9c8e0546 BH |
151 | Display.getDefault().asyncExec(new Runnable() { |
152 | @Override | |
153 | public void run() { | |
154 | try { | |
c4c81d91 | 155 | final IEditorInput editorInput = new TmfEditorInput(file, trace); |
9c8e0546 BH |
156 | final IWorkbench wb = PlatformUI.getWorkbench(); |
157 | final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage(); | |
158 | ||
c4c81d91 | 159 | final IEditorPart editor = activePage.findEditor(new FileEditorInput(file)); |
9c8e0546 BH |
160 | if ((editor != null) && (editor instanceof IReusableEditor)) { |
161 | activePage.reuseEditor((IReusableEditor) editor, editorInput); | |
162 | activePage.activate(editor); | |
163 | } else { | |
164 | activePage.openEditor(editorInput, editorId); | |
c4c81d91 PT |
165 | IDE.setDefaultEditor(file, editorId); |
166 | // editor should dispose the trace on close | |
9c8e0546 BH |
167 | } |
168 | } catch (final PartInitException e) { | |
169 | displayErrorMsg(Messages.OpenTraceHandler_Error + "\n\n" + e.getMessage()); //$NON-NLS-1$ | |
170 | Activator.getDefault().logError("Error opening trace " + traceElement.getName(), e); //$NON-NLS-1$ | |
171 | trace.dispose(); | |
172 | } | |
173 | } | |
174 | }); | |
175 | ||
12c155f5 | 176 | } |
9c8e0546 BH |
177 | }; |
178 | ||
179 | thread.start(); | |
12c155f5 FC |
180 | return null; |
181 | } | |
182 | ||
abbdd66a | 183 | private static void displayErrorMsg(final String errorMsg) { |
9c8e0546 BH |
184 | Display.getDefault().asyncExec(new Runnable() { |
185 | @Override | |
186 | public void run() { | |
187 | final MessageBox mb = new MessageBox(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell()); | |
188 | mb.setText(Messages.OpenTraceHandler_Title); | |
189 | mb.setMessage(errorMsg); | |
190 | mb.open(); | |
191 | } | |
192 | }); | |
12c155f5 FC |
193 | } |
194 | ||
195 | } |