connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/view/ConnectionsView.java
author dadubrow
Mon, 06 Apr 2009 14:33:24 -0500
branchRCL_2_0
changeset 55 8254273d11c0
parent 2 d760517a8095
child 163 b25acbfc406a
permissions -rw-r--r--
[Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
cawthron
parents:
diff changeset
     1
/*
cawthron
parents:
diff changeset
     2
* Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies).
cawthron
parents:
diff changeset
     3
* All rights reserved.
cawthron
parents:
diff changeset
     4
* This component and the accompanying materials are made available
cawthron
parents:
diff changeset
     5
* under the terms of the License "Eclipse Public License v1.0"
cawthron
parents:
diff changeset
     6
* which accompanies this distribution, and is available
cawthron
parents:
diff changeset
     7
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
cawthron
parents:
diff changeset
     8
*
cawthron
parents:
diff changeset
     9
* Initial Contributors:
cawthron
parents:
diff changeset
    10
* Nokia Corporation - initial contribution.
cawthron
parents:
diff changeset
    11
*
cawthron
parents:
diff changeset
    12
* Contributors:
cawthron
parents:
diff changeset
    13
*
cawthron
parents:
diff changeset
    14
* Description: 
cawthron
parents:
diff changeset
    15
*
cawthron
parents:
diff changeset
    16
*/
cawthron
parents:
diff changeset
    17
cawthron
parents:
diff changeset
    18
cawthron
parents:
diff changeset
    19
package com.nokia.carbide.remoteconnections.view;
cawthron
parents:
diff changeset
    20
cawthron
parents:
diff changeset
    21
import com.nokia.carbide.remoteconnections.Messages;
cawthron
parents:
diff changeset
    22
import com.nokia.carbide.remoteconnections.RemoteConnectionsActivator;
cawthron
parents:
diff changeset
    23
import com.nokia.carbide.remoteconnections.interfaces.*;
cawthron
parents:
diff changeset
    24
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus;
cawthron
parents:
diff changeset
    25
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatusChangedListener;
cawthron
parents:
diff changeset
    26
import com.nokia.carbide.remoteconnections.interfaces.IConnectedService.IStatus.EStatus;
cawthron
parents:
diff changeset
    27
import com.nokia.carbide.remoteconnections.interfaces.IConnectionsManager.IConnectionsManagerListener;
cawthron
parents:
diff changeset
    28
import com.nokia.carbide.remoteconnections.settings.ui.SettingsWizard;
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
    29
import com.nokia.cpp.internal.api.utils.core.TextUtils;
2
cawthron
parents:
diff changeset
    30
cawthron
parents:
diff changeset
    31
import org.eclipse.jface.action.*;
cawthron
parents:
diff changeset
    32
import org.eclipse.jface.resource.ImageDescriptor;
cawthron
parents:
diff changeset
    33
import org.eclipse.jface.resource.JFaceResources;
cawthron
parents:
diff changeset
    34
import org.eclipse.jface.viewers.*;
cawthron
parents:
diff changeset
    35
import org.eclipse.swt.SWT;
cawthron
parents:
diff changeset
    36
import org.eclipse.swt.events.*;
cawthron
parents:
diff changeset
    37
import org.eclipse.swt.graphics.Color;
cawthron
parents:
diff changeset
    38
import org.eclipse.swt.graphics.Image;
cawthron
parents:
diff changeset
    39
import org.eclipse.swt.widgets.*;
cawthron
parents:
diff changeset
    40
import org.eclipse.ui.*;
cawthron
parents:
diff changeset
    41
import org.eclipse.ui.part.ViewPart;
cawthron
parents:
diff changeset
    42
cawthron
parents:
diff changeset
    43
import java.util.*;
cawthron
parents:
diff changeset
    44
import java.util.List;
cawthron
parents:
diff changeset
    45
cawthron
parents:
diff changeset
    46
cawthron
parents:
diff changeset
    47
/**
cawthron
parents:
diff changeset
    48
 * The view part for Remote connections
cawthron
parents:
diff changeset
    49
 */
cawthron
parents:
diff changeset
    50
public class ConnectionsView extends ViewPart {
cawthron
parents:
diff changeset
    51
cawthron
parents:
diff changeset
    52
	private TreeViewer viewer;
cawthron
parents:
diff changeset
    53
	private IConnectionsManagerListener connectionStoreChangedListener;
cawthron
parents:
diff changeset
    54
	private Map<IConnectedService, IStatusChangedListener> serviceToListenerMap;
cawthron
parents:
diff changeset
    55
	private List<Action> actions;
cawthron
parents:
diff changeset
    56
	private List<Action> connectionSelectedActions;
cawthron
parents:
diff changeset
    57
	private List<Action> serviceSelectedActions;
cawthron
parents:
diff changeset
    58
	private static final String UID = ".uid"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    59
cawthron
parents:
diff changeset
    60
	private static final ImageDescriptor STATUS_AVAIL_IMGDESC = 
cawthron
parents:
diff changeset
    61
		RemoteConnectionsActivator.getImageDescriptor("icons/statusAvailable.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    62
	private static final ImageDescriptor STATUS_UNAVAIL_IMGDESC = 
cawthron
parents:
diff changeset
    63
		RemoteConnectionsActivator.getImageDescriptor("icons/statusUnavailable.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    64
	private static final ImageDescriptor STATUS_UNK_IMGDESC = 
cawthron
parents:
diff changeset
    65
		RemoteConnectionsActivator.getImageDescriptor("icons/statusUnknown.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    66
	private static final ImageDescriptor CONNECTION_IMGDESC = 
cawthron
parents:
diff changeset
    67
		RemoteConnectionsActivator.getImageDescriptor("icons/connection.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    68
	private static final ImageDescriptor CONNECTION_NEW_IMGDESC = 
cawthron
parents:
diff changeset
    69
		RemoteConnectionsActivator.getImageDescriptor("icons/connectionNew.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    70
	private static final ImageDescriptor CONNECTION_EDIT_IMGDESC = 
cawthron
parents:
diff changeset
    71
		RemoteConnectionsActivator.getImageDescriptor("icons/connectionEdit.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    72
	private static final ImageDescriptor SERVICE_TEST_IMGDESC = 
cawthron
parents:
diff changeset
    73
		RemoteConnectionsActivator.getImageDescriptor("icons/serviceTest.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    74
	private static final ImageDescriptor STATUS_INUSE_IMGDESC = 
cawthron
parents:
diff changeset
    75
		RemoteConnectionsActivator.getImageDescriptor("icons/statusInUse.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    76
	private static final ImageDescriptor CONNECTION_REFRESH_IMGDESC = 
cawthron
parents:
diff changeset
    77
		RemoteConnectionsActivator.getImageDescriptor("icons/connectionRefresh.png"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    78
	private static final Image STATUS_AVAIL_IMG = STATUS_AVAIL_IMGDESC.createImage();
cawthron
parents:
diff changeset
    79
	private static final Image STATUS_UNAVAIL_IMG = STATUS_UNAVAIL_IMGDESC.createImage();
cawthron
parents:
diff changeset
    80
	private static final Image STATUS_INUSE_IMG = STATUS_INUSE_IMGDESC.createImage();
cawthron
parents:
diff changeset
    81
	private static final Image STATUS_UNK_IMG = STATUS_UNK_IMGDESC.createImage();
cawthron
parents:
diff changeset
    82
	private static final Image CONNECTION_IMG = CONNECTION_IMGDESC.createImage();
cawthron
parents:
diff changeset
    83
	private static final Color COLOR_RED = new Color(Display.getDefault(), 192, 0, 0);
cawthron
parents:
diff changeset
    84
	private static final Color COLOR_GREEN = new Color(Display.getDefault(), 0, 128, 0);
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
    85
	private static final Color COLOR_ELECTRIC = new Color(Display.getDefault(), 0, 0, 255);
2
cawthron
parents:
diff changeset
    86
	private static final Color COLOR_GREY = new Color(Display.getDefault(), 96, 96, 96);
cawthron
parents:
diff changeset
    87
	private static final String NEW_ACTION = "ConnectionsView.new"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    88
	private static final String EDIT_ACTION = "ConnectionsView.edit"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    89
	private static final String RENAME_ACTION = "ConnectionsView.rename"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    90
	private static final String ENABLE_SERVICE_ACTION = "ConnectionsView.enableService"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    91
	private static final String REFRESH_ACTION = "ConnectionsView.refresh"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    92
	private static final String DELETE_ACTION = "ConnectionsView.delete"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    93
	private static final String HELP_ACTION = "ConnectionsView.help"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    94
	private KeyAdapter keyListener;
cawthron
parents:
diff changeset
    95
	private boolean isDisposed;
cawthron
parents:
diff changeset
    96
	
cawthron
parents:
diff changeset
    97
	private TreeNode[] loadConnections() {
cawthron
parents:
diff changeset
    98
		if (serviceToListenerMap == null)
cawthron
parents:
diff changeset
    99
			serviceToListenerMap = new HashMap<IConnectedService, IStatusChangedListener>();
cawthron
parents:
diff changeset
   100
		removeServiceListeners();
cawthron
parents:
diff changeset
   101
		List<TreeNode> connectionNodes = new ArrayList<TreeNode>();
cawthron
parents:
diff changeset
   102
		Collection<IConnection> connections = 
cawthron
parents:
diff changeset
   103
			RemoteConnectionsActivator.getConnectionsManager().getConnections();
cawthron
parents:
diff changeset
   104
		for (IConnection connection : connections) {
cawthron
parents:
diff changeset
   105
			// create a node for the connection
cawthron
parents:
diff changeset
   106
			TreeNode connectionNode = new TreeNode(connection);
cawthron
parents:
diff changeset
   107
			// create subnodes for the connected services
cawthron
parents:
diff changeset
   108
			List<TreeNode> serviceNodes = new ArrayList<TreeNode>();
cawthron
parents:
diff changeset
   109
			Collection<IConnectedService> connectedServicesForConnection = 
cawthron
parents:
diff changeset
   110
				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
cawthron
parents:
diff changeset
   111
			for (IConnectedService connectedService : connectedServicesForConnection) {
cawthron
parents:
diff changeset
   112
				final TreeNode treeNode = new TreeNode(connectedService);
cawthron
parents:
diff changeset
   113
				IStatusChangedListener statusChangedListener = new IStatusChangedListener() {
cawthron
parents:
diff changeset
   114
					public void statusChanged(final IStatus status) {
cawthron
parents:
diff changeset
   115
						Display.getDefault().asyncExec(new Runnable() {
cawthron
parents:
diff changeset
   116
							public void run() {
cawthron
parents:
diff changeset
   117
								if (!isDisposed) {
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   118
									viewer.refresh(true);
2
cawthron
parents:
diff changeset
   119
									packColumns();
cawthron
parents:
diff changeset
   120
								}
cawthron
parents:
diff changeset
   121
							}
cawthron
parents:
diff changeset
   122
						});
cawthron
parents:
diff changeset
   123
					}
cawthron
parents:
diff changeset
   124
				};
cawthron
parents:
diff changeset
   125
				connectedService.addStatusChangedListener(statusChangedListener);
cawthron
parents:
diff changeset
   126
				serviceToListenerMap.put(connectedService, statusChangedListener);
cawthron
parents:
diff changeset
   127
				connectedService.setEnabled(true);
cawthron
parents:
diff changeset
   128
				serviceNodes.add(treeNode);
cawthron
parents:
diff changeset
   129
			}
cawthron
parents:
diff changeset
   130
			for (TreeNode serviceNode : serviceNodes) {
cawthron
parents:
diff changeset
   131
				serviceNode.setParent(connectionNode);
cawthron
parents:
diff changeset
   132
			}
cawthron
parents:
diff changeset
   133
			connectionNode.setChildren((TreeNode[]) serviceNodes.toArray(new TreeNode[serviceNodes.size()]));
cawthron
parents:
diff changeset
   134
			connectionNodes.add(connectionNode);
cawthron
parents:
diff changeset
   135
		}
cawthron
parents:
diff changeset
   136
		
cawthron
parents:
diff changeset
   137
		return (TreeNode[]) connectionNodes.toArray(new TreeNode[connectionNodes.size()]);
cawthron
parents:
diff changeset
   138
	}
cawthron
parents:
diff changeset
   139
cawthron
parents:
diff changeset
   140
	private class NameEditingSupport extends EditingSupport {
cawthron
parents:
diff changeset
   141
		private TextCellEditor editor;
cawthron
parents:
diff changeset
   142
cawthron
parents:
diff changeset
   143
		private NameEditingSupport(ColumnViewer viewer) {
cawthron
parents:
diff changeset
   144
			super(viewer);
cawthron
parents:
diff changeset
   145
			editor = new TextCellEditor((Composite) viewer.getControl(), SWT.BORDER);
cawthron
parents:
diff changeset
   146
		}
cawthron
parents:
diff changeset
   147
cawthron
parents:
diff changeset
   148
		@Override
cawthron
parents:
diff changeset
   149
		protected boolean canEdit(Object element) {
cawthron
parents:
diff changeset
   150
			if (((TreeNode) element).getValue() instanceof IConnection)
cawthron
parents:
diff changeset
   151
				return true;
cawthron
parents:
diff changeset
   152
			return false;
cawthron
parents:
diff changeset
   153
		}
cawthron
parents:
diff changeset
   154
cawthron
parents:
diff changeset
   155
		@Override
cawthron
parents:
diff changeset
   156
		protected CellEditor getCellEditor(Object element) {
cawthron
parents:
diff changeset
   157
			return editor;
cawthron
parents:
diff changeset
   158
		}
cawthron
parents:
diff changeset
   159
cawthron
parents:
diff changeset
   160
		@Override
cawthron
parents:
diff changeset
   161
		protected Object getValue(Object element) {
cawthron
parents:
diff changeset
   162
			IConnection connection = (IConnection) ((TreeNode) element).getValue();
cawthron
parents:
diff changeset
   163
			return connection.getDisplayName();
cawthron
parents:
diff changeset
   164
		}
cawthron
parents:
diff changeset
   165
cawthron
parents:
diff changeset
   166
		@Override
cawthron
parents:
diff changeset
   167
		protected void setValue(Object element, Object value) {
cawthron
parents:
diff changeset
   168
			IConnection connection = (IConnection) ((TreeNode) element).getValue();
cawthron
parents:
diff changeset
   169
			connection.setDisplayName(value.toString());
cawthron
parents:
diff changeset
   170
			viewer.refresh(true);
cawthron
parents:
diff changeset
   171
			packColumns();
cawthron
parents:
diff changeset
   172
		}
cawthron
parents:
diff changeset
   173
	}
cawthron
parents:
diff changeset
   174
cawthron
parents:
diff changeset
   175
	private class NameLabelProvider extends ColumnLabelProvider {
cawthron
parents:
diff changeset
   176
cawthron
parents:
diff changeset
   177
		public String getText(Object obj) {
cawthron
parents:
diff changeset
   178
			return getNodeDisplayName(obj);
cawthron
parents:
diff changeset
   179
		}
cawthron
parents:
diff changeset
   180
cawthron
parents:
diff changeset
   181
		public Image getImage(Object obj) {
cawthron
parents:
diff changeset
   182
			TreeNode node = (TreeNode) obj;
cawthron
parents:
diff changeset
   183
			Object value = node.getValue();
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   184
			if (value instanceof IConnection) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   185
				if (isConnectionInUse((IConnection) value)) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   186
					return STATUS_INUSE_IMG;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   187
				}
2
cawthron
parents:
diff changeset
   188
				return CONNECTION_IMG;
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   189
			}
2
cawthron
parents:
diff changeset
   190
			else if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   191
				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
cawthron
parents:
diff changeset
   192
				switch (status) {
cawthron
parents:
diff changeset
   193
				case DOWN:
cawthron
parents:
diff changeset
   194
					return STATUS_UNAVAIL_IMG;
cawthron
parents:
diff changeset
   195
				case UP:
cawthron
parents:
diff changeset
   196
					return STATUS_AVAIL_IMG;
cawthron
parents:
diff changeset
   197
				case IN_USE:
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   198
					return CONNECTION_IMG;
2
cawthron
parents:
diff changeset
   199
				case UNKNOWN:
cawthron
parents:
diff changeset
   200
					return STATUS_UNK_IMG;
cawthron
parents:
diff changeset
   201
				}
cawthron
parents:
diff changeset
   202
			}
cawthron
parents:
diff changeset
   203
			return null;
cawthron
parents:
diff changeset
   204
		}
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   205
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   206
		private boolean isConnectionInUse(IConnection connection) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   207
			Collection<IConnectedService> connectedServices = 
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   208
				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   209
			// if any service is in-use, then connection is in-use
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   210
			for (IConnectedService connectedService : connectedServices) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   211
				IStatus status = connectedService.getStatus();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   212
				if (status.getEStatus().equals(EStatus.IN_USE))
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   213
					return true;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   214
			}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   215
			
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   216
			return false;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   217
		}
2
cawthron
parents:
diff changeset
   218
	}
cawthron
parents:
diff changeset
   219
	
cawthron
parents:
diff changeset
   220
	private class StatusLabelProvider extends ColumnLabelProvider {
cawthron
parents:
diff changeset
   221
cawthron
parents:
diff changeset
   222
		public String getText(Object obj) {
cawthron
parents:
diff changeset
   223
			TreeNode node = (TreeNode) obj;
cawthron
parents:
diff changeset
   224
			Object value = node.getValue();
cawthron
parents:
diff changeset
   225
			if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   226
				IStatus status = ((IConnectedService) value).getStatus();
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   227
				if (!status.getEStatus().equals(EStatus.IN_USE)) // if in-use, we show it in the connection row
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   228
					return status.getShortDescription();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   229
			}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   230
			else if (value instanceof IConnection) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   231
				IStatus status = getFirstInUseStatus((IConnection) value);
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   232
				if (status != null)
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   233
					return status.getShortDescription();
2
cawthron
parents:
diff changeset
   234
			}
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   235
				
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   236
			return null;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   237
		}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   238
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   239
		private IStatus getFirstInUseStatus(IConnection connection) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   240
			Collection<IConnectedService> connectedServices = 
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   241
				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   242
			// if any service is in-use, then connection is in-use
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   243
			for (IConnectedService connectedService : connectedServices) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   244
				IStatus status = connectedService.getStatus();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   245
				if (status.getEStatus().equals(EStatus.IN_USE))
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   246
					return status;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   247
			}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   248
			
2
cawthron
parents:
diff changeset
   249
			return null;
cawthron
parents:
diff changeset
   250
		}
cawthron
parents:
diff changeset
   251
cawthron
parents:
diff changeset
   252
		@Override
cawthron
parents:
diff changeset
   253
		public Color getForeground(Object obj) {
cawthron
parents:
diff changeset
   254
			TreeNode node = (TreeNode) obj;
cawthron
parents:
diff changeset
   255
			Object value = node.getValue();
cawthron
parents:
diff changeset
   256
			if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   257
				EStatus status = ((IConnectedService) value).getStatus().getEStatus();
cawthron
parents:
diff changeset
   258
				switch (status) {
cawthron
parents:
diff changeset
   259
				case DOWN:
cawthron
parents:
diff changeset
   260
					return COLOR_RED;
cawthron
parents:
diff changeset
   261
				case UP:
cawthron
parents:
diff changeset
   262
					return COLOR_GREEN;
cawthron
parents:
diff changeset
   263
				case UNKNOWN:
cawthron
parents:
diff changeset
   264
					return COLOR_GREY;
cawthron
parents:
diff changeset
   265
				}
cawthron
parents:
diff changeset
   266
			}
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   267
			else if (value instanceof IConnection) // only showing in-use for connections
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   268
				return COLOR_ELECTRIC;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   269
			
2
cawthron
parents:
diff changeset
   270
			return null;
cawthron
parents:
diff changeset
   271
		}
cawthron
parents:
diff changeset
   272
	}
cawthron
parents:
diff changeset
   273
	
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   274
	public class DescriptionLabelProvider extends ColumnLabelProvider {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   275
		
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   276
		@Override
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   277
		public String getText(Object obj) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   278
			TreeNode node = (TreeNode) obj;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   279
			Object value = node.getValue();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   280
			if (value instanceof IConnectedService) {
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   281
				IStatus status = ((IConnectedService) value).getStatus();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   282
				if (!status.getEStatus().equals(EStatus.IN_USE)) { // if in-use, we show it in the connection row
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   283
					String longDescription = status.getLongDescription();
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   284
					if (longDescription != null)
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   285
						longDescription = TextUtils.canonicalizeNewlines(longDescription, " ");
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   286
					return longDescription;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   287
				}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   288
			}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   289
			
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   290
			return null;
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   291
		}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   292
	}
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   293
2
cawthron
parents:
diff changeset
   294
	private class TypeLabelProvider extends ColumnLabelProvider {
cawthron
parents:
diff changeset
   295
		
cawthron
parents:
diff changeset
   296
		public String getText(Object obj) {
cawthron
parents:
diff changeset
   297
			TreeNode node = (TreeNode) obj;
cawthron
parents:
diff changeset
   298
			Object value = node.getValue();
cawthron
parents:
diff changeset
   299
			if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   300
				return ((IConnection) value).getConnectionType().getDisplayName();
cawthron
parents:
diff changeset
   301
			}
cawthron
parents:
diff changeset
   302
			return null;
cawthron
parents:
diff changeset
   303
		}
