KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > WorkbenchWindowConfigurer


1 /*******************************************************************************
2  * Copyright (c) 2003, 2007 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;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Map JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18 import org.eclipse.jface.action.CoolBarManager;
19 import org.eclipse.jface.action.IAction;
20 import org.eclipse.jface.action.IContributionItem;
21 import org.eclipse.jface.action.ICoolBarManager;
22 import org.eclipse.jface.action.IMenuManager;
23 import org.eclipse.jface.action.IStatusLineManager;
24 import org.eclipse.jface.action.IToolBarManager;
25 import org.eclipse.jface.internal.provisional.action.ICoolBarManager2;
26 import org.eclipse.jface.internal.provisional.action.IToolBarContributionItem;
27 import org.eclipse.jface.window.Window;
28 import org.eclipse.osgi.util.TextProcessor;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.dnd.DropTarget;
31 import org.eclipse.swt.dnd.DropTargetListener;
32 import org.eclipse.swt.dnd.Transfer;
33 import org.eclipse.swt.graphics.Point;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Menu;
37 import org.eclipse.swt.widgets.Shell;
38 import org.eclipse.ui.IMemento;
39 import org.eclipse.ui.IWorkbenchPage;
40 import org.eclipse.ui.IWorkbenchPreferenceConstants;
41 import org.eclipse.ui.IWorkbenchWindow;
42 import org.eclipse.ui.application.IActionBarConfigurer;
43 import org.eclipse.ui.application.IWorkbenchConfigurer;
44 import org.eclipse.ui.application.IWorkbenchWindowConfigurer;
45 import org.eclipse.ui.application.WorkbenchAdvisor;
46 import org.eclipse.ui.internal.StartupThreading.StartupRunnable;
47 import org.eclipse.ui.internal.presentations.r33.WorkbenchPresentationFactory_33;
48 import org.eclipse.ui.internal.provisional.application.IActionBarConfigurer2;
49 import org.eclipse.ui.internal.provisional.presentations.IActionBarPresentationFactory;
50 import org.eclipse.ui.internal.util.PrefUtil;
51 import org.eclipse.ui.presentations.AbstractPresentationFactory;
52
53 /**
54  * Internal class providing special access for configuring workbench windows.
55  * <p>
56  * Note that these objects are only available to the main application
57  * (the plug-in that creates and owns the workbench).
58  * </p>
59  * <p>
60  * This class is not intended to be instantiated or subclassed by clients.
61  * </p>
62  *
63  * @since 3.0
64  */

