KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > swt > RootPageForm


1 /*******************************************************************************
2  * Copyright (c) 2004, 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.intro.impl.swt;
12
13 import org.eclipse.swt.SWT;
14 import org.eclipse.swt.graphics.Color;
15 import org.eclipse.swt.graphics.Point;
16 import org.eclipse.swt.graphics.Rectangle;
17 import org.eclipse.swt.layout.GridData;
18 import org.eclipse.swt.layout.GridLayout;
19 import org.eclipse.swt.widgets.Composite;
20 import org.eclipse.swt.widgets.Control;
21 import org.eclipse.swt.widgets.Label;
22 import org.eclipse.swt.widgets.Layout;
23 import org.eclipse.ui.forms.IFormColors;
24 import org.eclipse.ui.forms.events.HyperlinkAdapter;
25 import org.eclipse.ui.forms.events.HyperlinkEvent;
26 import org.eclipse.ui.forms.widgets.Form;
27 import org.eclipse.ui.forms.widgets.FormToolkit;
28 import org.eclipse.ui.forms.widgets.ImageHyperlink;
29 import org.eclipse.ui.forms.widgets.ScrolledPageBook;
30 import org.eclipse.ui.internal.intro.impl.IIntroConstants;
31 import org.eclipse.ui.internal.intro.impl.Messages;
32 import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement;
33 import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement;
34 import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider;
35 import org.eclipse.ui.internal.intro.impl.model.IntroGroup;
36 import org.eclipse.ui.internal.intro.impl.model.IntroHomePage;
37 import org.eclipse.ui.internal.intro.impl.model.IntroLink;
38 import org.eclipse.ui.internal.intro.impl.model.IntroModelRoot;
39 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser;
40 import org.eclipse.ui.internal.intro.impl.util.DialogUtil;
41 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
42 import org.eclipse.ui.internal.intro.impl.util.StringUtil;
43 import org.eclipse.ui.internal.intro.impl.util.Util;
44 import org.eclipse.ui.intro.config.IIntroContentProviderSite;
45
46 /**
47  * A Composite that represents the Root Page. It is swapped in the main page
48  * book in the FormIntroPartImplementation class.
49  */