cawthron
parents:
diff changeset
   304
	}
cawthron
parents:
diff changeset
   305
	
cawthron
parents:
diff changeset
   306
	private class EnableConnectedServiceAction extends Action {
cawthron
parents:
diff changeset
   307
		private EnableConnectedServiceAction() {
cawthron
parents:
diff changeset
   308
			super("", SERVICE_TEST_IMGDESC); //$NON-NLS-1$
cawthron
parents:
diff changeset
   309
		}
cawthron
parents:
diff changeset
   310
cawthron
parents:
diff changeset
   311
		private IConnectedService getSelectedConnectedService() {
cawthron
parents:
diff changeset
   312
			ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   313
			if (selection.isEmpty())
cawthron
parents:
diff changeset
   314
				return null;
cawthron
parents:
diff changeset
   315
			TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   316
			Object value = node.getValue();
cawthron
parents:
diff changeset
   317
			if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   318
				return (IConnectedService) value;
cawthron
parents:
diff changeset
   319
			}
cawthron
parents:
diff changeset
   320
			return null;
cawthron
parents:
diff changeset
   321
		}
cawthron
parents:
diff changeset
   322
cawthron
parents:
diff changeset
   323
		@Override
cawthron
parents:
diff changeset
   324
		public void run() {
cawthron
parents:
diff changeset
   325
			IConnectedService connectedService = getSelectedConnectedService();
cawthron
parents:
diff changeset
   326
			if (connectedService != null) {
cawthron
parents:
diff changeset
   327
				connectedService.setEnabled(!connectedService.isEnabled());
cawthron
parents:
diff changeset
   328
				updateLabel();
cawthron
parents:
diff changeset
   329
			}
cawthron
parents:
diff changeset
   330
		}
