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
;
17 import java
.util
.List
;
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
.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
.event
.TmfTimeRange
;
31 import org
.eclipse
.linuxtools
.tmf
.core
.exceptions
.TmfTraceException
;
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
.TmfExperiment
;
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
.editors
.TmfEventsEditor
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
41 import org
.eclipse
.swt
.widgets
.MessageBox
;
42 import org
.eclipse
.ui
.IEditorInput
;
43 import org
.eclipse
.ui
.IEditorPart
;
44 import org
.eclipse
.ui
.IReusableEditor
;
45 import org
.eclipse
.ui
.IWorkbench
;
46 import org
.eclipse
.ui
.IWorkbenchPage
;
47 import org
.eclipse
.ui
.IWorkbenchPart
;
48 import org
.eclipse
.ui
.IWorkbenchWindow
;
49 import org
.eclipse
.ui
.PlatformUI
;
50 import org
.eclipse
.ui
.ide
.IDE
;
51 import org
.eclipse
.ui
.part
.FileEditorInput
;
54 * <b><u>OpenExperimentHandler</u></b>
57 public class OpenExperimentHandler
extends AbstractHandler
{
59 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
61 private TmfExperimentElement fExperiment
= null;
63 // ------------------------------------------------------------------------
65 // ------------------------------------------------------------------------
68 public boolean isEnabled() {
70 // Check if we are closing down
71 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
76 final IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
77 final IWorkbenchPart part
= page
.getActivePart();
78 final ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
79 if (selectionProvider
== null)
81 final ISelection selection
= selectionProvider
.getSelection();
83 // Make sure there is only one selection and that it is an experiment
85 if (selection
instanceof TreeSelection
) {
86 final TreeSelection sel
= (TreeSelection
) selection
;
87 // There should be only one item selected as per the plugin.xml
88 final Object element
= sel
.getFirstElement();
89 if (element
instanceof TmfExperimentElement
)
90 fExperiment
= (TmfExperimentElement
) element
;
93 // We only enable opening from the Traces folder for now
94 return (fExperiment
!= null);
97 // ------------------------------------------------------------------------
99 // ------------------------------------------------------------------------
101 @SuppressWarnings({ "rawtypes", "unchecked" })
103 public Object
execute(final ExecutionEvent event
) throws ExecutionException
{
105 // Check if we are closing down
106 final IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
111 final IFile bookmarksFile
= fExperiment
.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
112 if (!bookmarksFile
.exists()) {
113 final InputStream source
= new ByteArrayInputStream(new byte[0]);
114 bookmarksFile
.create(source
, true, null);
116 bookmarksFile
.setHidden(true);
118 final IFile file
= fExperiment
.getResource().getFile(fExperiment
.getName() + '_');
119 if (!file
.exists()) {
120 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
122 file
.setHidden(true);
123 file
.setPersistentProperty(TmfCommonConstants
.TRACETYPE
, TmfExperiment
.class.getCanonicalName());
125 // Instantiate the experiment's traces
126 final List
<TmfTraceElement
> traceEntries
= fExperiment
.getTraces();
127 final int nbTraces
= traceEntries
.size();
128 int cacheSize
= Integer
.MAX_VALUE
;
129 boolean useEditor
= true;
130 String experimentEditorId
= null;
131 final ITmfTrace
<?
>[] traces
= new ITmfTrace
[nbTraces
];
132 for (int i
= 0; i
< nbTraces
; i
++) {
133 TmfTraceElement element
= traceEntries
.get(i
);
135 // Since trace is under an experiment, use the original trace from the traces folder
136 element
= element
.getElementUnderTraceFolder();
138 final ITmfTrace trace
= element
.instantiateTrace();
139 final ITmfEvent traceEvent
= element
.instantiateEvent();
140 if ((trace
== null) || (traceEvent
== null)) {
141 displayErrorMsg(Messages
.OpenExperimentHandler_NoTraceType
);
142 for (int j
= 0; j
< i
; j
++) {
148 trace
.initTrace(element
.getResource(), element
.getLocation().getPath(), traceEvent
.getClass());
149 } catch (final TmfTraceException e
) {
150 displayErrorMsg(""); //$NON-NLS-1$
152 cacheSize
= Math
.min(cacheSize
, trace
.getCacheSize());
153 final String editorId
= element
.getEditorId();
154 if (editorId
== null) {
156 experimentEditorId
= null;
157 } else if (useEditor
) {
158 if (experimentEditorId
== null) {
159 experimentEditorId
= editorId
;
160 } else if (!editorId
.equals(experimentEditorId
)) {
167 // Create the experiment
168 TmfExperiment experiment
;
170 experiment
= new TmfExperiment(ITmfEvent
.class, fExperiment
.getName(), traces
, cacheSize
) {
172 public void initTrace(IResource resource
, String path
, Class type
) {
173 super.initTrace(resource
, path
, type
);
174 getIndexer().buildIndex(getNbEvents(), TmfTimeRange
.ETERNITY
, false);
178 experiment
= new TmfExperiment(ITmfEvent
.class, fExperiment
.getName(), traces
, cacheSize
);
180 experiment
.setBookmarksFile(file
);
183 final IEditorInput editorInput
= new TmfEditorInput(file
, experiment
);
184 final IWorkbench wb
= PlatformUI
.getWorkbench();
185 final IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
187 final String editorId
= TmfEventsEditor
.ID
;
188 final IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
189 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
190 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
191 activePage
.activate(editor
);
193 activePage
.openEditor(editorInput
, editorId
);
195 experiment
.initTrace(null, null, null);
196 IDE
.setDefaultEditor(file
, editorId
);
197 // editor should dispose the experiment on close
199 TmfExperiment
.setCurrentExperiment(experiment
);
200 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
201 IDE
.setDefaultEditor(file
, EventsViewEditor
.ID
);
203 } catch (final CoreException e
) {
204 displayErrorMsg(e
.getMessage());
210 private void displayErrorMsg(final String errorMsg
) {
211 final MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
212 mb
.setText(Messages
.OpenExperimentHandler_Title
);
213 mb
.setMessage(errorMsg
);