KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > wizards > NewLocationWizard


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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.wizards;
12
13
14 import java.lang.reflect.InvocationTargetException JavaDoc;
15 import java.util.Properties JavaDoc;
16
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.core.runtime.IStatus;
19 import org.eclipse.jface.dialogs.*;
20 import org.eclipse.jface.operation.IRunnableWithProgress;
21 import org.eclipse.jface.preference.IPreferenceStore;
22 import org.eclipse.jface.viewers.IStructuredSelection;
23 import org.eclipse.jface.wizard.Wizard;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.team.core.TeamException;
26 import org.eclipse.team.internal.ccvs.core.CVSException;
27 import org.eclipse.team.internal.ccvs.core.ICVSRepositoryLocation;
28 import org.eclipse.team.internal.ccvs.core.util.KnownRepositories;
29 import org.eclipse.team.internal.ccvs.ui.*;
30 import org.eclipse.team.internal.ui.Utils;
31 import org.eclipse.ui.*;
32
33 public class NewLocationWizard extends Wizard implements INewWizard {
34     
35     private ConfigurationWizardMainPage mainPage;
36     private Properties JavaDoc properties = null;
37     private boolean switchPerspectives = true;
38     
39     /**
40      * Return the settings used for all location pages
41      */

42     public static IDialogSettings getLocationDialogSettings() {
43         IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings();
44         IDialogSettings section = workbenchSettings.getSection("NewLocationWizard");//$NON-NLS-1$
45
if (section == null) {
46             section = workbenchSettings.addNewSection("NewLocationWizard");//$NON-NLS-1$
47
}
48         return section;
49     }
50     
51     public NewLocationWizard() {
52         IDialogSettings section = getLocationDialogSettings();
53         setDialogSettings(section);
54         setWindowTitle(CVSUIMessages.NewLocationWizard_title);
55         setNeedsProgressMonitor(true);
56     }
57     
58
59     public NewLocationWizard(Properties JavaDoc initialProperties) {
60         this();
61         this.properties = initialProperties;
62     }
63
64     /**
65      * Creates the wizard pages
66      */

67     public void addPages() {
68         mainPage = new ConfigurationWizardMainPage("repositoryPage1", CVSUIMessages.NewLocationWizard_heading, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION)); //$NON-NLS-1$
69
if (properties != null) {
70             mainPage.setProperties(properties);
71         }
72         mainPage.setShowValidate(true);
73         mainPage.setDescription(CVSUIMessages.NewLocationWizard_description);
74         mainPage.setDialogSettings(getDialogSettings());
75         addPage(mainPage);
76     }
77     /*
78      * @see IWizard#performFinish
79      */

