|
1 /* |
|
2 * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of the License "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: |
|
15 * |
|
16 */ |
|
17 |
|
18 package com.nokia.carbide.remoteconnections.internal.ui; |
|
19 |
|
20 import java.text.MessageFormat; |
|
21 import java.util.ArrayList; |
|
22 import java.util.Collections; |
|
23 import java.util.Comparator; |
|
24 import java.util.List; |
|
25 |
|
26 import org.eclipse.jface.layout.GridDataFactory; |
|
27 import org.eclipse.jface.layout.GridLayoutFactory; |
|
28 import org.eclipse.swt.SWT; |
|
29 import org.eclipse.swt.custom.CLabel; |
|
30 import org.eclipse.swt.events.MouseAdapter; |
|
31 import org.eclipse.swt.events.MouseEvent; |
|
32 import org.eclipse.swt.events.SelectionAdapter; |
|
33 import org.eclipse.swt.events.SelectionEvent; |
|
34 import org.eclipse.swt.graphics.Image; |
|
35 import org.eclipse.swt.graphics.Point; |
|
36 import org.eclipse.swt.widgets.Composite; |
|
37 import org.eclipse.swt.widgets.Control; |
|
38 import org.eclipse.swt.widgets.Display; |
|
39 import org.eclipse.swt.widgets.Menu; |
|
40 import org.eclipse.swt.widgets.MenuItem; |
|
41 import org.eclipse.swt.widgets.Shell; |
|
42 import org.eclipse.ui.IWorkbench; |
|
43 import org.eclipse.ui.IWorkbenchWindow; |
|
44 import org.eclipse.ui.PartInitException; |
|
45 import org.eclipse.ui.PlatformUI; |
|
46 import org.eclipse.ui.menus.WorkbenchWindowControlContribution; |
|
47 |
|
48 import com.nokia.carbide.remoteconnections.Messages; |
|
49 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator; |
|
50 import com.nokia.carbide.remoteconnections.interfaces.IConnection; |
|
51 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager; |
|
52 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener; |
|
53 import com.nokia.carbide.remoteconnections.internal.api.IConnection2; |
|
54 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IStatus; |
|
55 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IStatusChangedListener; |
|
56 import com.nokia.carbide.remoteconnections.view.ConnectionsView; |
|
57 import com.nokia.cpp.internal.api.utils.core.TextUtils; |
|
58 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
|
59 |
|
60 |
|
61 /** |
|
62 * This widget appears in the Eclipse trim and allows the user to select the |
|
63 * "default" device connection and also see its status at a glance. |
|
64 */ |
|
65 public class DeviceStatusSelectorContribution extends WorkbenchWindowControlContribution |
|
66 implements IConnectionListener, IStatusChangedListener { |
|
67 |
|
68 private Composite container; |
|
69 private CLabel deviceInfo; |
|
70 private IConnectionsManager manager; |
|
71 private Image deviceImage; |
|
72 private IConnection defaultConnection; |
|
73 |
|
74 public DeviceStatusSelectorContribution() { |
|
75 manager = RemoteConnectionsActivator.getConnectionsManager(); |
|
76 } |
|
77 |
|
78 /* |
|
79 * (non-Javadoc) |
|
80 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite) |
|
81 */ |
|
82 @Override |
|
83 protected Control createControl(Composite parent) { |
|
84 |
|
85 // This UI is recreated whenever the default connection changes. |
|
86 |
|
87 manager.addConnectionListener(this); |
|
88 |
|
89 container = new Composite(parent, SWT.NONE); |
|
90 GridLayoutFactory.fillDefaults().margins(2, 0).applyTo(container); |
|
91 |
|
92 // Create a label for the trim. |
|
93 deviceInfo = new CLabel(container, SWT.FLAT); |
|
94 GridDataFactory.fillDefaults().grab(false, true).applyTo(deviceInfo); |
|
95 |
|
96 String text = Messages.getString("DeviceStatusSelectorContribution_NoDefaultConnectionMessage"); //$NON-NLS-1$ |
|
97 defaultConnection = manager.getDefaultConnection(); |
|
98 if (defaultConnection != null) { |
|
99 text = defaultConnection.getDisplayName(); |
|
100 if (defaultConnection instanceof IConnection2) { |
|
101 ((IConnection2) defaultConnection).addStatusChangedListener(DeviceStatusSelectorContribution.this); |
|
102 } |
|
103 } |
|
104 |
|
105 deviceInfo.setText(text); |
|
106 |
|
107 updateDeviceStatus(getDeviceStatus(defaultConnection)); |
|
108 |
|
109 deviceInfo.addMouseListener (new MouseAdapter() { |
|
110 public void mouseDown(MouseEvent event) { |
|
111 Shell shell = deviceInfo.getShell(); |
|
112 final Display display = shell.getDisplay(); |
|
113 |
|
114 final Menu menu = new Menu(shell, SWT.POP_UP); |
|
115 populateConnectionMenu(menu); |
|
116 |
|
117 Point screenLoc = deviceInfo.toDisplay(event.x, event.y); |
|
118 menu.setLocation(screenLoc.x, screenLoc.y); |
|
119 menu.setVisible(true); |
|
120 |
|
121 while (!menu.isDisposed() && menu.isVisible()) { |
|
122 if (!display.readAndDispatch()) |
|
123 display.sleep(); |
|
124 } |
|
125 menu.dispose(); |
|
126 } |
|
127 /* (non-Javadoc) |
|
128 * @see org.eclipse.swt.events.MouseAdapter#mouseDoubleClick(org.eclipse.swt.events.MouseEvent) |
|
129 */ |
|
130 @Override |
|
131 public void mouseDoubleClick(MouseEvent ev) { |
|
132 // NOTE: the menu usually comes up before double-click is seen |
|
133 if (ev.button == 1) { |
|
134 openConnectionsView(); |
|
135 } |
|
136 } |
|
137 }); |
|
138 |
|
139 // TODO PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, null); |
|
140 return container; |
|
141 } |
|
142 |
|
143 /** |
|
144 * @param ev |
|
145 */ |
|
146 protected void openConnectionsView() { |
|
147 try { |
|
148 WorkbenchUtils.getView(ConnectionsView.VIEW_ID); |
|
149 } |
|
150 catch (PartInitException e) { |
|
151 RemoteConnectionsActivator.logError(e); |
|
152 } |
|
153 } |
|
154 |
|
155 /** |
|
156 * @param defaultConnection |
|
157 * @param status |
|
158 * @return |
|
159 */ |
|
160 private String createDeviceStatusTooltip(IConnection defaultConnection, |
|
161 IStatus status) { |
|
162 if (defaultConnection == null) { |
|
163 return Messages.getString("DeviceStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$ |
|
164 } |
|
165 |
|
166 String statusString = null; |
|
167 if (status != null) { |
|
168 statusString = status.getDescription(); |
|
169 } |
|
170 |
|
171 if (TextUtils.isEmpty(statusString)) |
|
172 statusString = Messages.getString("DeviceStatusSelectorContribution.UnknownStatus"); //$NON-NLS-1$ |
|
173 |
|
174 return MessageFormat.format(Messages.getString("DeviceStatusSelectorContribution.DeviceStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$ |
|
175 } |
|
176 |
|
177 /** |
|
178 * Get the image representing the device status. |
|
179 * @param connection |
|
180 * @return Image, to be disposed |
|
181 */ |
|
182 private IStatus getDeviceStatus(IConnection connection) { |
|
183 if (!(connection instanceof IConnection2)) { |
|
184 return null; |
|
185 } else { |
|
186 return ((IConnection2) connection).getStatus(); |
|
187 } |
|
188 } |
|
189 |
|
190 /** |
|
191 * @return |
|
192 */ |
|
193 protected void populateConnectionMenu(Menu menu) { |
|
194 IConnection defaultConnection = manager.getDefaultConnection(); |
|
195 |
|
196 // Display the connections with dynamic ones first, |
|
197 // then static ones, separated by a separator |
|
198 |
|
199 List<IConnection> dynamicConnections = new ArrayList<IConnection>(); |
|
200 List<IConnection> staticConnections = new ArrayList<IConnection>(); |
|
201 for (IConnection connection : RemoteConnectionsActivator.getConnectionsManager().getConnections()) { |
|
202 if (connection instanceof IConnection2 && ((IConnection2)connection).isDynamic()) |
|
203 dynamicConnections.add(connection); |
|
204 else |
|
205 staticConnections.add(connection); |
|
206 } |
|
207 |
|
208 |
|
209 Comparator<IConnection> connectionComparator = new Comparator<IConnection>() { |
|
210 public int compare(IConnection o1, IConnection o2) { |
|
211 return o1.getDisplayName().compareToIgnoreCase(o2.getDisplayName()); |
|
212 } |
|
213 }; |
|
214 Collections.sort(dynamicConnections, connectionComparator); |
|
215 Collections.sort(staticConnections, connectionComparator); |
|
216 |
|
217 MenuItem label = new MenuItem(menu, SWT.NONE); |
|
218 label.setEnabled(false); |
|
219 label.setText(Messages.getString("DeviceStatusSelectorContribution_SelectTheDefaultConnectionMessage")); //$NON-NLS-1$ |
|
220 |
|
221 for (IConnection connection : dynamicConnections) { |
|
222 createConnectionMenuItem(menu, connection, defaultConnection); |
|
223 } |
|
224 |
|
225 new MenuItem(menu, SWT.SEPARATOR); |
|
226 |
|
227 for (IConnection connection : staticConnections) { |
|
228 createConnectionMenuItem(menu, connection, defaultConnection); |
|
229 } |
|
230 |
|
231 new MenuItem(menu, SWT.SEPARATOR); |
|
232 |
|
233 MenuItem openView = new MenuItem(menu, SWT.PUSH); |
|
234 openView.setText("Open Remote Connections view"); |
|
235 openView.addSelectionListener(new SelectionAdapter() { |
|
236 @Override |
|
237 public void widgetSelected(SelectionEvent e) { |
|
238 openConnectionsView(); |
|
239 } |
|
240 }); |
|
241 } |
|
242 |
|
243 /** |
|
244 * @param menu |
|
245 * @param connection |
|
246 * @param defaultConnection |
|
247 */ |
|
248 private MenuItem createConnectionMenuItem(Menu menu, final IConnection connection, IConnection defaultConnection) { |
|
249 MenuItem item = new MenuItem(menu, SWT.CHECK); |
|
250 |
|
251 boolean isDefault = false; |
|
252 isDefault = connection.equals(defaultConnection); |
|
253 |
|
254 item.setSelection(isDefault); |
|
255 |
|
256 item.setText(connection.getDisplayName()); |
|
257 |
|
258 item.addSelectionListener(new SelectionAdapter() { |
|
259 @Override |
|
260 public void widgetSelected(SelectionEvent e) { |
|
261 manager.setDefaultConnection(connection); |
|
262 } |
|
263 }); |
|
264 |
|
265 return item; |
|
266 } |
|
267 |
|
268 /* |
|
269 * (non-Javadoc) |
|
270 * @see org.eclipse.jface.action.ContributionItem#dispose() |
|
271 */ |
|
272 public void dispose() { |
|
273 if (deviceImage != null) { |
|
274 deviceImage.dispose(); |
|
275 deviceImage = null; |
|
276 } |
|
277 if (defaultConnection instanceof IConnection2) |
|
278 ((IConnection2) defaultConnection).removeStatusChangedListener(this); |
|
279 |
|
280 manager.removeConnectionListener(this); |
|
281 super.dispose(); |
|
282 } |
|
283 |
|
284 /* (non-Javadoc) |
|
285 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionAdded(com.nokia.carbide.remoteconnections.interfaces.IConnection) |
|
286 */ |
|
287 public void connectionAdded(IConnection connection) { |
|
288 updateUI(); |
|
289 } |
|
290 |
|
291 /* (non-Javadoc) |
|
292 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionRemoved(com.nokia.carbide.remoteconnections.interfaces.IConnection) |
|
293 */ |
|
294 public void connectionRemoved(IConnection connection) { |
|
295 updateUI(); |
|
296 } |
|
297 /* (non-Javadoc) |
|
298 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#defaultConnectionSet(com.nokia.carbide.remoteconnections.interfaces.IConnection) |
|
299 */ |
|
300 public void defaultConnectionSet(IConnection connection) { |
|
301 updateUI(); |
|
302 } |
|
303 |
|
304 /* (non-Javadoc) |
|
305 * @see com.nokia.carbide.remoteconnections.internal.IConnection2.IStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.internal.IConnection2.IStatus) |
|
306 */ |
|
307 public void statusChanged(IStatus status) { |
|
308 updateDeviceStatus(status); |
|
309 } |
|
310 |
|
311 /** |
|
312 * @param status |
|
313 */ |
|
314 private void updateDeviceStatus(IStatus status) { |
|
315 if (deviceImage != null) |
|
316 deviceImage.dispose(); |
|
317 |
|
318 deviceImage = ConnectionUIUtils.getConnectionStatusImage(status); |
|
319 deviceInfo.setImage(deviceImage); |
|
320 deviceInfo.setToolTipText(createDeviceStatusTooltip(defaultConnection, status)); |
|
321 } |
|
322 |
|
323 /** |
|
324 * |
|
325 */ |
|
326 private void updateUI() { |
|
327 // perform update in UI thread |
|
328 final IWorkbench workbench = PlatformUI.getWorkbench(); |
|
329 workbench.getDisplay().asyncExec(new Runnable() { |
|
330 public void run() { |
|
331 IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); |
|
332 if (window != null) { |
|
333 update(); |
|
334 } |
|
335 } |
|
336 }); |
|
337 } |
|
338 |
|
339 /* |
|
340 * (non-Javadoc) |
|
341 * @see org.eclipse.jface.action.ContributionItem#isDynamic() |
|
342 */ |
|
343 @Override |
|
344 public boolean isDynamic() { |
|
345 return true; |
|
346 } |
|
347 |
|
348 /* |
|
349 * (non-Javadoc) |
|
350 * @see org.eclipse.jface.action.ContributionItem#update() |
|
351 */ |
|
352 @Override |
|
353 public void update() { |
|
354 getParent().update(true); |
|
355 } |
|
356 |
|
357 } |