65 public final class WorkbenchWindowConfigurer implements
66         IWorkbenchWindowConfigurer {
67
68     /**
69      * The workbench window associated with this configurer.
70      */

71     private WorkbenchWindow window;
72
73     /**
74      * The shell style bits to use when the window's shell is being created.
75      */

76     private int shellStyle = SWT.SHELL_TRIM | Window.getDefaultOrientation();
77
78     /**
79      * The window title to set when the window's shell has been created.
80      */

81     private String JavaDoc windowTitle;
82
83     /**
84      * Whether the workbench window should show the fast view bars.
85      */

86     private boolean showFastViewBars = false;
87
88     /**
89      * Whether the workbench window should show the perspective bar
90      */

91     private boolean showPerspectiveBar = false;
92
93     /**
94      * Whether the workbench window should show the status line.
95      */

96     private boolean showStatusLine = true;
97
98     /**
99      * Whether the workbench window should show the main tool bar.
100      */

101     private boolean showToolBar = true;
102
103     /**
104      * Whether the workbench window should show the main menu bar.
105      */

106     private boolean showMenuBar = true;
107
108     /**
109      * Whether the workbench window should have a progress indicator.
110      */

111     private boolean showProgressIndicator = false;
112
113     /**
114      * Table to hold arbitrary key-data settings (key type: <code>String</code>,
115      * value type: <code>Object</code>).
116      * @see #setData
117      */

118     private Map JavaDoc extraData = new HashMap JavaDoc(1);
119
120     /**
121      * Holds the list drag and drop <code>Transfer</code> for the
122      * editor area
123      */

124     private ArrayList JavaDoc transferTypes = new ArrayList JavaDoc(3);
125
126     /**
127      * The <code>DropTargetListener</code> implementation for handling a
128      * drop into the editor area.
129      */

130     private DropTargetListener dropTargetListener = null;
131
132     /**
133      * Object for configuring this workbench window's action bars.
134      * Lazily initialized to an instance unique to this window.
135      */

136     private WindowActionBarConfigurer actionBarConfigurer = null;
137
138     /**
139      * The initial size to use for the shell.
140      */

141     private Point initialSize = new Point(1024, 768);
142
143     /**
144      * The presentation factory. Lazily initialized in getPresentationFactory
145      * if not already assigned in setPresentationFactory.
146      */

147     private AbstractPresentationFactory presentationFactory = null;
148
149     /**
150      * Action bar configurer that changes this workbench window.
151      * This implementation keeps track of of cool bar items
152      */

153     class WindowActionBarConfigurer implements IActionBarConfigurer2 {
154
155         private IActionBarConfigurer2 proxy;
156         
157         /**
158          * Sets the proxy to use, or <code>null</code> for none.
159          *
160          * @param proxy the proxy
161          */

162         public void setProxy(IActionBarConfigurer2 proxy) {
163             this.proxy = proxy;
164         }
165         
166         /* (non-Javadoc)
167          * @see org.eclipse.ui.application.IActionBarConfigurer#getWindowConfigurer()
168          */

169         public IWorkbenchWindowConfigurer getWindowConfigurer() {
170             return window.getWindowConfigurer();
171         }
172         
173         /**
174          * Returns whether the given id is for a cool item.
175          *
176          * @param the item id
177          * @return <code>true</code> if it is a cool item,
178          * and <code>false</code> otherwise
179          */

180         /* package */boolean containsCoolItem(String JavaDoc id) {
181             ICoolBarManager cbManager = getCoolBarManager();
182             if (cbManager == null) {
183                 return false;
184             }
185             IContributionItem cbItem = cbManager.find(id);
186             if (cbItem == null) {
187                 return false;
188             }
189             //@ issue: maybe we should check if cbItem is visible?
190
return true;
191         }
192
193         /* (non-Javadoc)
194          * @see org.eclipse.ui.application.IActionBarConfigurer
195          */

196         public IStatusLineManager getStatusLineManager() {
197             if (proxy != null) {
198                 return proxy.getStatusLineManager();
199             }
200             return window.getStatusLineManager();
201         }
202
203         /* (non-Javadoc)
204          * @see org.eclipse.ui.application.IActionBarConfigurer
205          */

206         public IMenuManager getMenuManager() {
207             if (proxy != null) {
208                 return proxy.getMenuManager();
209             }
210             return window.getMenuManager();
211         }
212
213         /* (non-Javadoc)
214          * @see org.eclipse.ui.internal.AbstractActionBarConfigurer
215          */

216         public ICoolBarManager getCoolBarManager() {
217             if (proxy != null) {
218                 return proxy.getCoolBarManager();
219             }
220             return window.getCoolBarManager2();
221         }
222
223         /* (non-Javadoc)
224          * @see org.eclipse.ui.application.IActionBarConfigurer
225          */

226         public void registerGlobalAction(IAction action) {
227             if (proxy != null) {
228                 proxy.registerGlobalAction(action);
229             }
230             window.registerGlobalAction(action);
231         }
232
233         private IActionBarPresentationFactory getActionBarPresentationFactory() {
234             WorkbenchWindow window = (WorkbenchWindow)getWindowConfigurer().getWindow();
235             return window.getActionBarPresentationFactory();
236         }
237
238         /* (non-Javadoc)
239          * @see org.eclipse.ui.application.IActionBarConfigurer#createToolBarManager()
240          */

241         public IToolBarManager createToolBarManager() {
242             if (proxy != null) {
243                 return proxy.createToolBarManager();
244             }
245             return getActionBarPresentationFactory().createToolBarManager();
246         }
247
248         /* (non-Javadoc)
249          * @see org.eclipse.ui.application.IActionBarConfigurer#createToolBarContributionItem(org.eclipse.jface.action.IToolBarManager, java.lang.String)
250          */

251         public IToolBarContributionItem createToolBarContributionItem(IToolBarManager toolBarManager, String JavaDoc id) {
252             if (proxy != null) {
253                 return proxy.createToolBarContributionItem(toolBarManager, id);
254             }
255             return getActionBarPresentationFactory().createToolBarContributionItem(toolBarManager, id);
256         }
257     }
258
259     /**
260      * Creates a new workbench window configurer.
261      * <p>
262      * This method is declared package-private. Clients obtain instances
263      * via {@link WorkbenchAdvisor#getWindowConfigurer
264      * WorkbenchAdvisor.getWindowConfigurer}
265      * </p>
266      *
267      * @param window the workbench window that this object configures
268      * @see WorkbenchAdvisor#getWindowConfigurer
269      */

270     WorkbenchWindowConfigurer(WorkbenchWindow window) {
271         if (window == null) {
272             throw new IllegalArgumentException JavaDoc();
273         }
274         this.window = window;
275         windowTitle = WorkbenchPlugin.getDefault().getProductName();
276         if (windowTitle == null) {
277             windowTitle = ""; //$NON-NLS-1$
278
}
279     }
280
281     /* (non-javadoc)
282      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getWindow
283      */

284     public IWorkbenchWindow getWindow() {
285         return window;
286     }
287
288     /* (non-Javadoc)
289      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getWorkbenchConfigurer()
290      */

291     public IWorkbenchConfigurer getWorkbenchConfigurer() {
292         return Workbench.getInstance().getWorkbenchConfigurer();
293     }
294
295     /**
296      * Returns the title as set by <code>setTitle</code>, without consulting the shell.
297      *
298      * @return the window title as set, or <code>null</code> if not set
299      */

300     /* package */String JavaDoc basicGetTitle() {
301         return windowTitle;
302     }
303
304     /* (non-javadoc)
305      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getTitle
306      */

307     public String JavaDoc getTitle() {
308         Shell shell = window.getShell();
309         if (shell != null) {
310             // update the cached title
311
windowTitle = shell.getText();
312         }
313         return windowTitle;
314     }
315
316     /* (non-javadoc)
317      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#setTitle
318      */

319     public void setTitle(String JavaDoc title) {
320         if (title == null) {
321             throw new IllegalArgumentException JavaDoc();
322         }
323         windowTitle = title;
324         Shell shell = window.getShell();
325         if (shell != null && !shell.isDisposed()) {
326             shell.setText(TextProcessor.process(title, WorkbenchWindow.TEXT_DELIMITERS));
327         }
328     }
329
330     /* (non-javadoc)
331      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getShowMenuBar
332      */

333     public boolean getShowMenuBar() {
334         return showMenuBar;
335     }
336
337     /* (non-javadoc)
338      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#setShowMenuBar
339      */

340     public void setShowMenuBar(boolean show) {
341         showMenuBar = show;
342         WorkbenchWindow win = (WorkbenchWindow) getWindow();
343         Shell shell = win.getShell();
344         if (shell != null) {
345             boolean showing = shell.getMenuBar() != null;
346             if (show != showing) {
347                 if (show) {
348                     shell.setMenuBar(win.getMenuBarManager().getMenu());
349                 } else {
350                     shell.setMenuBar(null);
351                 }
352             }
353         }
354     }
355
356     /* (non-javadoc)
357      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getShowToolBar
358      */

359     public boolean getShowCoolBar() {
360         return showToolBar;
361     }
362
363     /* (non-javadoc)
364      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
365      */

366     public void setShowCoolBar(boolean show) {
367         showToolBar = show;
368         window.setCoolBarVisible(show);
369         // @issue need to be able to reconfigure after window's controls created
370
}
371
372     /* (non-javadoc)
373      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
374      */

375     public boolean getShowFastViewBars() {
376         return showFastViewBars;
377     }
378
379     /* (non-javadoc)
380      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
381      */

382     public void setShowFastViewBars(boolean show) {
383         showFastViewBars = show;
384         window.setFastViewBarVisible(show);
385         // @issue need to be able to reconfigure after window's controls created
386
}
387
388     /* (non-javadoc)
389      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
390      */

391     public boolean getShowPerspectiveBar() {
392         return showPerspectiveBar;
393     }
394
395     /* (non-javadoc)
396      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
397      */

398     public void setShowPerspectiveBar(boolean show) {
399         showPerspectiveBar = show;
400         window.setPerspectiveBarVisible(show);
401         // @issue need to be able to reconfigure after window's controls created
402
}
403
404     /* (non-javadoc)
405      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getShowStatusLine
406      */

407     public boolean getShowStatusLine() {
408         return showStatusLine;
409     }
410
411     /* (non-javadoc)
412      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#setShowStatusLine
413      */

414     public void setShowStatusLine(boolean show) {
415         showStatusLine = show;
416         // @issue need to be able to reconfigure after window's controls created
417
}
418
419     /* (non-Javadoc)
420      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
421      */

422     public boolean getShowProgressIndicator() {
423         return showProgressIndicator;
424     }
425
426     /* (non-Javadoc)
427      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
428      */

429     public void setShowProgressIndicator(boolean show) {
430         showProgressIndicator = show;
431         // @issue need to be able to reconfigure after window's controls created
432
}
433
434     /* (non-javadoc)
435      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#getData
436      */

437     public Object JavaDoc getData(String JavaDoc key) {
438         if (key == null) {
439             throw new IllegalArgumentException JavaDoc();
440         }
441         return extraData.get(key);
442     }
443
444     /* (non-javadoc)
445      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#setData
446      */

447     public void setData(String JavaDoc key, Object JavaDoc data) {
448         if (key == null) {
449             throw new IllegalArgumentException JavaDoc();
450         }
451         if (data != null) {
452             extraData.put(key, data);
453         } else {
454             extraData.remove(key);
455         }
456     }
457
458     /* (non-Javadoc)
459      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#addEditorAreaTransfer
460      */

461     public void addEditorAreaTransfer(Transfer tranfer) {
462         if (tranfer != null && !transferTypes.contains(tranfer)) {
463             transferTypes.add(tranfer);
464             Transfer[] transfers = new Transfer[transferTypes.size()];
465             transferTypes.toArray(transfers);
466             IWorkbenchPage[] pages = window.getPages();
467             for (int i = 0; i < pages.length; i++) {
468                 WorkbenchPage page = (WorkbenchPage) pages[i];
469                 DropTarget dropTarget = ((EditorSashContainer) page
470                         .getEditorPresentation().getLayoutPart())
471                         .getDropTarget();
472                 if (dropTarget != null) {
473                     dropTarget.setTransfer(transfers);
474                 }
475             }
476         }
477     }
478
479     /* (non-Javadoc)
480      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
481      */

482     public void configureEditorAreaDropListener(
483             DropTargetListener dropTargetListener) {
484         if (dropTargetListener != null) {
485             this.dropTargetListener = dropTargetListener;
486             IWorkbenchPage[] pages = window.getPages();
487             for (int i = 0; i < pages.length; i++) {
488                 WorkbenchPage page = (WorkbenchPage) pages[i];
489                 DropTarget dropTarget = ((EditorSashContainer) page
490                         .getEditorPresentation().getLayoutPart())
491                         .getDropTarget();
492                 if (dropTarget != null) {
493                     dropTarget.addDropListener(this.dropTargetListener);
494                 }
495             }
496         }
497     }
498
499     /**
500      * Returns the array of <code>Transfer</code> added by the application
501      */

502     /* package */Transfer[] getTransfers() {
503         Transfer[] transfers = new Transfer[transferTypes.size()];
504         transferTypes.toArray(transfers);
505         return transfers;
506     }
507
508     /**
509      * Returns the drop listener provided by the application.
510      */

511     /* package */DropTargetListener getDropTargetListener() {
512         return dropTargetListener;
513     }
514
515     /* (non-javadoc)
516      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
517      */

518     public IActionBarConfigurer getActionBarConfigurer() {
519         if (actionBarConfigurer == null) {
520             // lazily initialize
521
actionBarConfigurer = new WindowActionBarConfigurer();
522         }
523         return actionBarConfigurer;
524     }
525
526     /**
527      * Returns whether the given id is for a cool item.
528      *
529      * @param the item id
530      * @return <code>true</code> if it is a cool item,
531      * and <code>false</code> otherwise
532      */

533     /* package */boolean containsCoolItem(String JavaDoc id) {
534         // trigger lazy initialization
535
getActionBarConfigurer();
536         return actionBarConfigurer.containsCoolItem(id);
537     }
538
539     /* (non-Javadoc)
540      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
541      */

542     public int getShellStyle() {
543         return shellStyle;
544     }
545
546     /* (non-Javadoc)
547      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
548      */

549     public void setShellStyle(int shellStyle) {
550         this.shellStyle = shellStyle;
551     }
552
553     /* (non-Javadoc)
554      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
555      */

556     public Point getInitialSize() {
557         return initialSize;
558     }
559
560     /* (non-Javadoc)
561      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
562      */

563     public void setInitialSize(Point size) {
564         initialSize = size;
565     }
566
567     /* (non-Javadoc)
568      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
569      */

570     public AbstractPresentationFactory getPresentationFactory() {
571         if (presentationFactory == null) {
572             presentationFactory = createDefaultPresentationFactory();
573         }
574         return presentationFactory;
575     }
576
577     /**
578      * Creates the default presentation factory by looking up the presentation
579      * factory extension with the id specified by the presentation factory preference.
580      * If the preference is null or if no matching extension is found, a
581      * factory default presentation factory is used.
582      */

583     private AbstractPresentationFactory createDefaultPresentationFactory() {
584         final String JavaDoc factoryId = ((Workbench) window.getWorkbench())
585                 .getPresentationId();
586
587         if (factoryId != null && factoryId.length() > 0) {
588             final AbstractPresentationFactory [] factory = new AbstractPresentationFactory[1];
589             StartupThreading.runWithoutExceptions(new StartupRunnable() {
590
591                 public void runWithException() throws Throwable JavaDoc {
592                     factory[0] = WorkbenchPlugin.getDefault()
593                             .getPresentationFactory(factoryId);
594                 }
595             });
596             
597             if (factory[0] != null) {
598                 return factory[0];
599             }
600         }
601         // presentation ID must be a bogus value, reset it to the default
602
PrefUtil.getAPIPreferenceStore().setValue(
603                 IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID,
604                 IWorkbenchConstants.DEFAULT_PRESENTATION_ID);
605         return new WorkbenchPresentationFactory_33();
606     }
607
608     /* (non-Javadoc)
609      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
610      */

611     public void setPresentationFactory(AbstractPresentationFactory factory) {
612         if (factory == null) {
613             throw new IllegalArgumentException JavaDoc();
614         }
615         presentationFactory = factory;
616     }
617
618     /**
619      * Creates the default window contents.
620      *
621      * @param shell the shell
622      */

623     public void createDefaultContents(Shell shell) {
624         window.createDefaultContents(shell);
625     }
626
627     /* (non-Javadoc)
628      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
629      */

630     public Menu createMenuBar() {
631         return window.getMenuManager().createMenuBar(window.getShell());
632     }
633
634     /* (non-Javadoc)
635      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
636      */

637     public Control createCoolBarControl(Composite parent) {
638         ICoolBarManager coolBarManager = window.getCoolBarManager2();
639         if (coolBarManager != null) {
640             if (coolBarManager instanceof ICoolBarManager2) {
641                 return ((ICoolBarManager2) coolBarManager).createControl2(parent);
642             }
643             if (coolBarManager instanceof CoolBarManager) {
644                 return ((CoolBarManager) coolBarManager).createControl(parent);
645             }
646         }
647         return null;
648     }
649
650     /* (non-Javadoc)
651      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
652      */

653     public Control createStatusLineControl(Composite parent) {
654         return window.getStatusLineManager().createControl(parent);
655     }
656
657     /* (non-Javadoc)
658      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer
659      */

660     public Control createPageComposite(Composite parent) {
661         return window.createPageComposite(parent);
662     }
663     
664     /* (non-Javadoc)
665      * @see org.eclipse.ui.application.IWorkbenchWindowConfigurer#saveState(org.eclipse.ui.IMemento)
666      */

667     public IStatus saveState(IMemento memento) {
668         return window.saveState(memento);
669     }
670
671 }
672
Popular Tags