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