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