1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 Ericsson, Montavista Software
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 * Yufen Kuo (ykuo@mvista.com) - add support to allow user specify trace library path
12 * Yufen Kuo (ykuo@mvista.com) - bug 340341: handle gracefully when native library failed to initialize
13 *******************************************************************************/
15 package org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
;
17 import java
.io
.FileNotFoundException
;
19 import org
.eclipse
.core
.resources
.IProject
;
20 import org
.eclipse
.core
.resources
.IResource
;
21 import org
.eclipse
.core
.resources
.IResourceChangeEvent
;
22 import org
.eclipse
.core
.resources
.IResourceChangeListener
;
23 import org
.eclipse
.core
.resources
.IWorkspace
;
24 import org
.eclipse
.core
.resources
.ResourcesPlugin
;
25 import org
.eclipse
.jface
.action
.MenuManager
;
26 import org
.eclipse
.jface
.dialogs
.MessageDialog
;
27 import org
.eclipse
.jface
.viewers
.StructuredSelection
;
28 import org
.eclipse
.jface
.viewers
.TreeSelection
;
29 import org
.eclipse
.jface
.viewers
.TreeViewer
;
30 import org
.eclipse
.jface
.viewers
.ViewerSorter
;
31 import org
.eclipse
.linuxtools
.lttng
.TraceHelper
;
32 import org
.eclipse
.linuxtools
.lttng
.event
.LttngEvent
;
33 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngExperiment
;
34 import org
.eclipse
.linuxtools
.lttng
.trace
.LTTngTrace
;
35 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.dialogs
.Messages
;
36 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.ILTTngProjectTreeNode
;
37 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngExperimentNode
;
38 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectContentProvider
;
39 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectLabelProvider
;
40 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectNode
;
41 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectRoot
;
42 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngTraceNode
;
43 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
44 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
45 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
46 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
47 import org
.eclipse
.swt
.SWT
;
48 import org
.eclipse
.swt
.events
.MouseAdapter
;
49 import org
.eclipse
.swt
.events
.MouseEvent
;
50 import org
.eclipse
.swt
.widgets
.Composite
;
51 import org
.eclipse
.swt
.widgets
.Menu
;
52 import org
.eclipse
.swt
.widgets
.Tree
;
55 * <b><u>ProjectView</u></b>
57 * The ProjectView keeps track of the LTTng projects in the workspace.
59 public class ProjectView
extends TmfView
{
61 public static final String ID
= "org.eclipse.linuxtools.lttng.ui.views.project"; //$NON-NLS-1$
63 // ------------------------------------------------------------------------
64 // Main data structures
65 // ------------------------------------------------------------------------
67 private TreeViewer fViewer
;
68 private LTTngProjectRoot fProjectRoot
;
69 private LTTngExperiment
<LttngEvent
> fSelectedExperiment
= null;
71 private IWorkspace fWorkspace
;
72 private IResourceChangeListener fResourceChangeListener
;
74 // ------------------------------------------------------------------------
76 // ------------------------------------------------------------------------
78 // Perform updates on the UI thread
79 private Runnable fViewRefresher
= new Runnable() {
82 if ((fViewer
!= null) && (!fViewer
.getTree().isDisposed())) {
83 Object
[] elements
= fViewer
.getExpandedElements();
85 fViewer
.setExpandedElements(elements
);
90 public LTTngProjectRoot
getRoot() {
94 // ------------------------------------------------------------------------
96 // ------------------------------------------------------------------------
98 public ProjectView() {
100 super("ProjectView"); //$NON-NLS-1$
101 fProjectRoot
= new LTTngProjectRoot(this);
103 fWorkspace
= ResourcesPlugin
.getWorkspace();
104 fResourceChangeListener
= new IResourceChangeListener() {
106 public void resourceChanged(IResourceChangeEvent event
) {
107 if (event
.getType() == IResourceChangeEvent
.POST_CHANGE
) {
108 fProjectRoot
.refreshChildren();
113 fWorkspace
.addResourceChangeListener(fResourceChangeListener
);
116 public void refresh() {
117 Tree tree
= fViewer
.getTree();
118 if (tree
!= null && !tree
.isDisposed())
119 tree
.getDisplay().asyncExec(fViewRefresher
);
122 public void setSelection(ILTTngProjectTreeNode node
) {
123 fViewer
.setSelection(new StructuredSelection(node
), true);
127 public void dispose() {
128 fWorkspace
.removeResourceChangeListener(fResourceChangeListener
);
132 public void createPartControl(Composite parent
) {
134 fViewer
= new TreeViewer(parent
, SWT
.SINGLE
);
135 fViewer
.setContentProvider(new LTTngProjectContentProvider());
136 fViewer
.setSorter(new ViewerSorter());
137 fViewer
.setLabelProvider(new LTTngProjectLabelProvider());
138 fViewer
.setInput(fProjectRoot
);
140 getSite().setSelectionProvider(fViewer
);
146 // ------------------------------------------------------------------------
148 // ------------------------------------------------------------------------
151 @SuppressWarnings("nls")
152 public String
toString() {
153 return "[ProjectView]";
156 // ------------------------------------------------------------------------
158 // ------------------------------------------------------------------------
160 private void hookMouse() {
161 fViewer
.getTree().addMouseListener(new MouseAdapter() {
163 public void mouseDoubleClick(MouseEvent event
) {
164 TreeSelection selection
= (TreeSelection
) fViewer
.getSelection();
165 Object element
= selection
.getFirstElement();
166 if (element
instanceof LTTngExperimentNode
) {
167 LTTngExperimentNode experiment
= (LTTngExperimentNode
) element
;
168 selectExperiment(experiment
);
170 if (element
instanceof LTTngTraceNode
) {
171 LTTngTraceNode trace
= (LTTngTraceNode
) element
;
179 private void selectTrace(LTTngTraceNode traceNode
) {
180 if (fSelectedExperiment
!= null) {
181 fSelectedExperiment
.dispose();
185 ITmfTrace
[] traces
= new ITmfTrace
[1];
186 IResource res
= traceNode
.getFolder();
187 String location
= res
.getLocation().toOSString();
188 LTTngProjectNode projectNode
= traceNode
.getProject();
189 IProject project
= projectNode
.getProject();
190 String traceLibPath
= TraceHelper
.getTraceLibDirFromProject(project
);
191 ITmfTrace trace
= new LTTngTrace(location
, traceLibPath
, waitForCompletion
, false);
193 fSelectedExperiment
= new LTTngExperiment
<LttngEvent
>(LttngEvent
.class, traceNode
.getName(), traces
);
194 TmfExperiment
.setCurrentExperiment(fSelectedExperiment
);
196 // Make sure the lttng-core, experiment selection context is ready
197 // for an event request from any view
198 // StateManagerFactory.getExperimentManager().experimentSelected_prep(
199 // (TmfExperiment<LttngEvent>) fSelectedExperiment);
201 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
202 } catch (FileNotFoundException e
) {
204 } catch (Exception e
) {
205 MessageDialog
.openError(getViewSite().getShell(), Messages
.SelectTrace_ErrorTitle
, e
.getMessage());
209 private boolean waitForCompletion
= true;
214 public void selectExperiment(LTTngExperimentNode experiment
) {
215 String expId
= experiment
.getName();
216 if (fSelectedExperiment
!= null) {
217 // System.out.println(fSelectedExperiment.getName() + ": nbEvents=" + fSelectedExperiment.getNbEvents() +
218 // ", nbReads=" + ((LTTngTrace) fSelectedExperiment.getTraces()[0]).nbEventsRead);
219 fSelectedExperiment
.dispose();
222 LTTngTraceNode
[] traceEntries
= experiment
.getTraces();
223 int nbTraces
= traceEntries
.length
;
224 ITmfTrace
[] traces
= new ITmfTrace
[nbTraces
];
225 for (int i
= 0; i
< nbTraces
; i
++) {
226 IResource res
= traceEntries
[i
].getFolder();
227 String location
= res
.getLocation().toOSString();
228 IProject project
= res
.getProject().getProject();
229 String traceLibPath
= TraceHelper
.getTraceLibDirFromProject(project
);
230 ITmfTrace trace
= new LTTngTrace(location
, traceLibPath
, waitForCompletion
, false);
233 fSelectedExperiment
= new LTTngExperiment
<LttngEvent
>(LttngEvent
.class, expId
, traces
);
234 TmfExperiment
.setCurrentExperiment(fSelectedExperiment
);
236 // Make sure the lttng-core, experiment selection context is ready
237 // for an event request from any view
238 // StateManagerFactory.getExperimentManager().experimentSelected_prep(
239 // (TmfExperiment<LttngEvent>) fSelectedExperiment);
241 // System.out.println(System.currentTimeMillis() + ": Experiment selected");
242 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
243 } catch (FileNotFoundException e
) {
245 } catch (Exception e
) {
250 // ------------------------------------------------------------------------
252 // ------------------------------------------------------------------------
254 // Populated from the plug-in
255 private void createContextMenu() {
256 MenuManager menuManager
= new MenuManager("#PopupMenu"); //$NON-NLS-1$
257 menuManager
.setRemoveAllWhenShown(true);
258 Menu menu
= menuManager
.createContextMenu(fViewer
.getControl());
259 fViewer
.getControl().setMenu(menu
);
260 getSite().registerContextMenu(menuManager
, fViewer
);