cawthron
parents:
diff changeset
   331
cawthron
parents:
diff changeset
   332
		public void updateLabel() {
cawthron
parents:
diff changeset
   333
			IConnectedService connectedService = getSelectedConnectedService();
cawthron
parents:
diff changeset
   334
			if (connectedService == null)
cawthron
parents:
diff changeset
   335
				return;
cawthron
parents:
diff changeset
   336
			String label = connectedService.isEnabled() ?
cawthron
parents:
diff changeset
   337
					Messages.getString("ConnectionsView.DisableTestActionLabel") : //$NON-NLS-1$
cawthron
parents:
diff changeset
   338
					Messages.getString("ConnectionsView.EnableTestActionLabel"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   339
			setText(label);
cawthron
parents:
diff changeset
   340
		}
cawthron
parents:
diff changeset
   341
cawthron
parents:
diff changeset
   342
		@Override
cawthron
parents:
diff changeset
   343
		public void setEnabled(boolean enabled) {
cawthron
parents:
diff changeset
   344
			super.setEnabled(enabled);
cawthron
parents:
diff changeset
   345
			updateLabel();
cawthron
parents:
diff changeset
   346
		}
cawthron
parents:
diff changeset
   347
cawthron
parents:
diff changeset
   348
		@Override
cawthron
parents:
diff changeset
   349
		public boolean isEnabled() {
cawthron
parents:
diff changeset
   350
			IConnectedService connectedService = getSelectedConnectedService();
cawthron
parents:
diff changeset
   351
			return connectedService != null && connectedService.getService().isTestable();
cawthron
parents:
diff changeset
   352
		}
cawthron
parents:
diff changeset
   353
	}
