1 11 package org.eclipse.ui.internal.ide.application; 12 13 import java.net.URL ; 14 import java.util.ArrayList ; 15 import java.util.Iterator ; 16 import java.util.List ; 17 import java.util.Map ; 18 19 import org.eclipse.core.runtime.IAdaptable; 20 import org.eclipse.core.runtime.IProduct; 21 import org.eclipse.core.runtime.IStatus; 22 import org.eclipse.core.runtime.Platform; 23 import org.eclipse.core.runtime.Status; 24 import org.eclipse.jface.action.IAction; 25 import org.eclipse.jface.action.ToolBarManager; 26 import org.eclipse.jface.dialogs.ErrorDialog; 27 import org.eclipse.jface.dialogs.IDialogConstants; 28 import org.eclipse.jface.dialogs.MessageDialogWithToggle; 29 import org.eclipse.jface.preference.IPreferenceStore; 30 import org.eclipse.jface.resource.JFaceResources; 31 import org.eclipse.osgi.util.NLS; 32 import org.eclipse.swt.SWT; 33 import org.eclipse.swt.dnd.FileTransfer; 34 import org.eclipse.swt.graphics.Color; 35 import org.eclipse.swt.layout.GridLayout; 36 import org.eclipse.swt.widgets.Composite; 37 import org.eclipse.swt.widgets.Control; 38 import org.eclipse.swt.widgets.Display; 39 import org.eclipse.swt.widgets.Label; 40 import org.eclipse.swt.widgets.ToolBar; 41 import org.eclipse.ui.IEditorPart; 42 import org.eclipse.ui.IEditorReference; 43 import org.eclipse.ui.IPageListener; 44 import org.eclipse.ui.IPartListener2; 45 import org.eclipse.ui.IPerspectiveDescriptor; 46 import org.eclipse.ui.IPerspectiveRegistry; 47 import org.eclipse.ui.IPropertyListener; 48 import org.eclipse.ui.IWorkbench; 49 import org.eclipse.ui.IWorkbenchPage; 50 import org.eclipse.ui.IWorkbenchPartConstants; 51 import org.eclipse.ui.IWorkbenchPartReference; 52 import org.eclipse.ui.IWorkbenchWindow; 53 import org.eclipse.ui.PartInitException; 54 import org.eclipse.ui.PerspectiveAdapter; 55 import org.eclipse.ui.WorkbenchException; 56 import org.eclipse.ui.actions.ActionFactory; 57 import org.eclipse.ui.application.ActionBarAdvisor; 58 import org.eclipse.ui.application.IActionBarConfigurer; 59 import org.eclipse.ui.application.IWorkbenchWindowConfigurer; 60 import org.eclipse.ui.application.WorkbenchWindowAdvisor; 61 import org.eclipse.ui.internal.ide.AboutInfo; 62 import org.eclipse.ui.internal.ide.EditorAreaDropAdapter; 63 import org.eclipse.ui.internal.ide.IDEInternalPreferences; 64 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages; 65 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin; 66 import org.eclipse.ui.internal.ide.WorkbenchActionBuilder; 67 import org.eclipse.ui.internal.ide.dialogs.WelcomeEditorInput; 68 import org.eclipse.ui.part.EditorInputTransfer; 69 import org.eclipse.ui.part.MarkerTransfer; 70 import org.eclipse.ui.part.ResourceTransfer; 71 import org.eclipse.ui.statushandlers.StatusManager; 72 import org.eclipse.update.configurator.ConfiguratorUtils; 73 import org.eclipse.update.configurator.IPlatformConfiguration; 74 import org.osgi.framework.Bundle; 75 import org.osgi.framework.BundleException; 76 77 80 public class IDEWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor { 81 82 private static final String WELCOME_EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; 84 private IDEWorkbenchAdvisor wbAdvisor; 85 private boolean editorsAndIntrosOpened = false; 86 private IEditorPart lastActiveEditor = null; 87 private IPerspectiveDescriptor lastPerspective = null; 88 89 private IWorkbenchPage lastActivePage; 90 private String lastEditorTitle = ""; 92 private IPropertyListener editorPropertyListener = new IPropertyListener() { 93 public void propertyChanged(Object source, int propId) { 94 if (propId == IWorkbenchPartConstants.PROP_TITLE) { 95 if (lastActiveEditor != null) { 96 String newTitle = lastActiveEditor.getTitle(); 97 if (!lastEditorTitle.equals(newTitle)) { 98 recomputeTitle(); 99 } 100 } 101 } 102 } 103 }; 104 105 private IAdaptable lastInput; 106 107 115 public IDEWorkbenchWindowAdvisor(IDEWorkbenchAdvisor wbAdvisor, 116 IWorkbenchWindowConfigurer configurer) { 117 super(configurer); 118 this.wbAdvisor = wbAdvisor; 119 } 120 121 126 public ActionBarAdvisor createActionBarAdvisor( 127 IActionBarConfigurer configurer) { 128 return new WorkbenchActionBuilder(configurer); 129 } 130 131 136 private IWorkbench getWorkbench() { 137 return getWindowConfigurer().getWorkbenchConfigurer().getWorkbench(); 138 } 139 140 145 public boolean preWindowShellClose() { 146 if (getWorkbench().getWorkbenchWindowCount() > 1) { 147 return true; 148 } 149 IPreferenceStore store = IDEWorkbenchPlugin.getDefault() 152 .getPreferenceStore(); 153 boolean promptOnExit = store 154 .getBoolean(IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW); 155 156 if (promptOnExit) { 157 String message; 158 159 String productName = null; 160 IProduct product = Platform.getProduct(); 161 if (product != null) { 162 productName = product.getName(); 163 } 164 if (productName == null) { 165 message = IDEWorkbenchMessages.PromptOnExitDialog_message0; 166 } else { 167 message = NLS.bind( 168 IDEWorkbenchMessages.PromptOnExitDialog_message1, 169 productName); 170 } 171 172 MessageDialogWithToggle dlg = MessageDialogWithToggle 173 .openOkCancelConfirm(getWindowConfigurer().getWindow() 174 .getShell(), 175 IDEWorkbenchMessages.PromptOnExitDialog_shellTitle, 176 message, 177 IDEWorkbenchMessages.PromptOnExitDialog_choice, 178 false, null, null); 179 if (dlg.getReturnCode() != IDialogConstants.OK_ID) { 180 return false; 181 } 182 if (dlg.getToggleState()) { 183 store 184 .setValue( 185 IDEInternalPreferences.EXIT_PROMPT_ON_CLOSE_LAST_WINDOW, 186 false); 187 IDEWorkbenchPlugin.getDefault().savePluginPreferences(); 188 } 189 } 190 191 return true; 192 } 193 194 199 public void preWindowOpen() { 200 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 201 202 configurer.setShowPerspectiveBar(true); 205 configurer.setShowFastViewBars(true); 206 configurer.setShowProgressIndicator(true); 207 208 configurer.addEditorAreaTransfer(EditorInputTransfer.getInstance()); 210 configurer.addEditorAreaTransfer(ResourceTransfer.getInstance()); 211 configurer.addEditorAreaTransfer(FileTransfer.getInstance()); 212 configurer.addEditorAreaTransfer(MarkerTransfer.getInstance()); 213 configurer.configureEditorAreaDropListener(new EditorAreaDropAdapter( 214 configurer.getWindow())); 215 216 hookTitleUpdateListeners(configurer); 217 } 218 219 224 private void hookTitleUpdateListeners(IWorkbenchWindowConfigurer configurer) { 225 configurer.getWindow().addPageListener(new IPageListener() { 227 public void pageActivated(IWorkbenchPage page) { 228 updateTitle(); 229 } 230 231 public void pageClosed(IWorkbenchPage page) { 232 updateTitle(); 233 } 234 235 public void pageOpened(IWorkbenchPage page) { 236 } 238 }); 239 configurer.getWindow().addPerspectiveListener(new PerspectiveAdapter() { 240 public void perspectiveActivated(IWorkbenchPage page, 241 IPerspectiveDescriptor perspective) { 242 updateTitle(); 243 } 244 245 public void perspectiveSavedAs(IWorkbenchPage page, 246 IPerspectiveDescriptor oldPerspective, 247 IPerspectiveDescriptor newPerspective) { 248 updateTitle(); 249 } 250 251 public void perspectiveDeactivated(IWorkbenchPage page, 252 IPerspectiveDescriptor perspective) { 253 updateTitle(); 254 } 255 }); 256 configurer.getWindow().getPartService().addPartListener( 257 new IPartListener2() { 258 public void partActivated(IWorkbenchPartReference ref) { 259 if (ref instanceof IEditorReference) { 260 updateTitle(); 261 } 262 } 263 264 public void partBroughtToTop(IWorkbenchPartReference ref) { 265 if (ref instanceof IEditorReference) { 266 updateTitle(); 267 } 268 } 269 270 public void partClosed(IWorkbenchPartReference ref) { 271 updateTitle(); 272 } 273 274 public void partDeactivated(IWorkbenchPartReference ref) { 275 } 277 278 public void partOpened(IWorkbenchPartReference ref) { 279 } 281 282 public void partHidden(IWorkbenchPartReference ref) { 283 } 285 286 public void partVisible(IWorkbenchPartReference ref) { 287 } 289 290 public void partInputChanged(IWorkbenchPartReference ref) { 291 } 293 }); 294 } 295 296 private String computeTitle() { 297 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 298 IWorkbenchPage currentPage = configurer.getWindow().getActivePage(); 299 IEditorPart activeEditor = null; 300 if (currentPage != null) { 301 activeEditor = currentPage.getActiveEditor(); 302 } 303 304 String title = null; 305 IProduct product = Platform.getProduct(); 306 if (product != null) { 307 title = product.getName(); 308 } 309 if (title == null) { 310 title = ""; } 312 313 if (currentPage != null) { 314 if (activeEditor != null) { 315 lastEditorTitle = activeEditor.getTitleToolTip(); 316 title = NLS.bind( 317 IDEWorkbenchMessages.WorkbenchWindow_shellTitle, 318 lastEditorTitle, title); 319 } 320 IPerspectiveDescriptor persp = currentPage.getPerspective(); 321 String label = ""; if (persp != null) { 323 label = persp.getLabel(); 324 } 325 IAdaptable input = currentPage.getInput(); 326 if (input != null && !input.equals(wbAdvisor.getDefaultPageInput())) { 327 label = currentPage.getLabel(); 328 } 329 if (label != null && !label.equals("")) { title = NLS.bind( 331 IDEWorkbenchMessages.WorkbenchWindow_shellTitle, label, 332 title); 333 } 334 } 335 336 String workspaceLocation = wbAdvisor.getWorkspaceLocation(); 337 if (workspaceLocation != null) { 338 title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, 339 title, workspaceLocation); 340 } 341 342 return title; 343 } 344 345 private void recomputeTitle() { 346 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 347 String oldTitle = configurer.getTitle(); 348 String newTitle = computeTitle(); 349 if (!newTitle.equals(oldTitle)) { 350 configurer.setTitle(newTitle); 351 } 352 } 353 354 358 private void updateTitle() { 359 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 360 IWorkbenchWindow window = configurer.getWindow(); 361 IEditorPart activeEditor = null; 362 IWorkbenchPage currentPage = window.getActivePage(); 363 IPerspectiveDescriptor persp = null; 364 IAdaptable input = null; 365 366 if (currentPage != null) { 367 activeEditor = currentPage.getActiveEditor(); 368 persp = currentPage.getPerspective(); 369 input = currentPage.getInput(); 370 } 371 372 if (activeEditor == lastActiveEditor && currentPage == lastActivePage 374 && persp == lastPerspective && input == lastInput) { 375 return; 376 } 377 378 if (lastActiveEditor != null) { 379 lastActiveEditor.removePropertyListener(editorPropertyListener); 380 } 381 382 lastActiveEditor = activeEditor; 383 lastActivePage = currentPage; 384 lastPerspective = persp; 385 lastInput = input; 386 387 if (activeEditor != null) { 388 activeEditor.addPropertyListener(editorPropertyListener); 389 } 390 391 recomputeTitle(); 392 } 393 394 399 public void postWindowRestore() throws WorkbenchException { 400 IWorkbenchWindowConfigurer configurer = getWindowConfigurer(); 401 IWorkbenchWindow window = configurer.getWindow(); 402 403 int index = getWorkbench().getWorkbenchWindowCount() - 1; 404 405 AboutInfo[] welcomePerspectiveInfos = wbAdvisor 406 .getWelcomePerspectiveInfos(); 407 if (index >= 0 && welcomePerspectiveInfos != null 408 && index < welcomePerspectiveInfos.length) { 409 IWorkbenchPage page = window.getActivePage(); 411 if (page == null) { 412 IWorkbenchPage[] pages = window.getPages(); 413 if (pages != null && pages.length > 0) { 414 page = pages[0]; 415 } 416 } 417 418 String perspectiveId = welcomePerspectiveInfos[index] 420 .getWelcomePerspectiveId(); 421 if (page == null) { 422 IAdaptable root = wbAdvisor.getDefaultPageInput(); 423 page = window.openPage(perspectiveId, root); 424 } else { 425 IPerspectiveRegistry reg = getWorkbench() 426 .getPerspectiveRegistry(); 427 IPerspectiveDescriptor desc = reg 428 .findPerspectiveWithId(perspectiveId); 429 if (desc != null) { 430 page.setPerspective(desc); 431 } 432 } 433 434 window.setActivePage(page); 436 page.openEditor(new WelcomeEditorInput( 437 welcomePerspectiveInfos[index]), WELCOME_EDITOR_ID, true); 438 } 439 } 440 441 447 public void openIntro() { 448 if (editorsAndIntrosOpened) { 449 return; 450 } 451 452 editorsAndIntrosOpened = true; 453 454 if (wbAdvisor.hasIntro()) { 456 super.openIntro(); 457 } else { 458 openWelcomeEditors(getWindowConfigurer().getWindow()); 459 IDEWorkbenchPlugin.getDefault().savePluginPreferences(); 461 } 462 } 463 464 468 private void openWelcomeEditors(IWorkbenchWindow window) { 469 if (IDEWorkbenchPlugin.getDefault().getPreferenceStore().getBoolean( 470 IDEInternalPreferences.WELCOME_DIALOG)) { 471 IProduct product = Platform.getProduct(); 474 if (product == null) { 475 return; 476 } 477 478 AboutInfo productInfo = new AboutInfo(product); 479 URL url = productInfo.getWelcomePageURL(); 480 if (url == null) { 481 return; 482 } 483 484 IDEWorkbenchPlugin.getDefault().getPreferenceStore().setValue( 485 IDEInternalPreferences.WELCOME_DIALOG, false); 486 openWelcomeEditor(window, new WelcomeEditorInput(productInfo), null); 487 } else { 488 List welcomeFeatures = new ArrayList (); 490 for (Iterator it = wbAdvisor.getNewlyAddedBundleGroups().entrySet() 491 .iterator(); it.hasNext();) { 492 Map.Entry entry = (Map.Entry ) it.next(); 493 String versionedId = (String ) entry.getKey(); 494 String featureId = versionedId.substring(0, versionedId 495 .indexOf(':')); 496 AboutInfo info = (AboutInfo) entry.getValue(); 497 498 if (info != null && info.getWelcomePageURL() != null) { 499 welcomeFeatures.add(info); 500 IPlatformConfiguration platformConfiguration = ConfiguratorUtils 503 .getCurrentPlatformConfiguration(); 504 IPlatformConfiguration.IFeatureEntry feature = platformConfiguration 505 .findConfiguredFeatureEntry(featureId); 506 if (feature != null) { 507 String pi = feature.getFeaturePluginIdentifier(); 508 if (pi != null) { 509 Bundle bundle = Platform.getBundle(pi); 511 if (bundle != null) { 512 try { 513 bundle.start(Bundle.START_TRANSIENT); 514 } catch (BundleException exception) { 515 StatusManager 516 .getManager() 517 .handle( 518 new Status( 519 IStatus.ERROR, 520 IDEApplication.PLUGIN_ID, 521 "Failed to load feature", exception)); } 523 } 524 } 525 } 526 } 527 } 528 529 int wCount = getWorkbench().getWorkbenchWindowCount(); 530 for (int i = 0; i < welcomeFeatures.size(); i++) { 531 AboutInfo newInfo = (AboutInfo) welcomeFeatures.get(i); 532 String id = newInfo.getWelcomePerspectiveId(); 533 if (id == null || i >= wCount) { 535 openWelcomeEditor(window, new WelcomeEditorInput(newInfo), 536 id); 537 } 538 } 539 } 540 } 541 542 545 private void openWelcomeEditor(IWorkbenchWindow window, 546 WelcomeEditorInput input, String perspectiveId) { 547 if (getWorkbench().getWorkbenchWindowCount() == 0) { 548 return; 551 } 552 553 IWorkbenchWindow win = window; 554 if (perspectiveId != null) { 555 try { 556 win = getWorkbench().openWorkbenchWindow(perspectiveId, 557 wbAdvisor.getDefaultPageInput()); 558 if (win == null) { 559 win = window; 560 } 561 } catch (WorkbenchException e) { 562 IDEWorkbenchPlugin 563 .log( 564 "Error opening window with welcome perspective.", e.getStatus()); return; 566 } 567 } 568 569 if (win == null) { 570 win = getWorkbench().getWorkbenchWindows()[0]; 571 } 572 573 IWorkbenchPage page = win.getActivePage(); 574 String id = perspectiveId; 575 if (id == null) { 576 id = getWorkbench().getPerspectiveRegistry() 577 .getDefaultPerspective(); 578 } 579 580 if (page == null) { 581 try { 582 page = win.openPage(id, wbAdvisor.getDefaultPageInput()); 583 } catch (WorkbenchException e) { 584 ErrorDialog.openError(win.getShell(), 585 IDEWorkbenchMessages.Problems_Opening_Page, e 586 .getMessage(), e.getStatus()); 587 } 588 } 589 if (page == null) { 590 return; 591 } 592 593 if (page.getPerspective() == null) { 594 try { 595 page = getWorkbench().showPerspective(id, win); 596 } catch (WorkbenchException e) { 597 ErrorDialog 598 .openError( 599 win.getShell(), 600 IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle, 601 IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage, 602 e.getStatus()); 603 return; 604 } 605 } 606 607 page.setEditorAreaVisible(true); 608 609 IEditorPart editor = page.findEditor(input); 611 if (editor != null) { 612 page.activate(editor); 613 return; 614 } 615 616 try { 617 page.openEditor(input, WELCOME_EDITOR_ID); 618 } catch (PartInitException e) { 619 ErrorDialog 620 .openError( 621 win.getShell(), 622 IDEWorkbenchMessages.Workbench_openEditorErrorDialogTitle, 623 IDEWorkbenchMessages.Workbench_openEditorErrorDialogMessage, 624 e.getStatus()); 625 } 626 return; 627 } 628 629 635 public Control createEmptyWindowContents(Composite parent) { 636 final IWorkbenchWindow window = getWindowConfigurer().getWindow(); 637 Composite composite = new Composite(parent, SWT.NONE); 638 composite.setLayout(new GridLayout(2, false)); 639 Display display = composite.getDisplay(); 640 Color bgCol = display 641 .getSystemColor(SWT.COLOR_TITLE_INACTIVE_BACKGROUND); 642 composite.setBackground(bgCol); 643 Label label = new Label(composite, SWT.WRAP); 644 label.setForeground(display 645 .getSystemColor(SWT.COLOR_TITLE_INACTIVE_FOREGROUND)); 646 label.setBackground(bgCol); 647 label.setFont(JFaceResources.getFontRegistry().getBold( 648 JFaceResources.DEFAULT_FONT)); 649 String msg = IDEWorkbenchMessages.IDEWorkbenchAdvisor_noPerspective; 650 label.setText(msg); 651 ToolBarManager toolBarManager = new ToolBarManager(); 652 IAction openPerspectiveAction = ActionFactory.OPEN_PERSPECTIVE_DIALOG 654 .create(window); 655 toolBarManager.add(openPerspectiveAction); 656 ToolBar toolBar = toolBarManager.createControl(composite); 657 toolBar.setBackground(bgCol); 658 return composite; 659 } 660 661 } 662 | Popular Tags |