2010-11-09 Francois Chouinard <fchouinard@gmail.com> Contribution for Bug315307
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / ProjectView.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2010 Ericsson
3 *
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
8 *
9 * Contributors:
10 * Francois Chouinard - Initial API and implementation
11 *******************************************************************************/
12
13 package org.eclipse.linuxtools.lttng.ui.views.project;
14
15 import java.io.FileNotFoundException;
16
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;
47
48 /**
49 * <b><u>ProjectView</u></b>
50 * <p>
51 * The ProjectView keeps track of the LTTng projects in the workspace.
52 */
53 public class ProjectView extends TmfView {
54
55 public static final String ID = "org.eclipse.linuxtools.lttng.ui.views.project"; //$NON-NLS-1$
56
57 // ------------------------------------------------------------------------
58 // Main data structures
59 // ------------------------------------------------------------------------
60
61 private TreeViewer fViewer;
62 private LTTngProjectRoot fProjectRoot;
63 private LTTngExperiment<LttngEvent> fSelectedExperiment = null;
64
65 private IWorkspace fWorkspace;
66 private IResourceChangeListener fResourceChangeListener;
67
68 // ------------------------------------------------------------------------
69 // View refresher
70 // ------------------------------------------------------------------------
71
72 // Perform updates on the UI thread
73 private Runnable fViewRefresher = new Runnable() {
74 @Override
75 public void run() {
76 if ((fViewer != null) && (!fViewer.getTree().isDisposed())) {
77 Object[] elements = fViewer.getExpandedElements();
78 fViewer.refresh();
79 fViewer.setExpandedElements(elements);
80 }
81 }
82 };
83
84 public LTTngProjectRoot getRoot() {
85 return fProjectRoot;
86 }
87
88 // ------------------------------------------------------------------------
89 // Constructor
90 // ------------------------------------------------------------------------
91
92 public ProjectView() {
93
94 super("ProjectView"); //$NON-NLS-1$
95 fProjectRoot = new LTTngProjectRoot(this);
96
97 fWorkspace = ResourcesPlugin.getWorkspace();
98 fResourceChangeListener = new IResourceChangeListener() {
99 @Override
100 public void resourceChanged(IResourceChangeEvent event) {
101 if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
102 fProjectRoot.refreshChildren();
103 refresh();
104 }
105 }
106 };
107 fWorkspace.addResourceChangeListener(fResourceChangeListener);
108 }
109
110 public void refresh() {
111 Tree tree = fViewer.getTree();
112 if (tree != null && !tree.isDisposed())
113 tree.getDisplay().asyncExec(fViewRefresher);
114 }
115
116 public void setSelection(ILTTngProjectTreeNode node) {
117 fViewer.setSelection(new StructuredSelection(node), true);
118 }
119
120 @Override
121 public void dispose() {
122 fWorkspace.removeResourceChangeListener(fResourceChangeListener);
123 }
124
125 @Override
126 public void createPartControl(Composite parent) {
127
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);
133
134 getSite().setSelectionProvider(fViewer);
135 hookMouse();
136
137 createContextMenu();
138 }
139
140 // ------------------------------------------------------------------------
141 // ViewPart
142 // ------------------------------------------------------------------------
143
144 @Override
145 @SuppressWarnings("nls")
146 public String toString() {
147 return "[ProjectView]";
148 }
149
150 // ------------------------------------------------------------------------
151 // hookMouse
152 // ------------------------------------------------------------------------
153
154 private void hookMouse() {
155 fViewer.getTree().addMouseListener(new MouseAdapter() {
156 @Override
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);
163 } else {
164 if (element instanceof LTTngTraceNode) {
165 LTTngTraceNode trace = (LTTngTraceNode) element;
166 selectTrace(trace);
167 }
168 }
169 }
170 });
171 }
172
173 private void selectTrace(LTTngTraceNode traceNode) {
174 if (fSelectedExperiment != null) {
175 fSelectedExperiment.dispose();
176 }
177
178 try {
179 ITmfTrace[] traces = new ITmfTrace[1];
180 IResource res = traceNode.getFolder();
181 String location = res.getLocation().toOSString();
182 ITmfTrace trace = new LTTngTrace(location, waitForCompletion);
183 traces[0] = trace;
184 fSelectedExperiment = new LTTngExperiment<LttngEvent>(LttngEvent.class, traceNode.getName(), traces);
185 TmfExperiment.setCurrentExperiment(fSelectedExperiment);
186
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);
191
192 broadcast(new TmfExperimentSelectedSignal<LttngEvent>(this, fSelectedExperiment));
193 } catch (FileNotFoundException e) {
194 return;
195 } catch (Exception e) {
196 e.printStackTrace();
197 }
198 }
199
200 private boolean waitForCompletion = true;
201
202 /**
203 * @param experiment
204 */
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();
211 }
212 try {
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);
220 traces[i] = trace;
221 }
222 fSelectedExperiment = new LTTngExperiment<LttngEvent>(LttngEvent.class, expId, traces);
223 TmfExperiment.setCurrentExperiment(fSelectedExperiment);
224
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);
229
230 // System.out.println(System.currentTimeMillis() + ": Experiment selected");
231 broadcast(new TmfExperimentSelectedSignal<LttngEvent>(this, fSelectedExperiment));
232 } catch (FileNotFoundException e) {
233 return;
234 } catch (Exception e) {
235 e.printStackTrace();
236 }
237 }
238
239 // ------------------------------------------------------------------------
240 // createContextMenu
241 // ------------------------------------------------------------------------
242
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);
250 }
251
252 }
This page took 0.035741 seconds and 5 git commands to generate.