cawthron
parents:
diff changeset
   354
cawthron
parents:
diff changeset
   355
	/* (non-Javadoc)
cawthron
parents:
diff changeset
   356
	 * @see org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets.Composite)
cawthron
parents:
diff changeset
   357
	 */
cawthron
parents:
diff changeset
   358
	public void createPartControl(Composite parent) {
cawthron
parents:
diff changeset
   359
		viewer = new TreeViewer(parent, SWT.H_SCROLL | SWT.V_SCROLL);
cawthron
parents:
diff changeset
   360
		
cawthron
parents:
diff changeset
   361
		TreeViewerColumn nameColumn = new TreeViewerColumn(viewer, SWT.LEFT);
cawthron
parents:
diff changeset
   362
		nameColumn.setLabelProvider(new TreeColumnViewerLabelProvider(new NameLabelProvider()));
cawthron
parents:
diff changeset
   363
		nameColumn.getColumn().setText(Messages.getString("ConnectionsView.NameColumnHeader")); //$NON-NLS-1$
cawthron
parents:
diff changeset
   364
		nameColumn.setEditingSupport(new NameEditingSupport(nameColumn.getViewer()));
cawthron
parents:
diff changeset
   365
		ColumnViewerEditorActivationStrategy activationStrategy = new ColumnViewerEditorActivationStrategy(nameColumn.getViewer()) {
cawthron
parents:
diff changeset
   366
			@Override
cawthron
parents:
diff changeset
   367
			protected boolean isEditorActivationEvent(ColumnViewerEditorActivationEvent event) {
cawthron
parents:
diff changeset
   368
				return event.eventType == ColumnViewerEditorActivationEvent.PROGRAMMATIC;
cawthron
parents:
diff changeset
   369
			}
cawthron
parents:
diff changeset
   370
		};
cawthron
parents:
diff changeset
   371
		TreeViewerEditor.create(viewer, activationStrategy, ColumnViewerEditor.DEFAULT);
cawthron
parents:
diff changeset
   372
cawthron
parents:
diff changeset
   373
		TreeViewerColumn typeColumn = new TreeViewerColumn(viewer, SWT.LEFT);
cawthron
parents:
diff changeset
   374
		typeColumn.setLabelProvider(new TypeLabelProvider());
cawthron
parents:
diff changeset
   375
		typeColumn.getColumn().setText(Messages.getString("ConnectionsView.TypeColumnHeader")); //$NON-NLS-1$
cawthron
parents:
diff changeset
   376
		
cawthron
parents:
diff changeset
   377
		TreeViewerColumn statusColumn = new TreeViewerColumn(viewer, SWT.LEFT);
cawthron
parents:
diff changeset
   378
		statusColumn.setLabelProvider(new StatusLabelProvider());
cawthron
parents:
diff changeset
   379
		statusColumn.getColumn().setText(Messages.getString("ConnectionsView.StatusColumnHeader")); //$NON-NLS-1$
cawthron
parents:
diff changeset
   380
		
55
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   381
		TreeViewerColumn descriptionColumn = new TreeViewerColumn(viewer, SWT.LEFT);
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   382
		descriptionColumn.setLabelProvider(new DescriptionLabelProvider());
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   383
		descriptionColumn.getColumn().setText("Description");
8254273d11c0 [Bug 8297] add description column to remote connections view with long description of status and move in-use UI to connection rather than displaying on each service - use blue color for in-use status text rather than yellow
dadubrow
parents: 2
diff changeset
   384
		
2
cawthron
parents:
diff changeset
   385
		viewer.setContentProvider(new TreeNodeContentProvider());
cawthron
parents:
diff changeset
   386
		viewer.setInput(loadConnections());
cawthron
parents:
diff changeset
   387
		viewer.expandAll();
cawthron
parents:
diff changeset
   388
		viewer.getTree().setHeaderVisible(true);
cawthron
parents:
diff changeset
   389
		viewer.getControl().setData(UID, "viewer"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   390
		viewer.setSorter(new ViewerSorter() {
cawthron
parents:
diff changeset
   391
			@Override
cawthron
parents:
diff changeset
   392
			public int compare(Viewer viewer, Object e1, Object e2) {
cawthron
parents:
diff changeset
   393
				return getNodeDisplayName(e1).compareToIgnoreCase(getNodeDisplayName(e2));
cawthron
parents:
diff changeset
   394
			}
cawthron
parents:
diff changeset
   395
		});
cawthron
parents:
diff changeset
   396
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
cawthron
parents:
diff changeset
   397
			public void selectionChanged(SelectionChangedEvent event) {
cawthron
parents:
diff changeset
   398
				enableConnectionSelectedActions(false);
cawthron
parents:
diff changeset
   399
				enableServiceSelectedActions(false);
cawthron
parents:
diff changeset
   400
				ISelection selection = event.getSelection();
cawthron
parents:
diff changeset
   401
				if (!selection.isEmpty()) {
cawthron
parents:
diff changeset
   402
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
cawthron
parents:
diff changeset
   403
					TreeNode treeNode = (TreeNode) structuredSelection.getFirstElement();
cawthron
parents:
diff changeset
   404
					Object value = treeNode.getValue();
cawthron
parents:
diff changeset
   405
					if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   406
						enableConnectionSelectedActions(true);
cawthron
parents:
diff changeset
   407
					}
cawthron
parents:
diff changeset
   408
					else if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   409
						enableServiceSelectedActions(true);
cawthron
parents:
diff changeset
   410
					}
cawthron
parents:
diff changeset
   411
				}
cawthron
parents:
diff changeset
   412
			}
