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
.trace
.LTTngTrace
;
29 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.ILTTngProjectTreeNode
;
30 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngExperimentNode
;
31 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectContentProvider
;
32 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectLabelProvider
;
33 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngProjectRoot
;
34 import org
.eclipse
.linuxtools
.lttng
.ui
.views
.project
.model
.LTTngTraceNode
;
35 import org
.eclipse
.linuxtools
.tmf
.experiment
.TmfExperiment
;
36 import org
.eclipse
.linuxtools
.tmf
.signal
.TmfExperimentSelectedSignal
;
37 import org
.eclipse
.linuxtools
.tmf
.trace
.ITmfTrace
;
38 import org
.eclipse
.linuxtools
.tmf
.ui
.views
.TmfView
;
39 import org
.eclipse
.swt
.SWT
;
40 import org
.eclipse
.swt
.events
.MouseAdapter
;
41 import org
.eclipse
.swt
.events
.MouseEvent
;
42 import org
.eclipse
.swt
.widgets
.Composite
;
43 import org
.eclipse
.swt
.widgets
.Menu
;
44 import org
.eclipse
.swt
.widgets
.Tree
;
47 * <b><u>ProjectView</u></b>
49 * The ProjectView keeps track of the LTTng projects in the workspace.
51 public class ProjectView
extends TmfView
{
53 public static final String ID
= "org.eclipse.linuxtools.lttng.ui.views.project";
55 // ------------------------------------------------------------------------
56 // Main data structures
57 // ------------------------------------------------------------------------
59 private TreeViewer fViewer
;
60 private LTTngProjectRoot fProjectRoot
;
61 private TmfExperiment
<LttngEvent
> fSelectedExperiment
= null;
63 private IWorkspace fWorkspace
;
64 private IResourceChangeListener fResourceChangeListener
;
66 // ------------------------------------------------------------------------
68 // ------------------------------------------------------------------------
70 // Perform updates on the UI thread
71 private Runnable fViewRefresher
= new Runnable() {
73 if (fViewer
!= null) {
74 Object
[] elements
= fViewer
.getExpandedElements();
76 fViewer
.setExpandedElements(elements
);
81 public LTTngProjectRoot
getRoot() {
85 // ------------------------------------------------------------------------
87 // ------------------------------------------------------------------------
89 public ProjectView() {
92 fProjectRoot
= new LTTngProjectRoot(this);
94 fWorkspace
= ResourcesPlugin
.getWorkspace();
95 fResourceChangeListener
= new IResourceChangeListener() {
96 public void resourceChanged(IResourceChangeEvent event
) {
97 if (event
.getType() == IResourceChangeEvent
.POST_CHANGE
) {
98 fProjectRoot
.refreshChildren();
103 fWorkspace
.addResourceChangeListener(fResourceChangeListener
);
106 public void refresh() {
107 Tree tree
= fViewer
.getTree();
108 if (tree
!= null && !tree
.isDisposed())
109 tree
.getDisplay().asyncExec(fViewRefresher
);
112 public void setSelection(ILTTngProjectTreeNode node
) {
113 fViewer
.setSelection(new StructuredSelection(node
), true);
117 public void dispose() {
118 fWorkspace
.removeResourceChangeListener(fResourceChangeListener
);
122 public void createPartControl(Composite parent
) {
124 fViewer
= new TreeViewer(parent
, SWT
.SINGLE
);
125 fViewer
.setContentProvider(new LTTngProjectContentProvider());
126 fViewer
.setSorter(new ViewerSorter());
127 fViewer
.setLabelProvider(new LTTngProjectLabelProvider());
128 fViewer
.setInput(fProjectRoot
);
130 getSite().setSelectionProvider(fViewer
);
136 // ------------------------------------------------------------------------
138 // ------------------------------------------------------------------------
141 public String
toString() {
142 return "[ProjectView]";
145 // ------------------------------------------------------------------------
147 // ------------------------------------------------------------------------
149 private void hookMouse() {
150 fViewer
.getTree().addMouseListener(new MouseAdapter() {
152 public void mouseDoubleClick(MouseEvent event
) {
153 TreeSelection selection
= (TreeSelection
) fViewer
.getSelection();
154 Object element
= selection
.getFirstElement();
155 if (element
instanceof LTTngExperimentNode
) {
156 LTTngExperimentNode experiment
= (LTTngExperimentNode
) element
;
157 selectExperiment(experiment
);
163 private boolean waitForCompletion
= true;
165 public void selectExperiment(LTTngExperimentNode experiment
) {
166 String expId
= experiment
.getName();
167 if (fSelectedExperiment
!= null)
168 fSelectedExperiment
.dispose();
170 LTTngTraceNode
[] traceEntries
= experiment
.getTraces();
171 int nbTraces
= traceEntries
.length
;
172 ITmfTrace
[] traces
= new ITmfTrace
[nbTraces
];
173 for (int i
= 0; i
< nbTraces
; i
++) {
174 IResource res
= traceEntries
[i
].getFolder();
175 String location
= res
.getLocation().toOSString();
176 ITmfTrace trace
= new LTTngTrace(location
, waitForCompletion
);
179 fSelectedExperiment
= new TmfExperiment
<LttngEvent
>(LttngEvent
.class, expId
, traces
);
180 fSelectedExperiment
.indexExperiment(waitForCompletion
);
181 broadcast(new TmfExperimentSelectedSignal
<LttngEvent
>(this, fSelectedExperiment
));
182 } catch (FileNotFoundException e
) {
184 } catch (Exception e
) {
189 // ------------------------------------------------------------------------
191 // ------------------------------------------------------------------------
193 // Populated from the plug-in
194 private void createContextMenu() {
195 MenuManager menuManager
= new MenuManager("#PopupMenu");
196 menuManager
.setRemoveAllWhenShown(true);
197 Menu menu
= menuManager
.createContextMenu(fViewer
.getControl());
198 fViewer
.getControl().setMenu(menu
);
199 getSite().registerContextMenu(menuManager
, fViewer
);