Close trace and improve error handling on delete and rename commands
[deliverable/tracecompass.git] / org.eclipse.linuxtools.tmf.ui / src / org / eclipse / linuxtools / internal / tmf / ui / project / handlers / OpenTraceHandler.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 org.eclipse.core.commands.AbstractHandler;
16 import org.eclipse.core.commands.ExecutionEvent;
17 import org.eclipse.core.commands.ExecutionException;
18 import org.eclipse.core.resources.IFile;
19 import org.eclipse.core.runtime.CoreException;
20 import org.eclipse.jface.viewers.ISelection;
21 import org.eclipse.jface.viewers.ISelectionProvider;
22 import org.eclipse.jface.viewers.TreeSelection;
23 import org.eclipse.linuxtools.internal.tmf.ui.Activator;
24 import org.eclipse.linuxtools.tmf.core.event.ITmfEvent;
25 import org.eclipse.linuxtools.tmf.core.exceptions.TmfTraceException;
26 import org.eclipse.linuxtools.tmf.core.trace.ITmfTrace;
27 import org.eclipse.linuxtools.tmf.ui.editors.TmfEditorInput;
28 import org.eclipse.linuxtools.tmf.ui.editors.TmfEventsEditor;
29 import org.eclipse.linuxtools.tmf.ui.project.model.TmfTraceElement;
30 import org.eclipse.swt.widgets.Display;
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 import org.eclipse.ui.ide.IDE;
42 import org.eclipse.ui.part.FileEditorInput;
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
65 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
66 if (window == null) {
67 return false;
68 }
69
70 // Get the selection
71 final IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
72 final IWorkbenchPart part = page.getActivePart();
73 final ISelectionProvider selectionProvider = part.getSite().getSelectionProvider();
74 if (selectionProvider == null) {
75 return false;
76 }
77 final ISelection selection = selectionProvider.getSelection();
78
79 // Make sure there is only one selection and that it is a trace
80 fTrace = null;
81 if (selection instanceof TreeSelection) {
82 final TreeSelection sel = (TreeSelection) selection;
83 // There should be only one item selected as per the plugin.xml
84 final Object element = sel.getFirstElement();
85 if (element instanceof TmfTraceElement) {
86 fTrace = (TmfTraceElement) element;
87 }
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
99 public Object execute(final ExecutionEvent event) throws ExecutionException {
100
101 // Check if we are closing down
102 final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
103 if (window == null) {
104 return null;
105 }
106
107 // Check that the trace is valid
108 if (fTrace == null) {
109 return null;
110 }
111
112 // If trace is under an experiment, use the original trace from the traces folder
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;
127 }
128
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;
139 }
140
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;
149 }
150
151 Display.getDefault().asyncExec(new Runnable() {
152 @Override
153 public void run() {
154 try {
155 final IEditorInput editorInput = new TmfEditorInput(file, trace);
156 final IWorkbench wb = PlatformUI.getWorkbench();
157 final IWorkbenchPage activePage = wb.getActiveWorkbenchWindow().getActivePage();
158
159 final IEditorPart editor = activePage.findEditor(new FileEditorInput(file));
160 if ((editor != null) && (editor instanceof IReusableEditor)) {
161 activePage.reuseEditor((IReusableEditor) editor, editorInput);
162 activePage.activate(editor);
163 } else {
164 activePage.openEditor(editorInput, editorId);
165 IDE.setDefaultEditor(file, editorId);
166 // editor should dispose the trace on close
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
176 }
177 };
178
179 thread.start();
180 return null;
181 }
182
183 private static void displayErrorMsg(final String errorMsg) {
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 });
193 }
194
195 }
This page took 0.03696 seconds and 5 git commands to generate.