cawthron
parents:
diff changeset
   413
		});
cawthron
parents:
diff changeset
   414
		viewer.addDoubleClickListener(new IDoubleClickListener() {
cawthron
parents:
diff changeset
   415
			public void doubleClick(DoubleClickEvent event) {
cawthron
parents:
diff changeset
   416
				ISelection selection = event.getSelection();
cawthron
parents:
diff changeset
   417
				if (!selection.isEmpty()) {
cawthron
parents:
diff changeset
   418
					IStructuredSelection structuredSelection = (IStructuredSelection) selection;
cawthron
parents:
diff changeset
   419
					TreeNode treeNode = (TreeNode) structuredSelection.getFirstElement();
cawthron
parents:
diff changeset
   420
					Object value = treeNode.getValue();
cawthron
parents:
diff changeset
   421
					if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   422
						SettingsWizard wizard = new SettingsWizard((IConnection) value);
cawthron
parents:
diff changeset
   423
						wizard.open(getViewSite().getShell());
cawthron
parents:
diff changeset
   424
					}
cawthron
parents:
diff changeset
   425
					else if (value instanceof IConnectedService) {
cawthron
parents:
diff changeset
   426
						IConnectedService connectedService = (IConnectedService) value;
cawthron
parents:
diff changeset
   427
						connectedService.setEnabled(true);
cawthron
parents:
diff changeset
   428
						connectedService.testStatus();
cawthron
parents:
diff changeset
   429
						((EnableConnectedServiceAction) getAction(ENABLE_SERVICE_ACTION)).updateLabel();
cawthron
parents:
diff changeset
   430
					}
cawthron
parents:
diff changeset
   431
				}
cawthron
parents:
diff changeset
   432
			}
cawthron
parents:
diff changeset
   433
		});
cawthron
parents:
diff changeset
   434
		
cawthron
parents:
diff changeset
   435
		packColumns();
cawthron
parents:
diff changeset
   436
		
cawthron
parents:
diff changeset
   437
		makeActions();
cawthron
parents:
diff changeset
   438
		hookContextMenu();
cawthron
parents:
diff changeset
   439
		contributeToActionBars();
cawthron
parents:
diff changeset
   440
		hookAccelerators();
cawthron
parents:
diff changeset
   441
		
cawthron
parents:
diff changeset
   442
		connectionStoreChangedListener = new IConnectionsManagerListener() {
cawthron
parents:
diff changeset
   443
			public void connectionStoreChanged() {
cawthron
parents:
diff changeset
   444
				Display.getDefault().asyncExec(new Runnable() {
cawthron
parents:
diff changeset
   445
					public void run() {
cawthron
parents:
diff changeset
   446
						viewer.setInput(loadConnections());
cawthron
parents:
diff changeset
   447
						viewer.expandAll();
cawthron
parents:
diff changeset
   448
						packColumns();
cawthron
parents:
diff changeset
   449
						if (viewer.getSelection().isEmpty() && viewer.getTree().getItemCount() > 0) {
cawthron
parents:
diff changeset
   450
							TreeItem item = viewer.getTree().getItem(0);
cawthron
parents:
diff changeset
   451
							if (item != null) {
cawthron
parents:
diff changeset
   452
								viewer.getTree().select(item);
cawthron
parents:
diff changeset
   453
							}
cawthron
parents:
diff changeset
   454
						}
cawthron
parents:
diff changeset
   455
						viewer.setSelection(viewer.getSelection()); // force selection changed
cawthron
parents:
diff changeset
   456
					}
cawthron
parents:
diff changeset
   457
				});
cawthron
parents:
diff changeset
   458
			}
cawthron
parents:
diff changeset
   459
cawthron
parents:
diff changeset
   460
			public void displayChanged() {
cawthron
parents:
diff changeset
   461
				Display.getDefault().asyncExec(new Runnable() {
cawthron
parents:
diff changeset
   462
					public void run() {
cawthron
parents:
diff changeset
   463
						viewer.refresh(true);
cawthron
parents:
diff changeset
   464
						packColumns();
cawthron
parents:
diff changeset
   465
					}
cawthron
parents:
diff changeset
   466
				});
cawthron
parents:
diff changeset
   467
			}
cawthron
parents:
diff changeset
   468
		};
