1 11 12 package org.eclipse.ui.wizards.newresource; 13 14 import java.lang.reflect.InvocationTargetException ; 15 import java.net.URI ; 16 import java.util.ArrayList ; 17 import java.util.HashSet ; 18 import java.util.List ; 19 import java.util.Set ; 20 import java.util.StringTokenizer ; 21 22 import org.eclipse.core.commands.ExecutionException; 23 import org.eclipse.core.resources.IProject; 24 import org.eclipse.core.resources.IProjectDescription; 25 import org.eclipse.core.resources.IResourceStatus; 26 import org.eclipse.core.resources.IWorkspace; 27 import org.eclipse.core.resources.ResourcesPlugin; 28 import org.eclipse.core.runtime.CoreException; 29 import org.eclipse.core.runtime.IConfigurationElement; 30 import org.eclipse.core.runtime.IExecutableExtension; 31 import org.eclipse.core.runtime.IProgressMonitor; 32 import org.eclipse.core.runtime.IStatus; 33 import org.eclipse.core.runtime.Status; 34 import org.eclipse.jface.dialogs.ErrorDialog; 35 import org.eclipse.jface.dialogs.IDialogConstants; 36 import org.eclipse.jface.dialogs.IDialogSettings; 37 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 38 import org.eclipse.jface.operation.IRunnableWithProgress; 39 import org.eclipse.jface.preference.IPreferenceStore; 40 import org.eclipse.jface.resource.ImageDescriptor; 41 import org.eclipse.jface.viewers.IStructuredSelection; 42 import org.eclipse.osgi.util.NLS; 43 import org.eclipse.ui.IPerspectiveDescriptor; 44 import org.eclipse.ui.IPerspectiveRegistry; 45 import org.eclipse.ui.IPluginContribution; 46 import org.eclipse.ui.IWorkbench; 47 import org.eclipse.ui.IWorkbenchPage; 48 import org.eclipse.ui.IWorkbenchPreferenceConstants; 49 import org.eclipse.ui.IWorkbenchWindow; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.WorkbenchException; 52 import org.eclipse.ui.activities.IActivityManager; 53 import org.eclipse.ui.activities.IIdentifier; 54 import org.eclipse.ui.activities.IWorkbenchActivitySupport; 55 import org.eclipse.ui.activities.WorkbenchActivityHelper; 56 import org.eclipse.ui.dialogs.WizardNewProjectCreationPage; 57 import org.eclipse.ui.dialogs.WizardNewProjectReferencePage; 58 import org.eclipse.ui.ide.IDE; 59 import org.eclipse.ui.ide.undo.CreateProjectOperation; 60 import org.eclipse.ui.ide.undo.WorkspaceUndoUtil; 61 import org.eclipse.ui.internal.IPreferenceConstants; 62 import org.eclipse.ui.internal.WorkbenchPlugin; 63 import org.eclipse.ui.internal.ide.IDEInternalPreferences; 64 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 65 import org.eclipse.ui.internal.ide.StatusUtil; 66 import org.eclipse.ui.internal.registry.PerspectiveDescriptor; 67 import org.eclipse.ui.internal.util.PrefUtil; 68 import org.eclipse.ui.internal.wizards.newresource.ResourceMessages; 69 import org.eclipse.ui.statushandlers.StatusAdapter; 70 import org.eclipse.ui.statushandlers.StatusManager; 71 72 95 public class BasicNewProjectResourceWizard extends BasicNewResourceWizard 96 implements IExecutableExtension { 97 private WizardNewProjectCreationPage mainPage; 98 99 private WizardNewProjectReferencePage referencePage; 100 101 private IProject newProject; 103 104 107 private IConfigurationElement configElement; 108 109 private static String WINDOW_PROBLEMS_TITLE = ResourceMessages.NewProject_errorOpeningWindow; 110 111 114 private static final String FINAL_PERSPECTIVE = "finalPerspective"; 116 119 private static final String PREFERRED_PERSPECTIVES = "preferredPerspectives"; 121 124 public BasicNewProjectResourceWizard() { 125 IDialogSettings workbenchSettings = IDEWorkbenchPlugin.getDefault() 126 .getDialogSettings(); 127 IDialogSettings section = workbenchSettings 128 .getSection("BasicNewProjectResourceWizard"); if (section == null) { 130 section = workbenchSettings 131 .addNewSection("BasicNewProjectResourceWizard"); } 133 setDialogSettings(section); 134 } 135 136 139 public void addPages() { 140 super.addPages(); 141 142 mainPage = new WizardNewProjectCreationPage("basicNewProjectPage"); mainPage.setTitle(ResourceMessages.NewProject_title); 144 mainPage.setDescription(ResourceMessages.NewProject_description); 145 this.addPage(mainPage); 146 147 if (ResourcesPlugin.getWorkspace().getRoot().getProjects().length > 0) { 149 referencePage = new WizardNewProjectReferencePage( 150 "basicReferenceProjectPage"); referencePage.setTitle(ResourceMessages.NewProject_referenceTitle); 152 referencePage 153 .setDescription(ResourceMessages.NewProject_referenceDescription); 154 this.addPage(referencePage); 155 } 156 } 157 158 174 private IProject createNewProject() { 175 if (newProject != null) { 176 return newProject; 177 } 178 179 final IProject newProjectHandle = mainPage.getProjectHandle(); 181 182 URI location = null; 184 if (!mainPage.useDefaults()) { 185 location = mainPage.getLocationURI(); 186 } 187 188 IWorkspace workspace = ResourcesPlugin.getWorkspace(); 189 final IProjectDescription description = workspace 190 .newProjectDescription(newProjectHandle.getName()); 191 description.setLocationURI(location); 192 193 if (referencePage != null) { 195 IProject[] refProjects = referencePage.getReferencedProjects(); 196 if (refProjects.length > 0) { 197 description.setReferencedProjects(refProjects); 198 } 199 } 200 201 IRunnableWithProgress op = new IRunnableWithProgress() { 203 public void run(IProgressMonitor monitor) 204 throws InvocationTargetException { 205 CreateProjectOperation op = new CreateProjectOperation( 206 description, ResourceMessages.NewProject_windowTitle); 207 try { 208 PlatformUI.getWorkbench().getOperationSupport() 209 .getOperationHistory().execute( 210 op, 211 monitor, 212 WorkspaceUndoUtil 213 .getUIInfoAdapter(getShell())); 214 } catch (ExecutionException e) { 215 throw new InvocationTargetException (e); 216 } 217 } 218 }; 219 220 try { 222 getContainer().run(true, true, op); 223 } catch (InterruptedException e) { 224 return null; 225 } catch (InvocationTargetException e) { 226 Throwable t = e.getTargetException(); 227 if (t instanceof ExecutionException 228 && t.getCause() instanceof CoreException) { 229 CoreException cause = (CoreException) t.getCause(); 230 StatusAdapter status; 231 if (cause.getStatus().getCode() == IResourceStatus.CASE_VARIANT_EXISTS) { 232 status = new StatusAdapter( 233 StatusUtil 234 .newStatus( 235 IStatus.WARNING, 236 NLS 237 .bind( 238 ResourceMessages.NewProject_caseVariantExistsError, 239 newProjectHandle 240 .getName()), 241 cause)); 242 } else { 243 status = new StatusAdapter(StatusUtil.newStatus(cause 244 .getStatus().getSeverity(), 245 ResourceMessages.NewProject_errorMessage, cause)); 246 } 247 status.setProperty(StatusAdapter.TITLE_PROPERTY, 248 ResourceMessages.NewProject_errorMessage); 249 StatusManager.getManager().handle(status, StatusManager.BLOCK); 250 } else { 251 StatusAdapter status = new StatusAdapter(new Status( 252 IStatus.WARNING, IDEWorkbenchPlugin.IDE_WORKBENCH, 0, 253 NLS.bind(ResourceMessages.NewProject_internalError, t 254 .getMessage()), t)); 255 status.setProperty(StatusAdapter.TITLE_PROPERTY, 256 ResourceMessages.NewProject_errorMessage); 257 StatusManager.getManager().handle(status, 258 StatusManager.LOG | StatusManager.BLOCK); 259 } 260 return null; 261 } 262 263 newProject = newProjectHandle; 264 265 return newProject; 266 } 267 268 274 public IProject getNewProject() { 275 return newProject; 276 } 277 278 281 public void init(IWorkbench workbench, IStructuredSelection currentSelection) { 282 super.init(workbench, currentSelection); 283 setNeedsProgressMonitor(true); 284 setWindowTitle(ResourceMessages.NewProject_windowTitle); 285 } 286 287 290 protected void initializeDefaultPageImageDescriptor() { 291 ImageDescriptor desc = IDEWorkbenchPlugin 292 .getIDEImageDescriptor("wizban/newprj_wiz.png"); setDefaultPageImageDescriptor(desc); 294 } 295 296 299 private static void openInNewWindow(IPerspectiveDescriptor desc) { 300 301 try { 303 PlatformUI.getWorkbench().openWorkbenchWindow(desc.getId(), 304 ResourcesPlugin.getWorkspace().getRoot()); 305 } catch (WorkbenchException e) { 306 IWorkbenchWindow window = PlatformUI.getWorkbench() 307 .getActiveWorkbenchWindow(); 308 if (window != null) { 309 ErrorDialog.openError(window.getShell(), WINDOW_PROBLEMS_TITLE, 310 e.getMessage(), e.getStatus()); 311 } 312 } 313 } 314 315 318 public boolean performFinish() { 319 createNewProject(); 320 321 if (newProject == null) { 322 return false; 323 } 324 325 updatePerspective(); 326 selectAndReveal(newProject); 327 328 return true; 329 } 330 331 334 private static void replaceCurrentPerspective(IPerspectiveDescriptor persp) { 335 336 IWorkbenchWindow window = PlatformUI.getWorkbench() 338 .getActiveWorkbenchWindow(); 339 if (window == null) { 340 return; 341 } 342 IWorkbenchPage page = window.getActivePage(); 343 if (page == null) { 344 return; 345 } 346 347 page.setPerspective(persp); 349 } 350 351 355 public void setInitializationData(IConfigurationElement cfig, 356 String propertyName, Object data) { 357 configElement = cfig; 358 } 359 360 363 protected void updatePerspective() { 364 updatePerspective(configElement); 365 } 366 367 387 public static void updatePerspective(IConfigurationElement configElement) { 388 if (configElement == null) { 391 return; 392 } 393 394 String perspSetting = PrefUtil.getAPIPreferenceStore().getString( 396 IDE.Preferences.PROJECT_OPEN_NEW_PERSPECTIVE); 397 398 String promptSetting = IDEWorkbenchPlugin.getDefault() 399 .getPreferenceStore().getString( 400 IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE); 401 402 if (!(promptSetting.equals(MessageDialogWithToggle.PROMPT)) 404 && perspSetting 405 .equals(IWorkbenchPreferenceConstants.NO_NEW_PERSPECTIVE)) { 406 return; 407 } 408 409 String finalPerspId = configElement.getAttribute(FINAL_PERSPECTIVE); 411 if (finalPerspId == null) { 412 return; 413 } 414 415 IPerspectiveRegistry reg = PlatformUI.getWorkbench() 417 .getPerspectiveRegistry(); 418 419 IPerspectiveDescriptor finalPersp = reg 422 .findPerspectiveWithId(finalPerspId); 423 if (finalPersp != null && finalPersp instanceof IPluginContribution) { 424 IPluginContribution contribution = (IPluginContribution) finalPersp; 425 if (contribution.getPluginId() != null) { 426 IWorkbenchActivitySupport workbenchActivitySupport = PlatformUI 427 .getWorkbench().getActivitySupport(); 428 IActivityManager activityManager = workbenchActivitySupport 429 .getActivityManager(); 430 IIdentifier identifier = activityManager 431 .getIdentifier(WorkbenchActivityHelper 432 .createUnifiedId(contribution)); 433 Set idActivities = identifier.getActivityIds(); 434 435 if (!idActivities.isEmpty()) { 436 Set enabledIds = new HashSet (activityManager 437 .getEnabledActivityIds()); 438 439 if (enabledIds.addAll(idActivities)) { 440 workbenchActivitySupport 441 .setEnabledActivityIds(enabledIds); 442 } 443 } 444 } 445 } else { 446 IDEWorkbenchPlugin.log("Unable to find persective " + finalPerspId 448 + " in BasicNewProjectResourceWizard.updatePerspective"); return; 450 } 451 452 ArrayList preferredPerspIds = new ArrayList (); 456 addPerspectiveAndDescendants(preferredPerspIds, finalPerspId); 457 String preferred = configElement.getAttribute(PREFERRED_PERSPECTIVES); 458 if (preferred != null) { 459 StringTokenizer tok = new StringTokenizer (preferred, " \t\n\r\f,"); while (tok.hasMoreTokens()) { 461 addPerspectiveAndDescendants(preferredPerspIds, tok.nextToken()); 462 } 463 } 464 465 IWorkbenchWindow window = PlatformUI.getWorkbench() 466 .getActiveWorkbenchWindow(); 467 if (window != null) { 468 IWorkbenchPage page = window.getActivePage(); 469 if (page != null) { 470 IPerspectiveDescriptor currentPersp = page.getPerspective(); 471 472 if (currentPersp != null 475 && preferredPerspIds.contains(currentPersp.getId())) { 476 return; 477 } 478 } 479 480 if (!confirmPerspectiveSwitch(window, finalPersp)) { 482 return; 483 } 484 } 485 486 int workbenchPerspectiveSetting = WorkbenchPlugin.getDefault() 487 .getPreferenceStore().getInt( 488 IPreferenceConstants.OPEN_PERSP_MODE); 489 490 if (workbenchPerspectiveSetting == IPreferenceConstants.OPM_NEW_WINDOW) { 492 openInNewWindow(finalPersp); 493 return; 494 } 495 496 replaceCurrentPerspective(finalPersp); 498 } 499 500 510 private static void addPerspectiveAndDescendants(List perspectiveIds, 511 String id) { 512 IPerspectiveRegistry registry = PlatformUI.getWorkbench() 513 .getPerspectiveRegistry(); 514 IPerspectiveDescriptor[] perspectives = registry.getPerspectives(); 515 for (int i = 0; i < perspectives.length; i++) { 516 PerspectiveDescriptor descriptor = ((PerspectiveDescriptor) perspectives[i]); 519 if (descriptor.getOriginalId().equals(id)) { 520 perspectiveIds.add(descriptor.getId()); 521 } 522 } 523 } 524 525 537 private static boolean confirmPerspectiveSwitch(IWorkbenchWindow window, 538 IPerspectiveDescriptor finalPersp) { 539 IPreferenceStore store = IDEWorkbenchPlugin.getDefault() 540 .getPreferenceStore(); 541 String pspm = store 542 .getString(IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE); 543 if (!IDEInternalPreferences.PSPM_PROMPT.equals(pspm)) { 544 return IDEInternalPreferences.PSPM_ALWAYS.equals(pspm); 546 } 547 String desc = finalPersp.getDescription(); 548 String message; 549 if (desc == null || desc.length() == 0) 550 message = NLS.bind(ResourceMessages.NewProject_perspSwitchMessage, 551 finalPersp.getLabel()); 552 else 553 message = NLS.bind( 554 ResourceMessages.NewProject_perspSwitchMessageWithDesc, 555 new String [] { finalPersp.getLabel(), desc }); 556 557 MessageDialogWithToggle dialog = MessageDialogWithToggle 558 .openYesNoQuestion(window.getShell(), 559 ResourceMessages.NewProject_perspSwitchTitle, message, 560 null , 561 false , store, 562 IDEInternalPreferences.PROJECT_SWITCH_PERSP_MODE); 563 int result = dialog.getReturnCode(); 564 565 if (dialog.getToggleState()) { 567 String preferenceValue; 568 if (result == IDialogConstants.YES_ID) { 569 preferenceValue = IWorkbenchPreferenceConstants.OPEN_PERSPECTIVE_REPLACE; 572 } else { 573 preferenceValue = IWorkbenchPreferenceConstants.NO_NEW_PERSPECTIVE; 574 } 575 576 PrefUtil.getAPIPreferenceStore().setValue( 578 IDE.Preferences.PROJECT_OPEN_NEW_PERSPECTIVE, 579 preferenceValue); 580 } 581 return result == IDialogConstants.YES_ID; 582 } 583 } 584 | Popular Tags |