1 11 package org.eclipse.ui.internal.ide; 12 13 import java.lang.reflect.InvocationTargetException ; 14 import java.net.URL ; 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.Map ; 18 import java.util.TreeMap ; 19 20 import org.eclipse.core.resources.IContainer; 21 import org.eclipse.core.resources.IResource; 22 import org.eclipse.core.resources.ResourcesPlugin; 23 import org.eclipse.core.resources.WorkspaceJob; 24 import org.eclipse.core.runtime.CoreException; 25 import org.eclipse.core.runtime.IAdaptable; 26 import org.eclipse.core.runtime.IBundleGroup; 27 import org.eclipse.core.runtime.IBundleGroupProvider; 28 import org.eclipse.core.runtime.IProgressMonitor; 29 import org.eclipse.core.runtime.IStatus; 30 import org.eclipse.core.runtime.MultiStatus; 31 import org.eclipse.core.runtime.Path; 32 import org.eclipse.core.runtime.Platform; 33 import org.eclipse.core.runtime.PluginVersionIdentifier; 34 import org.eclipse.core.runtime.Status; 35 import org.eclipse.core.runtime.jobs.Job; 36 import org.eclipse.jface.dialogs.Dialog; 37 import org.eclipse.jface.dialogs.ErrorDialog; 38 import org.eclipse.jface.dialogs.IDialogSettings; 39 import org.eclipse.jface.dialogs.MessageDialog; 40 import org.eclipse.jface.dialogs.TrayDialog; 41 import org.eclipse.jface.operation.IRunnableWithProgress; 42 import org.eclipse.jface.preference.IPreferenceStore; 43 import org.eclipse.jface.resource.ImageDescriptor; 44 import org.eclipse.jface.resource.ImageRegistry; 45 import org.eclipse.jface.resource.JFaceResources; 46 import org.eclipse.jface.window.Window; 47 import org.eclipse.swt.SWT; 48 import org.eclipse.swt.widgets.Display; 49 import org.eclipse.swt.widgets.Listener; 50 import org.eclipse.ui.PlatformUI; 51 import org.eclipse.ui.application.IWorkbenchConfigurer; 52 import org.eclipse.ui.application.IWorkbenchWindowConfigurer; 53 import org.eclipse.ui.application.WorkbenchAdvisor; 54 import org.eclipse.ui.application.WorkbenchWindowAdvisor; 55 import org.eclipse.ui.ide.IDE; 56 import org.eclipse.ui.internal.ISelectionConversionService; 57 import org.eclipse.ui.internal.Workbench; 58 import org.eclipse.ui.internal.ide.model.WorkbenchAdapterBuilder; 59 import org.eclipse.ui.internal.progress.ProgressMonitorJobsDialog; 60 import org.eclipse.ui.progress.IProgressService; 61 import org.eclipse.update.core.SiteManager; 62 import org.osgi.framework.Bundle; 63 64 73 public class IDEWorkbenchAdvisor extends WorkbenchAdvisor { 74 75 private static final String WORKBENCH_PREFERENCE_CATEGORY_ID = "org.eclipse.ui.preferencePages.Workbench"; 77 81 private static final String INSTALLED_FEATURES = "installedFeatures"; 83 private static IDEWorkbenchAdvisor workbenchAdvisor = null; 84 85 88 private IDEExceptionHandler exceptionHandler = null; 89 90 94 private String workspaceLocation = null; 95 96 101 private Map newlyAddedBundleGroups; 102 103 107 private AboutInfo[] welcomePerspectiveInfos = null; 108 109 112 private IDEWorkbenchActivityHelper activityHelper = null; 113 114 118 private IDEIdleHelper idleHelper; 119 120 private Listener settingsChangeListener; 121 122 125 public IDEWorkbenchAdvisor() { 126 super(); 127 if (workbenchAdvisor != null) { 128 throw new IllegalStateException (); 129 } 130 workbenchAdvisor = this; 131 } 132 133 138 public void initialize(IWorkbenchConfigurer configurer) { 139 140 configurer.setSaveAndRestore(true); 142 143 exceptionHandler = new IDEExceptionHandler(configurer); 145 146 WorkbenchAdapterBuilder.registerAdapters(); 148 149 String [] cmdLineArgs = Platform.getCommandLineArgs(); 151 152 for (int i = 0; i < cmdLineArgs.length; i++) { 155 if ("-showlocation".equalsIgnoreCase(cmdLineArgs[i])) { String name = null; 157 if (cmdLineArgs.length > i + 1) { 158 name = cmdLineArgs[i + 1]; 159 } 160 if (name != null && name.indexOf("-") == -1) { workspaceLocation = name; 162 } else { 163 workspaceLocation = Platform.getLocation().toOSString(); 164 } 165 break; 166 } 167 } 168 169 declareWorkbenchImages(); 171 172 activityHelper = IDEWorkbenchActivityHelper.getInstance(); 174 175 idleHelper = new IDEIdleHelper(configurer); 177 178 TrayDialog.setDialogHelpAvailable(true); 180 181 ImageRegistry reg = JFaceResources.getImageRegistry(); 183 reg 184 .put( 185 Dialog.DLG_IMG_HELP, 186 IDEInternalWorkbenchImages 187 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_LCL_LINKTO_HELP)); 188 } 189 190 195 public void preStartup() { 196 197 Platform.getJobManager().suspend(); 199 200 IProgressService service = PlatformUI.getWorkbench() 202 .getProgressService(); 203 ImageDescriptor newImage = IDEInternalWorkbenchImages 204 .getImageDescriptor(IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC); 205 service.registerIconForFamily(newImage, 206 ResourcesPlugin.FAMILY_MANUAL_BUILD); 207 service.registerIconForFamily(newImage, 208 ResourcesPlugin.FAMILY_AUTO_BUILD); 209 } 210 211 216 public void postStartup() { 217 try { 218 refreshFromLocal(); 219 checkUpdates(); 220 ((Workbench) PlatformUI.getWorkbench()).registerService( 221 ISelectionConversionService.class, 222 new IDESelectionConversionService()); 223 224 initializeSettingsChangeListener(); 225 Display.getCurrent().addListener(SWT.Settings, 226 settingsChangeListener); 227 } finally { Platform.getJobManager().resume(); 229 } 230 } 231 232 235 private void initializeSettingsChangeListener() { 236 settingsChangeListener = new Listener() { 237 238 boolean currentHighContrast = Display.getCurrent().getHighContrast(); 239 public void handleEvent(org.eclipse.swt.widgets.Event event) { 240 if(Display.getCurrent().getHighContrast() == currentHighContrast) 241 return; 242 243 currentHighContrast = !currentHighContrast; 244 245 if (new MessageDialog(null, 247 IDEWorkbenchMessages.SystemSettingsChange_title, null, 248 IDEWorkbenchMessages.SystemSettingsChange_message, 249 MessageDialog.QUESTION, new String [] { 250 IDEWorkbenchMessages.SystemSettingsChange_yes, 251 IDEWorkbenchMessages.SystemSettingsChange_no }, 252 1).open() == Window.OK) { 253 PlatformUI.getWorkbench().restart(); 254 } 255 } 256 }; 257 258 } 259 260 265 public void postShutdown() { 266 if (activityHelper != null) { 267 activityHelper.shutdown(); 268 activityHelper = null; 269 } 270 if (idleHelper != null) { 271 idleHelper.shutdown(); 272 idleHelper = null; 273 } 274 if (IDEWorkbenchPlugin.getPluginWorkspace() != null) { 275 disconnectFromWorkspace(); 276 } 277 } 278 279 284 public boolean preShutdown() { 285 Display.getCurrent().removeListener(SWT.Settings, 286 settingsChangeListener); 287 return super.preShutdown(); 288 } 289 290 295 public void eventLoopException(Throwable exception) { 296 super.eventLoopException(exception); 297 if (exceptionHandler != null) { 298 exceptionHandler.handleException(exception); 299 } else { 300 if (getWorkbenchConfigurer() != null) { 301 getWorkbenchConfigurer().emergencyClose(); 302 } 303 } 304 } 305 306 311 public WorkbenchWindowAdvisor createWorkbenchWindowAdvisor( 312 IWorkbenchWindowConfigurer configurer) { 313 return new IDEWorkbenchWindowAdvisor(this, configurer); 314 } 315 316 321 public boolean hasIntro() { 322 return getWorkbenchConfigurer().getWorkbench().getIntroManager() 323 .hasIntro(); 324 } 325 326 private void refreshFromLocal() { 327 String [] commandLineArgs = Platform.getCommandLineArgs(); 328 IPreferenceStore store = IDEWorkbenchPlugin.getDefault() 329 .getPreferenceStore(); 330 boolean refresh = store 331 .getBoolean(IDEInternalPreferences.REFRESH_WORKSPACE_ON_STARTUP); 332 if (!refresh) { 333 return; 334 } 335 336 for (int i = 0; i < commandLineArgs.length; i++) { 338 if (commandLineArgs[i].equalsIgnoreCase("-refresh")) { return; 340 } 341 } 342 343 final IContainer root = ResourcesPlugin.getWorkspace().getRoot(); 344 Job job = new WorkspaceJob(IDEWorkbenchMessages.Workspace_refreshing) { 345 public IStatus runInWorkspace(IProgressMonitor monitor) 346 throws CoreException { 347 root.refreshLocal(IResource.DEPTH_INFINITE, monitor); 348 return Status.OK_STATUS; 349 } 350 }; 351 job.setRule(root); 352 job.schedule(); 353 } 354 355 358 private void disconnectFromWorkspace() { 359 final MultiStatus status = new MultiStatus( 361 IDEWorkbenchPlugin.IDE_WORKBENCH, 1, 362 IDEWorkbenchMessages.ProblemSavingWorkbench, null); 363 IRunnableWithProgress runnable = new IRunnableWithProgress() { 364 public void run(IProgressMonitor monitor) { 365 try { 366 status.merge(ResourcesPlugin.getWorkspace().save(true, 367 monitor)); 368 } catch (CoreException e) { 369 status.merge(e.getStatus()); 370 } 371 } 372 }; 373 try { 374 new ProgressMonitorJobsDialog(null).run(true, false, runnable); 375 } catch (InvocationTargetException e) { 376 status 377 .merge(new Status(IStatus.ERROR, 378 IDEWorkbenchPlugin.IDE_WORKBENCH, 1, 379 IDEWorkbenchMessages.InternalError, e 380 .getTargetException())); 381 } catch (InterruptedException e) { 382 status.merge(new Status(IStatus.ERROR, 383 IDEWorkbenchPlugin.IDE_WORKBENCH, 1, 384 IDEWorkbenchMessages.InternalError, e)); 385 } 386 ErrorDialog.openError(null, 387 IDEWorkbenchMessages.ProblemsSavingWorkspace, null, status, 388 IStatus.ERROR | IStatus.WARNING); 389 if (!status.isOK()) { 390 IDEWorkbenchPlugin.log( 391 IDEWorkbenchMessages.ProblemsSavingWorkspace, status); 392 } 393 } 394 395 399 private void checkUpdates() { 400 boolean newUpdates = false; 401 String [] commandLineArgs = Platform.getCommandLineArgs(); 402 for (int i = 0; i < commandLineArgs.length; i++) { 403 if (commandLineArgs[i].equalsIgnoreCase("-newUpdates")) { newUpdates = true; 405 break; 406 } 407 } 408 409 if (newUpdates) { 410 try { 411 SiteManager.handleNewChanges(); 412 } catch (CoreException e) { 413 IDEWorkbenchPlugin.log( 414 "Problem opening update manager", e.getStatus()); } 416 } 417 } 418 419 424 public IAdaptable getDefaultPageInput() { 425 return ResourcesPlugin.getWorkspace().getRoot(); 426 } 427 428 433 public String getInitialWindowPerspectiveId() { 434 int index = PlatformUI.getWorkbench().getWorkbenchWindowCount() - 1; 435 436 String perspectiveId = null; 437 AboutInfo[] welcomeInfos = getWelcomePerspectiveInfos(); 438 if (index >= 0 && welcomeInfos != null && index < welcomeInfos.length) { 439 perspectiveId = welcomeInfos[index].getWelcomePerspectiveId(); 440 } 441 if (perspectiveId == null) { 442 perspectiveId = IDE.RESOURCE_PERSPECTIVE_ID; 443 } 444 return perspectiveId; 445 } 446 447 456 private Map computeBundleGroupMap() { 457 Map ids = new TreeMap (); 459 460 IBundleGroupProvider[] providers = Platform.getBundleGroupProviders(); 461 for (int i = 0; i < providers.length; ++i) { 462 IBundleGroup[] groups = providers[i].getBundleGroups(); 463 for (int j = 0; j < groups.length; ++j) { 464 IBundleGroup group = groups[j]; 465 AboutInfo info = new AboutInfo(group); 466 467 String version = info.getVersionId(); 468 version = version == null ? "0.0.0" : new PluginVersionIdentifier(version).toString(); 470 String versionedFeature = group.getIdentifier() + ":" + version; 472 ids.put(versionedFeature, info); 473 } 474 } 475 476 return ids; 477 } 478 479 487 public Map getNewlyAddedBundleGroups() { 488 if (newlyAddedBundleGroups == null) { 489 newlyAddedBundleGroups = createNewBundleGroupsMap(); 490 } 491 return newlyAddedBundleGroups; 492 } 493 494 497 private Map createNewBundleGroupsMap() { 498 IDialogSettings settings = IDEWorkbenchPlugin.getDefault() 500 .getDialogSettings(); 501 String [] previousFeaturesArray = settings.getArray(INSTALLED_FEATURES); 502 503 Map bundleGroups = computeBundleGroupMap(); 506 String [] currentFeaturesArray = new String [bundleGroups.size()]; 507 bundleGroups.keySet().toArray(currentFeaturesArray); 508 settings.put(INSTALLED_FEATURES, currentFeaturesArray); 509 510 if (previousFeaturesArray != null) { 512 for (int i = 0; i < previousFeaturesArray.length; ++i) { 513 bundleGroups.remove(previousFeaturesArray[i]); 514 } 515 } 516 517 return bundleGroups; 518 } 519 520 527 private void declareWorkbenchImages() { 528 529 final String ICONS_PATH = "$nl$/icons/full/"; final String PATH_ELOCALTOOL = ICONS_PATH + "elcl16/"; final String PATH_DLOCALTOOL = ICONS_PATH + "dlcl16/"; final String PATH_ETOOL = ICONS_PATH + "etool16/"; final String PATH_DTOOL = ICONS_PATH + "dtool16/"; final String PATH_OBJECT = ICONS_PATH + "obj16/"; final String PATH_WIZBAN = ICONS_PATH + "wizban/"; 537 Bundle ideBundle = Platform.getBundle(IDEWorkbenchPlugin.IDE_WORKBENCH); 538 539 declareWorkbenchImage(ideBundle, 540 IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC, PATH_ETOOL 541 + "build_exec.gif", false); declareWorkbenchImage(ideBundle, 543 IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_HOVER, 544 PATH_ETOOL + "build_exec.gif", false); declareWorkbenchImage(ideBundle, 546 IDEInternalWorkbenchImages.IMG_ETOOL_BUILD_EXEC_DISABLED, 547 PATH_DTOOL + "build_exec.gif", false); 549 declareWorkbenchImage(ideBundle, 550 IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC, PATH_ETOOL 551 + "search_src.gif", false); declareWorkbenchImage(ideBundle, 553 IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC_HOVER, 554 PATH_ETOOL + "search_src.gif", false); declareWorkbenchImage(ideBundle, 556 IDEInternalWorkbenchImages.IMG_ETOOL_SEARCH_SRC_DISABLED, 557 PATH_DTOOL + "search_src.gif", false); 559 declareWorkbenchImage(ideBundle, 560 IDEInternalWorkbenchImages.IMG_ETOOL_NEXT_NAV, PATH_ETOOL 561 + "next_nav.gif", false); 563 declareWorkbenchImage(ideBundle, 564 IDEInternalWorkbenchImages.IMG_ETOOL_PREVIOUS_NAV, PATH_ETOOL 565 + "prev_nav.gif", false); 567 declareWorkbenchImage(ideBundle, 568 IDEInternalWorkbenchImages.IMG_WIZBAN_NEWPRJ_WIZ, PATH_WIZBAN 569 + "newprj_wiz.png", false); declareWorkbenchImage(ideBundle, 571 IDEInternalWorkbenchImages.IMG_WIZBAN_NEWFOLDER_WIZ, 572 PATH_WIZBAN + "newfolder_wiz.png", false); declareWorkbenchImage(ideBundle, 574 IDEInternalWorkbenchImages.IMG_WIZBAN_NEWFILE_WIZ, PATH_WIZBAN 575 + "newfile_wiz.png", false); 577 declareWorkbenchImage(ideBundle, 578 IDEInternalWorkbenchImages.IMG_WIZBAN_IMPORTDIR_WIZ, 579 PATH_WIZBAN + "importdir_wiz.png", false); declareWorkbenchImage(ideBundle, 581 IDEInternalWorkbenchImages.IMG_WIZBAN_IMPORTZIP_WIZ, 582 PATH_WIZBAN + "importzip_wiz.png", false); 584 declareWorkbenchImage(ideBundle, 585 IDEInternalWorkbenchImages.IMG_WIZBAN_EXPORTDIR_WIZ, 586 PATH_WIZBAN + "exportdir_wiz.png", false); declareWorkbenchImage(ideBundle, 588 IDEInternalWorkbenchImages.IMG_WIZBAN_EXPORTZIP_WIZ, 589 PATH_WIZBAN + "exportzip_wiz.png", false); 591 declareWorkbenchImage(ideBundle, 592 IDEInternalWorkbenchImages.IMG_WIZBAN_RESOURCEWORKINGSET_WIZ, 593 PATH_WIZBAN + "workset_wiz.png", false); 595 declareWorkbenchImage(ideBundle, 596 IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG, PATH_WIZBAN 597 + "saveas_wiz.png", false); 599 declareWorkbenchImage(ideBundle, 600 IDEInternalWorkbenchImages.IMG_DLGBAN_QUICKFIX_DLG, PATH_WIZBAN 601 + "quick_fix.png", false); 603 declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJ_PROJECT, 604 PATH_OBJECT + "prj_obj.gif", true); declareWorkbenchImage(ideBundle, 606 IDE.SharedImages.IMG_OBJ_PROJECT_CLOSED, PATH_OBJECT 607 + "cprj_obj.gif", true); declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OPEN_MARKER, 609 PATH_ELOCALTOOL + "gotoobj_tsk.gif", true); 611 declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_ELCL_QUICK_FIX_ENABLED, 612 PATH_ELOCALTOOL + "smartmode_co.gif", true); 614 declareWorkbenchImage(ideBundle, IDEInternalWorkbenchImages.IMG_DLCL_QUICK_FIX_DISABLED, 615 PATH_DLOCALTOOL + "smartmode_co.gif", true); 617 625 declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_TASK_TSK, 626 PATH_OBJECT + "taskmrk_tsk.gif", true); declareWorkbenchImage(ideBundle, IDE.SharedImages.IMG_OBJS_BKMRK_TSK, 628 PATH_OBJECT + "bkmrk_tsk.gif", true); 630 declareWorkbenchImage(ideBundle, 631 IDEInternalWorkbenchImages.IMG_OBJS_COMPLETE_TSK, PATH_OBJECT 632 + "complete_tsk.gif", true); declareWorkbenchImage(ideBundle, 634 IDEInternalWorkbenchImages.IMG_OBJS_INCOMPLETE_TSK, PATH_OBJECT 635 + "incomplete_tsk.gif", true); declareWorkbenchImage(ideBundle, 637 IDEInternalWorkbenchImages.IMG_OBJS_WELCOME_ITEM, PATH_OBJECT 638 + "welcome_item.gif", true); declareWorkbenchImage(ideBundle, 640 IDEInternalWorkbenchImages.IMG_OBJS_WELCOME_BANNER, PATH_OBJECT 641 + "welcome_banner.gif", true); declareWorkbenchImage(ideBundle, 643 IDEInternalWorkbenchImages.IMG_OBJS_ERROR_PATH, PATH_OBJECT 644 + "error_tsk.gif", true); declareWorkbenchImage(ideBundle, 646 IDEInternalWorkbenchImages.IMG_OBJS_WARNING_PATH, PATH_OBJECT 647 + "warn_tsk.gif", true); declareWorkbenchImage(ideBundle, 649 IDEInternalWorkbenchImages.IMG_OBJS_INFO_PATH, PATH_OBJECT 650 + "info_tsk.gif", true); 652 declareWorkbenchImage(ideBundle, 653 IDEInternalWorkbenchImages.IMG_LCL_FLAT_LAYOUT, PATH_ELOCALTOOL 654 + "flatLayout.gif", true); declareWorkbenchImage(ideBundle, 656 IDEInternalWorkbenchImages.IMG_LCL_HIERARCHICAL_LAYOUT, 657 PATH_ELOCALTOOL + "hierarchicalLayout.gif", true); declareWorkbenchImage(ideBundle, 659 IDEInternalWorkbenchImages.IMG_ETOOL_PROBLEM_CATEGORY, 660 PATH_ETOOL + "problem_category.gif", true); declareWorkbenchImage(ideBundle, 662 IDEInternalWorkbenchImages.IMG_LCL_LINKTO_HELP, PATH_ELOCALTOOL 663 + "linkto_help.gif", false); 665 673 } 681 682 695 private void declareWorkbenchImage(Bundle ideBundle, String symbolicName, 696 String path, boolean shared) { 697 URL url = Platform.find(ideBundle, new Path(path)); 698 ImageDescriptor desc = ImageDescriptor.createFromURL(url); 699 getWorkbenchConfigurer().declareImage(symbolicName, desc, shared); 700 } 701 702 707 public String getMainPreferencePageId() { 708 return WORKBENCH_PREFERENCE_CATEGORY_ID; 710 } 711 712 716 public String getWorkspaceLocation() { 717 return workspaceLocation; 718 } 719 720 724 public AboutInfo[] getWelcomePerspectiveInfos() { 725 if (welcomePerspectiveInfos == null) { 726 if (!hasIntro()) { 728 Map m = getNewlyAddedBundleGroups(); 729 ArrayList list = new ArrayList (m.size()); 730 for (Iterator i = m.values().iterator(); i.hasNext();) { 731 AboutInfo info = (AboutInfo) i.next(); 732 if (info != null && info.getWelcomePerspectiveId() != null 733 && info.getWelcomePageURL() != null) { 734 list.add(info); 735 } 736 } 737 welcomePerspectiveInfos = new AboutInfo[list.size()]; 738 list.toArray(welcomePerspectiveInfos); 739 } 740 } 741 return welcomePerspectiveInfos; 742 } 743 } 744 | Popular Tags |