KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > repo > CVSRepositoryPropertiesPage


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.repo;
12
13 import java.lang.reflect.InvocationTargetException JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Properties JavaDoc;
18
19 import org.eclipse.core.resources.IProject;
20 import org.eclipse.core.resources.ResourcesPlugin;
21 import org.eclipse.core.runtime.*;
22 import org.eclipse.jface.dialogs.*;
23 import org.eclipse.jface.dialogs.Dialog;
24 import org.eclipse.jface.util.PropertyChangeEvent;
25 import org.eclipse.jface.window.Window;
26 import org.eclipse.osgi.util.NLS;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.events.SelectionAdapter;
29 import org.eclipse.swt.events.SelectionEvent;
30 import org.eclipse.swt.layout.GridData;
31 import org.eclipse.swt.layout.GridLayout;
32 import org.eclipse.swt.widgets.*;
33 import org.eclipse.team.core.RepositoryProvider;
34 import org.eclipse.team.core.TeamException;
35 import org.eclipse.team.internal.ccvs.core.*;
36 import org.eclipse.team.internal.ccvs.core.connection.CVSRepositoryLocation;
37 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
38 import org.eclipse.team.internal.ccvs.ui.*;
39 import org.eclipse.team.internal.ccvs.ui.Policy;
40 import org.eclipse.team.internal.ccvs.ui.wizards.ConfigurationWizardMainPage;
41 import org.eclipse.team.internal.ui.dialogs.DetailsDialogWithProjects;
42 import org.eclipse.ui.PlatformUI;
43 import org.eclipse.ui.actions.WorkspaceModifyOperation;
44 import org.eclipse.ui.dialogs.PropertyPage;
45
46 public class CVSRepositoryPropertiesPage extends PropertyPage {
47     private static final String JavaDoc FAKE_PASSWORD = "*********"; //$NON-NLS-1$
48
ICVSRepositoryLocation location;
49     
50     // Widgets
51
Text userText;
52     Text passwordText;
53     Combo methodType;
54     Text hostText;
55     Text pathText;
56     // Port
57
private Text portText;
58     private Button useDefaultPort;
59     private Button useCustomPort;
60     
61     // Caching password
62
private Button allowCachingButton;
63     private boolean allowCaching = false;
64     
65     boolean passwordChanged;
66     boolean connectionInfoChanged;
67
68     IUserInfo info;
69
70     // Label
71
private Button useLocationAsLabel;
72     private Button useCustomLabel;
73     private Text labelText;
74             
75     /*
76      * @see PreferencesPage#createContents
77      */

78     protected Control createContents(Composite parent) {
79         initialize();
80         
81         Composite composite = new Composite(parent, SWT.NULL);
82         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
83         GridLayout layout = new GridLayout();
84         layout.numColumns = 3;
85         composite.setLayout(layout);
86
87         // Repository Label
88
// create a composite to ensure the radio buttons come in the correct order
89
Composite labelGroup = new Composite(composite, SWT.NONE);
90         GridData data = new GridData();
91         data.horizontalSpan = 3;
92         labelGroup.setLayoutData(data);
93         layout = new GridLayout();
94         layout.numColumns = 3;
95         layout.marginHeight = 0;
96         layout.marginWidth = 0;
97         labelGroup.setLayout(layout);
98         Listener labelListener = new Listener() {
99             public void handleEvent(Event event) {
100                 updateWidgetEnablements();
101             }
102         };
103         useLocationAsLabel = createRadioButton(labelGroup, CVSUIMessages.CVSRepositoryPropertiesPage_useLocationAsLabel, 3);
104         useCustomLabel = createRadioButton(labelGroup, CVSUIMessages.CVSRepositoryPropertiesPage_useCustomLabel, 1);
105         useCustomLabel.addListener(SWT.Selection, labelListener);
106         labelText = createTextField(labelGroup);
107         labelText.addListener(SWT.Modify, labelListener);
108         
109         // Add some extra space
110
createLabel(composite, "", 3); //$NON-NLS-1$
111

112         createLabel(composite, CVSUIMessages.CVSPropertiesPage_connectionType, 1);
113         methodType = createCombo(composite);
114         
115         createLabel(composite, CVSUIMessages.CVSPropertiesPage_user, 1);
116         userText = createTextField(composite);
117         
118         createLabel(composite, CVSUIMessages.CVSPropertiesPage_password, 1);
119         passwordText = createPasswordField(composite);
120             
121         createLabel(composite, CVSUIMessages.CVSPropertiesPage_host, 1);
122         hostText = createTextField(composite);
123         
124         createLabel(composite, CVSUIMessages.CVSPropertiesPage_path, 1);
125         pathText = createTextField(composite);
126         
127         // Port number
128
// create a composite to ensure the radio buttons come in the correct order
129
Composite portGroup = new Composite(composite, SWT.NONE);
130         data = new GridData();
131         data.horizontalSpan = 3;
132         portGroup.setLayoutData(data);
133         layout = new GridLayout();
134         layout.numColumns = 3;
135         layout.marginHeight = 0;
136         layout.marginWidth = 0;
137         portGroup.setLayout(layout);
138         useDefaultPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_useDefaultPort, 3);
139         useCustomPort = createRadioButton(portGroup, CVSUIMessages.ConfigurationWizardMainPage_usePort, 1);
140         portText = createTextField(portGroup);
141
142         // Add some extra space
143
createLabel(composite, "", 3); //$NON-NLS-1$
144

145         allowCachingButton = new Button(composite, SWT.CHECK);
146         allowCachingButton.setText(CVSUIMessages.UserValidationDialog_6);
147         data = new GridData(GridData.FILL_HORIZONTAL | GridData.GRAB_HORIZONTAL);
148         data.horizontalSpan = 3;
149         allowCachingButton.setLayoutData(data);
150         allowCachingButton.addSelectionListener(new SelectionAdapter() {
151             public void widgetSelected(SelectionEvent e) {
152                 allowCaching = allowCachingButton.getSelection();
153             }
154         });
155     
156         Composite warningComposite = new Composite(composite, SWT.NONE);
157         layout = new GridLayout();
158         layout.numColumns = 2;
159         layout.marginHeight = 0;
160         layout.marginHeight = 0;
161         warningComposite.setLayout(layout);
162         data = new GridData(GridData.FILL_HORIZONTAL);
163         data.horizontalSpan = 3;
164         warningComposite.setLayoutData(data);
165         Label warningLabel = new Label(warningComposite, SWT.NONE);
166         warningLabel.setImage(Dialog.getImage(Dialog.DLG_IMG_MESSAGE_WARNING));
167         warningLabel.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_BEGINNING));
168         Label warningText = new Label(warningComposite, SWT.WRAP);
169         warningText.setText(CVSUIMessages.UserValidationDialog_7);
170         data = new GridData(GridData.FILL_HORIZONTAL);
171         data.widthHint = 300;
172         warningText.setLayoutData(data);
173         
174         // Add some extra space
175
createLabel(composite, "", 3); //$NON-NLS-1$
176