cawthron
parents:
diff changeset
   469
		RemoteConnectionsActivator.getConnectionsManager().addConnectionStoreChangedListener(connectionStoreChangedListener);
cawthron
parents:
diff changeset
   470
cawthron
parents:
diff changeset
   471
		RemoteConnectionsActivator.setHelp(parent, ".connections_view"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   472
	}
cawthron
parents:
diff changeset
   473
cawthron
parents:
diff changeset
   474
	private void packColumns() {
cawthron
parents:
diff changeset
   475
		TreeColumn[] columns = viewer.getTree().getColumns();
cawthron
parents:
diff changeset
   476
		for (TreeColumn column : columns) {
cawthron
parents:
diff changeset
   477
			column.pack();
cawthron
parents:
diff changeset
   478
		}
cawthron
parents:
diff changeset
   479
	}
cawthron
parents:
diff changeset
   480
cawthron
parents:
diff changeset
   481
	private String getNodeDisplayName(Object obj) {
cawthron
parents:
diff changeset
   482
		Object value = ((TreeNode) obj).getValue();
cawthron
parents:
diff changeset
   483
		if (value instanceof IConnection)
cawthron
parents:
diff changeset
   484
			return ((IConnection) value).getDisplayName();
cawthron
parents:
diff changeset
   485
		else if (value instanceof IConnectedService)
cawthron
parents:
diff changeset
   486
			return ((IConnectedService) value).getService().getDisplayName();
cawthron
parents:
diff changeset
   487
cawthron
parents:
diff changeset
   488
		return ""; //$NON-NLS-1$
cawthron
parents:
diff changeset
   489
	}
cawthron
parents:
diff changeset
   490
	
cawthron
parents:
diff changeset
   491
	private void hookContextMenu() {
cawthron
parents:
diff changeset
   492
		MenuManager menuMgr = new MenuManager("#PopupMenu"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   493
		menuMgr.setRemoveAllWhenShown(true);
cawthron
parents:
diff changeset
   494
		menuMgr.addMenuListener(new IMenuListener() {
cawthron
parents:
diff changeset
   495
			public void menuAboutToShow(IMenuManager manager) {
cawthron
parents:
diff changeset
   496
				ConnectionsView.this.fillContextMenu(manager);
cawthron
parents:
diff changeset
   497
			}
cawthron
parents:
diff changeset
   498
		});
cawthron
parents:
diff changeset
   499
		Menu menu = menuMgr.createContextMenu(viewer.getControl());
cawthron
parents:
diff changeset
   500
		viewer.getControl().setMenu(menu);
cawthron
parents:
diff changeset
   501
		getSite().registerContextMenu(menuMgr, viewer);
cawthron
parents:
diff changeset
   502
	}
cawthron
parents:
diff changeset
   503
cawthron
parents:
diff changeset
   504
	private void contributeToActionBars() {
cawthron
parents:
diff changeset
   505
		IActionBars bars = getViewSite().getActionBars();
cawthron
parents:
diff changeset
   506
		fillLocalPullDown(bars.getMenuManager());
cawthron
parents:
diff changeset
   507
		fillLocalToolBar(bars.getToolBarManager());
cawthron
parents:
diff changeset
   508
	}
cawthron
parents:
diff changeset
   509
cawthron
parents:
diff changeset
   510
	private void fillLocalPullDown(IMenuManager manager) {
cawthron
parents:
diff changeset
   511
		// nothing for now
cawthron
parents:
diff changeset
   512
	}
cawthron
parents:
diff changeset
   513
cawthron
parents:
diff changeset
   514
	private void fillContextMenu(IMenuManager manager) {
cawthron
parents:
diff changeset
   515
		manager.add(getAction(NEW_ACTION));
cawthron
parents:
diff changeset
   516
		ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   517
		if (selection.isEmpty())
cawthron
parents:
diff changeset
   518
			return;
cawthron
parents:
diff changeset
   519
		TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   520
		Object value = node.getValue();
cawthron
parents:
diff changeset
   521
		if (value instanceof IConnectedService)
cawthron
parents:
diff changeset
   522
			manager.add(getAction(ENABLE_SERVICE_ACTION));
cawthron
parents:
diff changeset
   523
		else {
cawthron
parents:
diff changeset
   524
			manager.add(getAction(RENAME_ACTION));
cawthron
parents:
diff changeset
   525
			manager.add(getAction(EDIT_ACTION));
cawthron
parents:
diff changeset
   526
			manager.add(getAction(DELETE_ACTION));
cawthron
parents:
diff changeset
   527
			manager.add(getAction(HELP_ACTION));
cawthron
parents:
diff changeset
   528
		}
cawthron
parents:
diff changeset
   529
	}
cawthron
parents:
diff changeset
   530
	
cawthron
parents:
diff changeset
   531
	private void fillLocalToolBar(IToolBarManager manager) {
cawthron
parents:
diff changeset
   532
		manager.add(getAction(REFRESH_ACTION));
cawthron
parents:
diff changeset
   533
		manager.add(getAction(NEW_ACTION));
cawthron
parents:
diff changeset
   534
		manager.add(getAction(EDIT_ACTION));
cawthron
parents:
diff changeset
   535
	}
cawthron
parents:
diff changeset
   536
cawthron
parents:
diff changeset
   537
	private void makeActions() {
cawthron
parents:
diff changeset
   538
		actions = new ArrayList<Action>();
cawthron
parents:
diff changeset
   539
		connectionSelectedActions = new ArrayList<Action>();
cawthron
parents:
diff changeset
   540
		serviceSelectedActions = new ArrayList<Action>();
cawthron
parents:
diff changeset
   541
		
cawthron
parents:
diff changeset
   542
		Action action = new Action(Messages.getString("ConnectionsView.NewActionLabel"), CONNECTION_NEW_IMGDESC) { //$NON-NLS-1$
cawthron
parents:
diff changeset
   543
			@Override
cawthron
parents:
diff changeset
   544
			public void run() {
cawthron
parents:
diff changeset
   545
				SettingsWizard wizard = new SettingsWizard();
cawthron
parents:
diff changeset
   546
				wizard.open(getViewSite().getShell());
cawthron
parents:
diff changeset
   547
			}
cawthron
parents:
diff changeset
   548
			
cawthron
parents:
diff changeset
   549
		};
cawthron
parents:
diff changeset
   550
		action.setId(NEW_ACTION);
cawthron
parents:
diff changeset
   551
		actions.add(action);
cawthron
parents:
diff changeset
   552
		action.setEnabled(!RemoteConnectionsActivator.getConnectionTypeProvider().getConnectionTypes().isEmpty());
cawthron
parents:
diff changeset
   553
		
cawthron
parents:
diff changeset
   554
		action = new Action(Messages.getString("ConnectionsView.EditActionLabel"), CONNECTION_EDIT_IMGDESC) { //$NON-NLS-1$
cawthron
parents:
diff changeset
   555
			@Override
cawthron
parents:
diff changeset
   556
			public void run() {
cawthron
parents:
diff changeset
   557
				ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   558
				if (selection.isEmpty())
cawthron
parents:
diff changeset
   559
					return;
cawthron
parents:
diff changeset
   560
				TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   561
				Object value = node.getValue();
cawthron
parents:
diff changeset
   562
				if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   563
					SettingsWizard wizard = new SettingsWizard((IConnection) value);
cawthron
parents:
diff changeset
   564
					wizard.open(getViewSite().getShell());
cawthron
parents:
diff changeset
   565
				}
cawthron
parents:
diff changeset
   566
			}
cawthron
parents:
diff changeset
   567
		};
cawthron
parents:
diff changeset
   568
		action.setId(EDIT_ACTION);
cawthron
parents:
diff changeset
   569
		actions.add(action);
cawthron
parents:
diff changeset
   570
		connectionSelectedActions.add(action);
cawthron
parents:
diff changeset
   571
		
cawthron
parents:
diff changeset
   572
		action = new Action() {
cawthron
parents:
diff changeset
   573
			@Override
cawthron
parents:
diff changeset
   574
			public void run() {
cawthron
parents:
diff changeset
   575
				ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   576
				if (selection.isEmpty())
cawthron
parents:
diff changeset
   577
					return;
cawthron
parents:
diff changeset
   578
				TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   579
				Object value = node.getValue();
cawthron
parents:
diff changeset
   580
				if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   581
					viewer.editElement(node, 0);
cawthron
parents:
diff changeset
   582
				}
cawthron
parents:
diff changeset
   583
			}
cawthron
parents:
diff changeset
   584
		};
