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
.LTTngTrace
;
30 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.ILTTngProjectTreeNode
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngExperimentNode
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectContentProvider
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectLabelProvider
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectRoot
;
35 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngTraceNode
;
36 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
37 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
38 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
39 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
40 import org
.eclipse
.swt
.SWT
;
41 import org
.eclipse
.swt
.events
.MouseAdapter
;
42 import org
.eclipse
.swt
.events
.MouseEvent
;
43 import org
.eclipse
.swt
.widgets
.Composite
;
44 import org
.eclipse
.swt
.widgets
.Menu
;
45 import org
.eclipse
.swt
.widgets
.Tree
;
48 * <b><u>ProjectView</u></b>
50 * The ProjectView keeps track of the LTTng projects in the workspace.
52 public class ProjectView
extends TmfView
{
54 public static final String ID
= "org.eclipse.linuxtools.lttng.ui.views.project";
56 // ------------------------------------------------------------------------
57 // Main data structures
58 // ------------------------------------------------------------------------
60 private TreeViewer fViewer
;
61 private LTTngProjectRoot fProjectRoot
;
62 private TmfExperiment
<LttngEvent
> fSelectedExperiment
= null;
64 private IWorkspace fWorkspace
;
65 private IResourceChangeListener fResourceChangeListener
;
67 // ------------------------------------------------------------------------
69 // ------------------------------------------------------------------------
71 // Perform updates on the UI thread
72 private Runnable fViewRefresher
= new Runnable() {
74 if (fViewer
!= null) {
75 Object
[] elements
= fViewer
.getExpandedElements();
77 fViewer
.setExpandedElements(elements
);
82 public LTTngProjectRoot
getRoot() {
86 // ------------------------------------------------------------------------
88 // ------------------------------------------------------------------------
90 public ProjectView() {
93 fProjectRoot
= new LTTngProjectRoot(this);
95 fWorkspace
= ResourcesPlugin
.getWorkspace();
96 fResourceChangeListener
= new IResourceChangeListener() {
97 public void resourceChanged(IResourceChangeEvent event
) {
98 if (event
.getType() == IResourceChangeEvent
.POST_CHANGE
) {
99 fProjectRoot
.refreshChildren();
104 fWorkspace
.addResourceChangeListener(fResourceChangeListener
);
107 public void refresh() {
108 Tree tree
= fViewer
.getTree();
109 if (tree
!= null && !tree
.isDisposed())
110 tree
.getDisplay().asyncExec(fViewRefresher
);
113 public void setSelection(ILTTngProjectTreeNode node
) {
114 fViewer
.setSelection(new StructuredSelection(node
), true);
118 public void dispose() {
119 fWorkspace
.removeResourceChangeListener(fResourceChangeListener
);
123 public void createPartControl(Composite parent
) {
125 fViewer
= new TreeViewer(parent
, SWT
.SINGLE
);
126 fViewer
.setContentProvider(new LTTngProjectContentProvider());
127 fViewer
.setSorter(new ViewerSorter());
128 fViewer
.setLabelProvider(new LTTngProjectLabelProvider());
129 fViewer
.setInput(fProjectRoot
);
131 getSite().setSelectionProvider(fViewer
);
137 // ------------------------------------------------------------------------
139 // ------------------------------------------------------------------------
142 public String
toString() {
143 return "[ProjectView]";
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
150 private void hookMouse() {
151 fViewer
.getTree().addMouseListener(new MouseAdapter() {
153 public void mouseDoubleClick(MouseEvent event
) {
154 TreeSelection selection
= (TreeSelection
) fViewer
.getSelection();
155 Object element
= selection
.getFirstElement();
156 if (element
instanceof LTTngExperimentNode
) {
157 LTTngExperimentNode experiment
= (LTTngExperimentNode
) element
;
158 selectExperiment(experiment
);
164 private boolean waitForCompletion
= true;
169 public void selectExperiment(LTTngExperimentNode experiment
) {
170 String expId
= experiment
.getName();
171 if (fSelectedExperiment
!= null) {
172 System
.out
.println(fSelectedExperiment
.getName() + ": nbEvents=" + fSelectedExperiment
.getNbEvents() +
173 ", nbReads=" + ((LTTngTrace
) fSelectedExperiment
.getTraces()[0]).nbEventsRead
);
174 fSelectedExperiment
.dispose();
177 LTTngTraceNode
[] traceEntries
= experiment
.getTraces();
178 int nbTraces
= traceEntries
.length
;
179 ITmfTrace
[] traces
= new ITmfTrace
[nbTraces
];
180 for (int i
= 0; i
< nbTraces
; i
++) {
181 IResource res
= traceEntries
[i
].getFolder();
182 String location
= res
.getLocation().toOSString();
183 ITmfTrace trace
= new LTTngTrace(location
, waitForCompletion
);
186 fSelectedExperiment
= new TmfExperiment
<LttngEvent
>(LttngEvent
.class, expId
, traces
);
188 // Make sure the lttng-core, experiment selection context is ready
189 // for an event request from any view
190 StateManagerFactory
.getExperimentManager().experimentSelected_prep(
191 (TmfExperiment
<LttngEvent
>) fSelectedExperiment
);
193 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
194 } catch (FileNotFoundException e
) {
196 } catch (Exception e
) {
201 // ------------------------------------------------------------------------
203 // ------------------------------------------------------------------------
205 // Populated from the plug-in
206 private void createContextMenu() {
207 MenuManager menuManager
= new MenuManager("#PopupMenu");
208 menuManager
.setRemoveAllWhenShown(true);
209 Menu menu
= menuManager
.createContextMenu(fViewer
.getControl());
210 fViewer
.getControl().setMenu(menu
);
211 getSite().registerContextMenu(menuManager
, fViewer
);