1 /*******************************************************************************
2 * Copyright (c) 2009, 2010, 2011 Ericsson
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
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
13 package org
.eclipse
.linuxtools
.internal
.tmf
.ui
.project
.handlers
;
15 import java
.io
.ByteArrayInputStream
;
16 import java
.io
.InputStream
;
18 import org
.eclipse
.core
.commands
.AbstractHandler
;
19 import org
.eclipse
.core
.commands
.ExecutionEvent
;
20 import org
.eclipse
.core
.commands
.ExecutionException
;
21 import org
.eclipse
.core
.resources
.IFile
;
22 import org
.eclipse
.core
.resources
.IFolder
;
23 import org
.eclipse
.core
.resources
.IResource
;
24 import org
.eclipse
.core
.runtime
.CoreException
;
25 import org
.eclipse
.jface
.viewers
.ISelection
;
26 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
27 import org
.eclipse
.jface
.viewers
.TreeSelection
;
28 import org
.eclipse
.linuxtools
.internal
.tmf
.ui
.Activator
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.TmfCommonConstants
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
37 import org
.eclipse
.swt
.widgets
.Display
;
38 import org
.eclipse
.swt
.widgets
.MessageBox
;
39 import org
.eclipse
.ui
.IEditorInput
;
40 import org
.eclipse
.ui
.IEditorPart
;
41 import org
.eclipse
.ui
.IReusableEditor
;
42 import org
.eclipse
.ui
.IWorkbench
;
43 import org
.eclipse
.ui
.IWorkbenchPage
;
44 import org
.eclipse
.ui
.IWorkbenchPart
;
45 import org
.eclipse
.ui
.IWorkbenchWindow
;
46 import org
.eclipse
.ui
.PartInitException
;
47 import org
.eclipse
.ui
.PlatformUI
;
48 import org
.eclipse
.ui
.ide
.IDE
;
49 import org
.eclipse
.ui
.part
.FileEditorInput
;
52 * <b><u>OpenTraceHandler</u></b>
54 * TODO: Add support for multiple trace selection
56 public class OpenTraceHandler
extends AbstractHandler
{
58 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
60 // ------------------------------------------------------------------------
62 // ------------------------------------------------------------------------
64 private TmfTraceElement fTrace
= null;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
71 public boolean isEnabled() {
73 // Check if we are closing down
74 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
80 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
81 final IWorkbenchPart part
= page
.getActivePart();
82 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
83 if (selectionProvider
== null) {
86 final ISelection selection
= selectionProvider
.getSelection();
88 // Make sure there is only one selection and that it is a trace
90 if (selection
instanceof TreeSelection
) {
91 final TreeSelection sel
= (TreeSelection
) selection
;
92 // There should be only one item selected as per the plugin.xml
93 final Object element
= sel
.getFirstElement();
94 if (element
instanceof TmfTraceElement
) {
95 fTrace
= (TmfTraceElement
) element
;
99 // We only enable opening from the Traces folder for now
100 return (fTrace
!= null);
103 // ------------------------------------------------------------------------
105 // ------------------------------------------------------------------------
108 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
110 // Check if we are closing down
111 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
112 if (window
== null) {
116 // Check that the trace is valid
117 if (fTrace
== null) {
121 // If trace is under an experiment, use the original trace from the traces folder
122 final TmfTraceElement traceElement
= fTrace
.getElementUnderTraceFolder();
124 Thread thread
= new Thread() {
128 final ITmfTrace trace
= traceElement
.instantiateTrace();
129 final ITmfEvent traceEvent
= traceElement
.instantiateEvent();
130 if ((trace
== null) || (traceEvent
== null)) {
131 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
138 // Get the editor_id from the extension point
139 String traceEditorId
= traceElement
.getEditorId();
140 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
143 trace
.initTrace(traceElement
.getResource(), traceElement
.getLocation().getPath(), traceEvent
.getClass());
144 } catch (final TmfTraceException e
) {
145 displayErrorMsg(Messages
.OpenTraceHandler_InitError
+ "\n\n" + e
); //$NON-NLS-1$
150 final IResource resource
= traceElement
.getResource();
152 if (resource
instanceof IFile
) {
153 file
= (IFile
) resource
;
154 } else if (resource
instanceof IFolder
) {
156 final IFile bookmarksFile
= traceElement
.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
157 if (!bookmarksFile
.exists()) {
158 final InputStream source
= new ByteArrayInputStream(new byte[0]);
159 bookmarksFile
.create(source
, true, null);
161 bookmarksFile
.setHidden(true);
163 final IFolder folder
= (IFolder
) resource
;
164 file
= folder
.getFile(traceElement
.getName() + '_');
165 if (!file
.exists()) {
166 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
168 file
.setHidden(true);
169 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfTrace
.class.getCanonicalName());
170 IDE
.setDefaultEditor(file
, editorId
);
171 // editor should dispose the trace on close
172 } catch (final CoreException e
) {
173 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
174 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
180 final IFile editorFile
= file
;
181 Display
.getDefault().asyncExec(new Runnable() {
185 final IEditorInput editorInput
= new TmfEditorInput(editorFile
, trace
);
186 final IWorkbench wb
= PlatformUI
.getWorkbench();
187 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
189 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(editorFile
));
190 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
191 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
192 activePage
.activate(editor
);
194 activePage
.openEditor(editorInput
, editorId
);
195 if (resource
instanceof IFile
) {
196 IDE
.setDefaultEditor((IFile
) resource
, editorId
);
199 } catch (final PartInitException e
) {
200 displayErrorMsg(Messages
.OpenTraceHandler_Error
+ "\n\n" + e
.getMessage()); //$NON-NLS-1$
201 Activator
.getDefault().logError("Error opening trace " + traceElement
.getName(), e
); //$NON-NLS-1$
214 private static void displayErrorMsg(final String errorMsg
) {
215 Display
.getDefault().asyncExec(new Runnable() {
218 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
219 mb
.setText(Messages
.OpenTraceHandler_Title
);
220 mb
.setMessage(errorMsg
);