cawthron
parents:
diff changeset
   585
		action.setId(RENAME_ACTION);
cawthron
parents:
diff changeset
   586
		action.setAccelerator(SWT.F2);
cawthron
parents:
diff changeset
   587
		action.setText(Messages.getString("ConnectionsView.RenameMenuLabel") +  //$NON-NLS-1$
cawthron
parents:
diff changeset
   588
				"\t" +  //$NON-NLS-1$
cawthron
parents:
diff changeset
   589
				LegacyActionTools.convertAccelerator(action.getAccelerator()));
cawthron
parents:
diff changeset
   590
		actions.add(action);
cawthron
parents:
diff changeset
   591
		connectionSelectedActions.add(action);
cawthron
parents:
diff changeset
   592
		
cawthron
parents:
diff changeset
   593
		action = new EnableConnectedServiceAction();
cawthron
parents:
diff changeset
   594
		action.setId(ENABLE_SERVICE_ACTION);
cawthron
parents:
diff changeset
   595
		actions.add(action);
cawthron
parents:
diff changeset
   596
		serviceSelectedActions.add(action);
cawthron
parents:
diff changeset
   597
		
cawthron
parents:
diff changeset
   598
		action = new Action(Messages.getString("ConnectionsView.RefreshActionLabel"), CONNECTION_REFRESH_IMGDESC) { //$NON-NLS-1$
cawthron
parents:
diff changeset
   599
			@Override
cawthron
parents:
diff changeset
   600
			public void run() {
cawthron
parents:
diff changeset
   601
				IConnectionsManager connectionsManager = RemoteConnectionsActivator.getConnectionsManager();
cawthron
parents:
diff changeset
   602
				for (IConnection connection : connectionsManager.getConnections()) {
cawthron
parents:
diff changeset
   603
					Collection<IConnectedService> connectedServices = connectionsManager.getConnectedServices(connection);
cawthron
parents:
diff changeset
   604
					for (IConnectedService connectedService : connectedServices) {
cawthron
parents:
diff changeset
   605
						connectedService.setEnabled(true);
cawthron
parents:
diff changeset
   606
						connectedService.testStatus();
cawthron
parents:
diff changeset
   607
					}
cawthron
parents:
diff changeset
   608
				}
cawthron
parents:
diff changeset
   609
				((EnableConnectedServiceAction) getAction(ENABLE_SERVICE_ACTION)).updateLabel();
cawthron
parents:
diff changeset
   610
			}
cawthron
parents:
diff changeset
   611
		};
cawthron
parents:
diff changeset
   612
		action.setAccelerator(SWT.F5);
cawthron
parents:
diff changeset
   613
		action.setId(REFRESH_ACTION);
cawthron
parents:
diff changeset
   614
		actions.add(action);
cawthron
parents:
diff changeset
   615
		
cawthron
parents:
diff changeset
   616
		action = new Action(Messages.getString("ConnectionsView.DeleteActionLabel"),  //$NON-NLS-1$
cawthron
parents:
diff changeset
   617
				PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_DELETE)) {
cawthron
parents:
diff changeset
   618
			@Override
cawthron
parents:
diff changeset
   619
			public void run() {
cawthron
parents:
diff changeset
   620
				ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   621
				if (selection.isEmpty())
cawthron
parents:
diff changeset
   622
					return;
cawthron
parents:
diff changeset
   623
				TreeNode node = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   624
				Object value = node.getValue();
cawthron
parents:
diff changeset
   625
				if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   626
					RemoteConnectionsActivator.getConnectionsManager().removeConnection((IConnection) value);
cawthron
parents:
diff changeset
   627
					RemoteConnectionsActivator.getConnectionsManager().storeConnections();
cawthron
parents:
diff changeset
   628
				}
cawthron
parents:
diff changeset
   629
			}
cawthron
parents:
diff changeset
   630
		};
cawthron
parents:
diff changeset
   631
		action.setAccelerator(SWT.DEL);
cawthron
parents:
diff changeset
   632
		action.setId(DELETE_ACTION);
cawthron
parents:
diff changeset
   633
		actions.add(action);
cawthron
parents:
diff changeset
   634
		connectionSelectedActions.add(action);
cawthron
parents:
diff changeset
   635
		
cawthron
parents:
diff changeset
   636
		Image image = JFaceResources.getImage(org.eclipse.jface.dialogs.Dialog.DLG_IMG_HELP);
cawthron
parents:
diff changeset
   637
		ImageDescriptor desc = ImageDescriptor.createFromImage(image);
