connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java
branchRCL_2_4
changeset 857 d66843399035
parent 381 9a15cc2691b5
child 1258 33a822a05021
equal deleted inserted replaced
855:3f37e327885c 857:d66843399035
    16 */
    16 */
    17 
    17 
    18 
    18 
    19 package com.nokia.carbide.remoteconnections.view;
    19 package com.nokia.carbide.remoteconnections.view;
    20 
    20 
       
    21 import java.text.MessageFormat;
       
    22 import java.util.ArrayList;
       
    23 import java.util.Collection;
       
    24 import java.util.HashMap;
       
    25 import java.util.List;
       
    26 import java.util.Map;
       
    27 
       
    28 import org.eclipse.jface.action.Action;
       
    29 import org.eclipse.jface.action.IAction;
       
    30 import org.eclipse.jface.action.IMenuListener;
       
    31 import org.eclipse.jface.action.IMenuManager;
       
    32 import org.eclipse.jface.action.IToolBarManager;
       
    33 import org.eclipse.jface.action.LegacyActionTools;
       
    34 import org.eclipse.jface.action.MenuManager;
       
    35 import org.eclipse.jface.action.Separator;
       
    36 import org.eclipse.jface.resource.ImageDescriptor;
       
    37 import org.eclipse.jface.resource.JFaceResources;
       
    38 import org.eclipse.jface.viewers.CellEditor;
       
    39 import org.eclipse.jface.viewers.ColumnLabelProvider;
       
    40 import org.eclipse.jface.viewers.ColumnViewer;
       
    41 import org.eclipse.jface.viewers.ColumnViewerEditor;
       
    42 import org.eclipse.jface.viewers.ColumnViewerEditorActivationEvent;
       
    43 import org.eclipse.jface.viewers.ColumnViewerEditorActivationStrategy;
       
    44 import org.eclipse.jface.viewers.DoubleClickEvent;
       
    45 import org.eclipse.jface.viewers.EditingSupport;
       
    46 import org.eclipse.jface.viewers.IDoubleClickListener;
       
    47 import org.eclipse.jface.viewers.ISelection;
       
    48 import org.eclipse.jface.viewers.ISelectionChangedListener;
       
    49 import org.eclipse.jface.viewers.IStructuredSelection;
       
    50 import org.eclipse.jface.viewers.SelectionChangedEvent;
       
    51 import org.eclipse.jface.viewers.StructuredSelection;
       
    52 import org.eclipse.jface.viewers.TextCellEditor;
       
    53 import org.eclipse.jface.viewers.TreeColumnViewerLabelProvider;
       
    54 import org.eclipse.jface.viewers.TreeNode;
       
    55 import org.eclipse.jface.viewers.TreeNodeContentProvider;
       
    56 import org.eclipse.jface.viewers.TreeViewer;
       
    57 import org.eclipse.jface.viewers.TreeViewerColumn;
       
    58 import org.eclipse.jface.viewers.TreeViewerEditor;
       
    59 import org.eclipse.jface.viewers.Viewer;
       
    60 import org.eclipse.jface.viewers.ViewerSorter;
       
    61 import org.eclipse.swt.SWT;
       
    62 import org.eclipse.swt.events.KeyAdapter;
       
    63 import org.eclipse.swt.events.KeyEvent;
       
    64 import org.eclipse.swt.graphics.Color;
       
    65 import org.eclipse.swt.graphics.Font;
       
    66 import org.eclipse.swt.graphics.Image;
       
    67 import org.eclipse.swt.widgets.Composite;
       
    68 import org.eclipse.swt.widgets.Display;
       
    69 import org.eclipse.swt.widgets.Menu;
       
    70 import org.eclipse.swt.widgets.TreeColumn;
       
    71 import org.eclipse.swt.widgets.TreeItem;
       
    72 import org.eclipse.ui.IActionBars;
       
    73 import org.eclipse.ui.ISharedImages;
       
    74 import org.eclipse.ui.PlatformUI;
       
    75 import org.eclipse.ui.part.ViewPart;
       
    76 
    21 import com.nokia.carbide.remoteconnections.Messages;
    77 import com.nokia.carbide.remoteconnections.Messages;
    22 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    78 import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
    23 import com.nokia.carbide.remoteconnections.interfaces.*;
    79 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService;
       
    80 import com.nokia.carbide.remoteconnections.interfaces.IConnection;
       
    81 import com.nokia.carbide.remoteconnections.interfaces.IConnectionType;
       
    82 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager;
    24 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
    83 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
    25 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
    84 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
    26 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
    85 import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
       
    86 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionListener;
    27 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
    87 import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
       
    88 import com.nokia.carbide.remoteconnections.internal.api.IConnection2;
       
    89 import com.nokia.carbide.remoteconnections.internal.api.IConnection2.IConnectionStatus;
       
    90 import com.nokia.carbide.remoteconnections.internal.registry.Registry;
       
    91 import com.nokia.carbide.remoteconnections.internal.ui.ConnectionUIUtils;
    28 import com.nokia.carbide.remoteconnections.settings.ui.SettingsWizard;
    92 import com.nokia.carbide.remoteconnections.settings.ui.SettingsWizard;
       
    93 import com.nokia.cpp.internal.api.utils.core.ObjectUtils;
    29 import com.nokia.cpp.internal.api.utils.core.TextUtils;
    94 import com.nokia.cpp.internal.api.utils.core.TextUtils;
    30 
       
    31 import org.eclipse.jface.action.*;
       
    32 import org.eclipse.jface.resource.ImageDescriptor;
       
    33 import org.eclipse.jface.resource.JFaceResources;
       
    34 import org.eclipse.jface.viewers.*;
       
    35 import org.eclipse.swt.SWT;
       
    36 import org.eclipse.swt.events.*;
       
    37 import org.eclipse.swt.graphics.Color;
       
    38 import org.eclipse.swt.graphics.Image;
       
    39 import org.eclipse.swt.widgets.*;
       
    40 import org.eclipse.ui.*;
       
    41 import org.eclipse.ui.part.ViewPart;
       
    42 
       
    43 import java.util.*;
       
    44 import java.util.List;
       
    45 
    95 
    46 
    96 
    47 /**
    97 /**
    48  * The view part for Remote connections
    98  * The view part for Remote connections
    49  */
    99  */
       
   100 @SuppressWarnings("deprecation")
    50 public class ConnectionsView extends ViewPart {
   101 public class ConnectionsView extends ViewPart {
       
   102 	public static final String VIEW_ID = "com.nokia.carbide.remoteconnections.view.ConnectionsView"; //$NON-NLS-1$
    51 
   103 
    52 	private TreeViewer viewer;
   104 	private TreeViewer viewer;
    53 	private IConnectionsManagerListener connectionStoreChangedListener;
   105 	private IConnectionsManagerListener connectionStoreChangedListener;
       
   106 	private IConnectionListener connectionListener;
    54 	private Map<IConnectedService, IStatusChangedListener> serviceToListenerMap;
   107 	private Map<IConnectedService, IStatusChangedListener> serviceToListenerMap;
    55 	private List<Action> actions;
   108 	private List<Action> actions;
    56 	private List<Action> connectionSelectedActions;
   109 	private List<Action> connectionSelectedActions;
    57 	private List<Action> serviceSelectedActions;
   110 	private List<Action> serviceSelectedActions;
    58 	private static final String UID = ".uid"; //$NON-NLS-1$
   111 	private static final String UID = ".uid"; //$NON-NLS-1$
    59 
   112 
    60 	private static final ImageDescriptor STATUS_AVAIL_IMGDESC = 
   113 	private static final ImageDescriptor CONNECTION_NEW_IMGDESC = RemoteConnectionsActivator.getImageDescriptor("icons/connectionNew.png"); //$NON-NLS-1$
    61 		RemoteConnectionsActivator.getImageDescriptor("icons/statusAvailable.png"); //$NON-NLS-1$
   114 	private static final ImageDescriptor CONNECTION_EDIT_IMGDESC = RemoteConnectionsActivator.getImageDescriptor("icons/connectionEdit.png"); //$NON-NLS-1$
    62 	private static final ImageDescriptor STATUS_UNAVAIL_IMGDESC = 
   115 	private static final ImageDescriptor SERVICE_TEST_IMGDESC = RemoteConnectionsActivator.getImageDescriptor("icons/serviceTest.png"); //$NON-NLS-1$
    63 		RemoteConnectionsActivator.getImageDescriptor("icons/statusUnavailable.png"); //$NON-NLS-1$
   116 	private static final ImageDescriptor CONNECTION_REFRESH_IMGDESC = RemoteConnectionsActivator.getImageDescriptor("icons/connectionRefresh.png"); //$NON-NLS-1$
    64 	private static final ImageDescriptor STATUS_UNK_IMGDESC = 
   117 
    65 		RemoteConnectionsActivator.getImageDescriptor("icons/statusUnknown.png"); //$NON-NLS-1$
       
    66 	private static final ImageDescriptor CONNECTION_IMGDESC = 
       
    67 		RemoteConnectionsActivator.getImageDescriptor("icons/connection.png"); //$NON-NLS-1$
       
    68 	private static final ImageDescriptor CONNECTION_NEW_IMGDESC = 
       
    69 		RemoteConnectionsActivator.getImageDescriptor("icons/connectionNew.png"); //$NON-NLS-1$
       
    70 	private static final ImageDescriptor CONNECTION_EDIT_IMGDESC = 
       
    71 		RemoteConnectionsActivator.getImageDescriptor("icons/connectionEdit.png"); //$NON-NLS-1$
       
    72 	private static final ImageDescriptor SERVICE_TEST_IMGDESC = 
       
    73 		RemoteConnectionsActivator.getImageDescriptor("icons/serviceTest.png"); //$NON-NLS-1$
       
    74 	private static final ImageDescriptor STATUS_INUSE_IMGDESC = 
       
    75 		RemoteConnectionsActivator.getImageDescriptor("icons/statusInUse.png"); //$NON-NLS-1$
       
    76 	private static final ImageDescriptor CONNECTION_REFRESH_IMGDESC = 
       
    77 		RemoteConnectionsActivator.getImageDescriptor("icons/connectionRefresh.png"); //$NON-NLS-1$
       
    78 	private static final Image STATUS_AVAIL_IMG = STATUS_AVAIL_IMGDESC.createImage();
       
    79 	private static final Image STATUS_UNAVAIL_IMG = STATUS_UNAVAIL_IMGDESC.createImage();
       
    80 	private static final Image STATUS_INUSE_IMG = STATUS_INUSE_IMGDESC.createImage();
       
    81 	private static final Image STATUS_UNK_IMG = STATUS_UNK_IMGDESC.createImage();
       
    82 	private static final Image CONNECTION_IMG = CONNECTION_IMGDESC.createImage();
       
    83 	private static final Color COLOR_RED = new Color(Display.getDefault(), 192, 0, 0);
       
    84 	private static final Color COLOR_GREEN = new Color(Display.getDefault(), 0, 128, 0);
       
    85 	private static final Color COLOR_ELECTRIC = new Color(Display.getDefault(), 0, 0, 255);
       
    86 	private static final Color COLOR_GREY = new Color(Display.getDefault(), 96, 96, 96);
       
    87 	private static final String NEW_ACTION = "ConnectionsView.new"; //$NON-NLS-1$
   118 	private static final String NEW_ACTION = "ConnectionsView.new"; //$NON-NLS-1$
    88 	private static final String EDIT_ACTION = "ConnectionsView.edit"; //$NON-NLS-1$
   119 	private static final String EDIT_ACTION = "ConnectionsView.edit"; //$NON-NLS-1$
    89 	private static final String RENAME_ACTION = "ConnectionsView.rename"; //$NON-NLS-1$
   120 	private static final String RENAME_ACTION = "ConnectionsView.rename"; //$NON-NLS-1$
    90 	private static final String ENABLE_SERVICE_ACTION = "ConnectionsView.enableService"; //$NON-NLS-1$
   121 	private static final String ENABLE_SERVICE_ACTION = "ConnectionsView.enableService"; //$NON-NLS-1$
    91 	private static final String REFRESH_ACTION = "ConnectionsView.refresh"; //$NON-NLS-1$
   122 	private static final String REFRESH_ACTION = "ConnectionsView.refresh"; //$NON-NLS-1$
    92 	private static final String DELETE_ACTION = "ConnectionsView.delete"; //$NON-NLS-1$
   123 	private static final String DELETE_ACTION = "ConnectionsView.delete"; //$NON-NLS-1$
    93 	private static final String HELP_ACTION = "ConnectionsView.help"; //$NON-NLS-1$
   124 	private static final String HELP_ACTION = "ConnectionsView.help"; //$NON-NLS-1$
       
   125 	private static final String SET_CURRENT_ACTION = "ConnectionsView.makeCurrent"; //$NON-NLS-1$
    94 	private KeyAdapter keyListener;
   126 	private KeyAdapter keyListener;
    95 	private boolean isDisposed;
   127 	private boolean isDisposed;
       
   128 
       
   129 	// handle, do not dispose
       
   130 	private Font boldViewerFont;
    96 	
   131 	
    97 	private TreeNode[] loadConnections() {
   132 	private TreeNode[] loadConnections() {
    98 		if (serviceToListenerMap == null)
   133 		if (serviceToListenerMap == null)
    99 			serviceToListenerMap = new HashMap<IConnectedService, IStatusChangedListener>();
   134 			serviceToListenerMap = new HashMap<IConnectedService, IStatusChangedListener>();
   100 		removeServiceListeners();
   135 		removeServiceListeners();
   101 		List<TreeNode> connectionNodes = new ArrayList<TreeNode>();
   136 		List<TreeNode> connectionNodes = new ArrayList<TreeNode>();
   102 		Collection<IConnection> connections = 
   137 		Collection<IConnection> connections = 
   103 			RemoteConnectionsActivator.getConnectionsManager().getConnections();
   138 			Registry.instance().getConnections();
   104 		for (IConnection connection : connections) {
   139 		for (IConnection connection : connections) {
   105 			// create a node for the connection
   140 			// create a node for the connection
   106 			TreeNode connectionNode = new TreeNode(connection);
   141 			TreeNode connectionNode = new TreeNode(connection);
   107 			// create subnodes for the connected services
   142 			// create subnodes for the connected services
   108 			List<TreeNode> serviceNodes = new ArrayList<TreeNode>();
   143 			List<TreeNode> serviceNodes = new ArrayList<TreeNode>();
   109 			Collection<IConnectedService> connectedServicesForConnection = 
   144 			Collection<IConnectedService> connectedServicesForConnection = 
   110 				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
   145 				Registry.instance().getConnectedServices(connection);
   111 			for (IConnectedService connectedService : connectedServicesForConnection) {
   146 			for (IConnectedService connectedService : connectedServicesForConnection) {
   112 				final TreeNode treeNode = new TreeNode(connectedService);
   147 				final TreeNode treeNode = new TreeNode(connectedService);
   113 				IStatusChangedListener statusChangedListener = new IStatusChangedListener() {
   148 				IStatusChangedListener statusChangedListener = new IStatusChangedListener() {
   114 					public void statusChanged(final IStatus status) {
   149 					public void statusChanged(final IStatus status) {
   115 						Display.getDefault().asyncExec(new Runnable() {
   150 						Display.getDefault().asyncExec(new Runnable() {
   142 
   177 
   143 		private NameEditingSupport(ColumnViewer viewer) {
   178 		private NameEditingSupport(ColumnViewer viewer) {
   144 			super(viewer);
   179 			super(viewer);
   145 			editor = new TextCellEditor((Composite) viewer.getControl(), SWT.BORDER);
   180 			editor = new TextCellEditor((Composite) viewer.getControl(), SWT.BORDER);
   146 		}
   181 		}
   147 
   182 		
   148 		@Override
   183 		@Override
   149 		protected boolean canEdit(Object element) {
   184 		protected boolean canEdit(Object element) {
   150 			if (((TreeNode) element).getValue() instanceof IConnection)
   185 			Object object = ((TreeNode) element).getValue();
       
   186 			if (object instanceof IConnection && !isDynamicConnection(object))
   151 				return true;
   187 				return true;
   152 			return false;
   188 			return false;
   153 		}
   189 		}
   154 
   190 
   155 		@Override
   191 		@Override
   165 
   201 
   166 		@Override
   202 		@Override
   167 		protected void setValue(Object element, Object value) {
   203 		protected void setValue(Object element, Object value) {
   168 			IConnection connection = (IConnection) ((TreeNode) element).getValue();
   204 			IConnection connection = (IConnection) ((TreeNode) element).getValue();
   169 			connection.setDisplayName(value.toString());
   205 			connection.setDisplayName(value.toString());
   170 			viewer.refresh(true);
   206 			//viewer.refresh(true);
   171 			packColumns();
   207 			//packColumns();
   172 			RemoteConnectionsActivator.getConnectionsManager().storeConnections();
   208 			Registry.instance().updateDisplays();
       
   209 			Registry.instance().storeConnections();
   173 		}
   210 		}
   174 	}
   211 	}
   175 
   212 
   176 	private class NameLabelProvider extends ColumnLabelProvider {
   213 	private class NameLabelProvider extends ColumnLabelProvider {
   177 
   214 
   178 		public String getText(Object obj) {
   215 		public String getText(Object obj) {
   179 			return getNodeDisplayName(obj);
   216 			return getNodeDisplayName(obj);
       
   217 		}
       
   218 		
       
   219 		/* (non-Javadoc)
       
   220 		 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getFont(java.lang.Object)
       
   221 		 */
       
   222 		@Override
       
   223 		public Font getFont(Object element) {
       
   224 			if (element instanceof TreeNode) {
       
   225 				if (((TreeNode)element).getValue().equals(Registry.instance().getCurrentConnection())) {
       
   226 					return boldViewerFont;
       
   227 				}
       
   228 			}
       
   229 			return super.getFont(element);
   180 		}
   230 		}
   181 
   231 
   182 		public Image getImage(Object obj) {
   232 		public Image getImage(Object obj) {
   183 			TreeNode node = (TreeNode) obj;
   233 			TreeNode node = (TreeNode) obj;
   184 			Object value = node.getValue();
   234 			Object value = node.getValue();
   185 			if (value instanceof IConnection) {
   235 			if (value instanceof IConnection) {
   186 				if (isConnectionInUse((IConnection) value)) {
   236 				return ConnectionUIUtils.getConnectionImage((IConnection) value);
   187 					return STATUS_INUSE_IMG;
       
   188 				}
       
   189 				return CONNECTION_IMG;
       
   190 			}
   237 			}
   191 			else if (value instanceof IConnectedService) {
   238 			else if (value instanceof IConnectedService) {
   192 				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
   239 				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
   193 				IConnection connection = findConnection((IConnectedService) value);
   240 				IConnection connection = findConnection((IConnectedService) value);
   194 				if (connection != null && isConnectionInUse(connection))
   241 				if (connection != null && ConnectionUIUtils.isSomeServiceInUse(connection))
   195 					status = EStatus.IN_USE;
   242 					status = EStatus.IN_USE;
   196 				switch (status) {
   243 				return ConnectionUIUtils.getConnectedServiceStatusImage(status);
   197 				case DOWN:
       
   198 					return STATUS_UNAVAIL_IMG;
       
   199 				case UP:
       
   200 					return STATUS_AVAIL_IMG;
       
   201 				case IN_USE:
       
   202 					return CONNECTION_IMG;
       
   203 				case UNKNOWN:
       
   204 					return STATUS_UNK_IMG;
       
   205 				}
       
   206 			}
   244 			}
   207 			return null;
   245 			return null;
   208 		}
   246 		}
   209 	}
   247 	}
   210 	
   248 	
   214 			Object value = node.getValue();
   252 			Object value = node.getValue();
   215 			if (value instanceof IConnectedService) {
   253 			if (value instanceof IConnectedService) {
   216 				IStatus status = null;
   254 				IStatus status = null;
   217 				IConnection connection = findConnection((IConnectedService) value);
   255 				IConnection connection = findConnection((IConnectedService) value);
   218 				if (connection != null)
   256 				if (connection != null)
   219 					status = getFirstInUseStatus(connection);
   257 					status = ConnectionUIUtils.getFirstInUseServiceStatus(connection);
   220 				if (status == null) {
   258 				if (status == null) {
   221 					status = ((IConnectedService) value).getStatus();
   259 					status = ((IConnectedService) value).getStatus();
   222 					return status.getShortDescription();
   260 					return status.getShortDescription();
   223 				}
   261 				}
   224 			}
   262 			}
   225 			else if (value instanceof IConnection) {
   263 			else if (value instanceof IConnection) {
   226 				IStatus status = getFirstInUseStatus((IConnection) value);
   264 				if (isDynamicConnection(value)) {
   227 				if (status != null)
   265 					IConnectionStatus status = ((IConnection2) value).getStatus();
   228 					return status.getShortDescription();
   266 					if (status != null)
       
   267 						return status.getShortDescription();
       
   268 				}
       
   269 				else {	
       
   270 					IStatus status = ConnectionUIUtils.getFirstInUseServiceStatus((IConnection) value);
       
   271 					if (status != null)
       
   272 						return status.getShortDescription();
       
   273 				}
   229 			}
   274 			}
   230 				
   275 				
   231 			return null;
   276 			return null;
   232 		}
   277 		}
   233 
   278 
   237 			Object value = node.getValue();
   282 			Object value = node.getValue();
   238 			if (value instanceof IConnectedService) {
   283 			if (value instanceof IConnectedService) {
   239 				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
   284 				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
   240 				switch (status) {
   285 				switch (status) {
   241 				case DOWN:
   286 				case DOWN:
   242 					return COLOR_RED;
   287 					return ConnectionUIUtils.COLOR_RED;
   243 				case UP:
   288 				case UP:
   244 					return COLOR_GREEN;
   289 					return ConnectionUIUtils.COLOR_GREEN;
   245 				case UNKNOWN:
   290 				case UNKNOWN:
   246 					return COLOR_GREY;
   291 					return ConnectionUIUtils.COLOR_GREY;
   247 				}
   292 				}
   248 			}
   293 			}
   249 			else if (value instanceof IConnection) // only showing in-use for connections
   294 			else if (value instanceof IConnection) // only showing in-use for connections
   250 				return COLOR_ELECTRIC;
   295 				return ConnectionUIUtils.COLOR_ELECTRIC;
   251 			
   296 			
   252 			return null;
   297 			return null;
       
   298 		}
       
   299 		
       
   300 		/* (non-Javadoc)
       
   301 		 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getFont(java.lang.Object)
       
   302 		 */
       
   303 		@Override
       
   304 		public Font getFont(Object element) {
       
   305 			// we need this to avoid letting the bold name column influence the others 
       
   306 			return JFaceResources.getDefaultFont();
   253 		}
   307 		}
   254 	}
   308 	}
   255 	
   309 	
   256 	public class DescriptionLabelProvider extends ColumnLabelProvider {
   310 	public class DescriptionLabelProvider extends ColumnLabelProvider {
   257 		
   311 		
   261 			Object value = node.getValue();
   315 			Object value = node.getValue();
   262 			if (value instanceof IConnectedService) {
   316 			if (value instanceof IConnectedService) {
   263 				IStatus status = ((IConnectedService) value).getStatus();
   317 				IStatus status = ((IConnectedService) value).getStatus();
   264 				IConnection connection = findConnection((IConnectedService) value);
   318 				IConnection connection = findConnection((IConnectedService) value);
   265 				if (!status.getEStatus().equals(EStatus.IN_USE) ||
   319 				if (!status.getEStatus().equals(EStatus.IN_USE) ||
   266 						!(connection != null && isConnectionInUse(connection))) { // if in-use, we show it in the connection row
   320 						!(connection != null && ConnectionUIUtils.isSomeServiceInUse(connection))) { // if in-use, we show it in the connection row
   267 					String longDescription = status.getLongDescription();
   321 					String longDescription = status.getLongDescription();
   268 					if (longDescription != null)
   322 					if (longDescription != null)
   269 						longDescription = TextUtils.canonicalizeNewlines(longDescription, " "); //$NON-NLS-1$
   323 						longDescription = TextUtils.canonicalizeNewlines(longDescription, " "); //$NON-NLS-1$
   270 					return longDescription;
   324 					return longDescription;
   271 				}
   325 				}
   272 			}
   326 			}
   273 			else if (value instanceof IConnection) {
   327 			else if (value instanceof IConnection) {
   274 				if (isConnectionInUse((IConnection) value)) {
   328 				if (isDynamicConnection(value)) {
   275 					return Messages.getString("ConnectionsView.InUseDesc");
   329 					IConnectionStatus status = ((IConnection2) value).getStatus();
       
   330 					if (status != null)
       
   331 						return status.getLongDescription();
       
   332 				}
       
   333 				else if (ConnectionUIUtils.isSomeServiceInUse((IConnection) value)) {
       
   334 					return Messages.getString("ConnectionsView.InUseDesc"); //$NON-NLS-1$
   276 				}
   335 				}
   277 			}
   336 			}
   278 			
   337 			
   279 			return null;
   338 			return null;
       
   339 		}
       
   340 		
       
   341 		/* (non-Javadoc)
       
   342 		 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getFont(java.lang.Object)
       
   343 		 */
       
   344 		@Override
       
   345 		public Font getFont(Object element) {
       
   346 			// we need this to avoid letting the bold name column influence the others 
       
   347 			return JFaceResources.getDefaultFont();
   280 		}
   348 		}
   281 	}
   349 	}
   282 
   350 
   283 	private class TypeLabelProvider extends ColumnLabelProvider {
   351 	private class TypeLabelProvider extends ColumnLabelProvider {
   284 		
   352 		
   287 			Object value = node.getValue();
   355 			Object value = node.getValue();
   288 			if (value instanceof IConnection) {
   356 			if (value instanceof IConnection) {
   289 				return ((IConnection) value).getConnectionType().getDisplayName();
   357 				return ((IConnection) value).getConnectionType().getDisplayName();
   290 			}
   358 			}
   291 			return null;
   359 			return null;
       
   360 		}
       
   361 		
       
   362 		/* (non-Javadoc)
       
   363 		 * @see org.eclipse.jface.viewers.ColumnLabelProvider#getFont(java.lang.Object)
       
   364 		 */
       
   365 		@Override
       
   366 		public Font getFont(Object element) {
       
   367 			// we need this to avoid letting the bold name column influence the others 
       
   368 			return JFaceResources.getDefaultFont();
   292 		}
   369 		}
   293 	}
   370 	}
   294 	
   371 	
   295 	private class EnableConnectedServiceAction extends Action {
   372 	private class EnableConnectedServiceAction extends Action {
   296 		private EnableConnectedServiceAction() {
   373 		private EnableConnectedServiceAction() {
   356 			protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
   433 			protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
   357 				return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
   434 				return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
   358 			}
   435 			}
   359 		};
   436 		};
   360 		TreeViewerEditor.create(viewer, activationStrategy, ColumnViewerEditor.DEFAULT);
   437 		TreeViewerEditor.create(viewer, activationStrategy, ColumnViewerEditor.DEFAULT);
       
   438 		
       
   439 		boldViewerFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
   361 
   440 
   362 		TreeViewerColumn typeColumn = new TreeViewerColumn(viewer, SWT.LEFT);
   441 		TreeViewerColumn typeColumn = new TreeViewerColumn(viewer, SWT.LEFT);
   363 		typeColumn.setLabelProvider(new TypeLabelProvider());
   442 		typeColumn.setLabelProvider(new TypeLabelProvider());
   364 		typeColumn.getColumn().setText(Messages.getString("ConnectionsView.TypeColumnHeader")); //$NON-NLS-1$
   443 		typeColumn.getColumn().setText(Messages.getString("ConnectionsView.TypeColumnHeader")); //$NON-NLS-1$
   365 		
   444 		
   453 						packColumns();
   532 						packColumns();
   454 					}
   533 					}
   455 				});
   534 				});
   456 			}
   535 			}
   457 		};
   536 		};
   458 		RemoteConnectionsActivator.getConnectionsManager().addConnectionStoreChangedListener(connectionStoreChangedListener);
   537 		Registry.instance().addConnectionStoreChangedListener(connectionStoreChangedListener);
       
   538 		
       
   539 		connectionListener = new IConnectionListener() {
       
   540 			
       
   541 			public void currentConnectionSet(IConnection connection) {
       
   542 				Display.getDefault().asyncExec(new Runnable() {
       
   543 					public void run() {
       
   544 						viewer.refresh(true);
       
   545 						packColumns();
       
   546 					}
       
   547 				});				
       
   548 			}
       
   549 			
       
   550 			public void connectionRemoved(IConnection connection) {
       
   551 				// presumably the viewer itself handles this...
       
   552 			}
       
   553 			
       
   554 			public void connectionAdded(IConnection connection) {
       
   555 				// presumably the viewer itself handles this...
       
   556 			}
       
   557 		};
       
   558 		Registry.instance().addConnectionListener(connectionListener);
   459 
   559 
   460 		RemoteConnectionsActivator.setHelp(parent, ".connections_view"); //$NON-NLS-1$
   560 		RemoteConnectionsActivator.setHelp(parent, ".connections_view"); //$NON-NLS-1$
   461 	}
   561 	}
   462 
   562 
   463 	private void packColumns() {
   563 	private void packColumns() {
   505 		ISelection selection = viewer.getSelection();
   605 		ISelection selection = viewer.getSelection();
   506 		if (selection.isEmpty())
   606 		if (selection.isEmpty())
   507 			return;
   607 			return;
   508 		TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
   608 		TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
   509 		Object value = node.getValue();
   609 		Object value = node.getValue();
   510 		if (value instanceof IConnectedService)
   610 		if (value instanceof IConnectedService) {
       
   611 			manager.add(new Separator());
   511 			manager.add(getAction(ENABLE_SERVICE_ACTION));
   612 			manager.add(getAction(ENABLE_SERVICE_ACTION));
   512 		else {
   613 		}
       
   614 		else if (value instanceof IConnection) {
       
   615 			manager.add(new Separator());
   513 			manager.add(getAction(RENAME_ACTION));
   616 			manager.add(getAction(RENAME_ACTION));
   514 			manager.add(getAction(EDIT_ACTION));
   617 			manager.add(getAction(EDIT_ACTION));
   515 			manager.add(getAction(DELETE_ACTION));
   618 			manager.add(getAction(DELETE_ACTION));
   516 			manager.add(getAction(HELP_ACTION));
   619 			IAction helpAction = getAction(HELP_ACTION);
       
   620 			if (helpAction.isEnabled()) {
       
   621 				helpAction.setText(helpAction.getText());
       
   622 				manager.add(helpAction);
       
   623 			}
       
   624 			manager.add(getAction(SET_CURRENT_ACTION));
   517 		}
   625 		}
   518 	}
   626 	}
   519 	
   627 	
   520 	private void fillLocalToolBar(IToolBarManager manager) {
   628 	private void fillLocalToolBar(IToolBarManager manager) {
   521 		manager.add(getAction(REFRESH_ACTION));
   629 		manager.add(getAction(REFRESH_ACTION));
   536 			}
   644 			}
   537 			
   645 			
   538 		};
   646 		};
   539 		action.setId(NEW_ACTION);
   647 		action.setId(NEW_ACTION);
   540 		actions.add(action);
   648 		actions.add(action);
   541 		action.setEnabled(!RemoteConnectionsActivator.getConnectionTypeProvider().getConnectionTypes().isEmpty());
   649 		action.setEnabled(!Registry.instance().getConnectionTypes().isEmpty());
   542 		
   650 		
   543 		action = new Action(Messages.getString("ConnectionsView.EditActionLabel"), CONNECTION_EDIT_IMGDESC) { //$NON-NLS-1$
   651 		String editLabel = Messages.getString("ConnectionsView.EditActionLabel"); //$NON-NLS-1$
       
   652 		action = new Action(editLabel, CONNECTION_EDIT_IMGDESC) { //$NON-NLS-1$
   544 			@Override
   653 			@Override
   545 			public void run() {
   654 			public void run() {
   546 				ISelection selection = viewer.getSelection();
   655 				ISelection selection = viewer.getSelection();
   547 				if (selection.isEmpty())
   656 				if (selection.isEmpty())
   548 					return;
   657 					return;
   568 				Object value = node.getValue();
   677 				Object value = node.getValue();
   569 				if (value instanceof IConnection) {
   678 				if (value instanceof IConnection) {
   570 					viewer.editElement(node, 0);
   679 					viewer.editElement(node, 0);
   571 				}
   680 				}
   572 			}
   681 			}
       
   682 
       
   683 			@Override
       
   684 			public boolean isEnabled() {
       
   685 				return selectionCanBeEdited();
       
   686 			}
   573 		};
   687 		};
   574 		action.setId(RENAME_ACTION);
   688 		action.setId(RENAME_ACTION);
   575 		action.setAccelerator(SWT.F2);
   689 		action.setAccelerator(SWT.F2);
   576 		action.setText(Messages.getString("ConnectionsView.RenameMenuLabel") +  //$NON-NLS-1$
   690 		action.setText(Messages.getString("ConnectionsView.RenameMenuLabel") +  //$NON-NLS-1$
   577 				"\t" +  //$NON-NLS-1$
   691 				"\t" +  //$NON-NLS-1$
   585 		serviceSelectedActions.add(action);
   699 		serviceSelectedActions.add(action);
   586 		
   700 		
   587 		action = new Action(Messages.getString("ConnectionsView.RefreshActionLabel"), CONNECTION_REFRESH_IMGDESC) { //$NON-NLS-1$
   701 		action = new Action(Messages.getString("ConnectionsView.RefreshActionLabel"), CONNECTION_REFRESH_IMGDESC) { //$NON-NLS-1$
   588 			@Override
   702 			@Override
   589 			public void run() {
   703 			public void run() {
   590 				IConnectionsManager connectionsManager = RemoteConnectionsActivator.getConnectionsManager();
   704 				IConnectionsManager connectionsManager = Registry.instance();
   591 				for (IConnection connection : connectionsManager.getConnections()) {
   705 				for (IConnection connection : connectionsManager.getConnections()) {
   592 					Collection<IConnectedService> connectedServices = connectionsManager.getConnectedServices(connection);
   706 					Collection<IConnectedService> connectedServices = connectionsManager.getConnectedServices(connection);
   593 					for (IConnectedService connectedService : connectedServices) {
   707 					for (IConnectedService connectedService : connectedServices) {
   594 						connectedService.setEnabled(true);
   708 						connectedService.setEnabled(true);
   595 						connectedService.testStatus();
   709 						connectedService.testStatus();
   605 		action = new Action(Messages.getString("ConnectionsView.DeleteActionLabel"),  //$NON-NLS-1$
   719 		action = new Action(Messages.getString("ConnectionsView.DeleteActionLabel"),  //$NON-NLS-1$
   606 				PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)) {
   720 				PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)) {
   607 			@Override
   721 			@Override
   608 			public void run() {
   722 			public void run() {
   609 				ISelection selection = viewer.getSelection();
   723 				ISelection selection = viewer.getSelection();
   610 				if (selection.isEmpty())
   724 				if (selection.isEmpty() || !canBeEdited(selection))
   611 					return;
   725 					return;
   612 				TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
   726 				TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
   613 				Object value = node.getValue();
   727 				Object value = node.getValue();
   614 				if (value instanceof IConnection) {
   728 				if (value instanceof IConnection) {
   615 					RemoteConnectionsActivator.getConnectionsManager().removeConnection((IConnection) value);
   729 					Registry.instance().removeConnection((IConnection) value);
   616 					RemoteConnectionsActivator.getConnectionsManager().storeConnections();
   730 					Registry.instance().storeConnections();
   617 				}
   731 				}
       
   732 			}
       
   733 
       
   734 			@Override
       
   735 			public boolean isEnabled() {
       
   736 				return selectionCanBeEdited();
   618 			}
   737 			}
   619 		};
   738 		};
   620 		action.setAccelerator(SWT.DEL);
   739 		action.setAccelerator(SWT.DEL);
   621 		action.setId(DELETE_ACTION);
   740 		action.setId(DELETE_ACTION);
   622 		actions.add(action);
   741 		actions.add(action);
   623 		connectionSelectedActions.add(action);
   742 		connectionSelectedActions.add(action);
   624 		
   743 		
   625 		Image image = JFaceResources.getImage(org.eclipse.jface.dialogs.Dialog.DLG_IMG_HELP);
   744 		Image image = JFaceResources.getImage(org.eclipse.jface.dialogs.Dialog.DLG_IMG_HELP);
   626 		ImageDescriptor desc = ImageDescriptor.createFromImage(image);
   745 		ImageDescriptor desc = ImageDescriptor.createFromImage(image);
   627 		action = new Action(Messages.getString("ConnectionsView.HelpActionLabel"), desc) { //$NON-NLS-1$
   746 		action = new Action(Messages.getString("ConnectionsView.NoHelpActionLabel"), desc) { //$NON-NLS-1$
   628 			
   747 			
   629 			private String getHelpContextFromSelection() {
   748 			private String getHelpContextFromSelection() {
   630 				ISelection selection = viewer.getSelection();
   749 				IConnection connection = getSelectedConnection();
   631 				if (!selection.isEmpty()) {
   750 				if (connection != null) {
   632 					TreeNode treeNode = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
   751 					return connection.getConnectionType().getHelpContext();
   633 					Object value = treeNode.getValue();
       
   634 					if (value instanceof IConnection) {
       
   635 						return ((IConnection) value).getConnectionType().getHelpContext();
       
   636 					}
       
   637 				}
   752 				}
   638 				return null;
   753 				return null;
       
   754 			}
       
   755 			
       
   756 			@Override
       
   757 			public String getText() {
       
   758 				if (isEnabled()) {
       
   759 					IConnection connection = getSelectedConnection();
       
   760 					IConnectionType connectionType = connection.getConnectionType();
       
   761 					String displayName = connectionType.getDisplayName();
       
   762 					String pattern = Messages.getString("ConnectionsView.HelpActionLabel"); //$NON-NLS-1$
       
   763 					return MessageFormat.format(pattern, displayName);
       
   764 				}
       
   765 				return super.getText();
   639 			}
   766 			}
   640 			
   767 			
   641 			@Override
   768 			@Override
   642 			public boolean isEnabled() {
   769 			public boolean isEnabled() {
   643 				return getHelpContextFromSelection() != null;
   770 				return getHelpContextFromSelection() != null;
   649 			}
   776 			}
   650 		};
   777 		};
   651 		action.setId(HELP_ACTION);
   778 		action.setId(HELP_ACTION);
   652 		actions.add(action);
   779 		actions.add(action);
   653 		connectionSelectedActions.add(action);
   780 		connectionSelectedActions.add(action);
       
   781 		
       
   782 		desc = ConnectionUIUtils.CONNECTION_IMGDESC;
       
   783 		action = new Action(Messages.getString("ConnectionsView.SetCurrentActionLabel"), desc) { //$NON-NLS-1$
       
   784 			
       
   785 			@Override
       
   786 			public boolean isEnabled() {
       
   787 				return !ObjectUtils.equals(Registry.instance().getCurrentConnection(), getSelectedConnection());
       
   788 			}
       
   789 
       
   790 			@Override
       
   791 			public void run() {
       
   792 				Registry.instance().setCurrentConnection(getSelectedConnection());
       
   793 				setEnabled(false);
       
   794 			}
       
   795 		};
       
   796 		action.setId(SET_CURRENT_ACTION);
       
   797 		actions.add(action);
       
   798 		connectionSelectedActions.add(action);		
       
   799 		
   654 		enableConnectionSelectedActions(false);
   800 		enableConnectionSelectedActions(false);
   655 		enableServiceSelectedActions(false);
   801 		enableServiceSelectedActions(false);
   656 	}
   802 	}
   657 	
   803 	
   658 	private void enableConnectionSelectedActions(boolean enable) {
   804 	private void enableConnectionSelectedActions(boolean enable) {
   705 		serviceToListenerMap.clear();
   851 		serviceToListenerMap.clear();
   706 	}
   852 	}
   707 	
   853 	
   708 	private void disableAllConnectedServices() {
   854 	private void disableAllConnectedServices() {
   709 		Collection<IConnection> connections = 
   855 		Collection<IConnection> connections = 
   710 			RemoteConnectionsActivator.getConnectionsManager().getConnections();
   856 			Registry.instance().getConnections();
   711 		for (IConnection connection : connections) {
   857 		for (IConnection connection : connections) {
   712 			Collection<IConnectedService> connectedServicesForConnection = 
   858 			Collection<IConnectedService> connectedServicesForConnection = 
   713 				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
   859 				Registry.instance().getConnectedServices(connection);
   714 			for (IConnectedService connectedService : connectedServicesForConnection) {
   860 			for (IConnectedService connectedService : connectedServicesForConnection) {
   715 				connectedService.setEnabled(false);
   861 				connectedService.setEnabled(false);
   716 			}
   862 			}
   717 		}
   863 		}
   718 	}
   864 	}
   719 	
   865 	
   720 	@Override
   866 	@Override
   721 	public void dispose() {
   867 	public void dispose() {
   722 		removeServiceListeners();
   868 		removeServiceListeners();
   723 		RemoteConnectionsActivator.getConnectionsManager().removeConnectionStoreChangedListener(connectionStoreChangedListener);
   869 		Registry.instance().removeConnectionStoreChangedListener(connectionStoreChangedListener);
       
   870 		Registry.instance().removeConnectionListener(connectionListener);
   724 		disableAllConnectedServices();
   871 		disableAllConnectedServices();
   725 		isDisposed = true;
   872 		isDisposed = true;
   726 		super.dispose();
   873 		super.dispose();
   727 	}
   874 	}
   728 	
   875 	
   729 	private static IConnection findConnection(IConnectedService cs) {
   876 	private static IConnection findConnection(IConnectedService cs) {
   730 		for (IConnection connection : RemoteConnectionsActivator.getConnectionsManager().getConnections()) {
   877 		for (IConnection connection : Registry.instance().getConnections()) {
   731 			for (IConnectedService connectedService : RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection)) {
   878 			for (IConnectedService connectedService : Registry.instance().getConnectedServices(connection)) {
   732 				if (cs.equals(connectedService))
   879 				if (cs.equals(connectedService))
   733 					return connection;
   880 					return connection;
   734 			}
   881 			}
   735 		}
   882 		}
   736 		return null;
   883 		return null;
   737 	}
   884 	}
   738 
   885 
   739 	private static IStatus getFirstInUseStatus(IConnection connection) {
   886 	private static boolean isDynamicConnection(Object object) {
   740 		Collection<IConnectedService> connectedServices = 
   887 		return object instanceof IConnection2 && ((IConnection2) object).isDynamic();
   741 			RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
   888 	}
   742 		// if any service is in-use, then connection is in-use
   889 
   743 		for (IConnectedService connectedService : connectedServices) {
   890 	private boolean selectionCanBeEdited() {
   744 			IStatus status = connectedService.getStatus();
   891 		ISelection selection = viewer.getSelection();
   745 			if (status.getEStatus().equals(EStatus.IN_USE))
   892 		return canBeEdited(selection);
   746 				return status;
   893 	}
   747 		}
   894 
   748 		
   895 	private static boolean canBeEdited(ISelection selection) {
       
   896 		if (selection.isEmpty())
       
   897 			return false;
       
   898 		TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
       
   899 		return !isDynamicConnection(node.getValue());
       
   900 	}
       
   901 
       
   902 	private IConnection getSelectedConnection() {
       
   903 		ISelection selection = viewer.getSelection();
       
   904 		if (!selection.isEmpty()) {
       
   905 			TreeNode treeNode = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
       
   906 			Object value = treeNode.getValue();
       
   907 			if (value instanceof IConnection) {
       
   908 				return (IConnection) value;
       
   909 			}
       
   910 		}
   749 		return null;
   911 		return null;
   750 	}
   912 	}
   751 
   913 
   752 	private boolean isConnectionInUse(IConnection connection) {
   914 	public void setSelectedConnection(IConnection connection) {
   753 		return getFirstInUseStatus(connection) != null;
   915 		if (viewer != null && !viewer.getControl().isDisposed()) {
   754 	}
   916 			if (connection != null) {
   755 
   917 				TreeNode node = new TreeNode(connection);
       
   918 				viewer.setSelection(new StructuredSelection(node));
       
   919 			} else {
       
   920 				viewer.setSelection(null);
       
   921 			}
       
   922 		}
       
   923 	}
   756 }
   924 }
   757 
   925