connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/internal/ui/ConnectionStatusSelectorContribution.java
changeset 748 7bd40a2d0a18
parent 730 8ecd45ef360e
child 749 22f0d10fce10
equal deleted inserted replaced
747:11dc6d4e9da5 748:7bd40a2d0a18
    50 import org.eclipse.ui.menus.CommandContributionItem;
    50 import org.eclipse.ui.menus.CommandContributionItem;
    51 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    51 import org.eclipse.ui.menus.WorkbenchWindowControlContribution;
    52 
    52 
    53 import com.nokia.carbide.remoteconnections.Messages;
    53 import com.nokia.carbide.remoteconnections.Messages;
    54 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    54 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    55 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
       
    56 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
    55 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
    57 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
    56 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
    58 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
       
    59 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
       
    60 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
    57 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
    61 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
    58 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
    62 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
    59 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
    63 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
    60 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
    64 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
    61 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener;
    95 	private ToolBar toolbar;
    92 	private ToolBar toolbar;
    96 
    93 
    97 	/**
    94 	/**
    98 	 * Contains all the listeners.  In most cases we just recreate the contribution status item.
    95 	 * Contains all the listeners.  In most cases we just recreate the contribution status item.
    99 	 */
    96 	 */
   100 	class ListenerBlock implements IConnectionListener, IConnectionsManagerListener, IStatusChangedListener, IConnectionStatusChangedListener {
    97 	class ListenerBlock implements IConnectionListener, IConnectionsManagerListener, IConnectionStatusChangedListener {
   101 
    98 
   102 		/* (non-Javadoc)
    99 		/* (non-Javadoc)
   103 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionAdded(com.nokia.carbide.remoteconnections.interfaces.IConnection)
   100 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener#connectionAdded(com.nokia.carbide.remoteconnections.interfaces.IConnection)
   104 		 */
   101 		 */
   105 		public void connectionAdded(IConnection connection) {
   102 		public void connectionAdded(IConnection connection) {
   140 		/* (non-Javadoc)
   137 		/* (non-Javadoc)
   141 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#displayChanged()
   138 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener#displayChanged()
   142 		 */
   139 		 */
   143 		public void displayChanged() {
   140 		public void displayChanged() {
   144 			updateUI();
   141 			updateUI();
   145 		}
       
   146 
       
   147 		/* (non-Javadoc)
       
   148 		 * @see com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus)
       
   149 		 */
       
   150 		public void statusChanged(IStatus status) {
       
   151 			updateConnectionStatus(null);
       
   152 		}
   142 		}
   153 
   143 
   154 		/* (non-Javadoc)
   144 		/* (non-Javadoc)
   155 		 * @see com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus)
   145 		 * @see com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatusChangedListener#statusChanged(com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus)
   156 		 */
   146 		 */
   318 			
   308 			
   319 			for (IConnection connection : dynamicConnections) {
   309 			for (IConnection connection : dynamicConnections) {
   320 				createConnectionMenuItem(menu, connection, defaultConnection, number++);
   310 				createConnectionMenuItem(menu, connection, defaultConnection, number++);
   321 			}
   311 			}
   322 			
   312 			
   323 			new MenuItem(menu, SWT.SEPARATOR);
   313 			if (!staticConnections.isEmpty())
       
   314 				new MenuItem(menu, SWT.SEPARATOR);
   324 			
   315 			
   325 			for (IConnection connection : staticConnections) {
   316 			for (IConnection connection : staticConnections) {
   326 				createConnectionMenuItem(menu, connection, defaultConnection, number++);
   317 				createConnectionMenuItem(menu, connection, defaultConnection, number++);
   327 			}
   318 			}
   328 		}
   319 		}
   373 		
   364 		
   374 		if (defaultConnection != null) {
   365 		if (defaultConnection != null) {
   375 			if (defaultConnection instanceof IConnection2) {
   366 			if (defaultConnection instanceof IConnection2) {
   376 				((IConnection2) defaultConnection).addStatusChangedListener(listenerBlock);
   367 				((IConnection2) defaultConnection).addStatusChangedListener(listenerBlock);
   377 			}
   368 			}
   378 			for (IConnectedService service : manager.getConnectedServices(defaultConnection)) {
       
   379 				service.addStatusChangedListener(listenerBlock);
       
   380 			}
       
   381 		}
   369 		}
   382 	}
   370 	}
   383 
   371 
   384 	private void removeListeners() {
   372 	private void removeListeners() {
   385 		if (defaultConnection != null) {
   373 		if (defaultConnection != null) {
   386 			if (defaultConnection instanceof IConnection2) {
   374 			if (defaultConnection instanceof IConnection2) {
   387 				((IConnection2) defaultConnection).removeStatusChangedListener(listenerBlock);
   375 				((IConnection2) defaultConnection).removeStatusChangedListener(listenerBlock);
   388 			}
       
   389 			for (IConnectedService service : manager.getConnectedServices(defaultConnection)) {
       
   390 				service.removeStatusChangedListener(listenerBlock);
       
   391 			}
   376 			}
   392 		}
   377 		}
   393 		manager.removeConnectionListener(listenerBlock);
   378 		manager.removeConnectionListener(listenerBlock);
   394 		Registry.instance().removeConnectionStoreChangedListener(listenerBlock);
   379 		Registry.instance().removeConnectionStoreChangedListener(listenerBlock);
   395 	}
   380 	}
   433 			return Messages.getString("ConnectionStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$
   418 			return Messages.getString("ConnectionStatusSelectorContribution.NoDynamicOrManualConnectionsTooltip"); //$NON-NLS-1$
   434 		}
   419 		}
   435 		
   420 		
   436 		String statusString = null;
   421 		String statusString = null;
   437 		if (status != null) {
   422 		if (status != null) {
   438 			statusString = status.getDescription();
   423 			statusString = createStatusString(status);
   439 		}
   424 		}
   440 		
   425 		
   441 		if (TextUtils.isEmpty(statusString)) {
   426 		if (TextUtils.isEmpty(statusString)) {
   442 			// fallback string; the status should not be empty
   427 			// fallback string; the status should not be empty
   443 			if (ConnectionUIUtils.isSomeServiceInUse(defaultConnection))
   428 			if (ConnectionUIUtils.isSomeServiceInUse(defaultConnection))
   445 			else
   430 			else
   446 				statusString = Messages.getString("ConnectionStatusSelectorContribution.NotInUse"); //$NON-NLS-1$
   431 				statusString = Messages.getString("ConnectionStatusSelectorContribution.NotInUse"); //$NON-NLS-1$
   447 		}
   432 		}
   448 		
   433 		
   449 		return MessageFormat.format(Messages.getString("ConnectionStatusSelectorContribution.ConnectionStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$
   434 		return MessageFormat.format(Messages.getString("ConnectionStatusSelectorContribution.ConnectionStatusFormat"), defaultConnection.getDisplayName(), statusString); //$NON-NLS-1$
       
   435 	}
       
   436 
       
   437 	private String createStatusString(IConnectionStatus status) {
       
   438 		String shortDescription = status.getShortDescription();
       
   439 		if (shortDescription == null || shortDescription.length() == 0)
       
   440 			return ""; //$NON-NLS-1$
       
   441 		String pattern = Messages.getString("ConnectionStatusSelectorContribution.StatusFormat"); //$NON-NLS-1$
       
   442 		if (shortDescription != null)
       
   443 			shortDescription = shortDescription.toLowerCase();
       
   444 		return MessageFormat.format(pattern, shortDescription, status.getLongDescription());
   450 	}
   445 	}
   451 
   446 
   452 	/**
   447 	/**
   453 	 * Get the image representing the connection status.
   448 	 * Get the image representing the connection status.
   454 	 * @param connection
   449 	 * @param connection