connectivity/com.nokia.carbide.remoteConnections/src/com/nokia/carbide/remoteconnections/settings/ui/ConnectionTypePage.java
author dadubrow
Mon, 06 Apr 2009 14:33:24 -0500
branchRCL_2_0
changeset 55 8254273d11c0
parent 2 d760517a8095
child 337 c44c75698736
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.settings.ui;
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.cpp.internal.api.utils.core.Check;
cawthron
parents:
diff changeset
    25
cawthron
parents:
diff changeset
    26
import org.eclipse.jface.viewers.*;
cawthron
parents:
diff changeset
    27
import org.eclipse.jface.wizard.WizardPage;
cawthron
parents:
diff changeset
    28
import org.eclipse.swt.SWT;
cawthron
parents:
diff changeset
    29
import org.eclipse.swt.events.ModifyEvent;
cawthron
parents:
diff changeset
    30
import org.eclipse.swt.events.ModifyListener;
cawthron
parents:
diff changeset
    31
import org.eclipse.swt.layout.GridData;
cawthron
parents:
diff changeset
    32
import org.eclipse.swt.layout.GridLayout;
cawthron
parents:
diff changeset
    33
import org.eclipse.swt.widgets.*;
cawthron
parents:
diff changeset
    34
cawthron
parents:
diff changeset
    35
import java.text.MessageFormat;
cawthron
parents:
diff changeset
    36
import java.util.*;
cawthron
parents:
diff changeset
    37
import java.util.List;
cawthron
parents:
diff changeset
    38
cawthron
parents:
diff changeset
    39