cawthron
parents:
diff changeset
   638
		action = new Action(Messages.getString("ConnectionsView.HelpActionLabel"), desc) { //$NON-NLS-1$
cawthron
parents:
diff changeset
   639
			
cawthron
parents:
diff changeset
   640
			private String getHelpContextFromSelection() {
cawthron
parents:
diff changeset
   641
				ISelection selection = viewer.getSelection();
cawthron
parents:
diff changeset
   642
				if (!selection.isEmpty()) {
cawthron
parents:
diff changeset
   643
					TreeNode treeNode = (TreeNode) ((IStructuredSelection) selection).getFirstElement();
cawthron
parents:
diff changeset
   644
					Object value = treeNode.getValue();
cawthron
parents:
diff changeset
   645
					if (value instanceof IConnection) {
cawthron
parents:
diff changeset
   646
						return ((IConnection) value).getConnectionType().getHelpContext();
cawthron
parents:
diff changeset
   647
					}
cawthron
parents:
diff changeset
   648
				}
cawthron
parents:
diff changeset
   649
				return null;
cawthron
parents:
diff changeset
   650
			}
cawthron
parents:
diff changeset
   651
			
cawthron
parents:
diff changeset
   652
			@Override
cawthron
parents:
diff changeset
   653
			public boolean isEnabled() {
cawthron
parents:
diff changeset
   654
				return getHelpContextFromSelection() != null;
cawthron
parents:
diff changeset
   655
			}
cawthron
parents:
diff changeset
   656
cawthron
parents:
diff changeset
   657
			@Override
cawthron
parents:
diff changeset
   658
			public void run() {
cawthron
parents:
diff changeset
   659
				PlatformUI.getWorkbench().getHelpSystem().displayHelp(getHelpContextFromSelection());
cawthron
parents:
diff changeset
   660
			}
cawthron
parents:
diff changeset
   661
		};
cawthron
parents:
diff changeset
   662
		action.setId(HELP_ACTION);
cawthron
parents:
diff changeset
   663
		actions.add(action);
cawthron
parents:
diff changeset
   664
		connectionSelectedActions.add(action);
cawthron
parents:
diff changeset
   665
		enableConnectionSelectedActions(false);
cawthron
parents:
diff changeset
   666
		enableServiceSelectedActions(false);
cawthron
parents:
diff changeset
   667
	}
cawthron
parents:
diff changeset
   668
	
cawthron
parents:
diff changeset
   669
	private void enableConnectionSelectedActions(boolean enable) {
cawthron
parents:
diff changeset
   670
		for (Action action : connectionSelectedActions) {
cawthron
parents:
diff changeset
   671
			action.setEnabled(enable);
cawthron
parents:
diff changeset
   672
		}
cawthron
parents:
diff changeset
   673
	}
cawthron
parents:
diff changeset
   674
	
cawthron
parents:
diff changeset
   675
	private void enableServiceSelectedActions(boolean enable) {
cawthron
parents:
diff changeset
   676
		for (Action action : serviceSelectedActions) {
cawthron
parents:
diff changeset
   677
			action.setEnabled(enable);
cawthron
parents:
diff changeset
   678
		}
cawthron
parents:
diff changeset
   679
	}
cawthron
parents:
diff changeset
   680
	
cawthron
parents:
diff changeset
   681
	private Action getAction(String id) {
cawthron
parents:
diff changeset
   682
		for (Action action : actions) {
cawthron
parents:
diff changeset
   683
			if (action.getId().equals(id))
cawthron
parents:
diff changeset
   684
				return action;
cawthron
parents:
diff changeset
   685
		}
cawthron
parents:
diff changeset
   686
		
cawthron
parents:
diff changeset
   687
		return null;
cawthron
parents:
diff changeset
   688
	}
cawthron
parents:
diff changeset
   689
cawthron
parents:
diff changeset
   690
	private void hookAccelerators() {
cawthron
parents:
diff changeset
   691
		keyListener = new KeyAdapter() {
cawthron
parents:
diff changeset
   692
			public void keyPressed(KeyEvent e) {
cawthron
parents:
diff changeset
   693
				for (Action action : actions) {
cawthron
parents:
diff changeset
   694
					if (e.keyCode == action.getAccelerator()) {
cawthron
parents:
diff changeset
   695
						action.run();
cawthron
parents:
diff changeset
   696
						break;
cawthron
parents:
diff changeset
   697
					}
cawthron
parents:
diff changeset
   698
				}
cawthron
parents:
diff changeset
   699
			}
cawthron
parents:
diff changeset
   700
		};
cawthron
parents:
diff changeset
   701
		viewer.getControl().addKeyListener(keyListener);
cawthron
parents:
diff changeset
   702
	}
cawthron
parents:
diff changeset
   703
cawthron
parents:
diff changeset
   704
	/**
cawthron
parents:
diff changeset
   705
	 * Passing the focus request to the viewer's control.
cawthron
parents:
diff changeset
   706
	 */
cawthron
parents:
diff changeset
   707
	public void setFocus() {
cawthron
parents:
diff changeset
   708
		viewer.getControl().setFocus();
cawthron
parents:
diff changeset
   709
	}
cawthron
parents:
diff changeset
   710
	
cawthron
parents:
diff changeset
   711
	private void removeServiceListeners() {
cawthron
parents:
diff changeset
   712
		for (IConnectedService connectedService : serviceToListenerMap.keySet()) {
cawthron
parents:
diff changeset
   713
			IStatusChangedListener listener = serviceToListenerMap.get(connectedService);
cawthron
parents:
diff changeset
   714
			connectedService.removeStatusChangedListener(listener);
cawthron
parents:
diff changeset
   715
		}
cawthron
parents:
diff changeset
   716
		serviceToListenerMap.clear();
cawthron
parents:
diff changeset
   717
	}
cawthron
parents:
diff changeset
   718
	
cawthron
parents:
diff changeset
   719
	private void disableAllConnectedServices() {
cawthron
parents:
diff changeset
   720
		Collection<IConnection> connections = 
cawthron
parents:
diff changeset
   721
			RemoteConnectionsActivator.getConnectionsManager().getConnections();
cawthron
parents:
diff changeset
   722
		for (IConnection connection : connections) {
cawthron
parents:
diff changeset
   723
			Collection<IConnectedService> connectedServicesForConnection = 
cawthron
parents:
diff changeset
   724
				RemoteConnectionsActivator.getConnectionsManager().getConnectedServices(connection);
cawthron
parents:
diff changeset
   725
			for (IConnectedService connectedService : connectedServicesForConnection) {
cawthron
parents:
diff changeset
   726
				connectedService.setEnabled(false);
cawthron
parents:
diff changeset
   727
			}
cawthron
parents:
diff changeset
   728
		}
cawthron
parents:
diff changeset
   729
	}
cawthron
parents:
diff changeset
   730
	
cawthron
parents:
diff changeset
   731
	@Override
cawthron
parents:
diff changeset
   732
	public void dispose() {
cawthron
parents:
diff changeset
   733
		removeServiceListeners();
cawthron
parents:
diff changeset
   734
		RemoteConnectionsActivator.getConnectionsManager().removeConnectionStoreChangedListener(connectionStoreChangedListener);
cawthron
parents:
diff changeset
   735
		disableAllConnectedServices();
cawthron
parents:
diff changeset
   736
		isDisposed = true;
cawthron
parents:
diff changeset
   737
		super.dispose();
cawthron
parents:
diff changeset
   738
	}
cawthron
parents:
diff changeset
   739
	
cawthron
parents:
diff changeset
   740
}
cawthron
parents:
diff changeset
   741