80     public boolean performFinish() {
81         final ICVSRepositoryLocation[] location = new ICVSRepositoryLocation[] { null };
82         boolean keepLocation = false;
83         try {
84             // Create a handle to a repository location
85
location[0] = mainPage.getLocation();
86             // Add the location quitely so we can validate
87
location[0] = KnownRepositories.getInstance().addRepository(location[0], false /* don't tell anybody */);
88             
89             if (mainPage.getValidate()) {
90                 try {
91                     getContainer().run(true, true, new IRunnableWithProgress() {
92                         public void run(IProgressMonitor monitor) throws InvocationTargetException JavaDoc, InterruptedException JavaDoc {
93                             try {
94                                 location[0].validateConnection(monitor);
95                             } catch (TeamException e) {
96                                 throw new InvocationTargetException JavaDoc(e);
97                             }
98                         }
99                     });
100                     keepLocation = true;
101                 } catch (InterruptedException JavaDoc e) {
102                     // Cancelled by user. Fall through to dispose of location
103
} catch (InvocationTargetException JavaDoc e) {
104                     Throwable JavaDoc t = e.getTargetException();
105                     if (t instanceof TeamException) {
106                         throw (TeamException)t;
107                     } else if (t instanceof Exception JavaDoc) {
108                         throw CVSException.wrapException((Exception JavaDoc)t);
109                     } else {
110                         throw CVSException.wrapException(e);
111                     }
112                 }
113             } else {
114                 keepLocation = true;
115             }
116         } catch (TeamException e) {
117             if (location[0] == null) {
118                 // Exception creating the root, we cannot continue
119
CVSUIPlugin.openError(getContainer().getShell(), CVSUIMessages.NewLocationWizard_exception, null, e);
120                 return false;
121             } else {
122                 // Exception validating. We can continue if the user wishes.
123
IStatus error = e.getStatus();
124                 if (error.isMultiStatus() && error.getChildren().length == 1) {
125                     error = error.getChildren()[0];
126                 }
127                     
128                 if (error.isMultiStatus()) {
129                     CVSUIPlugin.openError(getContainer().getShell(), CVSUIMessages.NewLocationWizard_validationFailedTitle, null, e);
130                 } else {
131                     keepLocation = MessageDialog.openQuestion(getContainer().getShell(),
132                         CVSUIMessages.NewLocationWizard_validationFailedTitle,
133                         NLS.bind(CVSUIMessages.NewLocationWizard_validationFailedText, (new Object JavaDoc[] {error.getMessage()})));
134                 }
135             }
136         }
137         if (keepLocation) {
138             KnownRepositories.getInstance().addRepository(location[0], true /* let the world know */);
139             if (switchPerspectives) {
140                 final IWorkbench workbench= PlatformUI.getWorkbench();
141                 final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow();
142                 
143                 final String JavaDoc defaultPerspectiveID= promptForPerspectiveSwitch();
144
145                 if (defaultPerspectiveID != null) {
146                     try {
147                         workbench.showPerspective(defaultPerspectiveID, window);
148                     } catch (WorkbenchException e) {
149                         Utils.handleError(window.getShell(), e, CVSUIMessages.ShowAnnotationOperation_0, e.getMessage());
150                     }
151                 }
152             }
153         } else {
154             KnownRepositories.getInstance().disposeRepository(location[0]);
155         }
156         return keepLocation;
157     }
158
159     public void init(IWorkbench workbench, IStructuredSelection selection) {
160         // Nothing to do
161
}
162
163     public void setSwitchPerspectives(boolean switchPerspectives) {
164         this.switchPerspectives = switchPerspectives;
165     }
166     
167     private String JavaDoc promptForPerspectiveSwitch() {
168         // check whether we should ask the user.
169
final IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore();
170         final String JavaDoc option = store.getString(ICVSUIConstants.PREF_CHANGE_PERSPECTIVE_ON_NEW_REPOSITORY_LOCATION);
171         final String JavaDoc desiredID = CVSPerspective.ID;
172         
173         if (option.equals(MessageDialogWithToggle.ALWAYS))
174             return desiredID; // no, always switch
175

176         if (option.equals(MessageDialogWithToggle.NEVER))
177             return null; // no, never switch
178

179         // Check whether the desired perspective is already active.
180
final IPerspectiveRegistry registry= PlatformUI.getWorkbench().getPerspectiveRegistry();
181         final IPerspectiveDescriptor desired = registry.findPerspectiveWithId(desiredID);
182         final IWorkbenchPage page = CVSUIPlugin.getActivePage();
183         
184         if (page != null) {
185             final IPerspectiveDescriptor current = page.getPerspective();
186             if (current != null && current.getId().equals(desiredID)) {
187                 return null; // it is active, so no prompt and no switch
188
}
189         }
190         
191         if (desired != null) {
192             
193             String JavaDoc message;;
194             String JavaDoc desc = desired.getDescription();
195             if (desc == null) {
196                 message = NLS.bind(CVSUIMessages.NewLocationWizard_2, new String JavaDoc[] { desired.getLabel() });
197             } else {
198                 message = NLS.bind(CVSUIMessages.NewLocationWizard_3, new String JavaDoc[] { desired.getLabel(), desc });
199             }
200             // Ask the user whether to switch
201
final MessageDialogWithToggle m = MessageDialogWithToggle.openYesNoQuestion(
202                     Utils.getShell(null),
203                     CVSUIMessages.NewLocationWizard_1,
204                     message,
205                     CVSUIMessages.NewLocationWizard_4,
206                     false /* toggle state */,
207                     store,
208                     ICVSUIConstants.PREF_CHANGE_PERSPECTIVE_ON_NEW_REPOSITORY_LOCATION);
209             
210             final int result = m.getReturnCode();
211             switch (result) {
212             // yes
213
case IDialogConstants.YES_ID:
214             case IDialogConstants.OK_ID :
215                 return desiredID;
216             // no
217
case IDialogConstants.NO_ID :
218                 return null;
219             }
220         }
221         return null;
222     }
223 }
224
Popular Tags