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
.tmf
.core
.TmfCommonConstants
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperiment
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.EventsViewEditor
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
39 import org
.eclipse
.swt
.widgets
.MessageBox
;
40 import org
.eclipse
.ui
.IEditorInput
;
41 import org
.eclipse
.ui
.IEditorPart
;
42 import org
.eclipse
.ui
.IReusableEditor
;
43 import org
.eclipse
.ui
.IWorkbench
;
44 import org
.eclipse
.ui
.IWorkbenchPage
;
45 import org
.eclipse
.ui
.IWorkbenchPart
;
46 import org
.eclipse
.ui
.IWorkbenchWindow
;
47 import org
.eclipse
.ui
.PartInitException
;
48 import org
.eclipse
.ui
.PlatformUI
;
49 import org
.eclipse
.ui
.ide
.IDE
;
50 import org
.eclipse
.ui
.part
.FileEditorInput
;
53 * <b><u>OpenTraceHandler</u></b>
55 * TODO: Add support for multiple trace selection
57 public class OpenTraceHandler
extends AbstractHandler
{
59 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
61 // ------------------------------------------------------------------------
63 // ------------------------------------------------------------------------
65 private TmfTraceElement fTrace
= null;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
72 public boolean isEnabled() {
74 // Check if we are closing down
75 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)
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
;
97 // We only enable opening from the Traces folder for now
98 return (fTrace
!= null);
101 // ------------------------------------------------------------------------
103 // ------------------------------------------------------------------------
106 @SuppressWarnings({ "rawtypes", "unchecked" })
107 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
109 // Check if we are closing down
110 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
114 // Check that the trace is valid
118 // If trace is under an experiment, use the original trace from the traces folder
119 fTrace
= fTrace
.getElementUnderTraceFolder();
121 final ITmfTrace trace
= fTrace
.instantiateTrace();
122 final ITmfEvent traceEvent
= fTrace
.instantiateEvent();
123 if ((trace
== null) || (traceEvent
== null)) {
124 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
128 // Get the editor_id from the extension point
129 final String editorId
= fTrace
.getEditorId();
130 final boolean usesEditor
= (editorId
!= null) && (editorId
.length() > 0);
133 trace
.initTrace(fTrace
.getResource(), fTrace
.getLocation().getPath(), traceEvent
.getClass());
134 } catch (final TmfTraceException e
) {
135 displayErrorMsg(Messages
.OpenTraceHandler_NoTrace
+ "\n\n" + e
); //$NON-NLS-1$
139 final IResource resource
= fTrace
.getResource();
141 if (resource
instanceof IFile
)
142 file
= (IFile
) resource
;
143 else if (resource
instanceof IFolder
)
145 final IFile bookmarksFile
= fTrace
.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
146 if (!bookmarksFile
.exists()) {
147 final InputStream source
= new ByteArrayInputStream(new byte[0]);
148 bookmarksFile
.create(source
, true, null);
150 bookmarksFile
.setHidden(true);
152 final IFolder folder
= (IFolder
) resource
;
153 file
= folder
.getFile(fTrace
.getName() + '_');
155 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
156 file
.setHidden(true);
158 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, fTrace
.getTraceType());
160 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfTrace
.class.getCanonicalName());
161 } catch (final CoreException e
) {
167 final IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
168 final IWorkbench wb
= PlatformUI
.getWorkbench();
169 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
171 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
172 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
173 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
174 activePage
.activate(editor
);
176 activePage
.openEditor(editorInput
, editorId
);
177 if (resource
instanceof IFile
)
178 IDE
.setDefaultEditor((IFile
) resource
, editorId
);
180 } catch (final PartInitException e
) {
184 // Create the experiment
185 final ITmfTrace
[] traces
= new ITmfTrace
[] { trace
};
186 final TmfExperiment experiment
= new TmfExperiment(traceEvent
.getClass(), fTrace
.getName(), traces
, trace
.getCacheSize());
187 experiment
.setBookmarksFile(file
);
189 TmfExperiment
.setCurrentExperiment(experiment
);
190 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
191 IDE
.setDefaultEditor(file
, EventsViewEditor
.ID
);
196 private void displayErrorMsg(final String errorMsg
) {
197 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
198 mb
.setText(Messages
.OpenTraceHandler_Title
);
199 mb
.setMessage(errorMsg
);