connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionStatusSelectorContribution.java
changeset 714 eeeba49ae3f1
parent 710 47bb893ba749
child 721 dbd80b3d41cc
equal deleted inserted replaced
713:6c5fbb4d875b 714:eeeba49ae3f1
    37 import org.eclipse.swt.widgets.Control;
    37 import org.eclipse.swt.widgets.Control;
    38 import org.eclipse.swt.widgets.Display;
    38 import org.eclipse.swt.widgets.Display;
    39 import org.eclipse.swt.widgets.Menu;
    39 import org.eclipse.swt.widgets.Menu;
    40 import org.eclipse.swt.widgets.MenuItem;
    40 import org.eclipse.swt.widgets.MenuItem;
    41 import org.eclipse.swt.widgets.Shell;
    41 import org.eclipse.swt.widgets.Shell;
       
    42 import org.eclipse.swt.widgets.ToolTip;
    42 import org.eclipse.ui.IWorkbench;
    43 import org.eclipse.ui.IWorkbench;
    43 import org.eclipse.ui.IWorkbenchWindow;
    44 import org.eclipse.ui.IWorkbenchWindow;
    44 import org.eclipse.ui.PartInitException;
    45 import org.eclipse.ui.PartInitException;
    45 import org.eclipse.ui.PlatformUI;
    46 import org.eclipse.ui.PlatformUI;
    46 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    47 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    47 
    48 
    48 import com.nokia.carbide.remoteconnections.Messages;
    49 import com.nokia.carbide.remoteconnections.Messages;
    49 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    50 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
       
    51 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
    50 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
    52 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
    51 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
    53 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
       
    54 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
       
    55 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
    52 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
    56 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
    53 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
    57 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
    54 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
    58 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
    55 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
    59 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
    56 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
    60 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
    62 
    66 
    63 /**
    67 /**
    64  * This widget appears in the Eclipse trim and allows the user to select the
    68  * This widget appears in the Eclipse trim and allows the user to select the
    65  * "default" device connection and also see its status at a glance. 
    69  * "default" device connection and also see its status at a glance. 
    66  */
    70  */
    67 public class ConnectionStatusSelectorContribution extends WorkbenchWindowControlContribution
    71 @SuppressWarnings("deprecation")
    68 		implements IConnectionListener, IConnectionStatusChangedListener, IConnectionsManagerListener {
    72 public class ConnectionStatusSelectorContribution extends WorkbenchWindowControlContribution {
    69 
    73 
    70 	private Composite container;
    74 	private Composite container;
    71 	private CLabel connectionInfo;
    75 	private CLabel connectionInfo;
    72 	private IConnectionsManager manager;
    76 	private IConnectionsManager manager;
    73 	private IConnection defaultConnection;
    77 	private IConnection defaultConnection;
       
    78 	private ListenerBlock listenerBlock;
       
    79 	private ToolTip tooltip;
       
    80 
       
    81 	/**
       
    82 	 * Contains all the listeners.  In most cases we just recreate the contribution status item.
       
    83 	 */
       
    84 	class ListenerBlock implements IConnectionListener, IConnectionsManagerListener, IStatusChangedListener, IConnectionStatusChangedListener {
       
    85 
       
    86 		/* (non-Javadoc)
       
    87 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionAdded(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
    88 		 */
       
    89 		public void connectionAdded(IConnection connection) {
       
    90 			updateUI();
       
    91 			if (connection instanceof IConnection2 && ((IConnection2) connection).isDynamic())
       
    92 				launchBubble(MessageFormat.format(
       
    93 						Messages.getString("ConnectionStatusSelectorContribution.AddedConnectionFormat"),  //$NON-NLS-1$
       
    94 						connection.getDisplayName()));
       
    95 		}
       
    96 		
       
    97 		/* (non-Javadoc)
       
    98 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionRemoved(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
    99 		 */
       
   100 		public void connectionRemoved(IConnection connection) {
       
   101 			updateUI();
       
   102 			if (connection instanceof IConnection2 && ((IConnection2) connection).isDynamic())
       
   103 				launchBubble(MessageFormat.format(
       
   104 						Messages.getString("ConnectionStatusSelectorContribution.RemovedConnectionFormat"),  //$NON-NLS-1$
       
   105 						connection.getDisplayName()));
       
   106 		}
       
   107 		
       
   108 		/* (non-Javadoc)
       
   109 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#defaultConnectionSet(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
   110 		 */
       
   111 		public void defaultConnectionSet(IConnection connection) {
       
   112 			updateUI();
       
   113 		}
       
   114 
       
   115 		/* (non-Javadoc)
       
   116 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#connectionStoreChanged()
       
   117 		 */
       
   118 		public void connectionStoreChanged() {
       
   119 			updateUI();
       
   120 		}
       
   121 
       
   122 		/* (non-Javadoc)
       
   123 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#displayChanged()
       
   124 		 */
       
   125 		public void displayChanged() {
       
   126 			updateUI();
       
   127 		}
       
   128 
       
   129 		/* (non-Javadoc)
       
   130 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus)
       
   131 		 */
       
   132 		public void statusChanged(IStatus status) {
       
   133 			updateUI();
       
   134 		}
       
   135 
       
   136 		/* (non-Javadoc)
       
   137 		 * @see com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus)
       
   138 		 */
       
   139 		public void statusChanged(IConnectionStatus status) {
       
   140 			updateConnectionStatus(status);
       
   141 		}
       
   142 
       
   143 	}
    74 	
   144 	
    75 	public ConnectionStatusSelectorContribution() {
   145 	public ConnectionStatusSelectorContribution() {
    76 		manager = RemoteConnectionsActivator.getConnectionsManager();
   146 		manager = RemoteConnectionsActivator.getConnectionsManager();
       
   147 		listenerBlock = new ListenerBlock();
    77 	}
   148 	}
    78 	
   149 	
    79 	/*
   150 	/*
    80 	 * (non-Javadoc)
   151 	 * (non-Javadoc)
    81 	 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite)
   152 	 * @see org.eclipse.jface.action.ControlContribution#createControl(org.eclipse.swt.widgets.Composite)
    83 	@Override
   154 	@Override
    84 	protected Control createControl(Composite parent) {
   155 	protected Control createControl(Composite parent) {
    85 		
   156 		
    86 		// This UI is recreated whenever the default connection changes.
   157 		// This UI is recreated whenever the default connection changes.
    87 		
   158 		
    88 		manager.addConnectionListener(this);
       
    89 		Registry.instance().addConnectionStoreChangedListener(this);
       
    90 		
       
    91 		container = new Composite(parent, SWT.NONE);
   159 		container = new Composite(parent, SWT.NONE);
    92 		GridLayoutFactory.fillDefaults().margins(2, 0).applyTo(container);
   160 		GridLayoutFactory.fillDefaults().margins(2, 0).applyTo(container);
    93 
   161 
    94 		// Create a label for the trim.
   162 		// Create a label for the trim.
    95 		connectionInfo = new CLabel(container, SWT.FLAT);
   163 		connectionInfo = new CLabel(container, SWT.FLAT);
    96 		GridDataFactory.fillDefaults().grab(false, true).applyTo(connectionInfo);
   164 		GridDataFactory.fillDefaults().grab(false, true).applyTo(connectionInfo);
    97 		
   165 
    98 		String text = Messages.getString("DeviceStatusSelectorContribution_NoDefaultConnectionMessage"); //$NON-NLS-1$
   166 		String text = Messages.getString("ConnectionStatusSelectorContribution_NoDefaultConnectionMessage"); //$NON-NLS-1$
    99 		defaultConnection = manager.getDefaultConnection();
   167 		defaultConnection = manager.getDefaultConnection();
   100 		if (defaultConnection != null) {
   168 		if (defaultConnection != null)
   101 			text = defaultConnection.getDisplayName();
   169 			text = defaultConnection.getDisplayName();
   102 			if (defaultConnection instanceof IConnection2) {
       
   103 				((IConnection2) defaultConnection).addStatusChangedListener(ConnectionStatusSelectorContribution.this);
       
   104 			}
       
   105 		}
       
   106 		
   170 		
   107 		connectionInfo.setText(text);
   171 		connectionInfo.setText(text);
       
   172 
       
   173 		attachListeners();
   108 		
   174 		
   109 		updateConnectionStatus(getConnectionStatus(defaultConnection));
   175 		updateConnectionStatus(getConnectionStatus(defaultConnection));
   110 		
   176 		
   111 		connectionInfo.addMouseListener (new MouseAdapter() {
   177 		connectionInfo.addMouseListener (new MouseAdapter() {
   112 			public void mouseDown(MouseEvent event) {
   178 			public void mouseDown(MouseEvent event) {
   136 					openConnectionsView();
   202 					openConnectionsView();
   137 				}
   203 				}
   138 			}
   204 			}
   139 		});
   205 		});
   140 		
   206 		
   141 		RemoteConnectionsActivator.setHelp(container, "DeviceStatusSelector");
   207 		RemoteConnectionsActivator.setHelp(container, "ConnectionStatusSelector"); //$NON-NLS-1$
   142 		return container;
   208 		return container;
       
   209 	}
       
   210 
       
   211 	private void attachListeners() {
       
   212 		manager.addConnectionListener(listenerBlock);
       
   213 		Registry.instance().addConnectionStoreChangedListener(listenerBlock);
       
   214 		
       
   215 		if (defaultConnection != null) {
       
   216 			if (defaultConnection instanceof IConnection2) {
       
   217 				((IConnection2) defaultConnection).addStatusChangedListener(listenerBlock);
       
   218 			}
       
   219 			for (IConnectedService service : manager.getConnectedServices(defaultConnection)) {
       
   220 				service.addStatusChangedListener(listenerBlock);
       
   221 			}
       
   222 		}
       
   223 	}
       
   224 
       
   225 	private void removeListeners() {
       
   226 		if (defaultConnection != null) {
       
   227 			if (defaultConnection instanceof IConnection2) {
       
   228 				((IConnection2) defaultConnection).removeStatusChangedListener(listenerBlock);
       
   229 			}
       
   230 			for (IConnectedService service : manager.getConnectedServices(defaultConnection)) {
       
   231 				service.removeStatusChangedListener(listenerBlock);
       
   232 			}
       
   233 		}
       
   234 		manager.removeConnectionListener(listenerBlock);
       
   235 		Registry.instance().removeConnectionStoreChangedListener(listenerBlock);
       
   236 	}
       
   237 
       
   238 	/*
       
   239 	 * (non-Javadoc)
       
   240 	 * @see org.eclipse.jface.action.ContributionItem#dispose()
       
   241 	 */
       
   242 	public void dispose() {
       
   243 		removeListeners();
       
   244 		if (connectionInfo != null)
       
   245 			connectionInfo.dispose();
       
   246 		super.dispose();
   143 	}
   247 	}
   144 
   248 
   145 	/**
   249 	/**
   146 	 * @param ev
   250 	 * @param ev
   147 	 */
   251 	 */
   157 	/**
   261 	/**
   158 	 * @param defaultConnection
   262 	 * @param defaultConnection
   159 	 * @param status
   263 	 * @param status
   160 	 * @return
   264 	 * @return
   161 	 */
   265 	 */
   162 	private String createDeviceStatusTooltip(IConnection defaultConnection,
   266 	private String createConnectionStatusTooltip(IConnection defaultConnection,
   163 			IConnectionStatus status) {
   267 			IConnectionStatus status) {
   164 		if (defaultConnection == null) {
   268 		if (defaultConnection == null) {
   165 			return Messages.getString("DeviceStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$
   269 			return Messages.getString("ConnectionStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$
   166 		}
   270 		}
   167 		
   271 		
   168 		String statusString = null;
   272 		String statusString = null;
   169 		if (status != null) {
   273 		if (status != null) {
   170 			statusString = status.getDescription();
   274 			statusString = status.getDescription();
   171 		}
   275 		}
   172 		
   276 		
   173 		if (TextUtils.isEmpty(statusString))
   277 		if (TextUtils.isEmpty(statusString))
   174 			statusString = Messages.getString("DeviceStatusSelectorContribution.NotInUse"); //$NON-NLS-1$
   278 			statusString = Messages.getString("ConnectionStatusSelectorContribution.NotInUse"); //$NON-NLS-1$
   175 		
   279 		
   176 		return MessageFormat.format(Messages.getString("DeviceStatusSelectorContribution.DeviceStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$
   280 		return MessageFormat.format(Messages.getString("ConnectionStatusSelectorContribution.ConnectionStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$
   177 	}
   281 	}
   178 
   282 
   179 	/**
   283 	/**
   180 	 * Get the image representing the connection status.
   284 	 * Get the image representing the connection status.
   181 	 * @param connection
   285 	 * @param connection
   191 	
   295 	
   192 	/**
   296 	/**
   193 	 * @return
   297 	 * @return
   194 	 */
   298 	 */
   195 	protected void populateConnectionMenu(Menu menu) {
   299 	protected void populateConnectionMenu(Menu menu) {
   196 		IConnection defaultConnection = manager.getDefaultConnection();
       
   197 
       
   198 		// Display the connections with dynamic ones first, 
   300 		// Display the connections with dynamic ones first, 
   199 		// then static ones, separated by a separator
   301 		// then static ones, separated by a separator
   200 
   302 
   201 		List<IConnection> dynamicConnections = new ArrayList<IConnection>();
   303 		List<IConnection> dynamicConnections = new ArrayList<IConnection>();
   202 		List<IConnection> staticConnections = new ArrayList<IConnection>();
   304 		List<IConnection> staticConnections = new ArrayList<IConnection>();
   216 		Collections.sort(dynamicConnections, connectionComparator);
   318 		Collections.sort(dynamicConnections, connectionComparator);
   217 		Collections.sort(staticConnections, connectionComparator);
   319 		Collections.sort(staticConnections, connectionComparator);
   218 
   320 
   219 		MenuItem label = new MenuItem(menu, SWT.NONE);
   321 		MenuItem label = new MenuItem(menu, SWT.NONE);
   220 		label.setEnabled(false);
   322 		label.setEnabled(false);
   221 		label.setText(Messages.getString("DeviceStatusSelectorContribution_SelectTheDefaultConnectionMessage")); //$NON-NLS-1$
   323 		
   222 		
   324 		if (dynamicConnections.size() + staticConnections.size() == 0) {
   223 		for (IConnection connection : dynamicConnections) {
   325 			label.setText(Messages.getString("ConnectionStatusSelectorContribution.NoConnectionsDefinedOrDetected")); //$NON-NLS-1$
   224 			createConnectionMenuItem(menu, connection, defaultConnection);
   326 		} else {
       
   327 			label.setText(Messages.getString("ConnectionStatusSelectorContribution_SelectTheDefaultConnectionMessage")); //$NON-NLS-1$
       
   328 			
       
   329 			for (IConnection connection : dynamicConnections) {
       
   330 				createConnectionMenuItem(menu, connection, defaultConnection);
       
   331 			}
       
   332 			
       
   333 			new MenuItem(menu, SWT.SEPARATOR);
       
   334 			
       
   335 			for (IConnection connection : staticConnections) {
       
   336 				createConnectionMenuItem(menu, connection, defaultConnection);
       
   337 			}
   225 		}
   338 		}
   226 		
   339 		
   227 		new MenuItem(menu, SWT.SEPARATOR);
   340 		new MenuItem(menu, SWT.SEPARATOR);
   228 		
   341 		
   229 		for (IConnection connection : staticConnections) {
       
   230 			createConnectionMenuItem(menu, connection, defaultConnection);
       
   231 		}
       
   232 		
       
   233 		new MenuItem(menu, SWT.SEPARATOR);
       
   234 		
       
   235 		MenuItem openView = new MenuItem(menu, SWT.PUSH);
   342 		MenuItem openView = new MenuItem(menu, SWT.PUSH);
   236 		openView.setText("Open Remote Connections view");
   343 		openView.setText(Messages.getString("ConnectionStatusSelectorContribution.OpenRemoteConnectionsView")); //$NON-NLS-1$
   237 		openView.addSelectionListener(new SelectionAdapter() {
   344 		openView.addSelectionListener(new SelectionAdapter() {
   238 			@Override
   345 			@Override
   239 			public void widgetSelected(SelectionEvent e) {
   346 			public void widgetSelected(SelectionEvent e) {
   240 				openConnectionsView();
   347 				openConnectionsView();
   241 			}
   348 			}
   265 		});		
   372 		});		
   266 		
   373 		
   267 		return item;
   374 		return item;
   268 	}
   375 	}
   269 
   376 
   270 	/*
       
   271 	 * (non-Javadoc)
       
   272 	 * @see org.eclipse.jface.action.ContributionItem#dispose()
       
   273 	 */
       
   274 	public void dispose() {
       
   275 		if (defaultConnection instanceof IConnection2)
       
   276 			((IConnection2) defaultConnection).removeStatusChangedListener(this);
       
   277 		
       
   278 		manager.removeConnectionListener(this);
       
   279 		super.dispose();
       
   280 	}
       
   281 
       
   282 	/* (non-Javadoc)
       
   283 	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionAdded(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
   284 	 */
       
   285 	public void connectionAdded(IConnection connection) {
       
   286 		updateUI();
       
   287 	}
       
   288 	
       
   289 	/* (non-Javadoc)
       
   290 	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionRemoved(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
   291 	 */
       
   292 	public void connectionRemoved(IConnection connection) {
       
   293 		updateUI();
       
   294 	}
       
   295 	/* (non-Javadoc)
       
   296 	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#defaultConnectionSet(com.nokia.carbide.remoteconnections.interfaces.IConnection)
       
   297 	 */
       
   298 	public void defaultConnectionSet(IConnection connection) {
       
   299 		defaultConnection = connection;
       
   300 		updateUI();
       
   301 	}
       
   302 
       
   303 	/* (non-Javadoc)
       
   304 	 * @see com.nokia.carbide.remoteconnections.internal.IConnection2.IStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.internal.IConnection2.IStatus)
       
   305 	 */
       
   306 	public void statusChanged(IConnectionStatus status) {
       
   307 		updateConnectionStatus(status);
       
   308 	}
       
   309 	
       
   310 	/**
   377 	/**
   311 	 * @param status
   378 	 * @param status
   312 	 */
   379 	 */
   313 	private void updateConnectionStatus(IConnectionStatus status) {
   380 	private void updateConnectionStatus(IConnectionStatus status) {
   314 		Image deviceImage = ConnectionUIUtils.getConnectionStatusImage(status);
   381 		Image statusImage = ConnectionUIUtils.getConnectionStatusImage(status);
   315 		connectionInfo.setImage(deviceImage);
   382 		connectionInfo.setImage(statusImage);
   316 		connectionInfo.setToolTipText(createDeviceStatusTooltip(defaultConnection, status));
   383 		connectionInfo.setToolTipText(createConnectionStatusTooltip(defaultConnection, status));
   317 	}
   384 	}
   318 
   385 
   319 	/**
   386 	/**
   320 	 * 
   387 	 * 
   321 	 */
   388 	 */
   347 	 */
   414 	 */
   348 	@Override
   415 	@Override
   349 	public void update() {
   416 	public void update() {
   350 		getParent().update(true);
   417 		getParent().update(true);
   351 	}
   418 	}
   352 
   419 	
   353 	/* (non-Javadoc)
   420 	/**
   354 	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#connectionStoreChanged()
   421 	 * Show a status bubble for important changes.
   355 	 */
   422 	 * @param string
   356 	public void connectionStoreChanged() {
   423 	 */
   357 		updateUI();
   424 	public void launchBubble(final String string) {
   358 	}
   425 		Display.getDefault().asyncExec(new Runnable() {
   359 
   426 			public void run() {
   360 	/* (non-Javadoc)
   427 				if (tooltip != null)
   361 	 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#displayChanged()
   428 					tooltip.dispose();
   362 	 */
   429 				
   363 	public void displayChanged() {
   430 				if (connectionInfo == null || connectionInfo.isDisposed())
   364 		updateUI();
   431 					return;
   365 	}
   432 				
       
   433 				tooltip = new ToolTip(connectionInfo.getShell(), SWT.BALLOON | SWT.ICON_INFORMATION);
       
   434 				tooltip.setMessage(string);
       
   435 				Point center = connectionInfo.getSize();
       
   436 				center.x /= 2;
       
   437 				Point location = connectionInfo.toDisplay(center);
       
   438 				//System.out.println(connectionInfo.hashCode() + ": " + connectionInfo.getLocation() + " : " + location);
       
   439 				tooltip.setLocation(location);
       
   440 				tooltip.setVisible(true);
       
   441 			}
       
   442 		});
       
   443 	}
       
   444 
   366 
   445 
   367 }
   446 }