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