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
.MessageBox
;
38 import org
.eclipse
.ui
.IEditorInput
;
39 import org
.eclipse
.ui
.IEditorPart
;
40 import org
.eclipse
.ui
.IReusableEditor
;
41 import org
.eclipse
.ui
.IWorkbench
;
42 import org
.eclipse
.ui
.IWorkbenchPage
;
43 import org
.eclipse
.ui
.IWorkbenchPart
;
44 import org
.eclipse
.ui
.IWorkbenchWindow
;
45 import org
.eclipse
.ui
.PartInitException
;
46 import org
.eclipse
.ui
.PlatformUI
;
47 import org
.eclipse
.ui
.ide
.IDE
;
48 import org
.eclipse
.ui
.part
.FileEditorInput
;
51 * <b><u>OpenTraceHandler</u></b>
53 * TODO: Add support for multiple trace selection
55 public class OpenTraceHandler
extends AbstractHandler
{
57 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
59 // ------------------------------------------------------------------------
61 // ------------------------------------------------------------------------
63 private TmfTraceElement fTrace
= null;
65 // ------------------------------------------------------------------------
67 // ------------------------------------------------------------------------
70 public boolean isEnabled() {
72 // Check if we are closing down
73 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
79 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
80 final IWorkbenchPart part
= page
.getActivePart();
81 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
82 if (selectionProvider
== null) {
85 final ISelection selection
= selectionProvider
.getSelection();
87 // Make sure there is only one selection and that it is a trace
89 if (selection
instanceof TreeSelection
) {
90 final TreeSelection sel
= (TreeSelection
) selection
;
91 // There should be only one item selected as per the plugin.xml
92 final Object element
= sel
.getFirstElement();
93 if (element
instanceof TmfTraceElement
) {
94 fTrace
= (TmfTraceElement
) element
;
98 // We only enable opening from the Traces folder for now
99 return (fTrace
!= null);
102 // ------------------------------------------------------------------------
104 // ------------------------------------------------------------------------
107 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
109 // Check if we are closing down
110 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
111 if (window
== null) {
115 // Check that the trace is valid
116 if (fTrace
== null) {
120 // If trace is under an experiment, use the original trace from the traces folder
121 fTrace
= fTrace
.getElementUnderTraceFolder();
123 final ITmfTrace trace
= fTrace
.instantiateTrace();
124 final ITmfEvent traceEvent
= fTrace
.instantiateEvent();
125 if ((trace
== null) || (traceEvent
== null)) {
126 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
130 // Get the editor_id from the extension point
131 String traceEditorId
= fTrace
.getEditorId();
132 final String editorId
= (traceEditorId
!= null) ? traceEditorId
: TmfEventsEditor
.ID
;
135 trace
.initTrace(fTrace
.getResource(), fTrace
.getLocation().getPath(), traceEvent
.getClass());
136 } catch (final TmfTraceException e
) {
137 displayErrorMsg(Messages
.OpenTraceHandler_NoTrace
+ "\n\n" + e
); //$NON-NLS-1$
141 final IResource resource
= fTrace
.getResource();
143 if (resource
instanceof IFile
) {
144 file
= (IFile
) resource
;
145 } else if (resource
instanceof IFolder
) {
147 final IFile bookmarksFile
= fTrace
.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
148 if (!bookmarksFile
.exists()) {
149 final InputStream source
= new ByteArrayInputStream(new byte[0]);
150 bookmarksFile
.create(source
, true, null);
152 bookmarksFile
.setHidden(true);
154 final IFolder folder
= (IFolder
) resource
;
155 file
= folder
.getFile(fTrace
.getName() + '_');
156 if (!file
.exists()) {
157 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
159 file
.setHidden(true);
160 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfTrace
.class.getCanonicalName());
161 IDE
.setDefaultEditor(file
, editorId
);
162 } catch (final CoreException e
) {
163 Activator
.getDefault().logError("Error opening trace " + fTrace
.getName(), e
); //$NON-NLS-1$
168 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
169 final IWorkbench wb
= PlatformUI
.getWorkbench();
170 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
172 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
173 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
174 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
175 activePage
.activate(editor
);
177 activePage
.openEditor(editorInput
, editorId
);
178 if (resource
instanceof IFile
) {
179 IDE
.setDefaultEditor((IFile
) resource
, editorId
);
182 } catch (final PartInitException e
) {
183 Activator
.getDefault().logError("Error opening trace " + fTrace
.getName(), e
); //$NON-NLS-1$
188 private static void displayErrorMsg(final String errorMsg
) {
189 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
190 mb
.setText(Messages
.OpenTraceHandler_Title
);
191 mb
.setMessage(errorMsg
);