Commit | Line | Data |
---|---|---|
6e512b93 ASL |
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.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; | |
46 | ||
47 | /** | |
48 | * <b><u>ProjectView</u></b> | |
49 | * <p> | |
50 | * The ProjectView keeps track of the LTTng projects in the workspace. | |
51 | */ | |
52 | public class ProjectView extends TmfView { | |
53 | ||
54 | public static final String ID = "org.eclipse.linuxtools.lttng.ui.views.project"; | |
55 | ||
56 | // ------------------------------------------------------------------------ | |
57 | // Main data structures | |
58 | // ------------------------------------------------------------------------ | |
59 | ||
60 | private TreeViewer fViewer; | |
61 | private LTTngProjectRoot fProjectRoot; | |
62 | private TmfExperiment<LttngEvent> fSelectedExperiment = null; | |
63 | ||
64 | private IWorkspace fWorkspace; | |
65 | private IResourceChangeListener fResourceChangeListener; | |
66 | ||
67 | // ------------------------------------------------------------------------ | |
68 | // View refresher | |
69 | // ------------------------------------------------------------------------ | |
70 | ||
71 | // Perform updates on the UI thread | |
72 | private Runnable fViewRefresher = new Runnable() { | |
73 | public void run() { | |
74 | if (fViewer != null) { | |
75 | Object[] elements = fViewer.getExpandedElements(); | |
76 | fViewer.refresh(); | |
77 | fViewer.setExpandedElements(elements); | |
78 | } | |
79 | } | |
80 | }; | |
81 | ||
82 | public LTTngProjectRoot getRoot() { | |
83 | return fProjectRoot; | |
84 | } | |
85 | ||
86 | // ------------------------------------------------------------------------ | |
87 | // Constructor | |
88 | // ------------------------------------------------------------------------ | |
89 | ||
90 | public ProjectView() { | |
91 | ||
92 | super("ProjectView"); | |
93 | fProjectRoot = new LTTngProjectRoot(this); | |
94 | ||
95 | fWorkspace = ResourcesPlugin.getWorkspace(); | |
96 | fResourceChangeListener = new IResourceChangeListener() { | |
97 | public void resourceChanged(IResourceChangeEvent event) { | |
98 | if (event.getType() == IResourceChangeEvent.POST_CHANGE) { | |
99 | fProjectRoot.refreshChildren(); | |
100 | refresh(); | |
101 | } | |
102 | } | |
103 | }; | |
104 | fWorkspace.addResourceChangeListener(fResourceChangeListener); | |
105 | } | |
106 | ||
107 | public void refresh() { | |
108 | Tree tree = fViewer.getTree(); | |
109 | if (tree != null && !tree.isDisposed()) | |
110 | tree.getDisplay().asyncExec(fViewRefresher); | |
111 | } | |
112 | ||
113 | public void setSelection(ILTTngProjectTreeNode node) { | |
114 | fViewer.setSelection(new StructuredSelection(node), true); | |
115 | } | |
116 | ||
117 | @Override | |
118 | public void dispose() { | |
119 | fWorkspace.removeResourceChangeListener(fResourceChangeListener); | |
120 | } | |
121 | ||
122 | @Override | |
123 | public void createPartControl(Composite parent) { | |
124 | ||
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); | |
130 | ||
131 | getSite().setSelectionProvider(fViewer); | |
132 | hookMouse(); | |
133 | ||
134 | createContextMenu(); | |
135 | } | |
136 | ||
137 | // ------------------------------------------------------------------------ | |
138 | // ViewPart | |
139 | // ------------------------------------------------------------------------ | |
140 | ||
141 | @Override | |
142 | public String toString() { | |
143 | return "[ProjectView]"; | |
144 | } | |
145 | ||
146 | // ------------------------------------------------------------------------ | |
147 | // hookMouse | |
148 | // ------------------------------------------------------------------------ | |
149 | ||
150 | private void hookMouse() { | |
151 | fViewer.getTree().addMouseListener(new MouseAdapter() { | |
152 | @Override | |
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); | |
159 | } | |
160 | } | |
161 | }); | |
162 | } | |
163 | ||
164 | private boolean waitForCompletion = true; | |
165 | ||
166 | /** | |
167 | * @param experiment | |
168 | */ | |
169 | public void selectExperiment(LTTngExperimentNode experiment) { | |
170 | String expId = experiment.getName(); | |
9aae0442 ASL |
171 | if (fSelectedExperiment != null) { |
172 | System.out.println(fSelectedExperiment.getName() + ": nbEvents=" + fSelectedExperiment.getNbEvents() + | |
173 | ", nbReads=" + ((LTTngTrace) fSelectedExperiment.getTraces()[0]).nbEventsRead); | |
6e512b93 | 174 | fSelectedExperiment.dispose(); |
9aae0442 | 175 | } |
6e512b93 ASL |
176 | try { |
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); | |
184 | traces[i] = trace; | |
185 | } | |
186 | fSelectedExperiment = new TmfExperiment<LttngEvent>(LttngEvent.class, expId, traces); | |
9aae0442 | 187 | |
6e512b93 ASL |
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); | |
192 | ||
193 | broadcast(new TmfExperimentSelectedSignal<LttngEvent>(this, fSelectedExperiment)); | |
194 | } catch (FileNotFoundException e) { | |
195 | return; | |
196 | } catch (Exception e) { | |
197 | e.printStackTrace(); | |
198 | } | |
199 | } | |
200 | ||
201 | // ------------------------------------------------------------------------ | |
202 | // createContextMenu | |
203 | // ------------------------------------------------------------------------ | |
204 | ||
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); | |
212 | } | |
213 | ||
214 | } |