KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > presentations > IntroLaunchBar


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

9 package org.eclipse.ui.internal.intro.impl.presentations;
10
11 import java.util.Map JavaDoc;
12
13 import org.eclipse.jface.action.Action;
14 import org.eclipse.jface.action.IMenuListener;
15 import org.eclipse.jface.action.IMenuManager;
16 import org.eclipse.jface.action.IToolBarManager;
17 import org.eclipse.jface.action.MenuManager;
18 import org.eclipse.jface.action.Separator;
19 import org.eclipse.jface.action.ToolBarManager;
20 import org.eclipse.jface.dialogs.IDialogSettings;
21 import org.eclipse.jface.util.Geometry;
22 import org.eclipse.swt.SWT;
23 import org.eclipse.swt.events.PaintEvent;
24 import org.eclipse.swt.events.PaintListener;
25 import org.eclipse.swt.graphics.Color;
26 import org.eclipse.swt.graphics.GC;
27 import org.eclipse.swt.graphics.Point;
28 import org.eclipse.swt.graphics.RGB;
29 import org.eclipse.swt.graphics.Rectangle;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Display;
33 import org.eclipse.swt.widgets.Layout;
34 import org.eclipse.swt.widgets.Menu;
35 import org.eclipse.swt.widgets.ToolBar;
36 import org.eclipse.ui.IWorkbenchPreferenceConstants;
37 import org.eclipse.ui.IWorkbenchWindow;
38 import org.eclipse.ui.PlatformUI;
39 import org.eclipse.ui.internal.RectangleAnimation;
40 import org.eclipse.ui.internal.WorkbenchMessages;
41 import org.eclipse.ui.internal.WorkbenchWindow;
42 import org.eclipse.ui.internal.dnd.DragUtil;
43 import org.eclipse.ui.internal.intro.impl.IntroPlugin;
44 import org.eclipse.ui.internal.intro.impl.Messages;
45 import org.eclipse.ui.internal.intro.impl.model.IntroLaunchBarElement;
46 import org.eclipse.ui.internal.intro.impl.model.IntroLaunchBarShortcut;
47 import org.eclipse.ui.internal.intro.impl.model.IntroTheme;
48 import org.eclipse.ui.internal.intro.impl.swt.SharedStyleManager;
49 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
50 import org.eclipse.ui.internal.layout.ITrimManager;
51 import org.eclipse.ui.internal.layout.IWindowTrim;
52 import org.eclipse.ui.intro.IIntroPart;
53 import org.eclipse.ui.intro.config.CustomizableIntroPart;
54 import org.eclipse.ui.intro.config.IIntroURL;
55 import org.eclipse.ui.intro.config.IntroURLFactory;
56
57 /**
58  * This class is responsible for creating the intro launch bar in the provided parent. It creates
59  * 'restore' and 'close' actions, as well as actions for each shortcut element contributed in the
60  * extension point.
61  *
62  * @since 3.1
63  */

