KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > ide > IDEWorkbenchWindowAdvisor


1 /*******************************************************************************
2  * Copyright (c) 2005, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.ide;
12
13 import java.net.URL JavaDoc;
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17 import java.util.Map JavaDoc;
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 /**
68  * Window-level advisor for the IDE.
69  */

70 public class IDEWorkbenchWindowAdvisor extends WorkbenchWindowAdvisor {
71
72     private static final String JavaDoc WELCOME_EDITOR_ID = "org.eclipse.ui.internal.ide.dialogs.WelcomeEditor"; //$NON-NLS-1$
73

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 JavaDoc lastEditorTitle = ""; //$NON-NLS-1$
81

82     private IPropertyListener editorPropertyListener = new IPropertyListener() {
83         public void propertyChanged(Object JavaDoc source, int propId) {
84             if (propId == IWorkbenchPartConstants.PROP_TITLE) {
85                 if (lastActiveEditor != null) {
86                     String JavaDoc newTitle = lastActiveEditor.getTitle();
87                     if (!lastEditorTitle.equals(newTitle)) {
88                         recomputeTitle();
89                     }
90                 }
91             }
92         }
93     };
94
95     private IAdaptable lastInput;
96
97     /**
98      * Crates a new IDE workbench window advisor.
99      *
100      * @param wbAdvisor the workbench advisor
101      * @param configurer the window configurer
102      */

103     public IDEWorkbenchWindowAdvisor(IDEWorkbenchAdvisor wbAdvisor, IWorkbenchWindowConfigurer configurer) {
104         super(configurer);
105         this.wbAdvisor = wbAdvisor;
106     }
107
108     /* (non-Javadoc)
109      * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#createActionBarAdvisor(org.eclipse.ui.application.IActionBarConfigurer)
110      */

111     public ActionBarAdvisor createActionBarAdvisor(IActionBarConfigurer configurer) {
112         return new WorkbenchActionBuilder(configurer);
113     }
114     
115     /**
116      * Returns the workbench.
117      *
118      * @return the workbench
119      */

120     private IWorkbench getWorkbench() {
121         return getWindowConfigurer().getWorkbenchConfigurer().getWorkbench();
122     }
123     
124     /*
125      * (non-Javadoc)
126      *
127      * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowShellClose
128      */

129     public boolean preWindowShellClose() {
130         if (getWorkbench().getWorkbenchWindowCount() > 1) {
131             return true;
132         }
133         // the user has asked to close the last window, while will cause the
134
// workbench to close in due course - prompt the user for confirmation
135
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 JavaDoc message;
142
143             String JavaDoc 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     /* (non-Javadoc)
175      * @see org.eclipse.ui.application.WorkbenchAdvisor#preWindowOpen
176      */

177     public void preWindowOpen() {
178         IWorkbenchWindowConfigurer configurer = getWindowConfigurer();
179
180         // show the shortcut bar and progress indicator, which are hidden by default
181
configurer.setShowPerspectiveBar(true);
182         configurer.setShowFastViewBars(true);
183         configurer.setShowProgressIndicator(true);
184
185         // add the drag and drop support for the editor area
186
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     /**
198      * Hooks the listeners needed on the window
199      * @param configurer
200      */

201     private void hookTitleUpdateListeners(IWorkbenchWindowConfigurer configurer) {
202         // hook up the listeners to update the window title
203
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                 // do nothing
214
}
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                         // do nothing
249
}
250
251                     public void partOpened(IWorkbenchPartReference ref) {
252                         // do nothing
253
}
254
255                     public void partHidden(IWorkbenchPartReference ref) {
256                         // do nothing
257
}
258
259                     public void partVisible(IWorkbenchPartReference ref) {
260                         // do nothing
261
}
262
263                     public void partInputChanged(IWorkbenchPartReference ref) {
264                         // do nothing
265
}
266                 });
267     }
268
269     private String JavaDoc 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 JavaDoc title = null;
278         IProduct product = Platform.getProduct();
279         if (product != null) {
280             title = product.getName();
281         }
282         if (title == null) {
283             title = ""; //$NON-NLS-1$
284
}
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 JavaDoc label = ""; //$NON-NLS-1$
293
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("")) { //$NON-NLS-1$
301
title = NLS.bind(IDEWorkbenchMessages.WorkbenchWindow_shellTitle, label, title);
302             }
303         }
304
305         String JavaDoc 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 JavaDoc oldTitle = configurer.getTitle();
316         String JavaDoc newTitle = computeTitle();
317         if (!newTitle.equals(oldTitle)) {
318             configurer.setTitle(newTitle);
319         }
320     }
321         
322     /**
323      * Updates the window title. Format will be:
324      * [pageInput -] [currentPerspective -] [editorInput -] [workspaceLocation -] productName
325      */

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         // Nothing to do if the editor hasn't changed
341
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     /* (non-Javadoc)
364      * @see org.eclipse.ui.application.WorkbenchAdvisor#postWindowRestore
365      */

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             // find a page that exist in the window
377
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             // if the window does not contain a page, create one
386
String JavaDoc 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             // set the active page and open the welcome editor
402
window.setActivePage(page);
403             page.openEditor(new WelcomeEditorInput(
404                     welcomePerspectiveInfos[index]),
405                     WELCOME_EDITOR_ID, true);
406         }
407     }
408
409     /**
410      * Tries to open the intro, if one exists and otherwise will open the legacy
411      * Welcome pages.
412      *
413      * @see org.eclipse.ui.application.WorkbenchWindowAdvisor#openIntro()
414      */

