66 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
66 import com.nokia.cpp.internal.api.utils.ui.WorkbenchUtils; |
67 |
67 |
68 |
68 |
69 /** |
69 /** |
70 * This widget appears in the Eclipse trim and allows the user to select the |
70 * This widget appears in the Eclipse trim and allows the user to select the |
71 * "default" device connection and also see its status at a glance. |
71 * "current" device connection and also see its status at a glance. |
72 * <p> |
72 * <p> |
73 * Note: the UI for this control should behave similarly to that of the News Reader |
73 * Note: the UI for this control should behave similarly to that of the News Reader |
74 * trim. Due to the way we're modifying the icon and the state dynamically |
74 * trim. Due to the way we're modifying the icon and the state dynamically |
75 * for this contribution, they can't be implemented the same way, however. |
75 * for this contribution, they can't be implemented the same way, however. |
76 */ |
76 */ |
84 private static final String OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_ID = "openRemoteConnectionsView"; //$NON-NLS-1$ |
84 private static final String OPEN_REMOTE_CONNECTIONS_VIEW_COMMAND_ID = "openRemoteConnectionsView"; //$NON-NLS-1$ |
85 private Composite container; |
85 private Composite container; |
86 private CLabel connectionInfo; |
86 private CLabel connectionInfo; |
87 private ToolItem connectionIcon; |
87 private ToolItem connectionIcon; |
88 private IConnectionsManager manager; |
88 private IConnectionsManager manager; |
89 private IConnection defaultConnection; |
89 private IConnection currentConnection; |
90 private ListenerBlock listenerBlock; |
90 private ListenerBlock listenerBlock; |
91 private ToolTip tooltip; |
91 private ToolTip tooltip; |
92 private MouseAdapter toolbarListener; |
92 private MouseAdapter toolbarListener; |
93 private ToolBar toolbar; |
93 private ToolBar toolbar; |
94 |
94 |
120 Messages.getString("ConnectionStatusSelectorContribution.RemovedConnectionFormat"), //$NON-NLS-1$ |
120 Messages.getString("ConnectionStatusSelectorContribution.RemovedConnectionFormat"), //$NON-NLS-1$ |
121 connection.getDisplayName())); |
121 connection.getDisplayName())); |
122 } |
122 } |
123 |
123 |
124 /* (non-Javadoc) |
124 /* (non-Javadoc) |
125 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#defaultConnectionSet(com.nokia.carbide.remoteconnections.interfaces.IConnection) |
125 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#currentConnectionSet(com.nokia.carbide.remoteconnections.interfaces.IConnection) |
126 */ |
126 */ |
127 public void defaultConnectionSet(IConnection connection) { |
127 public void currentConnectionSet(IConnection connection) { |
128 updateUI(); |
128 updateUI(); |
129 } |
129 } |
130 |
130 |
131 /* (non-Javadoc) |
131 /* (non-Javadoc) |
132 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#connectionStoreChanged() |
132 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#connectionStoreChanged() |
161 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite) |
161 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite) |
162 */ |
162 */ |
163 @Override |
163 @Override |
164 protected Control createControl(Composite parent) { |
164 protected Control createControl(Composite parent) { |
165 |
165 |
166 // This UI is recreated whenever the default connection changes. |
166 // This UI is recreated whenever the current connection changes. |
167 |
167 |
168 // This is gross. The normal parent of this is a toolbar, |
168 // This is gross. The normal parent of this is a toolbar, |
169 // but we cannot add arbitrary stuff to the toolbar besides |
169 // but we cannot add arbitrary stuff to the toolbar besides |
170 // this control. (If you try, it will usually show up |
170 // this control. (If you try, it will usually show up |
171 // out of order!) |
171 // out of order!) |
198 |
198 |
199 // Create a label for the trim, outside the toolbar. |
199 // Create a label for the trim, outside the toolbar. |
200 connectionInfo = new CLabel(container, SWT.NONE); |
200 connectionInfo = new CLabel(container, SWT.NONE); |
201 GridDataFactory.fillDefaults().grab(false, true).applyTo(connectionInfo); |
201 GridDataFactory.fillDefaults().grab(false, true).applyTo(connectionInfo); |
202 |
202 |
203 String text = Messages.getString("ConnectionStatusSelectorContribution_NoDefaultConnectionMessage"); //$NON-NLS-1$ |
203 String text = Messages.getString("ConnectionStatusSelectorContribution_NoCurrentConnectionMessage"); //$NON-NLS-1$ |
204 defaultConnection = manager.getDefaultConnection(); |
204 currentConnection = manager.getCurrentConnection(); |
205 if (defaultConnection != null) |
205 if (currentConnection != null) |
206 text = defaultConnection.getDisplayName(); |
206 text = currentConnection.getDisplayName(); |
207 |
207 |
208 connectionInfo.setText(text); |
208 connectionInfo.setText(text); |
209 |
209 |
210 attachListeners(); |
210 attachListeners(); |
211 |
211 |
212 updateConnectionStatus(getConnectionStatus(defaultConnection)); |
212 updateConnectionStatus(getConnectionStatus(currentConnection)); |
213 |
213 |
214 |
214 |
215 // Yuck, toolbars and items have a wonky UI. We need to do these events on the parent, |
215 // Yuck, toolbars and items have a wonky UI. We need to do these events on the parent, |
216 // since the ToolItem itself is just an area inside the parent. (#getControl() is only for separators ?!) |
216 // since the ToolItem itself is just an area inside the parent. (#getControl() is only for separators ?!) |
217 |
217 |
303 |
303 |
304 int number = 1; |
304 int number = 1; |
305 if (dynamicConnections.size() + staticConnections.size() == 0) { |
305 if (dynamicConnections.size() + staticConnections.size() == 0) { |
306 label.setText(Messages.getString("ConnectionStatusSelectorContribution.NoConnectionsDefinedOrDetected")); //$NON-NLS-1$ |
306 label.setText(Messages.getString("ConnectionStatusSelectorContribution.NoConnectionsDefinedOrDetected")); //$NON-NLS-1$ |
307 } else { |
307 } else { |
308 label.setText(Messages.getString("ConnectionStatusSelectorContribution_SelectTheDefaultConnectionMessage")); //$NON-NLS-1$ |
308 label.setText(Messages.getString("ConnectionStatusSelectorContribution_SelectTheCurrentConnectionMessage")); //$NON-NLS-1$ |
309 |
309 |
310 for (IConnection connection : dynamicConnections) { |
310 for (IConnection connection : dynamicConnections) { |
311 createConnectionMenuItem(menu, connection, defaultConnection, number++); |
311 createConnectionMenuItem(menu, connection, currentConnection, number++); |
312 } |
312 } |
313 |
313 |
314 if (!staticConnections.isEmpty()) |
314 if (!staticConnections.isEmpty()) |
315 new MenuItem(menu, SWT.SEPARATOR); |
315 new MenuItem(menu, SWT.SEPARATOR); |
316 |
316 |
317 for (IConnection connection : staticConnections) { |
317 for (IConnection connection : staticConnections) { |
318 createConnectionMenuItem(menu, connection, defaultConnection, number++); |
318 createConnectionMenuItem(menu, connection, currentConnection, number++); |
319 } |
319 } |
320 } |
320 } |
321 |
321 |
322 new MenuItem(menu, SWT.SEPARATOR); |
322 new MenuItem(menu, SWT.SEPARATOR); |
323 |
323 |
332 } |
332 } |
333 |
333 |
334 /** |
334 /** |
335 * @param menu |
335 * @param menu |
336 * @param connection |
336 * @param connection |
337 * @param defaultConnection |
337 * @param currentConnection |
338 */ |
338 */ |
339 private MenuItem createConnectionMenuItem(Menu menu, |
339 private MenuItem createConnectionMenuItem(Menu menu, |
340 final IConnection connection, |
340 final IConnection connection, |
341 IConnection defaultConnection, |
341 IConnection currentConnection, |
342 int number) { |
342 int number) { |
343 MenuItem item = new MenuItem(menu, SWT.CHECK); |
343 MenuItem item = new MenuItem(menu, SWT.CHECK); |
344 |
344 |
345 boolean isDefault = false; |
345 boolean isCurrent = false; |
346 isDefault = connection.equals(defaultConnection); |
346 isCurrent = connection.equals(currentConnection); |
347 |
347 |
348 item.setSelection(isDefault); |
348 item.setSelection(isCurrent); |
349 |
349 |
350 item.setText(MessageFormat.format("&{0} - {1}", number, connection.getDisplayName())); //$NON-NLS-1$ |
350 item.setText(MessageFormat.format("&{0} - {1}", number, connection.getDisplayName())); //$NON-NLS-1$ |
351 |
351 |
352 item.addSelectionListener(new SelectionAdapter() { |
352 item.addSelectionListener(new SelectionAdapter() { |
353 @Override |
353 @Override |
354 public void widgetSelected(SelectionEvent e) { |
354 public void widgetSelected(SelectionEvent e) { |
355 manager.setDefaultConnection(connection); |
355 manager.setCurrentConnection(connection); |
356 } |
356 } |
357 }); |
357 }); |
358 |
358 |
359 return item; |
359 return item; |
360 } |
360 } |
361 |
361 |
362 private void attachListeners() { |
362 private void attachListeners() { |
363 manager.addConnectionListener(listenerBlock); |
363 manager.addConnectionListener(listenerBlock); |
364 Registry.instance().addConnectionStoreChangedListener(listenerBlock); |
364 Registry.instance().addConnectionStoreChangedListener(listenerBlock); |
365 |
365 |
366 if (defaultConnection != null) { |
366 if (currentConnection != null) { |
367 if (defaultConnection instanceof IConnection2) { |
367 if (currentConnection instanceof IConnection2) { |
368 ((IConnection2) defaultConnection).addStatusChangedListener(listenerBlock); |
368 ((IConnection2) currentConnection).addStatusChangedListener(listenerBlock); |
369 } |
369 } |
370 } |
370 } |
371 } |
371 } |
372 |
372 |
373 private void removeListeners() { |
373 private void removeListeners() { |
374 if (defaultConnection != null) { |
374 if (currentConnection != null) { |
375 if (defaultConnection instanceof IConnection2) { |
375 if (currentConnection instanceof IConnection2) { |
376 ((IConnection2) defaultConnection).removeStatusChangedListener(listenerBlock); |
376 ((IConnection2) currentConnection).removeStatusChangedListener(listenerBlock); |
377 } |
377 } |
378 } |
378 } |
379 manager.removeConnectionListener(listenerBlock); |
379 manager.removeConnectionListener(listenerBlock); |
380 Registry.instance().removeConnectionStoreChangedListener(listenerBlock); |
380 Registry.instance().removeConnectionStoreChangedListener(listenerBlock); |
381 } |
381 } |
400 * @param ev |
400 * @param ev |
401 */ |
401 */ |
402 protected void openConnectionsView() { |
402 protected void openConnectionsView() { |
403 try { |
403 try { |
404 IViewPart view = WorkbenchUtils.getView(ConnectionsView.VIEW_ID); |
404 IViewPart view = WorkbenchUtils.getView(ConnectionsView.VIEW_ID); |
405 if (defaultConnection != null && view instanceof ConnectionsView) { |
405 if (currentConnection != null && view instanceof ConnectionsView) { |
406 ((ConnectionsView) view).setSelectedConnection(defaultConnection); |
406 ((ConnectionsView) view).setSelectedConnection(currentConnection); |
407 } |
407 } |
408 } |
408 } |
409 catch (PartInitException e) { |
409 catch (PartInitException e) { |
410 RemoteConnectionsActivator.logError(e); |
410 RemoteConnectionsActivator.logError(e); |
411 } |
411 } |
412 } |
412 } |
413 |
413 |
414 /** |
414 /** |
415 * @param defaultConnection |
415 * @param currentConnection |
416 * @param status |
416 * @param status |
417 * @return |
417 * @return |
418 */ |
418 */ |
419 private String createConnectionStatusTooltip(IConnection defaultConnection, |
419 private String createConnectionStatusTooltip(IConnection currentConnection, |
420 IConnectionStatus status) { |
420 IConnectionStatus status) { |
421 if (defaultConnection == null) { |
421 if (currentConnection == null) { |
422 return Messages.getString("ConnectionStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$ |
422 return Messages.getString("ConnectionStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$ |
423 } |
423 } |
424 |
424 |
425 String statusString = null; |
425 String statusString = null; |
426 if (status != null) { |
426 if (status != null) { |
427 statusString = createStatusString(status); |
427 statusString = createStatusString(status); |
428 } |
428 } |
429 |
429 |
430 if (TextUtils.isEmpty(statusString)) { |
430 if (TextUtils.isEmpty(statusString)) { |
431 // fallback string; the status should not be empty |
431 // fallback string; the status should not be empty |
432 if (ConnectionUIUtils.isSomeServiceInUse(defaultConnection)) |
432 if (ConnectionUIUtils.isSomeServiceInUse(currentConnection)) |
433 statusString = Messages.getString("ConnectionStatusSelectorContribution.InUse"); //$NON-NLS-1$ |
433 statusString = Messages.getString("ConnectionStatusSelectorContribution.InUse"); //$NON-NLS-1$ |
434 else |
434 else |
435 statusString = Messages.getString("ConnectionStatusSelectorContribution.NotInUse"); //$NON-NLS-1$ |
435 statusString = Messages.getString("ConnectionStatusSelectorContribution.NotInUse"); //$NON-NLS-1$ |
436 } |
436 } |
437 |
437 |
438 return MessageFormat.format(Messages.getString("ConnectionStatusSelectorContribution.ConnectionStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$ |
438 return MessageFormat.format(Messages.getString("ConnectionStatusSelectorContribution.ConnectionStatusFormat"), currentConnection.getDisplayName(), statusString); //$NON-NLS-1$ |
439 } |
439 } |
440 |
440 |
441 private String createStatusString(IConnectionStatus status) { |
441 private String createStatusString(IConnectionStatus status) { |
442 String shortDescription = status.getShortDescription(); |
442 String shortDescription = status.getShortDescription(); |
443 if (shortDescription == null || shortDescription.length() == 0) |
443 if (shortDescription == null || shortDescription.length() == 0) |
472 |
472 |
473 Image statusImage; |
473 Image statusImage; |
474 if (status != null) |
474 if (status != null) |
475 statusImage = ConnectionUIUtils.getConnectionStatusImage(status); |
475 statusImage = ConnectionUIUtils.getConnectionStatusImage(status); |
476 else |
476 else |
477 statusImage = ConnectionUIUtils.getConnectionImage(defaultConnection); |
477 statusImage = ConnectionUIUtils.getConnectionImage(currentConnection); |
478 |
478 |
479 connectionIcon.setImage(statusImage); |
479 connectionIcon.setImage(statusImage); |
480 String tip = createConnectionStatusTooltip(defaultConnection, status); |
480 String tip = createConnectionStatusTooltip(currentConnection, status); |
481 connectionInfo.setToolTipText(tip); |
481 connectionInfo.setToolTipText(tip); |
482 |
482 |
483 String preamble = Messages.getString("ConnectionStatusSelectorContribution.IconTooltipPrefixMessage"); //$NON-NLS-1$ |
483 String preamble = Messages.getString("ConnectionStatusSelectorContribution.IconTooltipPrefixMessage"); //$NON-NLS-1$ |
484 connectionIcon.setToolTipText(preamble + tip); |
484 connectionIcon.setToolTipText(preamble + tip); |
485 } |
485 } |