Skip to main content
summaryrefslogtreecommitdiffstats
blob: 733c9d3c8d549078881077c02d6f7ac962d882ef (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
/****************************************************************************
 * Copyright (c) 2004 Composent, Inc. and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Composent, Inc. - initial API and implementation
 *****************************************************************************/
package org.eclipse.ecf.internal.example.collab.ui;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.eclipse.ecf.core.ContainerFactory;
import org.eclipse.ecf.core.ContainerTypeDescription;
import org.eclipse.ecf.internal.example.collab.ClientPlugin;
import org.eclipse.ecf.internal.example.collab.ClientPluginConstants;
import org.eclipse.ecf.internal.example.collab.Messages;
import org.eclipse.jface.dialogs.IDialogSettings;
import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ISelectionChangedListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.ModifyEvent;
import org.eclipse.swt.events.ModifyListener;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.Text;

public class ConnectionDialog extends TitleAreaDialog {
	private static final int PROVIDER_TABLE_HEIGHT = 200;

	private static final int PROVIDER_TABLE_WIDTH = 150;

	protected static final String CLASSNAME = JoinGroupWizardPage.class
			.getName();

	protected static final String USER_NAME_SYSTEM_PROPERTY = "user.name"; //$NON-NLS-1$

	protected static final String ISSERVER_PROP_NAME = CLASSNAME + ".isServer"; //$NON-NLS-1$

	protected static final String DEFAULTGROUPID_PROP_NAME = CLASSNAME
			+ ".defaultgroupid"; //$NON-NLS-1$

	protected static final String EXAMPLEGROUPID_PROP_NAME = CLASSNAME
			+ ".examplegroupid"; //$NON-NLS-1$

	protected static final String USEPASSWORD_PROP_NAME = CLASSNAME
			+ ".usepassword"; //$NON-NLS-1$

	protected static final String USENICKNAME_PROP_NAME = CLASSNAME
			+ ".usenickname"; //$NON-NLS-1$

	protected static final String URLPREFIX_NAME = CLASSNAME + ".urlprefix"; //$NON-NLS-1$

	protected static final String GROUPIDLABEL_PROP_NAME = CLASSNAME
			+ ".groupIDLabel"; //$NON-NLS-1$

	protected static final String NAMESPACE_PROP_NAME = CLASSNAME
			+ ".namespace"; //$NON-NLS-1$

	protected static final String PAGE_DESCRIPTION = Messages.ConnectionDialog_SELECT_PROVIDER_DESCRIPTION;

	protected static final String JOINGROUP_FIELDNAME = Messages.ConnectionDialog_JOIN_GROUP_FIELD_TEXT;

	protected static final String NICKNAME_FIELDNAME = Messages.ConnectionDialog_NICKNAME_TEXT;

	protected static final String ECF_DEFAULT_URL = "ecftcp://localhost:3282/server"; //$NON-NLS-1$

	protected static final String ECF_TEMPLATE_URL = Messages.ConnectionDialog_TEMPLATE_URL;

	protected static final String PAGE_TITLE = Messages.ConnectionDialog_CONNECT_PAGE_TITLE;

	protected static final String DEFAULT_CLIENT = "ecf.generic.client"; //$NON-NLS-1$

	private static final String DIALOG_SETTINGS = CLASSNAME;

	private Composite paramComp;

	private Text joingroup_text;

	private String joinGroup = ""; //$NON-NLS-1$

	private Text nickname_text;

	private String nickname = ""; //$NON-NLS-1$

	private Text password_text;

	private String password = ""; //$NON-NLS-1$

	private String urlPrefix;

	private String namespace = null;

	private String containerType = ""; //$NON-NLS-1$

	private TableViewer viewer;

	private IDialogSettings dialogSettings;

	private GlobalModifyListener listener = new GlobalModifyListener();

	private Button autoLogin = null;

	private boolean autoLoginFlag = false;

	public ConnectionDialog(Shell parentShell) {
		super(parentShell);
		setShellStyle(SWT.TITLE | SWT.BORDER | SWT.CLOSE | SWT.RESIZE);
	}

	protected Control createDialogArea(Composite parent) {
		Composite main = new Composite((Composite) super
				.createDialogArea(parent), SWT.NONE);
		main.setLayout(new GridLayout());
		main.setLayoutData(new GridData(GridData.FILL_BOTH));

		Label providerLabel = new Label(main, SWT.NONE);
		providerLabel.setText(Messages.ConnectionDialog_PROTOCOL_TEXT);

		Composite providerComp = new Composite(main, SWT.NONE);
		GridLayout layout = new GridLayout(2, false);
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		providerComp.setLayout(layout);
		providerComp.setLayoutData(new GridData(GridData.FILL_BOTH));

		viewer = new TableViewer(providerComp, SWT.BORDER | SWT.FULL_SELECTION);
		viewer.setContentProvider(new ECFProviderContentProvider());
		viewer.setLabelProvider(new ECFProviderLabelProvider());
		viewer.addSelectionChangedListener(new ProviderSelector());

		Table table = viewer.getTable();
		GridData gData = new GridData(GridData.FILL_VERTICAL);
		gData.widthHint = PROVIDER_TABLE_WIDTH;
		gData.heightHint = PROVIDER_TABLE_HEIGHT;
		table.setLayoutData(gData);

/*
 * table.setHeaderVisible(true); TableColumn tc = new TableColumn(table,
 * SWT.NONE); tc.setText("Name"); tc = new TableColumn(table, SWT.NONE);
 * tc.setText("Classname");
 * 
 */
		viewer.setInput(ContainerFactory.getDefault().getDescriptions());

		paramComp = new Composite(providerComp, SWT.NONE);
		GridLayout glayout = new GridLayout();
		glayout.marginTop = 0;
		glayout.marginBottom = 0;
		paramComp.setLayout(glayout);
		paramComp.setLayoutData(new GridData(GridData.FILL_BOTH));

		autoLogin = new Button(main, SWT.CHECK);
		autoLogin.setText(Messages.ConnectionDialog_AUTOLOGIN_TEXT);
		autoLogin.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
		autoLogin.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				autoLoginFlag = autoLogin.getSelection();
			}
		});

		new Label(main, SWT.NONE);
		Label sep = new Label(main, SWT.SEPARATOR | SWT.HORIZONTAL);
		sep.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		this.setTitle(Messages.ConnectionDialog_CONNECTION_TITLE);
		this
				.setMessage(Messages.ConnectionDialog_CHOOSE_PROVIDER_MESSAGE);

		this.getShell().setText(Messages.ConnectionDialog_CONNECT_TEXT);
		return parent;
	}

	protected Control createContents(Composite parent) {
		Control control = super.createContents(parent);

		try {
			restoreDialogSettings();
		} catch (IOException e) {
			e.printStackTrace();
		}

		return control;
	}

	protected Point getInitialSize() {
		return new Point(500, 400);
	}

	private class ECFProviderContentProvider implements
			IStructuredContentProvider {

		public Object[] getElements(Object inputElement) {
			List rawDescriptions = (List) inputElement;
			List elements = new ArrayList();

			for (Iterator i = rawDescriptions.iterator(); i.hasNext();) {
				final ContainerTypeDescription desc = (ContainerTypeDescription) i
						.next();
				if (!desc.isServer() && !desc.isHidden())
					elements.add(desc);
			}

			return elements.toArray();
		}

		public void dispose() {
		}

		public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
		}
	}

	public String getJoinGroupText() {
		String textValue = joinGroup.trim();
		String namespace = getNamespace();
		if (namespace != null) {
			return textValue;
		} else {
			if (!urlPrefix.equals("") && !textValue.startsWith(urlPrefix)) { //$NON-NLS-1$
				textValue = urlPrefix + textValue;
			}
			return textValue;
		}
	}

	public String getNicknameText() {
		return nickname;
	}

	public String getPasswordText() {
		return password;
	}

	public String getContainerType() {
		return containerType;
	}

	public String getNamespace() {
		return namespace;
	}

	public boolean getAutoLoginFlag() {
		return autoLoginFlag;
	}

	private void restoreDialogSettings() throws IOException {
		IDialogSettings dialogSettings = getDialogSettings();
		if (dialogSettings != null) {
			IDialogSettings pageSettings = dialogSettings
					.getSection(DIALOG_SETTINGS);
			if (pageSettings != null) {

				int intVal = pageSettings.getInt("provider"); //$NON-NLS-1$
				viewer.getTable().setSelection(intVal);
				viewer.setSelection(viewer.getSelection());
				String strVal = pageSettings.get("url"); //$NON-NLS-1$
				if (strVal != null && joingroup_text != null) {
					joingroup_text.setText(strVal);
				}

				strVal = pageSettings.get("nickname"); //$NON-NLS-1$
				if (strVal != null && nickname_text != null) {
					nickname_text.setText(strVal);
				}

				if (savePassword()) {
					strVal = pageSettings.get("password"); //$NON-NLS-1$
					if (strVal != null && password_text != null) {
						password_text.setText(strVal);
					}
				}

				listener.modifyText(null);
			}

		}
	}

	private void saveDialogSettings() {
		IDialogSettings dialogSettings = this.getDialogSettings();
		if (dialogSettings != null) {
			IDialogSettings pageSettings = dialogSettings
					.getSection(DIALOG_SETTINGS);
			if (pageSettings == null)
				pageSettings = dialogSettings.addNewSection(DIALOG_SETTINGS);

			pageSettings.put("url", this.getJoinGroupText()); //$NON-NLS-1$
			pageSettings.put("nickname", this.getNicknameText()); //$NON-NLS-1$
			pageSettings.put("password", this.getPasswordText()); //$NON-NLS-1$

			int i = viewer.getTable().getSelectionIndex();
			if (i >= 0)
				pageSettings.put("provider", i); //$NON-NLS-1$

/*
 * try { dialogSettings.save(this.getClass().toString()); } catch (IOException
 * e) { // TODO Auto-generated catch block e.printStackTrace(); }
 */		}
	}

	private boolean savePassword() {
		return ClientPlugin.getDefault().getPluginPreferences().getBoolean(
				ClientPlugin.PREF_STORE_PASSWORD);
	}

	protected void okPressed() {
		saveDialogSettings();
		super.okPressed();
	}

	private IDialogSettings getDialogSettings() {
		if (dialogSettings == null) {
			dialogSettings = ClientPlugin.getDefault().getDialogSettings();
		}

		return dialogSettings;
	}

	private class ECFProviderLabelProvider implements ITableLabelProvider {

		public Image getColumnImage(Object element, int columnIndex) {
			if (columnIndex == 0) {
				// TODO: If the container description contains an image for the
				// provider, display it here.
				return ClientPlugin.getDefault().getImageRegistry().get(
						ClientPluginConstants.DECORATION_DEFAULT_PROVIDER);
			}

			return null;
		}

		public String getColumnText(Object element, int columnIndex) {
			ContainerTypeDescription desc = (ContainerTypeDescription) element;
			switch (columnIndex) {
			case 0:
				return desc.getDescription();
			case 1:
				return desc.getName();
			}

			return ""; //$NON-NLS-1$
		}

		public void addListener(ILabelProviderListener listener) {
		}

		public void dispose() {
		}

		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		public void removeListener(ILabelProviderListener listener) {
		}

	}

	private class ProviderSelector implements ISelectionChangedListener {

		public void selectionChanged(SelectionChangedEvent event) {
			StructuredSelection selection = (StructuredSelection) event
					.getSelection();
			ContainerTypeDescription desc = (ContainerTypeDescription) selection
					.getFirstElement();
			if (desc != null) {
				containerType = desc.getName();
				// XXX replace with access to desc.getParameters()
				createPropertyComposite(paramComp, null);
				// createPropertyComposite(paramComp, desc.getProperties());
			}
		}

		protected void createPropertyComposite(Composite parent, Map properties) {
			if (properties != null) {
				String usePassword = (String) properties
						.get(USEPASSWORD_PROP_NAME);
				String examplegroupid = (String) properties
						.get(EXAMPLEGROUPID_PROP_NAME);
				String useNickname = (String) properties
						.get(USENICKNAME_PROP_NAME);
				urlPrefix = (String) properties.get(URLPREFIX_NAME);
				namespace = (String) properties.get(NAMESPACE_PROP_NAME);

				if (urlPrefix == null) {
					urlPrefix = ""; //$NON-NLS-1$
				}

				removeChildren(parent);

				String groupLabel = (String) properties
						.get(GROUPIDLABEL_PROP_NAME);

				Label groupIDLabel = new Label(parent, SWT.NONE);

				if (groupLabel != null) {
					groupIDLabel.setText(groupLabel);
				} else {
					groupIDLabel.setText(JOINGROUP_FIELDNAME);
				}

				joingroup_text = new Text(parent, SWT.BORDER);
				joingroup_text.setLayoutData(new GridData(
						GridData.FILL_HORIZONTAL));
				joingroup_text.addModifyListener(listener);

				if (examplegroupid != null) {
					Label example_label = new Label(parent, SWT.NONE);
					// set examplegroupid text
					example_label
							.setText((examplegroupid != null) ? examplegroupid
									: ""); //$NON-NLS-1$
					example_label.setLayoutData(new GridData(
							GridData.HORIZONTAL_ALIGN_END));
					// joingroup_text.setText((defaultgroupid != null) ?
					// defaultgroupid : "");
				}

				// turn off password unless used
				if (usePassword != null) {
					Label password_label = new Label(parent, SWT.NONE);
					password_label.setText(Messages.ConnectionDialog_PASSWORD_TEXT);
					password_text = new Text(parent, SWT.BORDER);
					password_text.setLayoutData(new GridData(
							GridData.FILL_HORIZONTAL));
					password_text.setEchoChar('*');
					password_text.addModifyListener(listener);
				}

				// turn off nickname unless used
				if (useNickname != null) {
					Label nickname_label = new Label(parent, SWT.NONE);
					nickname_label.setText(NICKNAME_FIELDNAME);

					nickname_text = new Text(parent, SWT.BORDER);
					nickname_text.setLayoutData(new GridData(
							GridData.FILL_HORIZONTAL));
					nickname_text.addModifyListener(listener);
				}
				autoLogin.setSelection(false);
				parent.layout();
			}
		}

		private void removeChildren(Composite composite) {
			if (composite != null && composite.getChildren() != null) {
				while (composite.getChildren().length > 0) {
					Control child = composite.getChildren()[0];
					if (child instanceof Composite) {
						removeChildren((Composite) child);
					}
					child.dispose();
				}
			}
		}
	}

	private class GlobalModifyListener implements ModifyListener {

		public void modifyText(ModifyEvent e) {
			if (password_text != null && !password_text.isDisposed()) {
				password = password_text.getText();
			}

			if (nickname_text != null && !nickname_text.isDisposed()) {
				nickname = nickname_text.getText();
			}

			if (joingroup_text != null && !joingroup_text.isDisposed()) {
				joinGroup = joingroup_text.getText();
			}
		}

	}
}

Back to the top