Bug 448058: Replace RSE by org.eclipse.remote
[deliverable/tracecompass.git] / org.eclipse.tracecompass.lttng2.control.ui / src / org / eclipse / tracecompass / internal / lttng2 / control / ui / views / ControlView.java
1 /*******************************************************************************
2 * Copyright (c) 2009, 2014 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 * Markus Schorn - Bug 448058: Use org.eclipse.remote in favor of RSE
13 *******************************************************************************/
14
15 package org.eclipse.tracecompass.internal.lttng2.control.ui.views;
16
17 import java.util.ArrayList;
18 import java.util.List;
19
20 import org.eclipse.core.runtime.IProgressMonitor;
21 import org.eclipse.core.runtime.IStatus;
22 import org.eclipse.core.runtime.Status;
23 import org.eclipse.jface.action.MenuManager;
24 import org.eclipse.jface.viewers.ColumnViewerToolTipSupport;
25 import org.eclipse.jface.viewers.ISelection;
26 import org.eclipse.jface.viewers.StructuredSelection;
27 import org.eclipse.jface.viewers.TreeViewer;
28 import org.eclipse.remote.core.IRemoteConnection;
29 import org.eclipse.remote.core.IRemoteServices;
30 import org.eclipse.remote.core.RemoteServices;
31 import org.eclipse.swt.SWT;
32 import org.eclipse.swt.widgets.Composite;
33 import org.eclipse.swt.widgets.Menu;
34 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponent;
35 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.ITraceControlComponentChangedListener;
36 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TargetNodeComponent;
37 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceControlContentProvider;
38 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceControlLabelProvider;
39 import org.eclipse.tracecompass.internal.lttng2.control.ui.views.model.impl.TraceControlRoot;
40 import org.eclipse.ui.IMemento;
41 import org.eclipse.ui.IViewSite;
42 import org.eclipse.ui.PartInitException;
43 import org.eclipse.ui.part.ViewPart;
44 import org.eclipse.ui.progress.UIJob;
45
46 /**
47 * <p>
48 * View implementation for Trace Control.
49 * </p>
50 *
51 * @author Bernd Hufmann
52 */
53 public class ControlView extends ViewPart implements ITraceControlComponentChangedListener {
54
55 // ------------------------------------------------------------------------
56 // Constants
57 // ------------------------------------------------------------------------
58
59 /**
60 * View ID.
61 */
62 public static final String ID = "org.eclipse.linuxtools.internal.lttng2.ui.views.control"; //$NON-NLS-1$
63
64 private static final String KEY_REMOTE_CONNECTION_NAME = "rc_name_"; //$NON-NLS-1$
65 private static final String KEY_REMOTE_PROVIDER = "rc_id_"; //$NON-NLS-1$
66
67 // ------------------------------------------------------------------------
68 // Attributes
69 // ------------------------------------------------------------------------
70
71 /**
72 * The tree viewer.
73 */
74 private TreeViewer fTreeViewer = null;
75
76 /**
77 * The trace control root node. This provides access to the whole model.
78 */
79 private ITraceControlComponent fRoot = null;
80
81 private List<IRemoteConnection> fInitialConnections;
82
83 // ------------------------------------------------------------------------
84 // Accessors
85 // ------------------------------------------------------------------------
86
87 /**
88 * Returns the trace control tree node (model)
89 *
90 * @return the trace control tree node (model).
91 */
92 public ITraceControlComponent getTraceControlRoot() {
93 return fRoot;
94 }
95
96 // ------------------------------------------------------------------------
97 // Operations
98 // ------------------------------------------------------------------------
99
100 @Override
101 public void createPartControl(Composite parent) {
102 // Create tree viewer
103 fTreeViewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
104 ColumnViewerToolTipSupport.enableFor(fTreeViewer);
105
106 fTreeViewer.setContentProvider(new TraceControlContentProvider());
107 fTreeViewer.setLabelProvider(new TraceControlLabelProvider());
108
109 // Create model root
110 fRoot = new TraceControlRoot();
111 fRoot.addComponentListener(this);
112 if (fInitialConnections != null) {
113 for (IRemoteConnection rc : fInitialConnections) {
114 TargetNodeComponent node = new TargetNodeComponent(rc.getName(), fRoot, rc);
115 fRoot.addChild(node);
116 }
117 fInitialConnections = null;
118 }
119 fTreeViewer.setInput(fRoot);
120
121 // Create context menu for the tree viewer
122 createContextMenu();
123
124 getSite().setSelectionProvider(fTreeViewer);
125 }
126
127 @Override
128 public void saveState(IMemento memento) {
129 int i = 0;
130 for (ITraceControlComponent cmp : fRoot.getChildren()) {
131 if (cmp instanceof TargetNodeComponent) {
132 IRemoteConnection rc = ((TargetNodeComponent) cmp).getRemoteConnection();
133 memento.putString(KEY_REMOTE_PROVIDER + i, rc.getRemoteServices().getId());
134 memento.putString(KEY_REMOTE_CONNECTION_NAME + i, rc.getName());
135 i++;
136 }
137 }
138 super.saveState(memento);
139 }
140
141
142 @Override
143 public void init(IViewSite site, IMemento memento) throws PartInitException {
144 super.init(site, memento);
145 if (memento != null) {
146 fInitialConnections = new ArrayList<>();
147 for(int i = 0; ; i++) {
148 String id = memento.getString(KEY_REMOTE_PROVIDER + i);
149 String name = memento.getString(KEY_REMOTE_CONNECTION_NAME + i);
150 if (id == null || name == null) {
151 break;
152 }
153 if (!Workaround_Bug449362.triggerRSEStartup(id)) {
154 // Skip the connection in order to avoid an infinite loop
155 } else {
156 IRemoteServices rs = RemoteServices.getRemoteServices(id);
157 if (rs != null) {
158 IRemoteConnection rc = rs.getConnectionManager().getConnection(name);
159 if (rc != null) {
160 fInitialConnections.add(rc);
161 }
162 }
163 }
164 }
165 }
166 }
167
168 @Override
169 public void setFocus() {
170 fTreeViewer.getControl().setFocus();
171 }
172
173 @Override
174 public void componentAdded(ITraceControlComponent parent, ITraceControlComponent component) {
175 componentChanged(parent);
176 }
177
178 @Override
179 public void componentRemoved(ITraceControlComponent parent, ITraceControlComponent component) {
180 componentChanged(parent);
181 }
182
183 @Override
184 public void componentChanged(final ITraceControlComponent component) {
185 if (fTreeViewer.getTree().isDisposed()) {
186 return;
187 }
188
189 UIJob myJob = new UIJob("Refresh") { //$NON-NLS-1$
190 @Override
191 public IStatus runInUIThread(IProgressMonitor monitor) {
192 if (fTreeViewer.getTree().isDisposed()) {
193 return Status.OK_STATUS;
194 }
195
196 fTreeViewer.refresh(component);
197
198 // Change selection needed
199 final ISelection sel = fTreeViewer.getSelection();
200 fTreeViewer.setSelection(null);
201 fTreeViewer.setSelection(sel);
202
203 // Show component that was changed
204 fTreeViewer.reveal(component);
205
206 return Status.OK_STATUS;
207 }
208 };
209 myJob.setUser(false);
210 myJob.setSystem(true);
211 myJob.schedule();
212 }
213
214 /**
215 * Sets the selected component in the tree
216 * @param component - component to select
217 */
218 public void setSelection(ITraceControlComponent component) {
219 ITraceControlComponent[] components = new ITraceControlComponent[1];
220 components[0] = component;
221 setSelection(components);
222 }
223
224 /**
225 * Sets the selected components in the tree
226 * @param components - array of components to select
227 */
228 public void setSelection(ITraceControlComponent[] components) {
229 final StructuredSelection selection = new StructuredSelection(components);
230 UIJob myJob = new UIJob("Select") { //$NON-NLS-1$
231 @Override
232 public IStatus runInUIThread(IProgressMonitor monitor) {
233 fTreeViewer.setSelection(selection);
234 return Status.OK_STATUS;
235 }
236 };
237 myJob.setUser(false);
238 myJob.schedule();
239 }
240
241 // ------------------------------------------------------------------------
242 // Helper methods
243 // ------------------------------------------------------------------------
244 /**
245 * Creates the context sensitive menu.
246 */
247 private void createContextMenu() {
248 // First we create a menu Manager
249 final MenuManager menuManager = new MenuManager();
250 final Menu menu = menuManager.createContextMenu(fTreeViewer.getTree());
251 // Set the MenuManager
252 fTreeViewer.getTree().setMenu(menu);
253 getSite().registerContextMenu(menuManager, fTreeViewer);
254 }
255 }
This page took 0.040056 seconds and 5 git commands to generate.