1 11 package org.eclipse.team.internal.ccvs.ui.wizards; 12 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.util.Properties ; 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 properties = null; 37 private boolean switchPerspectives = true; 38 39 42 public static IDialogSettings getLocationDialogSettings() { 43 IDialogSettings workbenchSettings = CVSUIPlugin.getPlugin().getDialogSettings(); 44 IDialogSettings section = workbenchSettings.getSection("NewLocationWizard"); if (section == null) { 46 section = workbenchSettings.addNewSection("NewLocationWizard"); } 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 initialProperties) { 60 this(); 61 this.properties = initialProperties; 62 } 63 64 67 public void addPages() { 68 mainPage = new ConfigurationWizardMainPage("repositoryPage1", CVSUIMessages.NewLocationWizard_heading, CVSUIPlugin.getPlugin().getImageDescriptor(ICVSUIConstants.IMG_WIZBAN_NEW_LOCATION)); 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 80 public boolean performFinish() { 81 final ICVSRepositoryLocation[] location = new ICVSRepositoryLocation[] { null }; 82 boolean keepLocation = false; 83 try { 84 location[0] = mainPage.getLocation(); 86 location[0] = KnownRepositories.getInstance().addRepository(location[0], false ); 88 89 if (mainPage.getValidate()) { 90 try { 91 getContainer().run(true, true, new IRunnableWithProgress() { 92 public void run(IProgressMonitor monitor) throws InvocationTargetException , InterruptedException { 93 try { 94 location[0].validateConnection(monitor); 95 } catch (TeamException e) { 96 throw new InvocationTargetException (e); 97 } 98 } 99 }); 100 keepLocation = true; 101 } catch (InterruptedException e) { 102 } catch (InvocationTargetException e) { 104 Throwable t = e.getTargetException(); 105 if (t instanceof TeamException) { 106 throw (TeamException)t; 107 } else if (t instanceof Exception ) { 108 throw CVSException.wrapException((Exception )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 CVSUIPlugin.openError(getContainer().getShell(), CVSUIMessages.NewLocationWizard_exception, null, e); 120 return false; 121 } else { 122 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 [] {error.getMessage()}))); 134 } 135 } 136 } 137 if (keepLocation) { 138 KnownRepositories.getInstance().addRepository(location[0], true ); 139 if (switchPerspectives) { 140 final IWorkbench workbench= PlatformUI.getWorkbench(); 141 final IWorkbenchWindow window = workbench.getActiveWorkbenchWindow(); 142 143 final String 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 } 162 163 public void setSwitchPerspectives(boolean switchPerspectives) { 164 this.switchPerspectives = switchPerspectives; 165 } 166 167 private String promptForPerspectiveSwitch() { 168 final IPreferenceStore store = CVSUIPlugin.getPlugin().getPreferenceStore(); 170 final String option = store.getString(ICVSUIConstants.PREF_CHANGE_PERSPECTIVE_ON_NEW_REPOSITORY_LOCATION); 171 final String desiredID = CVSPerspective.ID; 172 173 if (option.equals(MessageDialogWithToggle.ALWAYS)) 174 return desiredID; 176 if (option.equals(MessageDialogWithToggle.NEVER)) 177 return null; 179 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; } 189 } 190 191 if (desired != null) { 192 193 String message;; 194 String desc = desired.getDescription(); 195 if (desc == null) { 196 message = NLS.bind(CVSUIMessages.NewLocationWizard_2, new String [] { desired.getLabel() }); 197 } else { 198 message = NLS.bind(CVSUIMessages.NewLocationWizard_3, new String [] { desired.getLabel(), desc }); 199 } 200 final MessageDialogWithToggle m = MessageDialogWithToggle.openYesNoQuestion( 202 Utils.getShell(null), 203 CVSUIMessages.NewLocationWizard_1, 204 message, 205 CVSUIMessages.NewLocationWizard_4, 206 false , 207 store, 208 ICVSUIConstants.PREF_CHANGE_PERSPECTIVE_ON_NEW_REPOSITORY_LOCATION); 209 210 final int result = m.getReturnCode(); 211 switch (result) { 212 case IDialogConstants.YES_ID: 214 case IDialogConstants.OK_ID : 215 return desiredID; 216 case IDialogConstants.NO_ID : 218 return null; 219 } 220 } 221 return null; 222 } 223 } 224 | Popular Tags |