Rename xxx.lttng to xxx.lttng.core
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng.ui / src / org / eclipse / linuxtools / lttng / ui / views / project / ProjectView.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2011 Ericsson, Montavista Software
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 * 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 *******************************************************************************/
14
15 package org.eclipse.linuxtools.lttng.ui.views.project;
16
17 import java.io.FileNotFoundException;
18
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;
53
54 /**
55 * <b><u>ProjectView</u></b>
56 * <p>
57 * The ProjectView keeps track of the LTTng projects in the workspace.
58 */
59 public class ProjectView extends TmfView {
60
61 public static final String ID = "org.eclipse.linuxtools.lttng.ui.views.project"; //$NON-NLS-1$
62
63 // ------------------------------------------------------------------------
64 // Main data structures
65 // ------------------------------------------------------------------------
66
67 private TreeViewer fViewer;
68 private LTTngProjectRoot fProjectRoot;
69 private LTTngExperiment<LttngEvent> fSelectedExperiment = null;
70
71 private IWorkspace fWorkspace;
72 private IResourceChangeListener fResourceChangeListener;
73
74 // ------------------------------------------------------------------------
75 // View refresher
76 // ------------------------------------------------------------------------
77
78 // Perform updates on the UI thread
79 private Runnable fViewRefresher = new Runnable() {
80 @Override
81 public void run() {
82 if ((fViewer != null) && (!fViewer.getTree().isDisposed())) {
83 Object[] elements = fViewer.getExpandedElements();
84 fViewer.refresh();
85 fViewer.setExpandedElements(elements);
86 }
87 }
88 };
89
90 public LTTngProjectRoot getRoot() {
91 return fProjectRoot;
92 }
93
94 // ------------------------------------------------------------------------
95 // Constructor
96 // ------------------------------------------------------------------------
97
98 public ProjectView() {
99
100 super("ProjectView"); //$NON-NLS-1$
101 fProjectRoot = new LTTngProjectRoot(this);
102
103 fWorkspace = ResourcesPlugin.getWorkspace();
104 fResourceChangeListener = new IResourceChangeListener() {
105 @Override
106 public void resourceChanged(IResourceChangeEvent event) {
107 if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
108 fProjectRoot.refreshChildren();
109 refresh();
110 }
111 }
112 };
113 fWorkspace.addResourceChangeListener(fResourceChangeListener);
114 }
115
116 public void refresh() {
117 Tree tree = fViewer.getTree();
118 if (tree != null && !tree.isDisposed())
119 tree.getDisplay().asyncExec(fViewRefresher);
120 }
121
122 public void setSelection(ILTTngProjectTreeNode node) {
123 fViewer.setSelection(new StructuredSelection(node), true);
124 }
125
126 @Override
127 public void dispose() {
128 fWorkspace.removeResourceChangeListener(fResourceChangeListener);
129 }
130
131 @Override
132 public void createPartControl(Composite parent) {
133
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);
139
140 getSite().setSelectionProvider(fViewer);
141 hookMouse();
142
143 createContextMenu();
144 }
145
146 // ------------------------------------------------------------------------
147 // ViewPart
148 // ------------------------------------------------------------------------
149
150 @Override
151 @SuppressWarnings("nls")
152 public String toString() {
153 return "[ProjectView]";
154 }
155
156 // ------------------------------------------------------------------------
157 // hookMouse
158 // ------------------------------------------------------------------------
159
160 private void hookMouse() {
161 fViewer.getTree().addMouseListener(new MouseAdapter() {
162 @Override
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);
169 } else {
170 if (element instanceof LTTngTraceNode) {
171 LTTngTraceNode trace = (LTTngTraceNode) element;
172 selectTrace(trace);
173 }
174 }
175 }
176 });
177 }
178
179 private void selectTrace(LTTngTraceNode traceNode) {
180 if (fSelectedExperiment != null) {
181 fSelectedExperiment.dispose();
182 }
183
184 try {
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);
192 traces[0] = trace;
193 fSelectedExperiment = new LTTngExperiment<LttngEvent>(LttngEvent.class, traceNode.getName(), traces);
194 TmfExperiment.setCurrentExperiment(fSelectedExperiment);
195
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);
200
201 broadcast(new TmfExperimentSelectedSignal<LttngEvent>(this, fSelectedExperiment));
202 } catch (FileNotFoundException e) {
203 return;
204 } catch (Exception e) {
205 MessageDialog.openError(getViewSite().getShell(), Messages.SelectTrace_ErrorTitle, e.getMessage());
206 }
207 }
208
209 private boolean waitForCompletion = true;
210
211 /**
212 * @param experiment
213 */
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();
220 }
221 try {
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);
231 traces[i] = trace;
232 }
233 fSelectedExperiment = new LTTngExperiment<LttngEvent>(LttngEvent.class, expId, traces);
234 TmfExperiment.setCurrentExperiment(fSelectedExperiment);
235
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);
240
241 // System.out.println(System.currentTimeMillis() + ": Experiment selected");
242 broadcast(new TmfExperimentSelectedSignal<LttngEvent>(this, fSelectedExperiment));
243 } catch (FileNotFoundException e) {
244 return;
245 } catch (Exception e) {
246 e.printStackTrace();
247 }
248 }
249
250 // ------------------------------------------------------------------------
251 // createContextMenu
252 // ------------------------------------------------------------------------
253
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);
261 }
262
263 }
This page took 0.03735 seconds and 5 git commands to generate.