177         initializeValues();
178         updateWidgetEnablements();
179         Listener connectionInfoChangedListener = new Listener() {
180             public void handleEvent(Event event) {
181                 connectionInfoChanged = true;
182                 updateWidgetEnablements();
183             }
184         };
185         passwordText.addListener(SWT.Modify, new Listener() {
186             public void handleEvent(Event event) {
187                 passwordChanged = !passwordText.getText().equals(FAKE_PASSWORD);
188             }
189         });
190         userText.addListener(SWT.Modify, connectionInfoChangedListener);
191         methodType.addListener(SWT.Modify, connectionInfoChangedListener);
192         hostText.addListener(SWT.Modify, connectionInfoChangedListener);
193         portText.addListener(SWT.Modify, connectionInfoChangedListener);
194         useCustomPort.addListener(SWT.Selection, connectionInfoChangedListener);
195         pathText.addListener(SWT.Modify, connectionInfoChangedListener);
196         
197         PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IHelpContextIds.REPOSITORY_LOCATION_PROPERTY_PAGE);
198         Dialog.applyDialogFont(parent);
199         return composite;
200     }
201     
202     /**
203      * Utility method that creates a combo box
204      *
205      * @param parent the parent for the new label
206      * @return the new widget
207      */

208     protected Combo createCombo(Composite parent) {
209         Combo combo = new Combo(parent, SWT.READ_ONLY);
210         GridData data = new GridData(GridData.FILL_HORIZONTAL);
211         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
212         data.horizontalSpan = 2;
213         combo.setLayoutData(data);
214         return combo;
215     }
216     /**
217      * Utility method that creates a label instance
218      * and sets the default layout data.
219      *
220      * @param parent the parent for the new label
221      * @param text the text for the new label
222      * @return the new label
223      */

