KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > help > ui > internal > views > HelpTray


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.help.ui.internal.views;
12
13 import org.eclipse.help.ui.internal.Messages;
14 import org.eclipse.jface.action.ContributionItem;
15 import org.eclipse.jface.action.IContributionItem;
16 import org.eclipse.jface.action.ToolBarManager;
17 import org.eclipse.jface.dialogs.DialogTray;
18 import org.eclipse.jface.dialogs.IDialogPage;
19 import org.eclipse.jface.dialogs.IPageChangeProvider;
20 import org.eclipse.jface.dialogs.IPageChangedListener;
21 import org.eclipse.jface.dialogs.PageChangedEvent;
22 import org.eclipse.jface.dialogs.TrayDialog;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.graphics.Color;
25 import org.eclipse.swt.graphics.GC;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.graphics.ImageData;
28 import org.eclipse.swt.graphics.PaletteData;
29 import org.eclipse.swt.graphics.Point;
30 import org.eclipse.swt.graphics.RGB;
31 import org.eclipse.swt.layout.GridData;
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.Event;
37 import org.eclipse.swt.widgets.Label;
38 import org.eclipse.swt.widgets.Listener;
39 import org.eclipse.swt.widgets.Shell;
40 import org.eclipse.swt.widgets.TabFolder;
41 import org.eclipse.swt.widgets.TabItem;
42 import org.eclipse.swt.widgets.ToolBar;
43 import org.eclipse.swt.widgets.ToolItem;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.forms.HyperlinkGroup;
46 import org.eclipse.ui.forms.widgets.FormToolkit;
47
48 /**
49  * The tray that appears on the side of dialogs when the user summons context
50  * help or a cheat sheet follows the user into a dialog.
51  */

52 public class HelpTray extends DialogTray implements IPageChangedListener {
53     
54     public static final int MINIMUM_HEIGHT = 450;
55     private static final int DEFAULT_WIDTH = 210;
56     
57     private int originalHeight;
58     private int heightAdded;
59     
60     private FormToolkit toolkit;
61     private ReusableHelpPart helpPart;
62     private Shell shell;
63     private IContributionItem closeAction;
64     private Image normal;
65     private Image hover;
66     
67     /**
68      * Creates any actions needed by the tray.
69      */

70     private void createActions() {
71         createImages();
72         closeAction = new ContributionItem() {
73             public void fill(ToolBar parent, int index) {
74                 final ToolItem item = new ToolItem(parent, SWT.PUSH);
75                 item.setImage(normal);
76                 item.setHotImage(hover);
77                 item.setToolTipText(Messages.ReusableHelpPart_closeAction_tooltip);
78                 item.addListener(SWT.Selection, new Listener() {
79                     public void handleEvent(Event event) {
80                         // close the tray
81
TrayDialog dialog = (TrayDialog)shell.getData();
82                         dialog.closeTray();
83                         
84                         // set focus back to shell
85
shell.setFocus();
86                     }
87                 });
88             }
89         };
90     }
91     
92     /**
93      * Creates the contents of the tray.
94      *
95      * @param parent the parent composite that will contain the tray
96      */

97     protected Control createContents(Composite parent) {
98         // if the dialog is too short, make it taller
99
ensureMinimumHeight(parent.getShell());
100         
101         toolkit = new FormToolkit(parent.getDisplay());
102         toolkit.getHyperlinkGroup().setHyperlinkUnderlineMode(HyperlinkGroup.UNDERLINE_HOVER);
103         toolkit.getColors().initializeSectionToolBarColors();
104         Composite container = new Composite(parent, SWT.NONE);
105         GridLayout layout = new GridLayout();
106         layout.marginWidth = layout.marginHeight = 0;
107         layout.verticalSpacing = 0;
108         container.setLayout(layout);
109         container.addListener(SWT.Dispose, new Listener() {
110             public void handleEvent(Event event) {
111                 dispose();
112             }
113         });
114         
115         ToolBarManager tbm = new ToolBarManager(SWT.FLAT);
116         tbm.createControl(container);
117         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_END);
118         gd.grabExcessHorizontalSpace = true;
119         tbm.getControl().setLayoutData(gd);
120         Label separator = new Label(container, SWT.SEPARATOR | SWT.HORIZONTAL);
121         gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
122         gd.heightHint = 1;
123         separator.setLayoutData(gd);
124         helpPart = new ReusableHelpPart(PlatformUI.getWorkbench().getProgressService());
125         helpPart.init(null, tbm, null, null);
126         helpPart.setDefaultContextHelpText(Messages.HelpView_defaultText);
127         helpPart.createControl(container, toolkit);
128         gd = new GridData(GridData.FILL_BOTH);
129         gd.widthHint = DEFAULT_WIDTH;
130         helpPart.getControl().setLayoutData(gd);
131         
132         createActions();
133         tbm.add(closeAction);
134         
135         shell = parent.getShell();
136         hookPageChangeListener(shell);
137         helpPart.getControl().addListener(SWT.Dispose, new Listener() {
138             public void handleEvent(Event event) {
139                 unhookPageChangeListener(shell);
140             }
141         });
142         
143         return container;
144     }
145
146     /**
147      * Creates any custom needed by the tray, such as the close button.
148      */

