KickJava   Java API By Example, From Geeks To Geeks.

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


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
14 import org.eclipse.swt.SWT;
15 import org.eclipse.swt.SWTError;
16 import org.eclipse.swt.graphics.Color;
17 import org.eclipse.swt.graphics.Image;
18 import org.eclipse.swt.widgets.Composite;
19 import org.eclipse.swt.widgets.Control;
20 import org.eclipse.swt.widgets.Label;
21 import org.eclipse.ui.forms.IFormColors;
22 import org.eclipse.ui.forms.events.HyperlinkAdapter;
23 import org.eclipse.ui.forms.events.HyperlinkEvent;
24 import org.eclipse.ui.forms.widgets.FormText;
25 import org.eclipse.ui.forms.widgets.FormToolkit;
26 import org.eclipse.ui.forms.widgets.Hyperlink;
27 import org.eclipse.ui.forms.widgets.ImageHyperlink;
28 import org.eclipse.ui.forms.widgets.Section;
29 import org.eclipse.ui.forms.widgets.TableWrapData;
30 import org.eclipse.ui.forms.widgets.TableWrapLayout;
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.IntroHTML;
37 import org.eclipse.ui.internal.intro.impl.model.IntroImage;
38 import org.eclipse.ui.internal.intro.impl.model.IntroLink;
39 import org.eclipse.ui.internal.intro.impl.model.IntroSeparator;
40 import org.eclipse.ui.internal.intro.impl.model.IntroText;
41 import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager;
42 import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser;
43 import org.eclipse.ui.internal.intro.impl.util.DialogUtil;
44 import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
45 import org.eclipse.ui.internal.intro.impl.util.Log;
46 import org.eclipse.ui.internal.intro.impl.util.StringUtil;
47 import org.eclipse.ui.internal.intro.impl.util.Util;
48 import org.eclipse.ui.intro.config.IIntroContentProvider;
49 import org.eclipse.ui.intro.config.IIntroContentProviderSite;
50
51 /**
52  * Factory to create all UI forms widgets for the Forms intro presentation.
53  */

