KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.help.ui.internal.views;
12
13 import org.eclipse.help.ui.internal.HelpUIResources;
14 import org.eclipse.help.ui.internal.IHelpUIConstants;
15 import org.eclipse.help.ui.internal.Messages;
16 import org.eclipse.jface.action.IAction;
17 import org.eclipse.jface.action.IMenuManager;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.events.ControlAdapter;
20 import org.eclipse.swt.events.ControlEvent;
21 import org.eclipse.swt.graphics.GC;
22 import org.eclipse.swt.graphics.Image;
23 import org.eclipse.swt.graphics.Rectangle;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.layout.RowData;
27 import org.eclipse.swt.layout.RowLayout;
28 import org.eclipse.swt.widgets.Composite;
29 import org.eclipse.swt.widgets.Control;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.ui.IMemento;
32 import org.eclipse.ui.actions.ActionFactory;
33 import org.eclipse.ui.forms.AbstractFormPart;
34 import org.eclipse.ui.forms.FormColors;
35 import org.eclipse.ui.forms.HyperlinkGroup;
36 import org.eclipse.ui.forms.IFormColors;
37 import org.eclipse.ui.forms.events.HyperlinkAdapter;
38 import org.eclipse.ui.forms.events.HyperlinkEvent;
39 import org.eclipse.ui.forms.events.IHyperlinkListener;
40 import org.eclipse.ui.forms.widgets.FormToolkit;
41 import org.eclipse.ui.forms.widgets.ImageHyperlink;
42
43 public class SeeAlsoPart extends AbstractFormPart implements IHelpPart {
44     private Composite container;
45     private Composite linkContainer;
46     private ReusableHelpPart helpPart;
47     private String JavaDoc id;
48     private Image bgImage;
49     private HyperlinkGroup hyperlinkGroup;
50
51     /**
52      * @param parent
53      * @param toolkit
54      * @param style
55      */

56     public SeeAlsoPart(Composite parent, FormToolkit toolkit) {
57         container = new Composite(parent, SWT.NULL);
58         container.setBackgroundMode(SWT.INHERIT_DEFAULT);
59         container.addControlListener(new ControlAdapter() {
60             public void controlResized(ControlEvent e) {
61                 updateBackgroundImage();
62             }
63         });
64         GridLayout layout = new GridLayout();
65         layout.marginHeight = 0;
66         //layout.marginWidth = 0;
67
layout.verticalSpacing = 0;
68         layout.marginTop = 2;
69         container.setLayout(layout);
70         /*
71         Composite sep = toolkit.createCompositeSeparator(container);
72         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_FILL);
73         gd.heightHint = 1;
74         sep.setLayoutData(gd);
75         */

76         /*
77
78         Composite innerContainer = new Composite(container, SWT.NULL);
79         innerContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
80         layout = new GridLayout();
81         layout.marginHeight = 0;
82         innerContainer.setLayout(layout);
83         */

84         Label label = toolkit.createLabel(container, Messages.SeeAlsoPart_goto);
85         label.setForeground(toolkit.getColors().getColor(IFormColors.TITLE));
86         label.setBackground(null);
87         linkContainer = new Composite(container, SWT.NULL);
88         linkContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
89         RowLayout rlayout = new RowLayout();
90         rlayout.marginBottom = 0;
91         rlayout.marginTop = 0;
92         rlayout.marginLeft = 0;
93         rlayout.marginRight = 0;
94         rlayout.justify = false;
95         rlayout.wrap = true;
96         linkContainer.setLayout(rlayout);
97         
98         hyperlinkGroup = new HyperlinkGroup(container.getDisplay());
99         hyperlinkGroup.setHyperlinkUnderlineMode(toolkit.getHyperlinkGroup().getHyperlinkUnderlineMode());
100     }
101
102     private void updateBackgroundImage() {
103         FormToolkit toolkit = helpPart.getForm().getToolkit();
104         FormColors colors = toolkit.getColors();
105         Rectangle carea = container.getClientArea();
106         Image oldImage = bgImage;
107         if (bgImage!=null) {
108             Rectangle ibounds = bgImage.getBounds();
109             if (carea.height==ibounds.height)
110                 return;
111         }
112         bgImage = new Image(container.getDisplay(), 1, carea.height);
113         GC gc = new GC(bgImage);
114         gc.setBackground(colors.getColor(IFormColors.H_GRADIENT_END));
115         gc.setForeground(colors.getColor(IFormColors.H_GRADIENT_START));
116         gc.fillGradientRectangle(0, 0,
117                 1,
118                 carea.height,
119                 true);
120         gc.setForeground(colors.getColor(IFormColors.H_BOTTOM_KEYLINE2));
121         gc.drawLine(0, 0, 1, 0);
122         gc.setForeground(colors.getColor(IFormColors.H_BOTTOM_KEYLINE1));
123         gc.drawLine(0, 1, 1, 1);
124         gc.dispose();
125         container.setBackgroundImage(bgImage);
126         if (oldImage != null && !oldImage.isDisposed()) {
127             oldImage.dispose();
128         }
129     }
130     
131     private void updateLinks(String JavaDoc href) {
132         Control [] children = linkContainer.getChildren();
133         for (int i=0; i<children.length; i++) {
134             ImageHyperlink link = (ImageHyperlink)children[i];
135             RowData data = (RowData)link.getLayoutData();
136             data.exclude = link.getHref().equals(href);
137             link.setVisible(!data.exclude);
138         }
139         linkContainer.layout();
140         helpPart.reflow();
141     }
142
143     private void addLinks(final Composite container, FormToolkit toolkit) {
144         IHyperlinkListener listener = new HyperlinkAdapter() {
145             public void linkActivated(final HyperlinkEvent e) {
146                 container.getDisplay().asyncExec(new Runnable JavaDoc() {
147                     public void run() {
148                         SeeAlsoPart.this.helpPart.showPage((String JavaDoc) e.getHref(), true);
149                     }
150                 });
151             }
152         };
153         if ((helpPart.getStyle() & ReusableHelpPart.ALL_TOPICS) != 0)
154             addPageLink(container, toolkit, Messages.SeeAlsoPart_allTopics, IHelpUIConstants.HV_ALL_TOPICS_PAGE,
155                 IHelpUIConstants.IMAGE_ALL_TOPICS, listener);
156         if ((helpPart.getStyle() & ReusableHelpPart.SEARCH) != 0) {
157             addPageLink(container, toolkit, Messages.SeeAlsoPart_search, IHelpUIConstants.HV_FSEARCH_PAGE,
158                 IHelpUIConstants.IMAGE_HELP_SEARCH, listener);
159         }
160         if ((helpPart.getStyle() & ReusableHelpPart.CONTEXT_HELP) != 0) {
161             addPageLink(container, toolkit, Messages.SeeAlsoPart_contextHelp,
162                 IHelpUIConstants.HV_CONTEXT_HELP_PAGE,
163                 IHelpUIConstants.IMAGE_RELATED_TOPICS, listener);
164         }
165         if ((helpPart.getStyle() & ReusableHelpPart.BOOKMARKS) != 0) {
166             addPageLink(container, toolkit, Messages.SeeAlsoPart_bookmarks,
167                 IHelpUIConstants.HV_BOOKMARKS_PAGE,
168                 IHelpUIConstants.IMAGE_BOOKMARKS, listener);
169         }
170         if ((helpPart.getStyle() & ReusableHelpPart.INDEX) != 0) {
171             addPageLink(container, toolkit, Messages.SeeAlsoPart_index,
172                 IHelpUIConstants.HV_INDEX_PAGE,
173                 IHelpUIConstants.IMAGE_INDEX, listener);
174         }
175     }
176
177     private void addPageLink(Composite container, FormToolkit toolkit, String JavaDoc text, String JavaDoc id,
178             String JavaDoc imgRef, IHyperlinkListener listener) {
179         String JavaDoc cid = helpPart.getCurrentPageId();
180         if (cid!=null && cid.equals(id))
181             return;
182         ImageHyperlink link = new ImageHyperlink(container, SWT.WRAP|toolkit.getOrientation());
183         toolkit.adapt(link, true, true);
184         link.setImage(HelpUIResources.getImage(imgRef));
185         link.setText(text);
186         link.setHref(id);
187         link.setBackground(null);
188         link.addHyperlinkListener(listener);
189         hyperlinkGroup.add(link);
190         RowData data = new RowData();
191         data.exclude = false;
192         link.setLayoutData(data);
193     }
194
195     /*
196      * (non-Javadoc)
197      *
198      * @see org.eclipse.help.ui.internal.views.IHelpPart#getControl()
199      */

200     public Control getControl() {
201         return container;
202     }
203
204     /*
205      * (non-Javadoc)
206      *
207      * @see org.eclipse.help.ui.internal.views.IHelpPart#init(org.eclipse.help.ui.internal.views.NewReusableHelpPart)
208      */

209     public void init(ReusableHelpPart parent, String JavaDoc id, IMemento memento) {
210         this.helpPart = parent;
211         this.id = id;
212         addLinks(linkContainer, helpPart.getForm().getToolkit());
213     }
214
215     public String JavaDoc getId() {
216         return id;
217     }
218
219     /*
220      * (non-Javadoc)
221      *
222      * @see org.eclipse.help.ui.internal.views.IHelpPart#setVisible(boolean)
223      */

224     public void setVisible(boolean visible) {
225         container.setVisible(visible);
226         if (visible)
227             markStale();
228     }
229
230     /* (non-Javadoc)
231      * @see org.eclipse.ui.forms.AbstractFormPart#dispose()
232      */

233     public void dispose() {
234         super.dispose();
235         if (bgImage != null && !bgImage.isDisposed()) {
236             bgImage.dispose();
237         }
238     }
239     
240     /*
241      * (non-Javadoc)
242      *
243      * @see org.eclipse.help.ui.internal.views.IHelpPart#fillContextMenu(org.eclipse.jface.action.IMenuManager)
244      */

245     public boolean fillContextMenu(IMenuManager manager) {
246         return false;
247     }
248
249     /*
250      * (non-Javadoc)
251      *
252      * @see org.eclipse.help.ui.internal.views.IHelpPart#hasFocusControl(org.eclipse.swt.widgets.Control)
253      */

254     public boolean hasFocusControl(Control control) {
255         return control.getParent() == linkContainer;
256     }
257
258     public IAction getGlobalAction(String JavaDoc id) {
259         if (id.equals(ActionFactory.COPY.getId()))
260             return helpPart.getCopyAction();
261         return null;
262     }
263     public void stop() {
264     }
265     public void refresh() {
266         if (linkContainer!=null && helpPart.getCurrentPageId()!=null)
267             updateLinks(helpPart.getCurrentPageId());
268         super.refresh();
269     }
270
271     public void toggleRoleFilter() {
272     }
273
274     public void refilter() {
275     }
276
277     public void saveState(IMemento memento) {
278     }
279     
280     public void setFocus() {
281         if (linkContainer!=null)
282             linkContainer.setFocus();
283     }
284 }
285
Popular Tags