224     protected Label createLabel(Composite parent, String JavaDoc text, int span) {
225         Label label = new Label(parent, SWT.LEFT);
226         label.setText(text);
227         GridData data = new GridData();
228         data.horizontalSpan = span;
229         data.horizontalAlignment = GridData.FILL;
230         label.setLayoutData(data);
231         return label;
232     }
233     /**
234      * Create a text field specific for this application
235      *
236      * @param parent the parent of the new text field
237      * @return the new text field
238      */

239     protected Text createTextField(Composite parent) {
240         Text text = new Text(parent, SWT.SINGLE | SWT.BORDER);
241         return layoutTextField(text);
242     }
243     /**
244      * Create a password field specific for this application
245      *
246      * @param parent the parent of the new text field
247      * @return the new text field
248      */

249     protected Text createPasswordField(Composite parent) {
250         Text text = new Text(parent, SWT.SINGLE | SWT.BORDER | SWT.PASSWORD);
251         return layoutTextField(text);
252     }
253     /**
254      * Layout a text or password field specific for this application
255      *
256      * @param parent the parent of the new text field
257      * @return the new text field
258      */

259     protected Text layoutTextField(Text text) {
260         GridData data = new GridData(GridData.FILL_HORIZONTAL);
261         data.verticalAlignment = GridData.CENTER;
262         data.grabExcessVerticalSpace = false;
263         data.widthHint = IDialogConstants.ENTRY_FIELD_WIDTH;
264         data.horizontalSpan = 2;
265         text.setLayoutData(data);
266         return text;
267     }
268
269     /**
270      * Utility method to create a radio button
271      *
272      * @param parent the parent of the radio button
273      * @param label the label of the radio button
274      * @param span the number of columns to span
275      * @return the created radio button
276      */

277     protected Button createRadioButton(Composite parent, String JavaDoc label, int span) {
278         Button button = new Button(parent, SWT.RADIO);
279         button.setText(label);
280         GridData data = new GridData();
281         data.horizontalSpan = span;
282         button.setLayoutData(data);
283         return button;
284     }
285     /**
286      * Initializes the page
287      */

288     private void initialize() {
289         location = null;
290         IAdaptable element = getElement();
291         if (element instanceof ICVSRepositoryLocation) {
292             location = (ICVSRepositoryLocation)element;
293         } else {
294             Object JavaDoc adapter = element.getAdapter(ICVSRepositoryLocation.class);
295             if (adapter instanceof ICVSRepositoryLocation) {
296                 location = (ICVSRepositoryLocation)adapter;
297             }
298         }
299     }
300     /**
301      * Set the initial values of the widgets
302      */