64 public class IntroLaunchBar implements IWindowTrim {
65
66     private Composite container;
67
68     protected ToolBarManager toolBarManager;
69
70     protected int orientation;
71
72     protected int location;
73
74     protected String JavaDoc lastPageId;
75
76     protected Action closeAction = null;
77
78     private IntroLaunchBarElement element;
79
80     protected boolean simple;
81
82     private String JavaDoc presentationId;
83     
84     private IntroTheme theme;
85
86     static final int[] TOP_LEFT_CORNER = new int[] { 0, 6, 1, 5, 1, 4, 4, 1, 5, 1, 6, 0 };
87
88     static final int[] TOP_RIGHT_CORNER = new int[] { -6, 0, -5, 1, -4, 1, -1, 4, -1, 5, 0, 6 };
89
90     static final int[] BOTTOM_LEFT_CORNER = new int[] { 0, -6, 1, -5, 1, -4, 4, -1, 5, -1, 6, 0 };
91
92     static final int[] BOTTOM_RIGHT_CORNER = new int[] { -6, 0, -5, -1, -4, -1, -1, -4, -1, -5, 0, -6 };
93
94     static final int[] SIMPLE_TOP_LEFT_CORNER = new int[] { 0, 2, 1, 1, 2, 0 };
95
96     static final int[] SIMPLE_TOP_RIGHT_CORNER = new int[] { -2, 0, -1, 1, 0, 2 };
97
98     static final int[] SIMPLE_BOTTOM_LEFT_CORNER = new int[] { 0, -2, 1, -1, 2, 0 };
99
100     static final int[] SIMPLE_BOTTOM_RIGHT_CORNER = new int[] { -2, 0, -1, -1, 0, -2 };
101
102     static final String JavaDoc S_STORED_LOCATION = "introLaunchBar.location"; //$NON-NLS-1$
103

104     private final static String JavaDoc LAUNCH_COMMAND_BASE = "http://org.eclipse.ui.intro/showPage?id="; //$NON-NLS-1$
105

106     private Color fg;
107
108     private Color bg;
109
110     class BarLayout extends Layout {
111
112         protected Point computeSize(Composite composite, int wHint, int hHint, boolean changed) {
113             boolean vertical = (orientation & SWT.VERTICAL) != 0;
114             int marginWidth = vertical | isPlain() ? 1 : simple ? 3 : 7;
115             int marginHeight = !vertical | isPlain() ? 1 : simple ? 3 : 7;
116             int width = 0;
117             int height = 0;
118
119             Point tsize = toolBarManager.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
120
121             if (vertical) {
122                 width = tsize.x;
123                 height = tsize.y;
124             } else {
125                 height = tsize.y;
126                 width = tsize.x;
127             }
128             if (vertical) {
129                 width += marginWidth;
130                 height += marginHeight + marginHeight;
131             } else {
132                 width += marginWidth + marginWidth;
133                 height += marginHeight;
134             }
135             return new Point(width, height);
136         }
137
138         protected void layout(Composite composite, boolean changed) {
139             boolean vertical = (orientation & SWT.VERTICAL) != 0;
140             int marginWidth = vertical | isPlain() ? 1 : simple ? 4 : 7;
141             int marginHeight = !vertical | isPlain() ? 1 : simple ? 4 : 7;
142
143             Point tsize = toolBarManager.getControl().computeSize(SWT.DEFAULT, SWT.DEFAULT, changed);
144             Rectangle carea = composite.getClientArea();
145             int x = carea.x + (location == SWT.LEFT ? 0 : marginWidth);
146             int y = carea.y + marginHeight;
147
148             if (vertical) {
149                 toolBarManager.getControl().setBounds(x, y, carea.width - marginWidth, tsize.y);
150             } else {
151                 toolBarManager.getControl().setBounds(x, y, tsize.x, carea.height - marginHeight);
152             }
153         }
154     }
155
156     public IntroLaunchBar(int orientation, String JavaDoc lastPageId, IntroLaunchBarElement element, IntroTheme theme) {
157         this.orientation = orientation;
158         this.location = element.getLocation();
159         this.lastPageId = lastPageId;
160         this.element = element;
161         this.theme = theme;
162
163         simple = true;
164         presentationId = PlatformUI.getPreferenceStore().getString(
165                 IWorkbenchPreferenceConstants.PRESENTATION_FACTORY_ID);
166         loadStoredLocation();
167     }
168
169     private void loadStoredLocation() {
170         IDialogSettings settings = IntroPlugin.getDefault().getDialogSettings();
171         try {
172             int storedLocation = settings.getInt(S_STORED_LOCATION);
173             if (storedLocation > 0)
174                 setLocation(storedLocation);
175         } catch (NumberFormatException JavaDoc e) {
176             // The stored value either does not exist or
177
// is corrupted - just pick the default silently.
178
}
179     }
180
181     private void storeLocation() {
182         IDialogSettings settings = IntroPlugin.getDefault().getDialogSettings();
183         settings.put(S_STORED_LOCATION, this.location);
184     }
185
186     /**
187      * This method now calls dock(location) and then adds itself to the window trim. This is to
188      * support the re-ordering of IWindowTrim lifecycle related to dock().
189      */

190     public void createInActiveWindow() {
191         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
192
193         dock(location);
194
195         ITrimManager trimManager = getTrimManager();
196         trimManager.addTrim(location, this);
197         window.getShell().layout();
198     }
199
200     /**
201      * Get the trim manager from the default workbench window. If the current
202      * workbench window is -not- the <code>WorkbenchWindow</code> then return null.
203      *
204      * @return The trim manager for the current workbench window
205      */

206     private ITrimManager getTrimManager() {
207         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
208         if (window instanceof WorkbenchWindow)
209             return ((WorkbenchWindow)window).getTrimManager();
210         
211         return null; // not using the default workbench window
212
}
213     
214     protected boolean isPlain() {
215         return !"org.eclipse.ui.presentations.default".equals(presentationId); //$NON-NLS-1$
216
}
217
218     public void createControl(Composite parent) {
219         container = new Composite(parent, SWT.NULL);
220         computeColors(parent.getDisplay());
221         container.setLayout(new BarLayout());
222         // boolean vertical = (orientation & SWT.VERTICAL) != 0;
223
toolBarManager = new ToolBarManager(SWT.FLAT | orientation);
224
225
226         fillToolBar();
227         // coolBar = new CoolBar(container, SWT.NULL);
228
// CoolItem coolItem = new CoolItem(coolBar, SWT.NULL);
229
// toolBarManager.createControl(coolBar);
230
toolBarManager.createControl(container);
231         ToolBar toolBar = toolBarManager.getControl();
232
233         // coolItem.setControl(toolBar);
234
// Point toolBarSize = toolBar.computeSize(SWT.DEFAULT,
235
// SWT.DEFAULT);
236
// Set the preffered size to the size of the toolbar plus trim
237
// Point preferredSize = coolItem.computeSize(toolBarSize.x,
238
// toolBarSize.y);
239
// coolItem.setPreferredSize(preferredSize);
240

241         if (bg != null) {
242             toolBar.setBackground(bg);
243             // coolBar.setBackground(bg);
244
}
245         container.addPaintListener(new PaintListener() {
246
247             public void paintControl(PaintEvent e) {
248                 onPaint(e);
249             }
250         });
251         MenuManager manager = new MenuManager();
252         IMenuListener listener = new IMenuListener() {
253
254             public void menuAboutToShow(IMenuManager manager) {
255                 contextMenuAboutToShow(manager);
256             }
257         };
258         manager.setRemoveAllWhenShown(true);
259         manager.addMenuListener(listener);
260         Menu contextMenu = manager.createContextMenu(toolBarManager.getControl());
261         toolBarManager.getControl().setMenu(contextMenu);
262         IntroPlugin.getDefault().setLaunchBar(this);
263     }
264
265     protected void startDragging(Point position, boolean usingKeyboard) {
266         Rectangle dragRect = DragUtil.getDisplayBounds(getControl());
267         startDrag(this, dragRect, position, usingKeyboard);
268     }
269
270     private void startDrag(Object JavaDoc toDrag, Rectangle dragRect, Point position, boolean usingKeyboard) {
271
272         DragUtil.performDrag(toDrag, dragRect, position, !usingKeyboard);
273     }
274
275     protected void onPaint(PaintEvent e) {
276         GC gc = e.gc;
277         Color color = fg;
278         if (color == null) {
279             color = e.display.getSystemColor(SWT.COLOR_WIDGET_NORMAL_SHADOW);
280         }
281         gc.setForeground(color);
282         if (bg != null)
283             gc.setBackground(bg);
284         if (isPlain()) {
285             Point size = container.getSize();
286             gc.fillRectangle(0, 0, size.x, size.y);
287             gc.drawRectangle(0, 0, size.x - 1, size.y - 1);
288         } else {
289             switch (location) {
290             case SWT.LEFT:
291                 paintLeft(gc);
292                 break;
293             case SWT.RIGHT:
294                 paintRight(gc);
295                 break;
296             case SWT.BOTTOM:
297                 paintBottom(gc);
298                 break;
299             }
300         }
301     }
302
303     private void paintLeft(GC gc) {
304         int[] top = simple ? SIMPLE_TOP_RIGHT_CORNER : TOP_RIGHT_CORNER;
305         int[] bot = simple ? SIMPLE_BOTTOM_RIGHT_CORNER : BOTTOM_RIGHT_CORNER;
306         int[] shape = new int[top.length + bot.length + 4];
307         int index = 0;
308         Point size = container.getSize();
309         int x = size.x - 1;
310         int y = 0;
311         index = fillShape(shape, top, index, x, y, false);
312         y = size.y - 1;
313         index = fillShape(shape, bot, index, x, y, true);
314         shape[index++] = -1;
315         shape[index++] = size.y - 1;
316         shape[index++] = -1;
317         shape[index++] = 0;
318         gc.fillPolygon(shape);
319         gc.drawPolygon(shape);
320     }
321
322     private void paintBottom(GC gc) {
323         int[] left = simple ? SIMPLE_TOP_LEFT_CORNER : TOP_LEFT_CORNER;
324         int[] right = simple ? SIMPLE_TOP_RIGHT_CORNER : TOP_RIGHT_CORNER;
325         int[] shape = new int[left.length + right.length + 4];
326         int index = 0;
327         Point size = container.getSize();
328         int x = 0;
329         int y = 0;
330         index = fillShape(shape, left, index, x, y, false);
331         x = size.x - 1;
332         index = fillShape(shape, right, index, x, y, false);
333         shape[index++] = size.x - 1;
334         shape[index++] = size.y;
335         shape[index++] = 0;
336         shape[index++] = size.y;
337         gc.fillPolygon(shape);
338         gc.drawPolygon(shape);
339     }
340
341     private void paintRight(GC gc) {
342         int[] top = simple ? SIMPLE_TOP_LEFT_CORNER : TOP_LEFT_CORNER;
343         int[] bot = simple ? SIMPLE_BOTTOM_LEFT_CORNER : BOTTOM_LEFT_CORNER;
344         int[] shape = new int[top.length + bot.length + 4];
345         int index = 0;
346         Point size = container.getSize();
347         int x = 0;
348         int y = 0;
349         index = fillShape(shape, top, index, x, y, false);
350         shape[index++] = size.x;
351         shape[index++] = 0;
352         shape[index++] = size.x;
353         shape[index++] = size.y - 1;
354         x = 0;
355         y = size.y - 1;
356         fillShape(shape, bot, index, x, y, true);
357         gc.fillPolygon(shape);
358         gc.drawPolygon(shape);
359     }
360
361
362     private int fillShape(int[] shape, int[] points, int index, int x, int y, boolean reverse) {
363         int fill = points.length;
364         for (int i = 0; i < points.length / 2; i++) {
365             if (!reverse) {
366                 shape[index++] = x + points[2 * i];
367                 shape[index++] = y + points[2 * i + 1];
368             } else {
369                 shape[index + fill - 2 - 2 * i] = x + points[2 * i];
370                 shape[index + fill - 1 - 2 * i] = y + points[2 * i + 1];
371             }
372         }
373         if (reverse) {
374             index += fill;
375         }
376         return index;
377     }
378
379     private void computeColors(Display display) {
380         if (element.getBackground() != null) {
381             String JavaDoc value = resolveColor(element.getBackground());
382             if (value!=null) {
383                 RGB r = SharedStyleManager.parseRGB(value);
384                 if (r != null)
385                     bg = new Color(display, r);
386             }
387         }
388         if (element.getForeground() != null) {
389             String JavaDoc value = resolveColor(element.getForeground());
390             if (value!=null) {
391                 RGB r = SharedStyleManager.parseRGB(value);
392                 if (r != null)
393                     fg = new Color(display, r);
394             }
395         }
396     }
397     
398     private String JavaDoc resolveColor(String JavaDoc value) {
399         if (value.indexOf('$')== -1)
400             return value;
401         if (value.charAt(0)=='$' && value.charAt(value.length()-1)=='$' && theme!=null) {
402             Map JavaDoc properties = theme.getProperties();
403             if (properties!=null) {
404                 String JavaDoc key = value.substring(1, value.length()-1);
405                 return (String JavaDoc)properties.get(key);
406             }
407         }
408         return value;
409     }
410
411     public Control getControl() {
412         return container;
413     }
414
415     public void dispose() {
416         if (container != null) {
417             container.dispose();
418         }
419         if (toolBarManager != null) {
420             toolBarManager.dispose();
421             toolBarManager.removeAll();
422         }
423
424         toolBarManager = null;
425         container = null;
426
427         if (bg != null)
428             bg.dispose();
429         if (fg != null)
430             fg.dispose();
431     }
432
433     private void fillToolBar() {
434         Action action;
435
436         closeAction = new Action("close") { //$NON-NLS-1$
437

438             public void run() {
439                 closeLaunchBar(false);
440             }
441         };
442         closeAction.setText(Messages.IntroLaunchBar_close_label);
443         closeAction.setToolTipText(Messages.IntroLaunchBar_close_tooltip);
444         /*
445          * closeAction.setImageDescriptor(ImageUtil
446          * .createImageDescriptor("full/elcl16/close_view.gif")); //$NON-NLS-1$
447          */

448
449         action = new Action("restore") { //$NON-NLS-1$
450

451             public void run() {
452                 openPage(lastPageId);
453             }
454         };
455         action.setToolTipText(Messages.IntroLaunchBar_restore_tooltip);
456         action.setImageDescriptor(ImageUtil.createImageDescriptor("full/etool16/restore_welcome.gif")); //$NON-NLS-1$
457
// toolBarManager.add(closeAction);
458
toolBarManager.add(action);
459         toolBarManager.add(new Separator());
460         if (element == null)
461             return;
462         IntroLaunchBarShortcut[] shortcuts = element.getShortcuts();
463         for (int i = 0; i < shortcuts.length; i++) {
464             IntroLaunchBarShortcut shortcut = shortcuts[i];
465             addShortcut(shortcut, toolBarManager);
466         }
467     }
468
469     private void addShortcut(final IntroLaunchBarShortcut shortcut, IToolBarManager toolBarManager) {
470         Action action = new Action(shortcut.getToolTip()) {
471
472             public void run() {
473                 executeShortcut(shortcut.getURL());
474             }
475         };
476         action.setImageDescriptor(shortcut.getImageDescriptor());
477         action.setToolTipText(shortcut.getToolTip());
478         toolBarManager.add(action);
479     }
480
481     public void close() {
482         closeLaunchBar(false);
483     }
484
485     protected IIntroPart closeLaunchBar(boolean restore) {
486
487         IntroPlugin.getDefault().setLaunchBar(null);
488         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
489
490         // if we've already been removed, this won't hurt us
491
getTrimManager().removeTrim(this);
492
493         IIntroPart intro = null;
494         if (restore) {
495             intro = PlatformUI.getWorkbench().getIntroManager().showIntro(window, false);
496             CustomizableIntroPart cpart = (CustomizableIntroPart) intro;
497             Rectangle startBounds = Geometry.toDisplay(getControl().getParent(), getControl().getBounds());
498             Rectangle endBounds = Geometry.toDisplay(cpart.getControl().getParent(), cpart.getControl()
499                     .getBounds());
500
501             RectangleAnimation animation = new RectangleAnimation(window.getShell(), startBounds, endBounds);
502             animation.schedule();
503         }
504         dispose();
505         window.getShell().layout();
506         return intro;
507     }
508
509     protected void executeShortcut(String JavaDoc url) {
510         IIntroURL introURL = IntroURLFactory.createIntroURL(url);
511         if (introURL != null) {
512             IIntroPart intro = closeLaunchBar(true);
513             if (intro == null)
514                 return;
515             introURL.execute();
516         }
517     }
518
519     protected void openPage(String JavaDoc id) {
520         IIntroPart intro = closeLaunchBar(true);
521         if (intro == null)
522             return;
523         StringBuffer JavaDoc url = new StringBuffer JavaDoc();
524         url.append(LAUNCH_COMMAND_BASE);
525         url.append(id);
526         IIntroURL introURL = IntroURLFactory.createIntroURL(url.toString());
527         if (introURL != null)
528             introURL.execute();
529     }
530
531     protected void contextMenuAboutToShow(IMenuManager manager) {
532         manager.add(closeAction);
533     }
534
535     public void dock(int side) {
536         dispose();
537         setLocation(side);
538         storeLocation();
539         IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
540         createControl(window.getShell());
541     }
542
543     private void setLocation(int location) {
544         this.orientation = (location == SWT.LEFT || location == SWT.RIGHT) ? SWT.VERTICAL : SWT.HORIZONTAL;
545         this.location = location;
546     }
547
548     public int getValidSides() {
549         return SWT.LEFT | SWT.RIGHT | SWT.BOTTOM;
550     }
551
552     /*
553      * (non-Javadoc)
554      *
555      * @see org.eclipse.ui.internal.IWindowTrim#getId()
556      */

557     public String JavaDoc getId() {
558         return "org.eclipse.ui.internal.intro.impl.presentations.IntroLaunchBar"; //$NON-NLS-1$
559
}
560
561     /*
562      * (non-Javadoc)
563      *
564      * @see org.eclipse.ui.internal.IWindowTrim#getDisplayName()
565      */

566     public String JavaDoc getDisplayName() {
567         return WorkbenchMessages.TrimCommon_IntroBar_TrimName;
568     }
569
570     /*
571      * (non-Javadoc)
572      *
573      * @see org.eclipse.ui.internal.IWindowTrim#isCloseable()
574      */

575     public boolean isCloseable() {
576         return element.getClose();
577     }
578
579     /*
580      * (non-Javadoc)
581      *
582      * @see org.eclipse.ui.internal.IWindowTrim#handleClose()
583      */

584     public void handleClose() {
585         closeLaunchBar(false);
586     }
587
588     /*
589      * (non-Javadoc)
590      *
591      * @see org.eclipse.ui.IWindowTrim#getWidthHint()
592      */

593     public int getWidthHint() {
594         return SWT.DEFAULT;
595     }
596
597     /*
598      * (non-Javadoc)
599      *
600      * @see org.eclipse.ui.IWindowTrim#getHeightHint()
601      */

602     public int getHeightHint() {
603         return SWT.DEFAULT;
604     }
605
606     /*
607      * (non-Javadoc)
608      *
609      * @see org.eclipse.ui.IWindowTrim#isResizeable()
610      */

611     public boolean isResizeable() {
612         return false;
613     }
614
615 }
616
Popular Tags