1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 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
.lttng
.ui
.views
.project
;
15 import java
.io
.FileNotFoundException
;
17 import org
.eclipse
.core
.resources
.IResource
;
18 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
19 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
20 import org
.eclipse
.core
.resources
.IWorkspace
;
21 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
22 import org
.eclipse
.jface
.action
.MenuManager
;
23 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
24 import org
.eclipse
.jface
.viewers
.TreeSelection
;
25 import org
.eclipse
.jface
.viewers
.TreeViewer
;
26 import org
.eclipse
.jface
.viewers
.ViewerSorter
;
27 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEvent
;
28 import org
.eclipse
.linuxtools
.lttng
.state
.experiment
.StateManagerFactory
;
29 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngExperiment
;
30 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngTrace
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.ILTTngProjectTreeNode
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngExperimentNode
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectContentProvider
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectLabelProvider
;
35 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectRoot
;
36 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngTraceNode
;
37 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
38 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
39 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
40 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
41 import org
.eclipse
.swt
.SWT
;
42 import org
.eclipse
.swt
.events
.MouseAdapter
;
43 import org
.eclipse
.swt
.events
.MouseEvent
;
44 import org
.eclipse
.swt
.widgets
.Composite
;
45 import org
.eclipse
.swt
.widgets
.Menu
;
46 import org
.eclipse
.swt
.widgets
.Tree
;
49 * <b><u>ProjectView</u></b>
51 * The ProjectView keeps track of the LTTng projects in the workspace.
53 public class ProjectView
extends TmfView
{
55 public static final String ID
= "org.eclipse.linuxtools.lttng.ui.views.project"; //$NON-NLS-1$
57 // ------------------------------------------------------------------------
58 // Main data structures
59 // ------------------------------------------------------------------------
61 private TreeViewer fViewer
;
62 private LTTngProjectRoot fProjectRoot
;
63 private LTTngExperiment
<LttngEvent
> fSelectedExperiment
= null;
65 private IWorkspace fWorkspace
;
66 private IResourceChangeListener fResourceChangeListener
;
68 // ------------------------------------------------------------------------
70 // ------------------------------------------------------------------------
72 // Perform updates on the UI thread
73 private Runnable fViewRefresher
= new Runnable() {
76 if ((fViewer
!= null) && (!fViewer
.getTree().isDisposed())) {
77 Object
[] elements
= fViewer
.getExpandedElements();
79 fViewer
.setExpandedElements(elements
);
84 public LTTngProjectRoot
getRoot() {
88 // ------------------------------------------------------------------------
90 // ------------------------------------------------------------------------
92 public ProjectView() {
94 super("ProjectView"); //$NON-NLS-1$
95 fProjectRoot
= new LTTngProjectRoot(this);
97 fWorkspace
= ResourcesPlugin
.getWorkspace();
98 fResourceChangeListener
= new IResourceChangeListener() {
100 public void resourceChanged(IResourceChangeEvent event
) {
101 if (event
.getType() == IResourceChangeEvent
.POST_CHANGE
) {
102 fProjectRoot
.refreshChildren();
107 fWorkspace
.addResourceChangeListener(fResourceChangeListener
);
110 public void refresh() {
111 Tree tree
= fViewer
.getTree();
112 if (tree
!= null && !tree
.isDisposed())
113 tree
.getDisplay().asyncExec(fViewRefresher
);
116 public void setSelection(ILTTngProjectTreeNode node
) {
117 fViewer
.setSelection(new StructuredSelection(node
), true);
121 public void dispose() {
122 fWorkspace
.removeResourceChangeListener(fResourceChangeListener
);
126 public void createPartControl(Composite parent
) {
128 fViewer
= new TreeViewer(parent
, SWT
.SINGLE
);
129 fViewer
.setContentProvider(new LTTngProjectContentProvider());
130 fViewer
.setSorter(new ViewerSorter());
131 fViewer
.setLabelProvider(new LTTngProjectLabelProvider());
132 fViewer
.setInput(fProjectRoot
);
134 getSite().setSelectionProvider(fViewer
);
140 // ------------------------------------------------------------------------
142 // ------------------------------------------------------------------------
145 @SuppressWarnings("nls")
146 public String
toString() {
147 return "[ProjectView]";
150 // ------------------------------------------------------------------------
152 // ------------------------------------------------------------------------
154 private void hookMouse() {
155 fViewer
.getTree().addMouseListener(new MouseAdapter() {
157 public void mouseDoubleClick(MouseEvent event
) {
158 TreeSelection selection
= (TreeSelection
) fViewer
.getSelection();
159 Object element
= selection
.getFirstElement();
160 if (element
instanceof LTTngExperimentNode
) {
161 LTTngExperimentNode experiment
= (LTTngExperimentNode
) element
;
162 selectExperiment(experiment
);
164 if (element
instanceof LTTngTraceNode
) {
165 LTTngTraceNode trace
= (LTTngTraceNode
) element
;
173 private void selectTrace(LTTngTraceNode traceNode
) {
174 if (fSelectedExperiment
!= null) {
175 fSelectedExperiment
.dispose();
179 ITmfTrace
[] traces
= new ITmfTrace
[1];
180 IResource res
= traceNode
.getFolder();
181 String location
= res
.getLocation().toOSString();
182 ITmfTrace trace
= new LTTngTrace(location
, waitForCompletion
);
184 fSelectedExperiment
= new LTTngExperiment
<LttngEvent
>(LttngEvent
.class, traceNode
.getName(), traces
);
185 TmfExperiment
.setCurrentExperiment(fSelectedExperiment
);
187 // Make sure the lttng-core, experiment selection context is ready
188 // for an event request from any view
189 StateManagerFactory
.getExperimentManager().experimentSelected_prep(
190 (TmfExperiment
<LttngEvent
>) fSelectedExperiment
);
192 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
193 } catch (FileNotFoundException e
) {
195 } catch (Exception e
) {
200 private boolean waitForCompletion
= true;
205 public void selectExperiment(LTTngExperimentNode experiment
) {
206 String expId
= experiment
.getName();
207 if (fSelectedExperiment
!= null) {
208 // System.out.println(fSelectedExperiment.getName() + ": nbEvents=" + fSelectedExperiment.getNbEvents() +
209 // ", nbReads=" + ((LTTngTrace) fSelectedExperiment.getTraces()[0]).nbEventsRead);
210 fSelectedExperiment
.dispose();
213 LTTngTraceNode
[] traceEntries
= experiment
.getTraces();
214 int nbTraces
= traceEntries
.length
;
215 ITmfTrace
[] traces
= new ITmfTrace
[nbTraces
];
216 for (int i
= 0; i
< nbTraces
; i
++) {
217 IResource res
= traceEntries
[i
].getFolder();
218 String location
= res
.getLocation().toOSString();
219 ITmfTrace trace
= new LTTngTrace(location
, waitForCompletion
);
222 fSelectedExperiment
= new LTTngExperiment
<LttngEvent
>(LttngEvent
.class, expId
, traces
);
223 TmfExperiment
.setCurrentExperiment(fSelectedExperiment
);
225 // Make sure the lttng-core, experiment selection context is ready
226 // for an event request from any view
227 StateManagerFactory
.getExperimentManager().experimentSelected_prep(
228 (TmfExperiment
<LttngEvent
>) fSelectedExperiment
);
230 // System.out.println(System.currentTimeMillis() + ": Experiment selected");
231 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
232 } catch (FileNotFoundException e
) {
234 } catch (Exception e
) {
239 // ------------------------------------------------------------------------
241 // ------------------------------------------------------------------------
243 // Populated from the plug-in
244 private void createContextMenu() {
245 MenuManager menuManager
= new MenuManager("#PopupMenu"); //$NON-NLS-1$
246 menuManager
.setRemoveAllWhenShown(true);
247 Menu menu
= menuManager
.createContextMenu(fViewer
.getControl());
248 fViewer
.getControl().setMenu(menu
);
249 getSite().registerContextMenu(menuManager
, fViewer
);