303     private void initializeValues() {
304         passwordChanged = false;
305         connectionInfoChanged = false;
306         
307         IConnectionMethod[] methods = CVSRepositoryLocation.getPluggedInConnectionMethods();
308         for (int i = 0; i < methods.length; i++) {
309             methodType.add(methods[i].getName());
310         }
311         String JavaDoc method = location.getMethod().getName();
312         methodType.select(methodType.indexOf(method));
313         info = location.getUserInfo(true);
314         userText.setText(info.getUsername());
315         passwordText.setText(FAKE_PASSWORD);
316         hostText.setText(location.getHost());
317         int port = location.getPort();
318         if (port == ICVSRepositoryLocation.USE_DEFAULT_PORT) {
319             useDefaultPort.setSelection(true);
320             useCustomPort.setSelection(false);
321             portText.setEnabled(false);
322         } else {
323             useDefaultPort.setSelection(false);
324             useCustomPort.setSelection(true);
325             portText.setText("" + port); //$NON-NLS-1$
326
}
327         pathText.setText(location.getRootDirectory());
328         allowCachingButton.setSelection(location.getUserInfoCached());
329         
330         // get the repository label
331
String JavaDoc label = null;
332         RepositoryRoot root = CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryRootFor(location);
333         label = root.getName();
334         useLocationAsLabel.setSelection(label == null);
335         useCustomLabel.setSelection(!useLocationAsLabel.getSelection());
336         if (label == null) {
337             label = location.getLocation(true);
338         }
339         labelText.setText(label);
340     }
341     
342     private boolean performConnectionInfoChanges() {
343         // Set the caching mode of the location
344
if (!connectionInfoChanged) {
345             location.setAllowCaching(allowCaching);
346             if (!passwordChanged) {
347                 ((CVSRepositoryLocation)location).updateCache();
348             }
349         }
350         // Don't do anything else if there wasn't a password or connection change
351
if (!passwordChanged && !connectionInfoChanged) return true;
352         
353         try {
354             // Check if the password was the only thing to change.
355
if (passwordChanged && !connectionInfoChanged) {
356                 CVSRepositoryLocation oldLocation = (CVSRepositoryLocation)location;
357                 oldLocation.setPassword(getNewPassword());
358                 if (allowCaching) {
359                     oldLocation.updateCache();
360                 }
361                 passwordChanged = false;
362                 return true;
363             }
364         
365             // Otherwise change the connection info and the password
366
// This operation is done inside a workspace operation in case the sharing
367
// info for existing projects is changed
368
if (!(location.getHost().equals(hostText.getText()) && location.getRootDirectory().equals(pathText.getText()))) {
369                 // The host or path has changed
370
if (!MessageDialog.openConfirm(getShell(),
371                         CVSUIMessages.CVSRepositoryPropertiesPage_0,
372                         CVSUIMessages.CVSRepositoryPropertiesPage_1)) {
373                     return false;
374                 }
375             }
376             final boolean[] result = new boolean[] { false };
377             final ProgressMonitorDialog progressMonitorDialog = new ProgressMonitorDialog(getShell());
378             progressMonitorDialog.run(false, false, new WorkspaceModifyOperation(null) {
379                 public void execute(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
380                     try {
381                         // Create a new repository location with the new information
382
CVSRepositoryLocation newLocation = CVSRepositoryLocation.fromProperties(createProperties());
383                         newLocation.setEncoding(location.getEncoding());
384                         location.setAllowCaching(allowCaching);
385                         try {
386                             // For each project shared with the old location, set connection info to the new one
387
List JavaDoc projects = new ArrayList JavaDoc();
388                             IProject[] allProjects = ResourcesPlugin.getWorkspace().getRoot().getProjects();
389                             for (int i = 0; i < allProjects.length; i++) {
390                                 RepositoryProvider teamProvider = RepositoryProvider.getProvider(allProjects[i], CVSProviderPlugin.getTypeId());
391                                 if (teamProvider != null) {
392                                     CVSTeamProvider cvsProvider = (CVSTeamProvider)teamProvider;
393                                     if (cvsProvider.getCVSWorkspaceRoot().getRemoteLocation().equals(location)) {
394                                         projects.add(allProjects[i]);
395                                     }
396                                 }
397                             }
398                             if (projects.size() > 0) {
399                                 // To do: warn the user
400
DetailsDialogWithProjects dialog = new DetailsDialogWithProjects(
401                                     progressMonitorDialog.getShell(),
402                                     CVSUIMessages.CVSRepositoryPropertiesPage_Confirm_Project_Sharing_Changes_1,
403                                     CVSUIMessages.CVSRepositoryPropertiesPage_There_are_projects_in_the_workspace_shared_with_this_repository_2,
404                                     NLS.bind(CVSUIMessages.CVSRepositoryPropertiesPage_sharedProject, new String JavaDoc[] { location.toString() }),
405                                     (IProject[]) projects.toArray(new IProject[projects.size()]),
406                                     true,
407                                     Dialog.DLG_IMG_WARNING);
408                                 int r = dialog.open();
409                                 if (r != Window.OK) {
410                                     result[0] = false;
411                                     return;
412                                 }
413                                 monitor.beginTask(null, 1000 * projects.size());
414                                 try {
415                                     Iterator JavaDoc it = projects.iterator();
416                                     while (it.hasNext()) {
417                                         IProject project = (IProject)it.next();
418                                         RepositoryProvider teamProvider = RepositoryProvider.getProvider(project, CVSProviderPlugin.getTypeId());
419                                         CVSTeamProvider cvsProvider = (CVSTeamProvider)teamProvider;
420                                         cvsProvider.setRemoteRoot(newLocation, Policy.subMonitorFor(monitor, 1000));
421                                     }
422                                 } finally {
423                                     monitor.done();
424                                 }
425                             }
426                             
427                             // Dispose the old repository location
428
CVSUIPlugin.getPlugin().getRepositoryManager().replaceRepositoryLocation(location, newLocation);
429                             
430                         } finally {
431                             // Even if we failed, ensure that the new location appears in the repo view.
432
newLocation = (CVSRepositoryLocation)KnownRepositories.getInstance().addRepository(newLocation, !KnownRepositories.getInstance().isKnownRepository(newLocation.getLocation()));
433                         }
434                         
435                         // Set the location of the page to the new location in case Apply was chosen
436
location = newLocation;
437                         connectionInfoChanged = false;
438                         passwordChanged = false;
439                     } catch (TeamException e) {
440                         throw new InvocationTargetException JavaDoc(e);
441                     }
442                     result[0] = true;
443                 }
444             });
445             return result[0];
446         } catch (InvocationTargetException JavaDoc e) {
447             handle(e);
448         } catch (InterruptedException JavaDoc e) {
449         }
450         return false; /* we only get here if an exception occurred */
451     }
452     
453     private void performNonConnectionInfoChanges() {
454         recordNewLabel((CVSRepositoryLocation)location);
455     }
456     /*
457      * @see PreferencesPage#performOk
458      */

