Skip to main content
summaryrefslogtreecommitdiffstats
blob: 5aa52dc359e391b4a9f8798fb9fe6dee853492fd (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
/*******************************************************************************
 * Copyright (c) 2000, 2004 IBM Corporation 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:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.team.internal.ccvs.ui;
 
import java.util.*;
import java.util.List;
import org.eclipse.jface.dialogs.Dialog;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.jface.preference.PreferencePage;
import org.eclipse.jface.viewers.*;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TableEditor;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.*;
import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchPreferencePage;
import org.eclipse.ui.help.WorkbenchHelp;

public class PasswordManagementPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
	private TableViewer viewer;
	private Button removeButton;
	private Button removeAllButton;
	
	class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
		public String getColumnText(Object element, int columnIndex) {
			ICVSRepositoryLocation entry = (ICVSRepositoryLocation)element;
			switch (columnIndex) {
				case 0:
					return entry.toString();
				case 1:
					return entry.getUsername();
				default:
					return null;
			}
		}
		public Image getColumnImage(Object element, int columnIndex) {
			return null;
		}
	};
		
	public void init(IWorkbench workbench) {
		setDescription(CVSUIMessages.PasswordManagementPreferencePage_2); //$NON-NLS-1$
	}
	
	/**
	 * Creates preference page controls on demand.
	 *
	 * @param parent  the parent for the preference page
	 */
	protected Control createContents(Composite ancestor) {
		
		Composite parent = new Composite(ancestor, SWT.NULL);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		layout.numColumns = 2;
		parent.setLayout(layout);
		GridData data = new GridData();
		data.verticalAlignment = GridData.FILL;
		data.horizontalAlignment = GridData.FILL;
		parent.setLayoutData(data);
		
		viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
		Table table = viewer.getTable();
		new TableEditor(table);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		GridData gd = new GridData(GridData.FILL_BOTH);
		table.setLayoutData(gd);
		table.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				handleSelection();
			}
		});
		// Create the table columns
		new TableColumn(table, SWT.NULL);
		new TableColumn(table, SWT.NULL);
		TableColumn[] columns = table.getColumns();
		columns[0].setText(CVSUIMessages.PasswordManagementPreferencePage_3);  //$NON-NLS-1$
		columns[1].setText(CVSUIMessages.PasswordManagementPreferencePage_4);  //$NON-NLS-1$
		viewer.setColumnProperties(new String[] {"location", "username"}); //$NON-NLS-1$ //$NON-NLS-2$
		viewer.setLabelProvider(new TableLabelProvider());
		viewer.setContentProvider(new IStructuredContentProvider() {
			public void dispose() {
			}
			public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
			}
			public Object[] getElements(Object inputElement) {
				if (inputElement == null) return null;
				ICVSRepositoryLocation[] locations = ((KnownRepositories)inputElement).getRepositories();
				List repos = new ArrayList();
				for (int i = 0; i < locations.length; i++) {
					ICVSRepositoryLocation l = locations[i];
					if(l.getUserInfoCached()) 
						repos.add(l);
				}
				return (ICVSRepositoryLocation[]) repos.toArray(new ICVSRepositoryLocation[repos.size()]);
			}
		});
		TableLayout tl = new TableLayout();
		tl.addColumnData(new ColumnWeightData(70, 100));
		tl.addColumnData(new ColumnWeightData(30));
		table.setLayout(tl);
		
		Composite buttons = new Composite(parent, SWT.NULL);
		buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
		layout = new GridLayout();
		layout.marginHeight = 0;
		layout.marginWidth = 0;
		buttons.setLayout(layout);
		
		removeButton = new Button(buttons, SWT.PUSH);
		removeButton.setText(CVSUIMessages.PasswordManagementPreferencePage_5);  //$NON-NLS-1$
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
		int widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		data.widthHint = Math.max(widthHint, removeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
		removeButton.setLayoutData(data);
		removeButton.setEnabled(false);
		removeButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				remove();
			}
		});
		removeAllButton = new Button(buttons, SWT.PUSH);
		removeAllButton.setText(CVSUIMessages.PasswordManagementPreferencePage_6);  //$NON-NLS-1$
		data = new GridData();
		data.horizontalAlignment = GridData.FILL;
		data.heightHint = convertVerticalDLUsToPixels(IDialogConstants.BUTTON_HEIGHT);
		widthHint = convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
		data.widthHint = Math.max(widthHint, removeButton.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
		removeAllButton.setLayoutData(data);
		removeAllButton.setEnabled(true);
		removeAllButton.addListener(SWT.Selection, new Listener() {
			public void handleEvent(Event e) {
				removeAll();
			}
		});
		Dialog.applyDialogFont(ancestor);
		viewer.setInput(KnownRepositories.getInstance());
		handleSelection();
		
		WorkbenchHelp.setHelp(getControl(), IHelpContextIds.PASSWORD_MANAGEMENT_PAGE);
		
		return parent;
	}
	
	public boolean performOk() {
		return true;
	}
	
	protected void performDefaults() {
		super.performDefaults();
	}
	
	private void remove() {
		IStructuredSelection s = (IStructuredSelection)viewer.getSelection();
		for (Iterator it = s.iterator(); it.hasNext();) {
			ICVSRepositoryLocation location = (ICVSRepositoryLocation) it.next();
			location.flushUserInfo();
		}
		viewer.refresh();
		handleSelection();
	}
	
	private void removeAll() {
		ICVSRepositoryLocation[] locations = KnownRepositories.getInstance().getRepositories();
		for (int i = 0; i < locations.length; i++) {
			ICVSRepositoryLocation l = locations[i];
			if(l.getUserInfoCached()) 
				l.flushUserInfo();
		}
		viewer.refresh();
		handleSelection();
	}
	
	private void handleSelection() {
		if (viewer.getTable().getSelectionCount() > 0) {
			removeButton.setEnabled(true);
		} else {
			removeButton.setEnabled(false);
		}
		removeAllButton.setEnabled(viewer.getTable().getItemCount() > 0);
	}
}

Back to the top