149     private void createImages() {
150         Display display = Display.getCurrent();
151         int[] shape = new int[] {
152                 3, 3, 5, 3, 7, 5, 8, 5, 10, 3, 12, 3,
153                 12, 5, 10, 7, 10, 8, 12,10, 12,12,
154                 10,12, 8, 10, 7, 10, 5, 12, 3, 12,
155                 3, 10, 5, 8, 5, 7, 3, 5
156         };
157         
158         /*
159          * Use magenta as transparency color since it is used infrequently.
160          */

161         Color border = display.getSystemColor(SWT.COLOR_WIDGET_DARK_SHADOW);
162         Color background = display.getSystemColor(SWT.COLOR_LIST_BACKGROUND);
163         Color backgroundHot = new Color(display, new RGB(252, 160, 160));
164         Color transparent = display.getSystemColor(SWT.COLOR_MAGENTA);
165
166         PaletteData palette = new PaletteData(new RGB[] { transparent.getRGB(), border.getRGB(), background.getRGB(), backgroundHot.getRGB() });
167         ImageData data = new ImageData(16, 16, 8, palette);
168         data.transparentPixel = 0;
169
170         normal = new Image(display, data);
171         normal.setBackground(transparent);
172         GC gc = new GC(normal);
173         gc.setBackground(background);
174         gc.fillPolygon(shape);
175         gc.setForeground(border);
176         gc.drawPolygon(shape);
177         gc.dispose();
178
179         hover = new Image(display, data);
180         hover.setBackground(transparent);
181         gc = new GC(hover);
182         gc.setBackground(backgroundHot);
183         gc.fillPolygon(shape);
184         gc.setForeground(border);
185         gc.drawPolygon(shape);
186         gc.dispose();
187         
188         backgroundHot.dispose();
189     }
190
191     /**
192      * Disposes any resources used by the tray.
193      */

194     private void dispose() {
195         normal.dispose();
196         hover.dispose();
197         toolkit.dispose();
198         helpPart.dispose();
199         
200         /*
201          * Shell is about to be closed. Add a one-time-only listener
202          * that will return the dialog height back to original.
203          */

204         shell.addListener(SWT.Resize, new Listener() {
205             public void handleEvent(Event event) {
206                 shell.removeListener(SWT.Resize, this);
207                 Point p = shell.getSize();
208                 if (heightAdded > 0 && p.y > originalHeight) {
209                     p.y = Math.max(p.y - heightAdded, originalHeight);
210                     shell.setSize(p);
211                 }
212             }
213         });
214     }
215
216     /**
217      * Ensures that the dialog's height is sufficient to contain the help tray.
218      * If the dialog is too short, its height is increased. When closing the tray,
219      * the height is returned to original (see dispose()).
220      *
221      * @param shell the dialog's shell
222      */

223     private void ensureMinimumHeight(Shell shell) {
224         Point p = shell.getSize();
225         originalHeight = p.y;
226         if (p.y < MINIMUM_HEIGHT) {
227             heightAdded = MINIMUM_HEIGHT - p.y;
228             p.y = MINIMUM_HEIGHT;
229             shell.setSize(p);
230         }
231         else {
232             heightAdded = 0;
233         }
234     }
235     
236     /**
237      * Returns the ReusableHelpPart contained in the tray.
238      *
239      * @return the tray's ReusableHelpPart
240      */

241     public ReusableHelpPart getHelpPart() {
242         return helpPart;
243     }
244     
245     /**
246      * Add the listener that gets notified of page changes (to automatically
247      * update context help).
248      *
249      * @param parent the Composite to hook the listener to
250      */

251     private void hookPageChangeListener(Composite parent) {
252         Object JavaDoc data = parent.getData();
253         if (data instanceof IPageChangeProvider) {
254             ((IPageChangeProvider)data).addPageChangedListener(this);
255         }
256     }
257
258     /**
259      * Returns whether or not the help tray can handle the given shell. In some
260      * cases the help tray is not appropriate for shells that are too short and
261      * not resizable. In these cases infopops are used.
262      *
263      * @param shell the shell to check
264      * @return whether or not the help tray is appropriate for the hsell
265      */

266     public static boolean isAppropriateFor(Shell shell) {
267         if (shell != null && !shell.isDisposed() && shell.isVisible()) {
268             Object JavaDoc data = shell.getData();
269             return (data instanceof TrayDialog && (shell.getSize().y >= MINIMUM_HEIGHT || (shell.getStyle() & SWT.RESIZE) != 0));
270         }
271         return false;
272     }
273
274     /**
275      * Called whenever the dialog we're inside has changed pages. This updates
276      * the context help page if it is visible.
277      *
278      * @param event the page change event
279      */

280     public void pageChanged(PageChangedEvent event) {
281         Object JavaDoc page = event.getSelectedPage();
282         Control c = null;
283         if (page instanceof IDialogPage) {
284             c = ((IDialogPage) page).getControl();
285         } else {
286             c = shell.getDisplay().getFocusControl();
287             if (c instanceof TabFolder) {
288                 TabFolder folder = (TabFolder) c;
289                 TabItem[] selection = folder.getSelection();
290                 if (selection.length == 1) {
291                     c = selection[0].getControl();
292                 }
293             }
294         }
295         helpPart.update(null, null, c);
296     }
297     
298     /**
299      * Remove the listener that gets notified of page changes (to automatically
300      * update context help).
301      *
302      * @param parent the Composite that had the listener
303      */

304     private void unhookPageChangeListener(Composite parent) {
305         Object JavaDoc data = parent.getData();
306         if (data instanceof IPageChangeProvider) {
307             ((IPageChangeProvider)data).removePageChangedListener(this);
308         }
309     }
310 }
311
Popular Tags