459     public boolean performOk() {
460         if (performConnectionInfoChanges()) {
461             performNonConnectionInfoChanges();
462             return true;
463         }
464         return false;
465     }
466     
467     /* (non-Javadoc)
468      * @see org.eclipse.jface.preference.PreferencePage#performDefaults()
469      */

470     protected void performDefaults() {
471         super.performDefaults();
472         initializeValues();
473     }
474     
475     /**
476      * Shows the given errors to the user.
477      */

478     protected void handle(Throwable JavaDoc e) {
479         CVSUIPlugin.openError(getShell(), null, null, e);
480     }
481     
482     /**
483      * Updates widget enablements and sets error message if appropriate.
484      */

485     protected void updateWidgetEnablements() {
486         if (useLocationAsLabel.getSelection()) {
487             labelText.setEnabled(false);
488         } else {
489             labelText.setEnabled(true);
490         }
491         if (useDefaultPort.getSelection()) {
492             portText.setEnabled(false);
493         } else {
494             portText.setEnabled(true);
495         }
496         validateFields();
497     }
498     
499     private void validateFields() {
500         if (labelText.isEnabled()) {
501             if (labelText.getText().length() == 0) {
502                 setValid(false);
503                 return;
504             }
505         }
506         String JavaDoc user = userText.getText();
507         IStatus status = ConfigurationWizardMainPage.validateUserName(user);
508         if (!isStatusOK(status)) {
509             return;
510         }
511
512         String JavaDoc host = hostText.getText();
513         status = ConfigurationWizardMainPage.validateHost(host);
514         if (!isStatusOK(status)) {
515             return;
516         }
517
518         if (portText.isEnabled()) {
519             String JavaDoc port = portText.getText();
520             status = ConfigurationWizardMainPage.validatePort(port);
521             if (!isStatusOK(status)) {
522                 return;
523             }
524         }
525
526         String JavaDoc pathString = pathText.getText();
527         status = ConfigurationWizardMainPage.validatePath(pathString);
528         if (!isStatusOK(status)) {
529             return;
530         }
531         
532         try {
533             CVSRepositoryLocation l = CVSRepositoryLocation.fromProperties(createProperties());
534             if (!l.equals(location) && KnownRepositories.getInstance().isKnownRepository(l.getLocation())) {
535                 setErrorMessage(CVSUIMessages.ConfigurationWizardMainPage_0);
536                 setValid(false);
537                 return;
538             }
539         } catch (CVSException e) {
540             CVSUIPlugin.log(e);
541             // Let it pass. Creation should fail
542
}
543         
544         setErrorMessage(null);
545         setValid(true);
546     }
547     
548     private boolean isStatusOK(IStatus status) {
549         if (!status.isOK()) {
550             setErrorMessage(status.getMessage());
551             setValid(false);
552             return false;
553         }
554         return true;
555     }
556     
557     private void recordNewLabel(CVSRepositoryLocation location) {
558         String JavaDoc newLabel = getNewLabel(location);
559         if (newLabel == null) {
560             String JavaDoc oldLabel = getOldLabel(location);
561             if (oldLabel == null || oldLabel.equals(location.getLocation())) {
562                 return;
563             }
564         } else if (newLabel.equals(getOldLabel(location))) {
565             return;
566         }
567         CVSUIPlugin.getPlugin().getRepositoryManager().setLabel(location, newLabel);
568         CVSUIPlugin.broadcastPropertyChange(new PropertyChangeEvent(this,
569                 CVSUIPlugin.P_DECORATORS_CHANGED, null, null));
570
571     }
572     private String JavaDoc getOldLabel(CVSRepositoryLocation location) {
573         return CVSUIPlugin.getPlugin().getRepositoryManager().getRepositoryRootFor(location).getName();
574     }
575     private String JavaDoc getNewLabel(CVSRepositoryLocation location) {
576         String JavaDoc label = null;
577         if (useCustomLabel.getSelection()) {
578             label = labelText.getText();
579             if (label.equals(location.getLocation())) {
580                 label = null;
581             }
582         }
583         return label;
584     }
585     /* internal use only */ String JavaDoc getNewPassword() {
586         return passwordText.getText();
587     }
588     
589     private Properties JavaDoc createProperties() {
590         Properties JavaDoc result = new Properties JavaDoc();
591         result.setProperty("connection", methodType.getText()); //$NON-NLS-1$
592
result.setProperty("user", userText.getText()); //$NON-NLS-1$
593
if (passwordChanged) {
594             result.setProperty("password", passwordText.getText()); //$NON-NLS-1$
595
}
596         result.setProperty("host", hostText.getText()); //$NON-NLS-1$
597
if (useCustomPort.getSelection()) {
598             result.setProperty("port", portText.getText()); //$NON-NLS-1$
599
}
600         result.setProperty("root", pathText.getText()); //$NON-NLS-1$
601
return result;
602     }
603 }
604
605
Popular Tags