Improved GUI refresh and JUnit tests
[deliverable/tracecompass.git] / org.eclipse.linuxtools.lttng2.ui / src / org / eclipse / linuxtools / internal / lttng2 / ui / views / control / ControlView.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2012 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 * Bernd Hufmann - Filled with content
12 *******************************************************************************/
13
14 package org.eclipse.linuxtools.internal.lttng2.ui.views.control;
15
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.core.runtime.Status;
19 import org.eclipse.jface.action.MenuManager;
20 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.StructuredSelection;
23 import org.eclipse.jface.viewers.TreeViewer;
24 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent;
25 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener;
26 import org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.impl.TraceControlRoot;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Menu;
30 import org.eclipse.ui.part.ViewPart;
31 import org.eclipse.ui.progress.UIJob;
32
33 /**
34 * <b><u>ControlView</u></b>
35 * <p>
36 * View implementation for Trace Control.
37 * </p>
38 */
39 public class ControlView extends ViewPart implements ITraceControlComponentChangedListener {
40
41 // ------------------------------------------------------------------------
42 // Constants
43 // ------------------------------------------------------------------------
44 /**
45 * View ID.
46 */
47 public static final String ID = "org.eclipse.linuxtools.internal.lttng2.ui.views.control"; //$NON-NLS-1$
48
49 // ------------------------------------------------------------------------
50 // Attributes
51 // ------------------------------------------------------------------------
52
53 /**
54 * The tree viewer.
55 */
56 private TreeViewer fTreeViewer = null;
57
58 /**
59 * The trace control root node. This provides access to the whole model.
60 */
61 private ITraceControlComponent fRoot = null;
62
63 // ------------------------------------------------------------------------
64 // Constructors
65 // ------------------------------------------------------------------------
66
67 // ------------------------------------------------------------------------
68 // Accessors
69 // ------------------------------------------------------------------------
70
71 /**
72 * @return returns the trace control tree node (model).
73 */
74 public ITraceControlComponent getTraceControlRoot() {
75 return fRoot;
76 }
77
78 // ------------------------------------------------------------------------
79 // Operations
80 // ------------------------------------------------------------------------
81
82 /*
83 * (non-Javadoc)
84 *
85 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
86 */
87 @Override
88 public void createPartControl(Composite parent) {
89 // Create tree viewer
90 fTreeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
91 ColumnViewerToolTipSupport.enableFor(fTreeViewer);
92
93 fTreeViewer.setContentProvider(new TraceControlContentProvider());
94 fTreeViewer.setLabelProvider(new TraceControlLabelProvider());
95
96 // Create model root
97 fRoot = new TraceControlRoot();
98 fRoot.addComponentListener(this);
99 fTreeViewer.setInput(fRoot);
100
101 // Create context menu for the tree viewer
102 createContextMenu();
103
104 getSite().setSelectionProvider(fTreeViewer);
105 }
106
107 /*
108 * (non-Javadoc)
109 *
110 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
111 */
112 @Override
113 public void setFocus() {
114 fTreeViewer.getControl().setFocus();
115 }
116
117 /*
118 * (non-Javadoc)
119 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener#componentAdded(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent)
120 */
121 @Override
122 public void componentAdded(ITraceControlComponent parent, ITraceControlComponent component) {
123 componentChanged(parent);
124 }
125
126 /*
127 * (non-Javadoc)
128 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener#componentRemoved(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent, org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent)
129 */
130 @Override
131 public void componentRemoved(ITraceControlComponent parent, ITraceControlComponent component) {
132 componentChanged(parent);
133 }
134
135 /*
136 * (non-Javadoc)
137 * @see org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponentChangedListener#componentChanged(org.eclipse.linuxtools.internal.lttng2.ui.views.control.model.ITraceControlComponent)
138 */
139 @Override
140 public void componentChanged(final ITraceControlComponent component) {
141 if (fTreeViewer.getTree().isDisposed()) {
142 return;
143 }
144
145 UIJob myJob = new UIJob("Refresh") { //$NON-NLS-1$
146 @Override
147 public IStatus runInUIThread(IProgressMonitor monitor) {
148 if (fTreeViewer.getTree().isDisposed()) {
149 return Status.OK_STATUS;
150 }
151
152 fTreeViewer.refresh(component);
153
154 // Change selection needed
155 final ISelection sel = fTreeViewer.getSelection();
156 fTreeViewer.setSelection(null);
157 fTreeViewer.setSelection(sel);
158
159 return Status.OK_STATUS;
160 }
161 };
162 myJob.setUser(false);
163 myJob.schedule();
164 }
165
166 /**
167 * Sets the selected component in the tree
168 * @param component - component to select
169 */
170 public void setSelection(ITraceControlComponent component) {
171 ITraceControlComponent[] components = new ITraceControlComponent[1];
172 components[0] = component;
173 setSelection(components);
174 }
175
176 /**
177 * Sets the selected components in the tree
178 * @param component - array of components to select
179 */
180 public void setSelection(ITraceControlComponent[] components) {
181 final StructuredSelection selection = new StructuredSelection(components);
182 UIJob myJob = new UIJob("Select") { //$NON-NLS-1$
183 @Override
184 public IStatus runInUIThread(IProgressMonitor monitor) {
185 fTreeViewer.setSelection(selection);
186 return Status.OK_STATUS;
187 }
188 };
189 myJob.setUser(false);
190 myJob.schedule();
191 }
192
193 // public ITraceControlComponent getSelection() {
194 // ISelection selection = fTreeViewer.getSelection();
195 //
196 // }
197
198 // ------------------------------------------------------------------------
199 // Helper methods
200 // ------------------------------------------------------------------------
201 private void createContextMenu() {
202 // First we create a menu Manager
203 final MenuManager menuManager = new MenuManager();
204 final Menu menu = menuManager.createContextMenu(fTreeViewer.getTree());
205 // Set the MenuManager
206 fTreeViewer.getTree().setMenu(menu);
207 getSite().registerContextMenu(menuManager, fTreeViewer);
208 }
209 }
This page took 0.035494 seconds and 6 git commands to generate.