54 public class PageWidgetFactory {
55
56     protected HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() {
57
58         public void linkActivated(HyperlinkEvent e) {
59             String JavaDoc url = (String JavaDoc) e.getHref();
60             IntroURLParser parser = new IntroURLParser(url);
61             if (parser.hasIntroUrl()) {
62                 // execute the action embedded in the IntroURL
63
parser.getIntroURL().execute();
64                 return;
65             } else if (parser.hasProtocol()) {
66                 Util.openBrowser(url);
67                 return;
68             }
69             DialogUtil.displayInfoMessage(((Control) e.getSource()).getShell(),
70                 Messages.HyperlinkAdapter_urlIs + " " + url); //$NON-NLS-1$
71
}
72
73         public void linkEntered(HyperlinkEvent e) {
74         }
75
76         public void linkExited(HyperlinkEvent e) {
77         }
78     };
79
80
81     protected FormToolkit toolkit;
82     protected PageStyleManager styleManager;
83     protected IIntroContentProviderSite site;
84
85
86     /*
87      * protect bad creation.
88      */

89     protected PageWidgetFactory(FormToolkit toolkit,
90             PageStyleManager styleManager) {
91         this.toolkit = toolkit;
92         this.styleManager = styleManager;
93     }
94
95     public void setContentProviderSite(IIntroContentProviderSite site) {
96         this.site = site;
97     }
98
99     public void createIntroElement(Composite parent,
100             AbstractIntroElement element) {
101         // check if this element is filtered, and if yes, do not create it.
102
boolean isFiltered = getFilterState(element);
103         if (isFiltered)
104             return;
105
106         Control c = null;
107         switch (element.getType()) {
108         case AbstractIntroElement.GROUP:
109             IntroGroup group = (IntroGroup) element;
110             c = createGroup(parent, group);
111             updateLayoutData(c, element);
112             // c must be a composite.
113
Composite newParent = (Composite) c;
114             if (c instanceof Section)
115                 // client is a composite also.
116
newParent = (Composite) ((Section) newParent).getClient();
117             AbstractIntroElement[] children = group.getChildren();
118             for (int i = 0; i < children.length; i++)
119                 createIntroElement(newParent, children[i]);
120             break;
121         case AbstractIntroElement.LINK:
122             IntroLink link = (IntroLink) element;
123             c = createImageHyperlink(parent, link);
124             updateLayoutData(c, element);
125             break;
126         case AbstractIntroElement.TEXT:
127             IntroText text = (IntroText) element;
128             c = createText(parent, text);
129             updateLayoutData(c, element);
130             break;
131         case AbstractIntroElement.IMAGE:
132             IntroImage image = (IntroImage) element;
133             c = createImage(parent, image);
134             if (c!=null)
135                 updateLayoutData(c, element);
136             break;
137         case AbstractIntroElement.HTML:
138             IntroHTML html = (IntroHTML) element;
139             if (html.isInlined()) {
140                 IntroText htmlText = html.getIntroText();
141                 if (htmlText != null)
142                     c = createText(parent, htmlText);
143                 else {
144                     IntroImage htmlImage = html.getIntroImage();
145                     if (htmlImage != null)
146                         c = createImage(parent, htmlImage);
147                 }
148             } else {
149                 // embedded HTML, so we can show it from a link.
150
String JavaDoc embddedLink = html.getSrc();
151                 if (embddedLink == null)
152                     break;
153                 String JavaDoc linkText = StringUtil
154                     .concat(
155                         "<p><a HREF=\"http://org.eclipse.ui.intro/openBrowser?url=", //$NON-NLS-1$
156
embddedLink, "\">", //$NON-NLS-1$
157
Messages.HTML_embeddedLink, "</a></p>").toString(); //$NON-NLS-1$
158
linkText = generateFormText(linkText);
159                 c = createFormText(parent, linkText, null);
160             }
161             if (c != null)
162                 updateLayoutData(c, element);
163             break;
164         case AbstractIntroElement.CONTENT_PROVIDER:
165             IntroContentProvider provider = (IntroContentProvider) element;
166             c = createContentProvider(parent, provider);
167             updateLayoutData(c, element);
168             break;
169         case AbstractIntroElement.HR:
170             IntroSeparator sep = (IntroSeparator)element;
171             c = createSeparator(parent, sep);
172             updateLayoutData(c, element);
173             break;
174             
175         default:
176             break;
177         }
178     }
179
180
181     private void updateLayoutData(Control c, AbstractIntroElement element) {
182         TableWrapData currentTd = (TableWrapData) c.getLayoutData();
183         if (currentTd == null) {
184             currentTd = new TableWrapData(TableWrapData.FILL,
185                 TableWrapData.FILL);
186             currentTd.grabHorizontal = true;
187             c.setLayoutData(currentTd);
188         }
189
190         currentTd.colspan = styleManager
191             .getColSpan((AbstractBaseIntroElement) element);
192         currentTd.rowspan = styleManager
193             .getRowSpan((AbstractBaseIntroElement) element);
194
195     }
196
197     private Composite createGroup(Composite parent, IntroGroup group) {
198         String JavaDoc label = group.getLabel();
199         String JavaDoc description = styleManager.getDescription(group);
200         boolean expandable = group.isExpandable();
201         boolean expanded = group.isExpanded();
202         Composite client = null;
203         Composite control = null;
204         if (description != null || label != null || expandable) {
205             int style = description != null ? Section.DESCRIPTION : SWT.NULL;
206             if (expandable)
207                 style |= Section.TWISTIE | Section.FOCUS_TITLE | Section.CLIENT_INDENT;
208             if (expanded)
209                 style |= Section.EXPANDED;
210             Section section = toolkit.createSection(parent, style);
211             if (label != null)
212                 section.setText(label);
213             if (description != null)
214                 section.setDescription(description);
215             colorControl(section, group);
216             client = toolkit.createComposite(section, SWT.WRAP);
217             section.setClient(client);
218             control = section;
219         } else {
220             client = toolkit.createComposite(parent, SWT.WRAP);
221             control = client;
222         }
223
224         TableWrapLayout layout = new TableWrapLayout();
225         int numColumns = styleManager.getNumberOfColumns(group);
226         numColumns = numColumns < 1 ? 1 : numColumns;
227         layout.numColumns = numColumns;
228         layout.makeColumnsEqualWidth = styleManager.getEqualWidth(group);
229         layout.verticalSpacing = styleManager.getVerticalSpacing(group);
230         layout.horizontalSpacing = styleManager.getHorizantalSpacing(group);
231         client.setLayout(layout);
232         // Util.highlight(client, SWT.COLOR_YELLOW);
233
return control;
234     }
235
236     /**
237      * Creates an Image Hyperlink from an IntroLink. Model object is NOT cached.
238      *
239      * @param body
240      * @param link
241      */

242     private Control createImageHyperlink(Composite parent, IntroLink link) {
243         Control control;
244         Hyperlink linkControl;
245         boolean showLinkDescription = styleManager.getShowLinkDescription();
246         Image linkImage = styleManager.getImage(link, "link-icon", //$NON-NLS-1$
247
ImageUtil.DEFAULT_LINK);
248
249         if (showLinkDescription && link.getText() != null) {
250             Composite container = toolkit.createComposite(parent);
251             TableWrapLayout layout = new TableWrapLayout();
252             layout.leftMargin = layout.rightMargin = 0;
253             layout.topMargin = layout.bottomMargin = 0;
254             layout.verticalSpacing = 0;
255             layout.numColumns = 2;
256             container.setLayout(layout);
257
258             //Label ilabel = toolkit.createLabel(container, null);
259
ImageHyperlink ilabel = toolkit.createImageHyperlink(container, SWT.NULL);
260             ilabel.setImage(linkImage);
261             ilabel.setHoverImage(styleManager.getImage(link, "hover-icon", //$NON-NLS-1$
262
null));
263             ilabel.setHref(link.getUrl());
264             ilabel.addHyperlinkListener(hyperlinkAdapter);
265             TableWrapData td = new TableWrapData();
266             td.valign = TableWrapData.TOP;
267             td.rowspan = 2;
268             ilabel.setLayoutData(td);
269
270             linkControl = toolkit.createHyperlink(container, null, SWT.WRAP);
271             td = new TableWrapData(TableWrapData.LEFT, TableWrapData.BOTTOM);
272             td.grabVertical = true;
273             linkControl.setLayoutData(td);
274             // Util.highlight(linkControl, SWT.COLOR_RED);
275
// Util.highlight(container, SWT.COLOR_DARK_YELLOW);
276

277             Control desc = createText(container, link.getIntroText());
278             td = new TableWrapData(TableWrapData.FILL, TableWrapData.TOP);
279             td.grabHorizontal = true;
280             td.grabVertical = true;
281             desc.setLayoutData(td);
282             control = container;
283         } else {
284             ImageHyperlink imageLink = toolkit.createImageHyperlink(parent,
285                 SWT.WRAP | SWT.CENTER);
286             imageLink.setImage(linkImage);
287             imageLink.setHoverImage(styleManager.getImage(link, "hover-icon", //$NON-NLS-1$
288
null));
289             TableWrapData td = new TableWrapData();
290             td.grabHorizontal = true;
291             imageLink.setLayoutData(td);
292             linkControl = imageLink;
293             control = linkControl;
294         }
295         linkControl.setText(link.getLabel());
296         linkControl.setFont(PageStyleManager.getBannerFont());
297         colorControl(linkControl, link);
298         linkControl.setHref(link.getUrl());
299         linkControl.addHyperlinkListener(hyperlinkAdapter);
300         // Util.highlight(linkControl, SWT.COLOR_DARK_YELLOW);
301
return control;
302     }
303
304     /**
305      * Creates a forms Text or FormattedText.
306      *
307      * @param body
308      * @param link
309      */

310     protected Control createText(Composite parent, IntroText text) {
311         Color fg = styleManager.getColor(toolkit, text);
312         boolean isBold = styleManager.isBold(text);
313         // formatted case. If text is alredy formatted, the bold property is
314
// ignored.
315
if (text.isFormatted())
316             return createFormText(parent, generateFormText(text.getText()), fg);
317
318         // non formatted case.
319
if (isBold)
320             return createFormText(parent, generateBoldFormText(text.getText()),
321                 fg);
322         return createText(parent, text.getText(), fg);
323     }
324
325     private Control createFormText(Composite parent, String JavaDoc text, Color fg) {
326         FormText formText = toolkit.createFormText(parent, false);
327         formText.addHyperlinkListener(hyperlinkAdapter);
328         try {
329             formText.setText(text, true, true);
330         } catch (SWTError e) {
331             Log.error(e.getMessage(), e);
332             return createText(parent, text, fg);
333         }
334         if (fg != null)
335             formText.setForeground(fg);
336         return formText;
337     }
338
339
340     private Control createText(Composite parent, String JavaDoc text, Color fg) {
341         Label label = toolkit.createLabel(parent, text, SWT.WRAP);
342         if (fg != null)
343             label.setForeground(fg);
344         return label;
345     }
346
347
348
349     protected Control createImage(Composite parent, IntroImage image) {
350         Label ilabel = null;
351         Image imageFile = styleManager.getImage(image);
352         if (imageFile != null) {
353             ilabel = toolkit.createLabel(parent, null, SWT.LEFT);
354             ilabel.setImage(imageFile);
355             if (image.getAlt() != null)
356                 ilabel.setToolTipText(image.getAlt());
357         }
358         // for images, do not use default layout. Grab horizontal is not what we
359
// want.
360
if (ilabel!=null) {
361             TableWrapData td = new TableWrapData();
362             ilabel.setLayoutData(td);
363         }
364         return ilabel;
365     }
366     
367     public Control createContentProvider(Composite parent,
368             IntroContentProvider provider) {
369         // If we've already loaded the content provider for this element,
370
// retrieve it, otherwise load the class.
371
// Create parent composite to hold dynamic content, and set layout
372
// accordingly.
373
Composite container = toolkit.createComposite(parent);
374         TableWrapLayout layout = new TableWrapLayout();
375         layout.topMargin = 0;
376         layout.bottomMargin = 0;
377         layout.leftMargin = 0;
378         layout.rightMargin = 0;
379         container.setLayout(layout);
380         container.setData(provider);
381
382
383         IIntroContentProvider providerClass = ContentProviderManager.getInst()
384             .getContentProvider(provider);
385         if (providerClass == null)
386             // content provider never created before, create it.
387
providerClass = ContentProviderManager.getInst()
388                 .createContentProvider(provider, site);
389
390         if (providerClass != null) {
391             try {
392                 providerClass.createContent(provider.getId(), container,
393                     toolkit);
394             } catch (Exception JavaDoc e) {
395                 Log.error(
396                     "Failed to create the content of Intro model content provider: " //$NON-NLS-1$
397
+ provider.getClassName(), e);
398                 // null provider.
399
providerClass = null;
400             }
401         }
402
403         if (providerClass == null) {
404             // we failed to create a provider class, create the embedded text.
405
IntroText text = provider.getIntroText();
406             if (text != null)
407                 createText(container, text);
408         }
409         return container;
410     }
411     
412     protected Control createSeparator(Composite parent, IntroSeparator sep) {
413         String JavaDoc key = sep.getParentPage().getId()+".separator.fg"; //$NON-NLS-1$
414
Color fg = styleManager.getColor(toolkit, key);
415         //Composite l = toolkit.createCompositeSeparator(parent);
416
Composite l = new Composite(parent, SWT.NULL);
417         if (fg!=null)
418             l.setBackground(fg);
419         else
420             l.setBackground(toolkit.getColors().getColor(IFormColors.SEPARATOR));
421         TableWrapData td = new TableWrapData(TableWrapData.FILL,
422                 TableWrapData.FILL);
423         td.grabHorizontal = true;
424         td.maxHeight = 1;
425         l.setLayoutData(td);
426         return l;
427     }
428
429     private void colorControl(Control elementControl,
430             AbstractBaseIntroElement element) {
431         Color fg = styleManager.getColor(toolkit, element);
432         if (fg != null)
433             elementControl.setForeground(fg);
434         Color bg = styleManager.getBackgrond(toolkit, element);
435         if (bg != null)
436             elementControl.setBackground(bg);
437     }
438
439
440     /*
441      * creates form text on a formatted string. A formatted string is any string
442      * that has a " <" in it. If it starts with a <p> then it is assumed that
443      * the text if a proper UI forms formatted text. If not, the <p> tag is
444      * added.
445      */

446     private String JavaDoc generateFormText(String JavaDoc text) {
447         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
448         sbuf.append("<form>"); //$NON-NLS-1$
449
if (text.startsWith("<p>")) //$NON-NLS-1$
450
sbuf.append(text);
451         else {
452             sbuf.append("<p>"); //$NON-NLS-1$
453
sbuf.append(text);
454             sbuf.append("</p>"); //$NON-NLS-1$
455
}
456         sbuf.append("</form>"); //$NON-NLS-1$
457
return sbuf.toString();
458     }
459
460     /**
461      * Will be only called for non formatted text.
462      *
463      * @param text
464      * @return
465      */

466     private String JavaDoc generateBoldFormText(String JavaDoc text) {
467         StringBuffer JavaDoc sbuf = new StringBuffer JavaDoc();
468         sbuf.append("<form>"); //$NON-NLS-1$
469
sbuf.append("<p>"); //$NON-NLS-1$
470
sbuf.append("<b>"); //$NON-NLS-1$
471
sbuf.append(text);
472         sbuf.append("</b>"); //$NON-NLS-1$
473
sbuf.append("</p>"); //$NON-NLS-1$
474
sbuf.append("</form>"); //$NON-NLS-1$
475
return sbuf.toString();
476     }
477
478     /**
479      * Check the filter state of the element. Only base elements have the filter
480      * attribute.
481      *
482      * @param element
483      * @return
484      */

485     private boolean getFilterState(AbstractIntroElement element) {
486         if (element.isOfType(AbstractIntroElement.BASE_ELEMENT))
487             return ((AbstractBaseIntroElement) element).isFiltered();
488         return false;
489     }
490
491
492 }
493
Popular Tags