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
.tmf
.ui
.project
.handlers
;
15 import java
.io
.ByteArrayInputStream
;
16 import java
.io
.FileNotFoundException
;
17 import java
.io
.InputStream
;
19 import org
.eclipse
.core
.commands
.AbstractHandler
;
20 import org
.eclipse
.core
.commands
.ExecutionEvent
;
21 import org
.eclipse
.core
.commands
.ExecutionException
;
22 import org
.eclipse
.core
.resources
.IFile
;
23 import org
.eclipse
.core
.resources
.IFolder
;
24 import org
.eclipse
.core
.resources
.IResource
;
25 import org
.eclipse
.core
.runtime
.CoreException
;
26 import org
.eclipse
.jface
.viewers
.ISelection
;
27 import org
.eclipse
.jface
.viewers
.ISelectionProvider
;
28 import org
.eclipse
.jface
.viewers
.TreeSelection
;
29 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.experiment
.TmfExperiment
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfExperimentSelectedSignal
;
32 import org
.eclipse
.linuxtools
.tmf
.core
.signal
.TmfSignalManager
;
33 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.ITmfTrace
;
34 import org
.eclipse
.linuxtools
.tmf
.core
.trace
.TmfTrace
;
35 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.EventsViewEditor
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
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 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
80 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
81 IWorkbenchPart part
= page
.getActivePart();
82 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
83 if (selectionProvider
== null)
85 ISelection selection
= selectionProvider
.getSelection();
87 // Make sure there is only one selection and that it is a trace
89 if (selection
instanceof TreeSelection
) {
90 TreeSelection sel
= (TreeSelection
) selection
;
91 // There should be only one item selected as per the plugin.xml
92 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 @SuppressWarnings({ "rawtypes", "unchecked" })
108 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
110 // Check if we are closing down
111 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
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 if (fTrace
.getParent() instanceof TmfExperimentElement
) {
122 for (TmfTraceElement trace
: fTrace
.getProject().getTracesFolder().getTraces()) {
123 if (trace
.getName().equals(fTrace
.getName())) {
130 ITmfTrace trace
= fTrace
.instantiateTrace();
131 TmfEvent traceEvent
= fTrace
.instantiateEvent();
132 if (trace
== null || traceEvent
== null) {
133 displayErrorMsg(Messages
.OpenTraceHandler_NoTraceType
);
136 trace
.setResource(fTrace
.getResource());
138 // Get the editor_id from the extension point
139 String editorId
= fTrace
.getEditorId();
140 boolean usesEditor
= editorId
!= null && editorId
.length() > 0;
143 trace
.initTrace(fTrace
.getName(), fTrace
.getLocation().getPath(), traceEvent
.getClass(), usesEditor
);
144 } catch (FileNotFoundException e
) {
145 displayErrorMsg(Messages
.OpenTraceHandler_NoTrace
);
149 IResource resource
= fTrace
.getResource();
151 if (resource
instanceof IFile
) {
152 file
= (IFile
) resource
;
153 } else if (resource
instanceof IFolder
){
155 IFile bookmarksFile
= fTrace
.getProject().getTracesFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
156 if (!bookmarksFile
.exists()) {
157 InputStream source
= new ByteArrayInputStream(new byte[0]);
158 bookmarksFile
.create(source
, true, null);
160 bookmarksFile
.setHidden(true);
162 IFolder folder
= (IFolder
) resource
;
163 file
= folder
.getFile(fTrace
.getName() + '_');
164 if (!file
.exists()) {
165 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
167 file
.setHidden(true);
169 file
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, fTrace
.getTraceType());
171 file
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, TmfTrace
.class.getCanonicalName());
173 } catch (CoreException e
) {
179 if (trace
instanceof TmfTrace
) {
180 ((TmfTrace
) trace
).setResource(file
);
183 IEditorInput editorInput
= new TmfEditorInput(file
, trace
);
184 IWorkbench wb
= PlatformUI
.getWorkbench();
185 IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
187 IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
188 if (editor
!= null && editor
instanceof IReusableEditor
) {
189 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
190 activePage
.activate(editor
);
192 editor
= activePage
.openEditor(editorInput
, editorId
);
193 if (resource
instanceof IFile
) {
194 IDE
.setDefaultEditor((IFile
) resource
, editorId
);
197 } catch (PartInitException e
) {
201 // Create the experiment
202 ITmfTrace
[] traces
= new ITmfTrace
[] { trace
};
203 TmfExperiment experiment
= new TmfExperiment(traceEvent
.getClass(), fTrace
.getName(), traces
, trace
.getCacheSize());
204 experiment
.setBookmarksFile(file
);
206 TmfExperiment
.setCurrentExperiment(experiment
);
207 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
208 IDE
.setDefaultEditor(file
, EventsViewEditor
.ID
);
213 private void displayErrorMsg(String errorMsg
) {
214 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
215 mb
.setText(Messages
.OpenTraceHandler_Title
);
216 mb
.setMessage(errorMsg
);