KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > PasswordManagementPreferencePage


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.team.internal.ccvs.ui;
12  
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.preference.PreferencePage;
19 import org.eclipse.jface.viewers.*;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.TableEditor;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.layout.GridData;
24 import org.eclipse.swt.layout.GridLayout;
25 import org.eclipse.swt.widgets.*;
26 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
27 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
28 import org.eclipse.team.internal.ui.PixelConverter;
29 import org.eclipse.team.internal.ui.SWTUtils;
30 import org.eclipse.ui.*;
31
32 public class PasswordManagementPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
33     private TableViewer viewer;
34     private Button removeButton;
35     private Button removeAllButton;
36     
37     class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
38         public String JavaDoc getColumnText(Object JavaDoc element, int columnIndex) {
39             ICVSRepositoryLocation entry = (ICVSRepositoryLocation)element;
40             switch (columnIndex) {
41                 case 0:
42                     return entry.toString();
43                 case 1:
44                     return entry.getUsername();
45                 default:
46                     return null;
47             }
48         }
49         public Image getColumnImage(Object JavaDoc element, int columnIndex) {
50             return null;
51         }
52     };
53         
54     public void init(IWorkbench workbench) {
55         setDescription(CVSUIMessages.PasswordManagementPreferencePage_2);
56     }
57     
58     /**
59      * Creates preference page controls on demand.
60      *
61      * @param parent the parent for the preference page
62      */

63     protected Control createContents(Composite ancestor) {
64         
65         Composite parent = new Composite(ancestor, SWT.NULL);
66         GridLayout layout = new GridLayout();
67         layout.marginWidth = 0;
68         layout.marginHeight = 0;
69         layout.numColumns = 2;
70         parent.setLayout(layout);
71         GridData data = new GridData();
72         data.verticalAlignment = GridData.FILL;
73         data.horizontalAlignment = GridData.FILL;
74         parent.setLayoutData(data);
75         
76         viewer = new TableViewer(parent, SWT.MULTI | SWT.FULL_SELECTION | SWT.H_SCROLL | SWT.V_SCROLL | SWT.BORDER);
77         Table table = viewer.getTable();
78         new TableEditor(table);
79         table.setHeaderVisible(true);
80         table.setLinesVisible(true);
81         GridData gd = new GridData(GridData.FILL_BOTH);
82         table.setLayoutData(gd);
83         table.addListener(SWT.Selection, new Listener() {
84             public void handleEvent(Event e) {
85                 handleSelection();
86             }
87         });
88         // Create the table columns
89
new TableColumn(table, SWT.NULL);
90         new TableColumn(table, SWT.NULL);
91         TableColumn[] columns = table.getColumns();
92         columns[0].setText(CVSUIMessages.PasswordManagementPreferencePage_3);
93         columns[1].setText(CVSUIMessages.PasswordManagementPreferencePage_4);
94         viewer.setColumnProperties(new String JavaDoc[] {"location", "username"}); //$NON-NLS-1$ //$NON-NLS-2$
95
viewer.setLabelProvider(new TableLabelProvider());
96         viewer.setContentProvider(new IStructuredContentProvider() {
97             public void dispose() {
98             }
99             public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
100             }
101             public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
102                 if (inputElement == null) return null;
103                 ICVSRepositoryLocation[] locations = ((KnownRepositories)inputElement).getRepositories();
104                 List JavaDoc repos = new ArrayList JavaDoc();
105                 for (int i = 0; i < locations.length; i++) {
106                     ICVSRepositoryLocation l = locations[i];
107                     if(l.getUserInfoCached())
108                         repos.add(l);
109                 }
110                 return (ICVSRepositoryLocation[]) repos.toArray(new ICVSRepositoryLocation[repos.size()]);
111             }
112         });
113         TableLayout tl = new TableLayout();
114         Dialog.applyDialogFont(ancestor);
115         PixelConverter converter = new PixelConverter(table);
116         tl.addColumnData(new ColumnWeightData(70, converter.convertWidthInCharsToPixels(40)));
117         tl.addColumnData(new ColumnWeightData(30, converter.convertWidthInCharsToPixels(12)));
118         table.setLayout(tl);
119         
120         Composite buttons = new Composite(parent, SWT.NULL);
121         buttons.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING));
122         layout = new GridLayout();
123         layout.marginHeight = 0;
124         layout.marginWidth = 0;
125         buttons.setLayout(layout);
126         
127         removeButton = new Button(buttons, SWT.PUSH);
128         removeButton.setText(CVSUIMessages.PasswordManagementPreferencePage_5);
129         removeButton.setEnabled(false);
130         removeButton.addListener(SWT.Selection, new Listener() {
131             public void handleEvent(Event e) {
132                 remove();
133             }
134         });
135         removeAllButton = new Button(buttons, SWT.PUSH);
136         removeAllButton.setText(CVSUIMessages.PasswordManagementPreferencePage_6);
137         removeAllButton.setEnabled(true);
138         removeAllButton.addListener(SWT.Selection, new Listener() {
139             public void handleEvent(Event e) {
140                 removeAll();
141             }
142         });
143         
144         // Use the same size for the buttons and ensure they conform to the proper minimum size
145
int buttonWidth = SWTUtils.calculateControlSize(SWTUtils.createDialogPixelConverter(parent), new Button [] { removeButton, removeAllButton });
146         data = SWTUtils.createGridData(buttonWidth, SWT.DEFAULT, SWT.END, SWT.CENTER, false, false);
147         removeButton.setLayoutData(data);
148         removeAllButton.setLayoutData(data);
149         
150         Dialog.applyDialogFont(ancestor);
151         viewer.setInput(KnownRepositories.getInstance());
152         handleSelection();
153         
154         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.PASSWORD_MANAGEMENT_PAGE);
155         
156         return parent;
157     }
158     
159     public boolean performOk() {
160         return true;
161     }
162     
163     protected void performDefaults() {
164         super.performDefaults();
165     }
166     
167     private void remove() {
168         IStructuredSelection s = (IStructuredSelection)viewer.getSelection();
169         for (Iterator JavaDoc it = s.iterator(); it.hasNext();) {
170             ICVSRepositoryLocation location = (ICVSRepositoryLocation) it.next();
171             location.flushUserInfo();
172         }
173         viewer.refresh();
174         handleSelection();
175     }
176     
177     private void removeAll() {
178         ICVSRepositoryLocation[] locations = KnownRepositories.getInstance().getRepositories();
179         for (int i = 0; i < locations.length; i++) {
180             ICVSRepositoryLocation l = locations[i];
181             if(l.getUserInfoCached())
182                 l.flushUserInfo();
183         }
184         viewer.refresh();
185         handleSelection();
186     }
187     
188     private void handleSelection() {
189         if (viewer.getTable().getSelectionCount() > 0) {
190             removeButton.setEnabled(true);
191         } else {
192             removeButton.setEnabled(false);
193         }
194         removeAllButton.setEnabled(viewer.getTable().getItemCount() > 0);
195     }
196 }
197
Popular Tags