Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3973046ddd83d399530f5f43403e316160569499 (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
/*******************************************************************************
 * Copyright (c) 2016 Red Hat.
 * 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:
 *     Red Hat - Initial Contribution
 *******************************************************************************/
package org.eclipse.linuxtools.internal.docker.ui.preferences;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ColumnWeightData;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableLayout;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.Window;
import org.eclipse.linuxtools.docker.core.IRegistryAccount;
import org.eclipse.linuxtools.internal.docker.core.RegistryAccountManager;
import org.eclipse.linuxtools.internal.docker.ui.wizards.RegistryAccountDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
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.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;

/**
 * @since 2.0
 */
public class DockerRegistryAccountPreferencePage extends PreferencePage
		implements IWorkbenchPreferencePage, Listener {

	/**
	 * @since 2.1
	 */
	public static final String ACCOUNT_PREFERENCE_PAGE_ID = "org.eclipse.linuxtools.docker.ui.preferences.DockerRegistryAccountPreferencePage"; //$NON-NLS-1$

	/**
	 * Inner class to keep track of password modifications (without committing
	 * them to the keyring) while the user modifies the preferences.
	 */
	protected class PasswordModification {
		protected static final int ADD = 1;
		protected static final int DELETE = 2;

		protected int changeFlag;
		protected IRegistryAccount info;

		protected PasswordModification(int changeFlag,
				IRegistryAccount info) {
			this.changeFlag = changeFlag;
			this.info = info;
		}
	}

	private final class PasswordContentProvider
			implements IStructuredContentProvider, ITableLabelProvider {

		/**
		 * @see org.eclipse.jface.viewers.IStructuredContentProvider#getElements(Object)
		 */
		@Override
		public Object[] getElements(Object inputElement) {
			return passwords.toArray();
		}

		/**
		 * @see org.eclipse.jface.viewers.IContentProvider#dispose()
		 */
		@Override
		public void dispose() {
		}

		/**
		 * @see org.eclipse.jface.viewers.IContentProvider#inputChanged(Viewer,
		 *      Object, Object)
		 */
		@Override
		public void inputChanged(Viewer viewer, Object oldInput,
				Object newInput) {
		}

		/**
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnImage(Object,
		 *      int)
		 */
		@Override
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}

		/**
		 * @see org.eclipse.jface.viewers.ITableLabelProvider#getColumnText(Object,
		 *      int)
		 */
		@Override
		public String getColumnText(Object element, int columnIndex) {
			if (element instanceof IRegistryAccount) {
				IRegistryAccount info = (IRegistryAccount) element;
				switch (columnIndex) {
				case 0:
					return info.getServerAddress();
				case 1:
					return info.getUsername();
				case 2:
					return info.getEmail();
				}
			}

			// Should never get here
			return null;
		}

		/**
		 * @see org.eclipse.jface.viewers.IBaseLabelProvider#addListener(ILabelProviderListener)
		 */
		@Override
		public void addListener(ILabelProviderListener listener) {
		}

		/**
		 * @see org.eclipse.jface.viewers.IBaseLabelProvider#isLabelProperty(Object,
		 *      String)
		 */
		@Override
		public boolean isLabelProperty(Object element, String property) {
			return false;
		}

		/**
		 * @see org.eclipse.jface.viewers.IBaseLabelProvider#removeListener(ILabelProviderListener)
		 */
		@Override
		public void removeListener(ILabelProviderListener listener) {
		}

	}

	public DockerRegistryAccountPreferencePage() {
		noDefaultAndApplyButton();
		provider = new PasswordContentProvider();
		modifications = new ArrayList<>();
	}

	public DockerRegistryAccountPreferencePage(String title) {
		super(title);
	}

	public DockerRegistryAccountPreferencePage(String title,
			ImageDescriptor image) {
		super(title, image);
	}

	// SWT Widgets and content providers
	private Table pwdTable;
	private TableViewer pwdTableViewer;
	private PasswordContentProvider provider;
	private Button addButton, changeButton, removeButton;

	// List of information for table
	private List<IRegistryAccount> passwords;

	// List to keep track of additions / deletions / changes. We need to
	// keep track of these until the user decides whether to cancel the
	// preference
	// page (and we forget about the changes) or press ok (and we commit the
	// changes)
	private List<PasswordModification> modifications;

	/**
	 * @see org.eclipse.jface.preference.PreferencePage#createContents(Composite)
	 */
	@Override
	protected Control createContents(Composite parent) {

		Composite page = createComposite(parent, 1, 2, false, null, -1, -1, GridData.FILL);
		GridData gd = (GridData) page.getLayoutData();
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;

		// SystemWidgetHelpers.createLabel(page,
		// SystemResources.RESID_PREF_SIGNON_DESCRIPTION, 2);

		// Password table
		pwdTable = new Table(page, SWT.FULL_SELECTION | SWT.MULTI | SWT.V_SCROLL
				| SWT.H_SCROLL | SWT.BORDER);
		pwdTable.setLinesVisible(true);
		pwdTable.setHeaderVisible(true);
		pwdTable.addListener(SWT.Selection, this);

		TableLayout tableLayout = new TableLayout();
		tableLayout.addColumnData(new ColumnWeightData(100, true));
		tableLayout.addColumnData(new ColumnWeightData(100, true));
		tableLayout.addColumnData(new ColumnWeightData(100, true));
		pwdTable.setLayout(tableLayout);

		gd = new GridData(GridData.FILL_BOTH);
		gd.grabExcessHorizontalSpace = true;
		gd.grabExcessVerticalSpace = true;

		pwdTable.setLayoutData(gd);

		// Server Address column
		TableColumn hostnameColumn = new TableColumn(pwdTable, SWT.NONE);
		hostnameColumn
				.setText("Server Address");

		// Username column
		TableColumn sysTypeColumn = new TableColumn(pwdTable, SWT.NONE);
		sysTypeColumn.setText("Username");

		// Email column
		TableColumn useridColumn = new TableColumn(pwdTable, SWT.NONE);
		useridColumn.setText("Email");

		pwdTableViewer = new TableViewer(pwdTable);
		pwdTableViewer.setContentProvider(provider);
		pwdTableViewer.setLabelProvider(provider);
		pwdTableViewer.setInput(passwords);

		// Create the Button bar for add, change and remove
		Composite buttonBar = createComposite(page, 1, 1, false, null, -1, -1, GridData.FILL);
		gd = (GridData) buttonBar.getLayoutData();
		gd.grabExcessHorizontalSpace = false;
		gd.grabExcessVerticalSpace = true;

		addButton = createPushButton(buttonBar, this,
				"Add",
				"Add a new Docker registry account.");
		changeButton = createPushButton(buttonBar, this,
				"Edit",
				"Edit an existing Docker registry account.");
		removeButton = createPushButton(buttonBar, this,
				"Remove",
				"Remove an existing Docker registry account.");

		changeButton.setEnabled(false);
		removeButton.setEnabled(false);
		return parent;
	}

	/**
	 * @see org.eclipse.ui.IWorkbenchPreferencePage#init(IWorkbench)
	 */
	@Override
	public void init(IWorkbench workbench) {
		// get a local copy of the accounts
		passwords = new ArrayList<>(
				RegistryAccountManager.getInstance().getAccounts());

		// refresh password table
		if (pwdTableViewer != null) {
			pwdTableViewer.refresh();
		}
	}

	/**
	 * @see org.eclipse.swt.widgets.Listener#handleEvent(Event)
	 */
	@Override
	public void handleEvent(Event event) {
		if (event.type == SWT.Selection) {
			if (event.widget == addButton) {
				RegistryAccountDialog dialog = new RegistryAccountDialog(
						getShell(),
						"New Registry Account");
				if (dialog.open() == Window.OK) {
					IRegistryAccount info = dialog
							.getSignonInformation();
					passwords.add(info);
					modifications.add(new PasswordModification(
							PasswordModification.ADD, info));

					pwdTableViewer.refresh();
					pwdTable.select(passwords.size() - 1); // select the new
															// entry
				}

			} else if (event.widget == changeButton) {
				RegistryAccountDialog dialog = new RegistryAccountDialog(
						getShell(),
						"Edit Registry Account");
				int index = pwdTable.getSelectionIndex();
				IRegistryAccount info = passwords
						.get(index);
				dialog.setInputData(info);
				if (dialog.open() == Window.OK) {
					// Remove old and add new
					info = dialog.getSignonInformation();
					IRegistryAccount oldInfo = passwords
							.remove(index);
					passwords.add(index, info);

					modifications.add(new PasswordModification(
							PasswordModification.DELETE, oldInfo));
					modifications.add(new PasswordModification(
							PasswordModification.ADD, info));

					pwdTableViewer.refresh();
					pwdTable.select(index);
				}

			} else if (event.widget == removeButton) {
				int[] indicies = pwdTable.getSelectionIndices();
				for (int idx = indicies.length - 1; idx >= 0; idx--) {
					RegistryAccountManager.getInstance()
							.remove(passwords.get(indicies[idx]));
					modifications.add(new PasswordModification(
							PasswordModification.DELETE,
							passwords
									.remove(indicies[idx])));
				}

				pwdTableViewer.refresh();
			}

			// Update table buttons based on changes
			switch (pwdTable.getSelectionCount()) {
			case 0:
				changeButton.setEnabled(false);
				removeButton.setEnabled(false);
				break;

			case 1:
				changeButton.setEnabled(true);
				removeButton.setEnabled(true);
				break;

			default:
				changeButton.setEnabled(false);
				removeButton.setEnabled(true);
				break;
			}
		}
	}

	/**
	 * @see org.eclipse.jface.preference.IPreferencePage#performOk()
	 */
	@Override
	public boolean performOk() {

		if (modifications.size() > 0) {
			PasswordModification mod;
			RegistryAccountManager manager = RegistryAccountManager
					.getInstance();

			for (int i = 0; i < modifications.size(); i++) {
				mod = modifications.get(i);

				if (mod.changeFlag == PasswordModification.ADD) {
					manager.add(mod.info);
				} else if (mod.changeFlag == PasswordModification.DELETE) {
					manager.remove(mod.info);
				}
			}

			modifications.clear();
		}

		return super.performOk();
	}

	/**
	 * @see org.eclipse.jface.preference.IPreferencePage#performCancel()
	 */
	@Override
	public boolean performCancel() {
		modifications.clear();
		return super.performCancel();
	}

	private static Composite createComposite(Composite parent, int parentSpan, int numColumns, boolean border, String label, int marginSize, int spacingSize, int verticalAlignment) {
		//border = true;
		boolean borderNeeded = border;
		if (label != null)
			borderNeeded = true; // force the case
		int style = SWT.NULL;
		if (borderNeeded)
			style |= SWT.SHADOW_ETCHED_IN;
		Composite composite = null;
		if (borderNeeded) {
			composite = new Group(parent, style);
			if (label != null)
				 ((Group) composite).setText(label);
		} else {
			composite = new Composite(parent, style);
		}
		//GridLayout
		GridLayout layout = new GridLayout();
		layout.numColumns = numColumns;
		if (marginSize != -1) {
			layout.marginWidth = 0;
			layout.marginHeight = 0;
		}
		if (spacingSize != -1) {
			layout.horizontalSpacing = 0;
			layout.verticalSpacing = 0;
		}
		composite.setLayout(layout);
		//GridData
		GridData data = new GridData();
		data.horizontalSpan = parentSpan;
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;

		data.verticalAlignment = verticalAlignment;
		data.grabExcessVerticalSpace = false;

		composite.setLayoutData(data);
		return composite;
	}

	public static Button createPushButton(Composite group, Listener listener, String label, String tooltip) {
		Button button = new Button(group, SWT.PUSH);
		button.setText(label);
		if (listener != null)
			button.addListener(SWT.Selection, listener);
		GridData data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.grabExcessHorizontalSpace = true;
		button.setLayoutData(data);
		if (tooltip != null)
			button.setToolTipText(tooltip);
		return button;
	}

}

Back to the top