public class ConnectionTypePage extends WizardPage {
cawthron
parents:
diff changeset
    40
cawthron
parents:
diff changeset
    41
	private static final String INITIAL_NAME_FMT = "connection {0}"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    42
	private static final String UID = ".uid"; //$NON-NLS-1$
cawthron
parents:
diff changeset
    43
cawthron
parents:
diff changeset
    44
	private final SettingsWizard settingsWizard;
cawthron
parents:
diff changeset
    45
	private ListViewer viewer;
cawthron
parents:
diff changeset
    46
	private Text nameText;
cawthron
parents:
diff changeset
    47
	private Label connectionTypeDescLabel;
cawthron
parents:
diff changeset
    48
	private Label servicesLabel;
cawthron
parents:
diff changeset
    49
	private boolean initialized;
cawthron
parents:
diff changeset
    50
cawthron
parents:
diff changeset
    51
	protected ConnectionTypePage(SettingsWizard settingsWizard) {
cawthron
parents:
diff changeset
    52
		super("typepage"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    53
		this.settingsWizard = settingsWizard;
cawthron
parents:
diff changeset
    54
		setTitle(Messages.getString("ConnectionTypePage.Title")); //$NON-NLS-1$
cawthron
parents:
diff changeset
    55
		setDescription(Messages.getString("ConnectionTypePage.Description")); //$NON-NLS-1$
cawthron
parents:
diff changeset
    56
	}
cawthron
parents:
diff changeset
    57
	
cawthron
parents:
diff changeset
    58
	public void createControl(Composite parent) {
cawthron
parents:
diff changeset
    59
		Composite container = new Composite(parent, SWT.NONE);
cawthron
parents:
diff changeset
    60
		container.setLayout(new GridLayout(2, false));
cawthron
parents:
diff changeset
    61
		container.setData(UID, "ConnectionTypePage"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    62
		
cawthron
parents:
diff changeset
    63
		Label nameLabel = new Label(container, SWT.NONE);
cawthron
parents:
diff changeset
    64
		nameLabel.setText(Messages.getString("ConnectionTypePage.NameLabel")); //$NON-NLS-1$
cawthron
parents:
diff changeset
    65
		nameText = new Text(container, SWT.BORDER);
cawthron
parents:
diff changeset
    66
		GridData gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
cawthron
parents:
diff changeset
    67
		nameText.setLayoutData(gd);
cawthron
parents:
diff changeset
    68
		nameText.setToolTipText(Messages.getString("ConnectionTypePage.NameTextToolTip")); //$NON-NLS-1$
cawthron
parents:
diff changeset
    69
		nameText.setData(UID, "nameText"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    70
		nameText.setText(getInitialNameText());
cawthron
parents:
diff changeset
    71
		nameText.selectAll();
cawthron
parents:
diff changeset
    72
		nameText.addModifyListener(new ModifyListener() {
cawthron
parents:
diff changeset
    73
			public void modifyText(ModifyEvent e) {
cawthron
parents:
diff changeset
    74
				setPageComplete(validatePage());
cawthron
parents:
diff changeset
    75
			}
cawthron
parents:
diff changeset
    76
		});
cawthron
parents:
diff changeset
    77
cawthron
parents:
diff changeset
    78
		Label label = new Label(container, SWT.NONE);
cawthron
parents:
diff changeset
    79
		label.setLayoutData(new GridData(SWT.LEFT, SWT.TOP, false, false));
cawthron
parents:
diff changeset
    80
		label.setText(Messages.getString("ConnectionTypePage.ViewerLabel")); //$NON-NLS-1$
cawthron
parents:
diff changeset
    81
		
cawthron
parents:
diff changeset
    82
		viewer = new ListViewer(container, SWT.BORDER | SWT.READ_ONLY | SWT.V_SCROLL);
cawthron
parents:
diff changeset
    83
		viewer.setLabelProvider(new LabelProvider() {
cawthron
parents:
diff changeset
    84
			@Override
cawthron
parents:
diff changeset
    85
			public String getText(Object element) {
cawthron
parents:
diff changeset
    86
				Check.checkState(element instanceof IConnectionType);
cawthron
parents:
diff changeset
    87
				IConnectionType connectionType = (IConnectionType) element;
cawthron
parents:
diff changeset
    88
				return connectionType.getDisplayName();
cawthron
parents:
diff changeset
    89
			}
cawthron
parents:
diff changeset
    90
		});
cawthron
parents:
diff changeset
    91
		viewer.setContentProvider(new ArrayContentProvider());
cawthron
parents:
diff changeset
    92
		viewer.setInput(getConnectionTypes());
cawthron
parents:
diff changeset
    93
		gd = new GridData(SWT.FILL, SWT.FILL, true, false);
cawthron
parents:
diff changeset
    94
		viewer.getControl().setLayoutData(gd);
cawthron
parents:
diff changeset
    95
		viewer.getControl().setData(UID, "viewer"); //$NON-NLS-1$
cawthron
parents:
diff changeset
    96
		viewer.addSelectionChangedListener(new ISelectionChangedListener() {
cawthron
parents:
diff changeset
    97
			private IConnectionType previousSelection;
cawthron
parents:
diff changeset
    98
			public void selectionChanged(SelectionChangedEvent event) {
cawthron
parents:
diff changeset
    99
				Object currentSelection = ((IStructuredSelection) event.getSelection()).getFirstElement();
cawthron
parents:
diff changeset
   100
				if (!currentSelection.equals(previousSelection)) {
cawthron
parents:
diff changeset
   101
					connectionTypeDescLabel.setText(getConnectionTypeDescription());
cawthron
parents:
diff changeset
   102
					servicesLabel.setText(getServicesString());
cawthron
parents:
diff changeset
   103
					settingsWizard.connectionTypeChanged();
cawthron
parents:
diff changeset
   104
					previousSelection = (IConnectionType) currentSelection;
cawthron
parents:
diff changeset
   105
				}
cawthron
parents:
diff changeset
   106
			}
cawthron
parents:
diff changeset
   107
		});
cawthron
parents:
diff changeset
   108
		viewer.addDoubleClickListener(new IDoubleClickListener() {
cawthron
parents:
diff changeset
   109
			public void doubleClick(DoubleClickEvent event) {
cawthron
parents:
diff changeset
   110
				getWizard().getContainer().showPage(getNextPage());
cawthron
parents:
diff changeset
   111
			}
cawthron
parents:
diff changeset
   112
		});
cawthron
parents:
diff changeset
   113
		viewer.setSorter(new ViewerSorter() {
cawthron
parents:
diff changeset
   114
			@Override
cawthron
parents:
diff changeset
   115
			public int compare(Viewer viewer, Object e1, Object e2) {
cawthron
parents:
diff changeset
   116
				IConnectionType t1 = (IConnectionType) e1;
cawthron
parents:
diff changeset
   117
				IConnectionType t2 = (IConnectionType) e2;
cawthron
parents:
diff changeset
   118
				return t1.getDisplayName().compareToIgnoreCase(t2.getDisplayName());
cawthron
parents:
diff changeset
   119
			}
cawthron
parents:
diff changeset
   120
		});
cawthron
parents:
diff changeset
   121
		viewer.getList().select(getCurrentTypeIndex());
cawthron
parents:
diff changeset
   122
		
cawthron
parents:
diff changeset
   123
		connectionTypeDescLabel = new Label(container, SWT.WRAP);
cawthron
parents:
diff changeset
   124
		connectionTypeDescLabel.setText(getConnectionTypeDescription());
cawthron
parents:
diff changeset
   125
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
cawthron
parents:
diff changeset
   126
		gd.horizontalSpan = 2;
cawthron
parents:
diff changeset
   127
		connectionTypeDescLabel.setLayoutData(gd);
cawthron
parents:
diff changeset
   128
		connectionTypeDescLabel.setData(UID, "additionalNotesLabel"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   129
		
cawthron
parents:
diff changeset
   130
		servicesLabel = new Label(container, SWT.WRAP);
cawthron
parents:
diff changeset
   131
		servicesLabel.setText(getServicesString());
cawthron
parents:
diff changeset
   132
		gd = new GridData(SWT.FILL, SWT.CENTER, true, false);
cawthron
parents:
diff changeset
   133
		gd.horizontalSpan = 2;
cawthron
parents:
diff changeset
   134
		servicesLabel.setLayoutData(gd);
cawthron
parents:
diff changeset
   135
		servicesLabel.setData(UID, "servicesLabel"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   136
		
cawthron
parents:
diff changeset
   137
		setControl(container);
cawthron
parents:
diff changeset
   138
		RemoteConnectionsActivator.setHelp(container, ".connection_type_page"); //$NON-NLS-1$
cawthron
parents:
diff changeset
   139
	}
cawthron
parents:
diff changeset
   140
	
cawthron
parents:
diff changeset
   141
	private String getServicesString() {
cawthron
parents:
diff changeset
   142
		StringBuilder servicesString = new StringBuilder();
cawthron
parents:
diff changeset
   143
		Collection<IService> services = 
cawthron
parents:
diff changeset
   144
			RemoteConnectionsActivator.getConnectionTypeProvider().getCompatibleServices(getConnectionType());
cawthron
parents:
diff changeset
   145
		if (services == null || services.isEmpty())
cawthron
parents:
diff changeset
   146
			return ""; //$NON-NLS-1$
cawthron
parents:
diff changeset
   147
		for (Iterator<IService> iterator = services.iterator(); iterator.hasNext();) {
cawthron
parents:
diff changeset
   148
			servicesString.append(iterator.next().getDisplayName());
cawthron
parents:
diff changeset
   149
			if (iterator.hasNext())
cawthron
parents:
diff changeset
   150
				servicesString.append(", "); //$NON-NLS-1$
cawthron
parents:
diff changeset
   151
		}
cawthron
parents:
diff changeset
   152
		
cawthron
parents:
diff changeset
   153
		return MessageFormat.format(Messages.getString("ConnectionTypePage.SupportedServicesLabel"), new Object[] { servicesString.toString() }); //$NON-NLS-1$
cawthron
parents:
diff changeset
   154
	}
cawthron
parents:
diff changeset
   155
cawthron
parents:
diff changeset
   156
	private String getConnectionTypeDescription() {
cawthron
parents:
diff changeset
   157
		return getConnectionType().getDescription();
cawthron
parents:
diff changeset
   158
	}
cawthron
parents:
diff changeset
   159
	
cawthron
parents:
diff changeset
   160
	private int getCurrentTypeIndex() {
cawthron
parents:
diff changeset
   161
		IConnection connectionToEdit = settingsWizard.getConnectionToEdit();
cawthron
parents:
diff changeset
   162
		if (connectionToEdit != null) {
cawthron
parents:
diff changeset
   163
			Object input = viewer.getInput();
cawthron
parents:
diff changeset
   164
			if (input != null) {
cawthron
parents:
diff changeset
   165
				Collection<IConnectionType> connectionTypes = (Collection<IConnectionType>) input;
cawthron
parents:
diff changeset
   166
				for (int i = 0; i < connectionTypes.size(); i++) {
cawthron
parents:
diff changeset
   167
					IConnectionType connectionType = (IConnectionType) viewer.getElementAt(i);
cawthron
parents:
diff changeset
   168
					if (connectionToEdit.getConnectionType().equals(connectionType))
cawthron
parents:
diff changeset
   169
						return i;
cawthron
parents:
diff changeset
   170
				}
cawthron
parents:
diff changeset
   171
			}
cawthron
parents:
diff changeset
   172
		}
cawthron
parents:
diff changeset
   173
		return 0;
cawthron
parents:
diff changeset
   174
	}
cawthron
parents:
diff changeset
   175
	
cawthron
parents:
diff changeset
   176
	private Collection<IConnectionType> getConnectionTypes() {
cawthron
parents:
diff changeset
   177
		Collection<IConnectionType> connectionTypes = 
cawthron
parents:
diff changeset
   178
			RemoteConnectionsActivator.getConnectionTypeProvider().getConnectionTypes();
cawthron
parents:
diff changeset
   179
		IService serviceToRestrict = settingsWizard.getServiceToRestrict();
cawthron
parents:
diff changeset
   180
		if (serviceToRestrict != null) {
cawthron
parents:
diff changeset
   181
			List<IConnectionType> restrictedConnectionTypes = new ArrayList<IConnectionType>();
cawthron
parents:
diff changeset
   182
			Collection<String> compatibleConnectionTypeIds = 
cawthron
parents:
diff changeset
   183
				RemoteConnectionsActivator.getConnectionTypeProvider().getCompatibleConnectionTypeIds(serviceToRestrict);
cawthron
parents:
diff changeset
   184
			for (String connectionTypeId : compatibleConnectionTypeIds) {
cawthron
parents:
diff changeset
   185
				IConnectionType connectionType = 
cawthron
parents:
diff changeset
   186
					RemoteConnectionsActivator.getConnectionTypeProvider().getConnectionType(connectionTypeId);
cawthron
parents:
diff changeset
   187
				if (connectionTypes.contains(connectionType))
cawthron
parents:
diff changeset
   188
					restrictedConnectionTypes.add(connectionType);
cawthron
parents:
diff changeset
   189
			}
cawthron
parents:
diff changeset
   190
			return restrictedConnectionTypes;
cawthron
parents:
diff changeset
   191
		}
cawthron
parents:
diff changeset
   192
		
cawthron
parents:
diff changeset
   193
		return connectionTypes;
cawthron
parents:
diff changeset
   194
	}
cawthron
parents:
diff changeset
   195
cawthron
parents:
diff changeset
   196
	private boolean validatePage() {
cawthron
parents:
diff changeset
   197
		setErrorMessage(null);
cawthron
parents:
diff changeset
   198
		String name = getName();
cawthron
parents:
diff changeset
   199
		boolean isValid = isNameUnique(name);
cawthron
parents:
diff changeset
   200
		if (!isValid) {
cawthron
parents:
diff changeset
   201
			setErrorMessage(MessageFormat.format(Messages.getString("ConnectionTypePage.ConnectionNameInUseError"), new Object[] { name } )); //$NON-NLS-1$
cawthron
parents:
diff changeset
   202
		}
cawthron
parents:
diff changeset
   203
		
cawthron
parents:
diff changeset
   204
		return isValid;
cawthron
parents:
diff changeset
   205
	}
cawthron
parents:
diff changeset
   206
	
cawthron
parents:
diff changeset
   207
	private boolean isNameUnique(String name) {
cawthron
parents:
diff changeset
   208
		boolean inUse = RemoteConnectionsActivator.getConnectionsManager().connectionNameInUse(name);
cawthron
parents:
diff changeset
   209
		IConnection connectionToEdit = settingsWizard.getConnectionToEdit();
cawthron
parents:
diff changeset
   210
		if (connectionToEdit != null && inUse)
cawthron
parents:
diff changeset
   211
			inUse = !name.equals(connectionToEdit.getDisplayName());
cawthron
parents:
diff changeset
   212
		
cawthron
parents:
diff changeset
   213
		return !inUse;
cawthron
parents:
diff changeset
   214
	}
cawthron
parents:
diff changeset
   215
cawthron
parents:
diff changeset
   216
	private String getInitialNameText() {
cawthron
parents:
diff changeset
   217
		IConnection connectionToEdit = settingsWizard.getConnectionToEdit();
cawthron
parents:
diff changeset
   218
		if (connectionToEdit != null)
cawthron
parents:
diff changeset
   219
			return connectionToEdit.getDisplayName();
cawthron
parents:
diff changeset
   220
		
cawthron
parents:
diff changeset
   221
		long i = 1;
cawthron
parents:
diff changeset
   222
		while (true) {
cawthron
parents:
diff changeset
   223
			String name = MessageFormat.format(INITIAL_NAME_FMT, new Object[] { Long.toString(i++) });
cawthron
parents:
diff changeset
   224
			if (isNameUnique(name))
cawthron
parents:
diff changeset
   225
				return name;
cawthron
parents:
diff changeset
   226
		}
cawthron
parents:
diff changeset
   227
	}
cawthron
parents:
diff changeset
   228
cawthron
parents:
diff changeset
   229
	public String getName() {
cawthron
parents:
diff changeset
   230
		return nameText.getText().trim();
cawthron
parents:
diff changeset
   231
	}
cawthron
parents:
diff changeset
   232
	
cawthron
parents:
diff changeset
   233
	public IConnectionType getConnectionType() {
cawthron
parents:
diff changeset
   234
		return (IConnectionType) ((IStructuredSelection) viewer.getSelection()).getFirstElement();
cawthron
parents:
diff changeset
   235
	}
cawthron
parents:
diff changeset
   236
	
cawthron
parents:
diff changeset
   237
	public void setVisible(boolean visible) {
cawthron
parents:
diff changeset
   238
		super.setVisible(visible);
cawthron
parents:
diff changeset
   239
		if (!initialized && visible) {
cawthron
parents:
diff changeset
   240
			initialized = true;
cawthron
parents:
diff changeset
   241
			viewer.setSelection(viewer.getSelection());
cawthron
parents:
diff changeset
   242
			settingsWizard.connectionTypeChanged();
cawthron
parents:
diff changeset
   243
		}
cawthron
parents:
diff changeset
   244
	}
cawthron
parents:
diff changeset
   245
}