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
.FileNotFoundException
;
17 import java
.io
.InputStream
;
18 import java
.util
.List
;
20 import org
.eclipse
.core
.commands
.AbstractHandler
;
21 import org
.eclipse
.core
.commands
.ExecutionEvent
;
22 import org
.eclipse
.core
.commands
.ExecutionException
;
23 import org
.eclipse
.core
.resources
.IFile
;
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
.ITmfEvent
;
30 import org
.eclipse
.linuxtools
.tmf
.core
.event
.TmfEvent
;
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
.ui
.editors
.EventsViewEditor
;
36 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEditorInput
;
37 import org
.eclipse
.linuxtools
.tmf
.ui
.editors
.TmfEventsEditor
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfExperimentElement
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.project
.model
.TmfTraceElement
;
40 import org
.eclipse
.swt
.widgets
.MessageBox
;
41 import org
.eclipse
.ui
.IEditorInput
;
42 import org
.eclipse
.ui
.IEditorPart
;
43 import org
.eclipse
.ui
.IReusableEditor
;
44 import org
.eclipse
.ui
.IWorkbench
;
45 import org
.eclipse
.ui
.IWorkbenchPage
;
46 import org
.eclipse
.ui
.IWorkbenchPart
;
47 import org
.eclipse
.ui
.IWorkbenchWindow
;
48 import org
.eclipse
.ui
.PlatformUI
;
49 import org
.eclipse
.ui
.ide
.IDE
;
50 import org
.eclipse
.ui
.part
.FileEditorInput
;
53 * <b><u>OpenExperimentHandler</u></b>
56 public class OpenExperimentHandler
extends AbstractHandler
{
58 private static final String BOOKMARKS_HIDDEN_FILE
= ".bookmarks"; //$NON-NLS-1$
60 private TmfExperimentElement fExperiment
= null;
62 // ------------------------------------------------------------------------
64 // ------------------------------------------------------------------------
67 public boolean isEnabled() {
69 // Check if we are closing down
70 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
76 IWorkbenchPage page
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
77 IWorkbenchPart part
= page
.getActivePart();
78 ISelectionProvider selectionProvider
= part
.getSite().getSelectionProvider();
79 if (selectionProvider
== null) {
82 ISelection selection
= selectionProvider
.getSelection();
84 // Make sure there is only one selection and that it is an experiment
86 if (selection
instanceof TreeSelection
) {
87 TreeSelection sel
= (TreeSelection
) selection
;
88 // There should be only one item selected as per the plugin.xml
89 Object element
= sel
.getFirstElement();
90 if (element
instanceof TmfExperimentElement
) {
91 fExperiment
= (TmfExperimentElement
) element
;
95 // We only enable opening from the Traces folder for now
96 return (fExperiment
!= null);
99 // ------------------------------------------------------------------------
101 // ------------------------------------------------------------------------
103 @SuppressWarnings({ "rawtypes", "unchecked" })
105 public Object
execute(ExecutionEvent event
) throws ExecutionException
{
107 // Check if we are closing down
108 IWorkbenchWindow window
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow();
109 if (window
== null) {
114 IFile bookmarksFile
= fExperiment
.getProject().getExperimentsFolder().getResource().getFile(BOOKMARKS_HIDDEN_FILE
);
115 if (!bookmarksFile
.exists()) {
116 InputStream source
= new ByteArrayInputStream(new byte[0]);
117 bookmarksFile
.create(source
, true, null);
119 bookmarksFile
.setHidden(true);
121 IFile file
= fExperiment
.getResource().getFile(fExperiment
.getName() + '_');
122 if (!file
.exists()) {
123 file
.createLink(bookmarksFile
.getLocation(), IResource
.REPLACE
, null);
125 file
.setHidden(true);
126 file
.setPersistentProperty(TmfTraceElement
.TRACETYPE
, TmfExperiment
.class.getCanonicalName());
128 // Instantiate the experiment's traces
129 List
<TmfTraceElement
> traceEntries
= fExperiment
.getTraces();
130 int nbTraces
= traceEntries
.size();
131 int cacheSize
= Integer
.MAX_VALUE
;
132 boolean useEditor
= true;
133 String experimentEditorId
= null;
134 ITmfTrace
<?
>[] traces
= new ITmfTrace
[nbTraces
];
135 for (int i
= 0; i
< nbTraces
; i
++) {
136 TmfTraceElement element
= traceEntries
.get(i
);
137 ITmfTrace trace
= element
.instantiateTrace();
138 ITmfEvent traceEvent
= element
.instantiateEvent();
139 if ((trace
== null) || (traceEvent
== null)) {
140 displayErrorMsg(Messages
.OpenExperimentHandler_NoTraceType
);
141 for (int j
= 0; j
< i
; j
++) {
147 trace
.initTrace(element
.getName(), element
.getLocation().getPath(), traceEvent
.getClass());
148 } catch (FileNotFoundException e
) {
149 displayErrorMsg(""); //$NON-NLS-1$
151 trace
.setResource(element
.getResource());
152 cacheSize
= Math
.min(cacheSize
, trace
.getIndexPageSize());
153 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
= new TmfExperiment(TmfEvent
.class, fExperiment
.getName(), traces
, cacheSize
);
169 experiment
.setBookmarksFile(file
);
172 IEditorInput editorInput
= new TmfEditorInput(file
, experiment
);
173 IWorkbench wb
= PlatformUI
.getWorkbench();
174 IWorkbenchPage activePage
= wb
.getActiveWorkbenchWindow().getActivePage();
176 String editorId
= TmfEventsEditor
.ID
;
177 IEditorPart editor
= activePage
.findEditor(new FileEditorInput(file
));
178 if ((editor
!= null) && (editor
instanceof IReusableEditor
)) {
179 activePage
.reuseEditor((IReusableEditor
) editor
, editorInput
);
180 activePage
.activate(editor
);
182 activePage
.openEditor(editorInput
, editorId
);
184 experiment
.initTrace(null, null, null);
185 experiment
.indexTrace(true);
186 IDE
.setDefaultEditor(file
, editorId
);
187 // editor should dispose the experiment on close
189 TmfExperiment
.setCurrentExperiment(experiment
);
190 TmfSignalManager
.dispatchSignal(new TmfExperimentSelectedSignal(this, experiment
));
191 IDE
.setDefaultEditor(file
, EventsViewEditor
.ID
);
193 } catch (CoreException e
) {
194 displayErrorMsg(e
.getMessage());
200 private void displayErrorMsg(String errorMsg
) {
201 MessageBox mb
= new MessageBox(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
202 mb
.setText(Messages
.OpenExperimentHandler_Title
);
203 mb
.setMessage(errorMsg
);