50 public class RootPageForm implements IIntroConstants {
51
52     private FormToolkit toolkit;
53     private IntroHomePage rootPage;
54     private Form parentForm;
55     protected Label descriptionLabel;
56     private PageStyleManager rootPageStyleManager;
57     private IIntroContentProviderSite site;
58     private PageWidgetFactory factory;
59
60     class PageComposite extends Composite {
61
62         public PageComposite(Composite parent, int style) {
63             super(parent, style);
64         }
65
66         // Do not allow composite to take wHint as-is - layout manager
67
// can reject the hint and compute larger width.
68
public Point computeSize(int wHint, int hHint, boolean changed) {
69             return ((RootPageLayout) getLayout()).computeSize(this, wHint,
70                 hHint, changed);
71         }
72     }
73
74     class RootPageLayout extends Layout {
75
76         // gap between link composite and description label.
77
private int VERTICAL_SPACING = 20;
78
79         private int LABEL_MARGIN_WIDTH = 5;
80
81         /*
82          * Custom layout for Root Page Composite.
83          */

84         protected Point computeSize(Composite composite, int wHint, int hHint,
85                 boolean flushCache) {
86             int innerWHint = wHint;
87             if (wHint != SWT.DEFAULT)
88                 innerWHint -= LABEL_MARGIN_WIDTH + LABEL_MARGIN_WIDTH;
89             Control[] children = composite.getChildren();
90             Point s1 = children[0].computeSize(SWT.DEFAULT, SWT.DEFAULT);
91             Point s2 = children[1].computeSize(innerWHint, SWT.DEFAULT);
92             s2.x += LABEL_MARGIN_WIDTH;
93             int height = 2 * (s2.y + VERTICAL_SPACING + s1.y / 2);
94             Point size = new Point(Math.max(s1.x, s2.x), height + 5);
95             return size;
96         }
97
98         /*
99          * (non-Javadoc)
100          *
101          * @see org.eclipse.swt.widgets.Layout#layout(org.eclipse.swt.widgets.Composite,
102          * boolean)
103          */

104         protected void layout(Composite composite, boolean flushCache) {
105             Control[] children = composite.getChildren();
106             Rectangle carea = composite.getClientArea();
107             Control content = children[0];
108             Control label = children[1];
109             Point contentSize = content.computeSize(SWT.DEFAULT, SWT.DEFAULT);
110             Point labelSize = label.computeSize(carea.width - 2
111                     - LABEL_MARGIN_WIDTH * 2, SWT.DEFAULT);
112             content.setBounds(carea.width / 2 - contentSize.x / 2, carea.height
113                     / 2 - contentSize.y / 2, contentSize.x, contentSize.y);
114             label.setBounds(LABEL_MARGIN_WIDTH, content.getLocation().y
115                     + contentSize.y + VERTICAL_SPACING, carea.width
116                     - LABEL_MARGIN_WIDTH * 2, labelSize.y);
117         }
118     }
119
120     private HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() {
121
122         public void linkActivated(HyperlinkEvent e) {
123             ImageHyperlink imageLink = (ImageHyperlink) e.getSource();
124             IntroLink introLink = (IntroLink) imageLink.getData(INTRO_LINK);
125             IntroURLParser parser = new IntroURLParser(introLink.getUrl());
126             if (parser.hasIntroUrl()) {
127                 // execute the action embedded in the IntroURL
128
parser.getIntroURL().execute();
129                 return;
130             } else if (parser.hasProtocol()) {
131                 Util.openBrowser(introLink.getUrl());
132                 return;
133             }
134             DialogUtil.displayInfoMessage(imageLink.getShell(),
135                 Messages.HyperlinkAdapter_urlIs + introLink.getUrl());
136         }
137
138         public void linkEntered(HyperlinkEvent e) {
139             ImageHyperlink imageLink = (ImageHyperlink) e.getSource();
140             IntroLink introLink = (IntroLink) imageLink.getData(INTRO_LINK);
141             updateDescription(introLink.getText());
142         }
143
144         public void linkExited(HyperlinkEvent e) {
145             // empty text on exit.
146
updateDescription(""); //$NON-NLS-1$
147
}
148
149         private void updateDescription(String JavaDoc text) {
150             if (text == null)
151                 text = ""; //$NON-NLS-1$
152
descriptionLabel.setText(text);
153             descriptionLabel.getParent().layout();
154         }
155     };
156
157     /**
158      *
159      */

160     public RootPageForm(FormToolkit toolkit, IntroModelRoot modelRoot,
161             Form parentForm) {
162         this.toolkit = toolkit;
163         this.rootPage = modelRoot.getHomePage();
164         this.parentForm = parentForm;
165     }
166
167     /**
168      * Create the form for the root page. Number of columns there is equal to
169      * the number of links.
170      *
171      * @param pageBook
172      */

173     public void createPartControl(ScrolledPageBook mainPageBook,
174             SharedStyleManager shardStyleManager) {
175         // first, create the root page style manager from shared style manager.
176
rootPageStyleManager = new PageStyleManager(rootPage, shardStyleManager
177             .getProperties());
178
179         // Set title of Main form from root page title.
180
parentForm.setText(rootPage.getTitle());
181
182         // Composite for full root page. It has custom layout, and two
183
// children: the content composite and the description label.
184
Composite rootPageComposite = new PageComposite(mainPageBook
185             .getContainer(), SWT.NULL);
186         toolkit.adapt(rootPageComposite);
187
188         mainPageBook.registerPage(rootPage.getId(), rootPageComposite);
189         rootPageComposite.setLayout(new RootPageLayout());
190         // Util.highlight(pageComposite, SWT.COLOR_DARK_CYAN);
191

192         // create the contents composite in the center of the root page.
193
createRootPageContent(rootPageComposite);
194
195         // create description label for links description.
196
descriptionLabel = createHoverLabel(rootPageComposite);
197
198         // Clear memory. No need for style manager any more.
199
rootPageStyleManager = null;
200     }
201
202     /**
203      * Creates content of the root page.
204      */

205     private void createRootPageContent(Composite rootPageComposite) {
206         // setup page composite/layout
207
Composite contentComposite = toolkit.createComposite(rootPageComposite);
208         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
209         contentComposite.setLayoutData(gd);
210         AbstractIntroElement[] children = (AbstractIntroElement[]) rootPage
211             .getChildrenOfType(AbstractIntroElement.GROUP
212                     | AbstractIntroElement.LINK | AbstractIntroElement.CONTENT_PROVIDER);
213         int numChildren = children.length;
214         GridLayout layout = new GridLayout();
215         // separate links a bit more.
216
layout.horizontalSpacing = rootPageStyleManager
217             .getPageHorizantalSpacing();
218         layout.verticalSpacing = rootPageStyleManager.getPageVerticalSpacing();
219         // set number of columns.
220
int numColumns = rootPageStyleManager.getPageNumberOfColumns();
221         numColumns = numColumns == 0 ? numChildren : numColumns;
222         layout.numColumns = numColumns;
223         layout.horizontalSpacing = rootPageStyleManager
224             .getPageHorizantalSpacing();
225         layout.verticalSpacing = rootPageStyleManager.getPageVerticalSpacing();
226         contentComposite.setLayout(layout);
227         for (int i = 0; i < children.length; i++) {
228             if (((AbstractBaseIntroElement) children[i]).isFiltered())
229                 continue;
230             if (children[i].getType() == AbstractIntroElement.GROUP)
231                 createGroupContent(contentComposite, (IntroGroup) children[i]);
232             else if (children[i].getType() == AbstractIntroElement.LINK)
233                 createImageHyperlink(contentComposite, (IntroLink) children[i]);
234             else if (children[i].getType() == AbstractIntroElement.CONTENT_PROVIDER)
235                 createContentProvider(contentComposite, (IntroContentProvider)children[i]);
236         }
237     }
238
239     /**
240      * Creates content of the root page.
241      */

242     private void createGroupContent(Composite parent, IntroGroup group) {
243         AbstractIntroElement[] children = (AbstractIntroElement[]) group
244             .getChildrenOfType(AbstractIntroElement.GROUP
245                     | AbstractIntroElement.LINK |
246                     AbstractIntroElement.CONTENT_PROVIDER);
247         int numChildren = children.length;
248         // setup page composite/layout
249
Composite contentComposite = toolkit.createComposite(parent);
250         GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
251         gd.horizontalSpan = rootPageStyleManager.getColSpan(group);
252         gd.verticalSpan = rootPageStyleManager.getRowSpan(group);
253         contentComposite.setLayoutData(gd);
254         GridLayout layout = new GridLayout();
255         // separate links a bit more.
256
layout.horizontalSpacing = 20;
257         // set number of columns.
258
int numColumns = rootPageStyleManager.getNumberOfColumns(group);
259         numColumns = numColumns < 1 ? numChildren : numColumns;
260         layout.numColumns = numColumns;
261         layout.verticalSpacing = rootPageStyleManager.getVerticalSpacing(group);
262         layout.horizontalSpacing = rootPageStyleManager
263             .getHorizantalSpacing(group);
264         contentComposite.setLayout(layout);
265         for (int i = 0; i < children.length; i++) {
266             if (((AbstractBaseIntroElement) children[i]).isFiltered())
267                 continue;
268             if (children[i].getType() == AbstractIntroElement.GROUP)
269                 createGroupContent(contentComposite, (IntroGroup) children[i]);
270             else if (children[i].getType() == AbstractIntroElement.LINK)
271                 createImageHyperlink(contentComposite, (IntroLink) children[i]);
272             else if (children[i].getType() == AbstractIntroElement.CONTENT_PROVIDER)
273                 createContentProvider(contentComposite, (IntroContentProvider)children[i]);
274         }
275     }
276
277     /**
278      * Creates an Image Hyperlink from an IntroLink. Model object is cached in
279      * link.
280      *
281      * @param body
282      * @param link
283      */

284     private void createImageHyperlink(Composite parent, IntroLink link) {
285         // create the container composite that will hold the imageHyperLink and
286
// the label for the description.
287
Composite container = toolkit.createComposite(parent);
288         // Util.highlight(container, SWT.COLOR_CYAN);
289
GridData gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
290         gd.horizontalSpan = rootPageStyleManager.getColSpan(link);
291         gd.verticalSpan = rootPageStyleManager.getRowSpan(link);
292         container.setLayoutData(gd);
293
294         GridLayout layout = new GridLayout();
295         layout.marginWidth = 0;
296         layout.marginHeight = 0;
297         container.setLayout(layout);
298         ImageHyperlink imageLink = toolkit.createImageHyperlink(container,
299             SWT.NULL);
300         imageLink.setImage(rootPageStyleManager.getImage(link, "link-icon", //$NON-NLS-1$
301
ImageUtil.DEFAULT_ROOT_LINK));
302         imageLink.setHoverImage(rootPageStyleManager.getImage(link,
303             "hover-icon", null)); //$NON-NLS-1$
304
// each link is centered in cell.
305
gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
306         imageLink.setLayoutData(gd);
307         // cache the intro link model object for description and URL.
308
imageLink.setData(INTRO_LINK, link);
309         imageLink.addHyperlinkListener(hyperlinkAdapter);
310
311         // description label.
312
Label linkLabel = toolkit.createLabel(container, link.getLabel());
313         gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
314         linkLabel.setFont(PageStyleManager.getBannerFont());
315         linkLabel.setLayoutData(gd);
316     }
317
318     /**
319      * Creates a label to display the link description when you hover over a
320      * hyperlink.
321      *
322      * @param body
323      */

324     private Label createHoverLabel(Composite body) {
325         Label label = toolkit.createLabel(body, "", SWT.WRAP); //$NON-NLS-1$
326
String JavaDoc key = StringUtil.concat(rootPage.getId(), ".", "hover-text.fg") //$NON-NLS-1$ //$NON-NLS-2$
327
.toString();
328         Color fg = rootPageStyleManager.getColor(toolkit, key);
329         if (fg == null)
330             fg = toolkit.getColors().getColor(IFormColors.TITLE);
331         label.setForeground(fg);
332         label.setAlignment(SWT.CENTER);
333         label.setFont(PageStyleManager.getBannerFont());
334         return label;
335     }
336     
337     private void createContentProvider(Composite parent, IntroContentProvider providerElement) {
338         if (factory==null) {
339             factory = new PageWidgetFactory(toolkit, rootPageStyleManager);
340             factory.setContentProviderSite(site);
341         }
342         factory.createContentProvider(parent, providerElement);
343     }
344     public void setContentProviderSite(IIntroContentProviderSite site) {
345         this.site = site;
346     }
347 }
348
Popular Tags