415     public void openIntro() {
416         if (editorsAndIntrosOpened ) {
417             return;
418         }
419
420         editorsAndIntrosOpened = true;
421
422         // don't try to open the welcome editors if there is an intro
423
if (wbAdvisor.hasIntro()) {
424             super.openIntro();
425         } else {
426             openWelcomeEditors(getWindowConfigurer().getWindow());
427             // save any preferences changes caused by the above actions
428
IDEWorkbenchPlugin.getDefault().savePluginPreferences();
429         }
430     }
431
432     /*
433      * Open the welcome editor for the primary feature and
434      * for any newly installed features.
435      */

436     private void openWelcomeEditors(IWorkbenchWindow window) {
437         if (IDEWorkbenchPlugin.getDefault().getPreferenceStore().getBoolean(
438                 IDEInternalPreferences.WELCOME_DIALOG)) {
439             // show the welcome page for the product the first time the workbench opens
440
IProduct product = Platform.getProduct();
441             if (product == null) {
442                 return;
443             }
444
445             AboutInfo productInfo = new AboutInfo(product);
446             URL JavaDoc 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             // Show the welcome page for any newly installed features
456
List JavaDoc welcomeFeatures = new ArrayList JavaDoc();
457             for (Iterator JavaDoc it = wbAdvisor.getNewlyAddedBundleGroups().entrySet()
458                     .iterator(); it.hasNext();) {
459                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
460                 String JavaDoc versionedId = (String JavaDoc) entry.getKey();
461                 String JavaDoc 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                     // activate the feature plug-in so it can run some install code
468
IPlatformConfiguration platformConfiguration = ConfiguratorUtils
469                             .getCurrentPlatformConfiguration();
470                     IPlatformConfiguration.IFeatureEntry feature = platformConfiguration
471                             .findConfiguredFeatureEntry(featureId);
472                     if (feature != null) {
473                         String JavaDoc pi = feature.getFeaturePluginIdentifier();
474                         if (pi != null) {
475                             //Start the bundle if there is one
476
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);//$NON-NLS-1$
483
}
484                                 finally {
485                                     try {
486                                         bundle.stop();
487                                     } catch (BundleException ex) {
488                                         IDEWorkbenchPlugin.log("Failed to stop feature", ex);//$NON-NLS-1$
489
}
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 JavaDoc id = newInfo.getWelcomePerspectiveId();
501                 // Other editors were already opened in postWindowRestore(..)
502
if (id == null || i >= wCount) {
503                     openWelcomeEditor(window, new WelcomeEditorInput(newInfo),
504                             id);
505                 }
506             }
507         }
508     }
509
510     /**
511      * Open a welcome editor for the given input
512      */

513     private void openWelcomeEditor(IWorkbenchWindow window,
514             WelcomeEditorInput input, String JavaDoc perspectiveId) {
515         if (getWorkbench().getWorkbenchWindowCount() == 0) {
516             // Something is wrong, there should be at least
517
// one workbench window open by now.
518
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()); //$NON-NLS-1$
533
return;
534             }
535         }
536
537         if (win == null) {
538             win = getWorkbench().getWorkbenchWindows()[0];
539         }
540
541         IWorkbenchPage page = win.getActivePage();
542         String JavaDoc 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         // see if we already have an editor
577
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     /* (non-Javadoc)
597      * @see org.eclipse.ui.application.WorkbenchAdvisor#createEmptyWindowContents(org.eclipse.ui.application.IWorkbenchWindowConfigurer, org.eclipse.swt.widgets.Composite)
598      */

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 JavaDoc msg = IDEWorkbenchMessages.IDEWorkbenchAdvisor_noPerspective;
611         label.setText(msg);
612         ToolBarManager toolBarManager = new ToolBarManager();
613         // TODO: should obtain the open perspective action from ActionFactory
614
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