Skip to main content
summaryrefslogtreecommitdiffstats
blob: 68f6e12b13228bdd58b7711d3642cff6a64a4db8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
/*******************************************************************************
 * Copyright (c) 2004, 2016 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package org.eclipse.ui.internal.intro.impl.swt;


import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.forms.IFormColors;
import org.eclipse.ui.forms.events.HyperlinkAdapter;
import org.eclipse.ui.forms.events.HyperlinkEvent;
import org.eclipse.ui.forms.widgets.FormText;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Hyperlink;
import org.eclipse.ui.forms.widgets.ImageHyperlink;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;
import org.eclipse.ui.forms.widgets.TableWrapLayout;
import org.eclipse.ui.internal.intro.impl.Messages;
import org.eclipse.ui.internal.intro.impl.model.AbstractBaseIntroElement;
import org.eclipse.ui.internal.intro.impl.model.AbstractIntroElement;
import org.eclipse.ui.internal.intro.impl.model.IntroContentProvider;
import org.eclipse.ui.internal.intro.impl.model.IntroGroup;
import org.eclipse.ui.internal.intro.impl.model.IntroHTML;
import org.eclipse.ui.internal.intro.impl.model.IntroImage;
import org.eclipse.ui.internal.intro.impl.model.IntroLink;
import org.eclipse.ui.internal.intro.impl.model.IntroSeparator;
import org.eclipse.ui.internal.intro.impl.model.IntroText;
import org.eclipse.ui.internal.intro.impl.model.loader.ContentProviderManager;
import org.eclipse.ui.internal.intro.impl.model.url.IntroURLParser;
import org.eclipse.ui.internal.intro.impl.util.DialogUtil;
import org.eclipse.ui.internal.intro.impl.util.ImageUtil;
import org.eclipse.ui.internal.intro.impl.util.Log;
import org.eclipse.ui.internal.intro.impl.util.StringUtil;
import org.eclipse.ui.internal.intro.impl.util.Util;
import org.eclipse.ui.intro.config.IIntroContentProvider;
import org.eclipse.ui.intro.config.IIntroContentProviderSite;

/**
 * Factory to create all UI forms widgets for the Forms intro presentation.
 */
public class PageWidgetFactory {

    protected HyperlinkAdapter hyperlinkAdapter = new HyperlinkAdapter() {

        @Override
		public void linkActivated(HyperlinkEvent e) {
            String url = (String) e.getHref();
            IntroURLParser parser = new IntroURLParser(url);
            if (parser.hasIntroUrl()) {
                // execute the action embedded in the IntroURL
                parser.getIntroURL().execute();
                return;
            } else if (parser.hasProtocol()) {
                Util.openBrowser(url);
                return;
            }
            DialogUtil.displayInfoMessage(((Control) e.getSource()).getShell(),
                Messages.HyperlinkAdapter_urlIs + " " + url); //$NON-NLS-1$
        }

        @Override
		public void linkEntered(HyperlinkEvent e) {
        }

        @Override
		public void linkExited(HyperlinkEvent e) {
        }
    };


    protected FormToolkit toolkit;
    protected PageStyleManager styleManager;
    protected IIntroContentProviderSite site;


    /*
     * protect bad creation.
     */
    protected PageWidgetFactory(FormToolkit toolkit,
            PageStyleManager styleManager) {
        this.toolkit = toolkit;
        this.styleManager = styleManager;
    }

    public void setContentProviderSite(IIntroContentProviderSite site) {
        this.site = site;
    }

    public void createIntroElement(Composite parent,
            AbstractIntroElement element) {
        // check if this element is filtered, and if yes, do not create it.
        boolean isFiltered = getFilterState(element);
        if (isFiltered)
            return;

        Control c = null;
        switch (element.getType()) {
        case AbstractIntroElement.GROUP:
            IntroGroup group = (IntroGroup) element;
            c = createGroup(parent, group);
            updateLayoutData(c, element);
            // c must be a composite.
            Composite newParent = (Composite) c;
            if (c instanceof Section)
                // client is a composite also.
                newParent = (Composite) ((Section) newParent).getClient();
            AbstractIntroElement[] children = group.getChildren();
            for (int i = 0; i < children.length; i++)
                createIntroElement(newParent, children[i]);
            break;
        case AbstractIntroElement.LINK:
            IntroLink link = (IntroLink) element;
            c = createImageHyperlink(parent, link);
            updateLayoutData(c, element);
            break;
        case AbstractIntroElement.TEXT:
            IntroText text = (IntroText) element;
            c = createText(parent, text);
            updateLayoutData(c, element);
            break;
        case AbstractIntroElement.IMAGE:
            IntroImage image = (IntroImage) element;
            c = createImage(parent, image);
            if (c!=null)
            	updateLayoutData(c, element);
            break;
        case AbstractIntroElement.HTML:
            IntroHTML html = (IntroHTML) element;
            if (html.isInlined()) {
                IntroText htmlText = html.getIntroText();
                if (htmlText != null)
                    c = createText(parent, htmlText);
                else {
                    IntroImage htmlImage = html.getIntroImage();
                    if (htmlImage != null)
                        c = createImage(parent, htmlImage);
                }
            } else {
                // embedded HTML, so we can show it from a link.
                String embddedLink = html.getSrc();
                if (embddedLink == null)
                    break;
                String linkText = StringUtil
                    .concat(
                        "<p><a href=\"http://org.eclipse.ui.intro/openBrowser?url=", //$NON-NLS-1$
                        embddedLink, "\">", //$NON-NLS-1$
                        Messages.HTML_embeddedLink, "</a></p>").toString(); //$NON-NLS-1$
                linkText = generateFormText(linkText);
                c = createFormText(parent, linkText, null);
            }
            if (c != null)
                updateLayoutData(c, element);
            break;
        case AbstractIntroElement.CONTENT_PROVIDER:
            IntroContentProvider provider = (IntroContentProvider) element;
            c = createContentProvider(parent, provider);
            updateLayoutData(c, element);
            break;
        case AbstractIntroElement.HR:
        	IntroSeparator sep = (IntroSeparator)element;
            c = createSeparator(parent, sep);
            updateLayoutData(c, element);
            break;
        	
        default:
            break;
        }
    }


    private void updateLayoutData(Control c, AbstractIntroElement element) {
        TableWrapData currentTd = (TableWrapData) c.getLayoutData();
        if (currentTd == null) {
            currentTd = new TableWrapData(TableWrapData.FILL,
                TableWrapData.FILL);
            currentTd.grabHorizontal = true;
            c.setLayoutData(currentTd);
        }

        currentTd.colspan = styleManager
            .getColSpan((AbstractBaseIntroElement) element);
        currentTd.rowspan = styleManager
            .getRowSpan((AbstractBaseIntroElement) element);

    }

    private Composite createGroup(Composite parent, IntroGroup group) {
        String label = group.getLabel();
        String description = styleManager.getDescription(group);
        boolean expandable = group.isExpandable();
        boolean expanded = group.isExpanded();
        Composite client = null;
        Composite control = null;
        if (description != null || label != null || expandable) {
            int style = description != null ? Section.DESCRIPTION : SWT.NULL;
            if (expandable)
            	style |= Section.TWISTIE | Section.FOCUS_TITLE | Section.CLIENT_INDENT;
            if (expanded)
            	style |= Section.EXPANDED;
            Section section = toolkit.createSection(parent, style);
            if (label != null)
                section.setText(label);
            if (description != null)
                section.setDescription(description);
            colorControl(section, group);
            client = toolkit.createComposite(section, SWT.WRAP);
            section.setClient(client);
            control = section;
        } else {
            client = toolkit.createComposite(parent, SWT.WRAP);
            control = client;
        }

        TableWrapLayout layout = new TableWrapLayout();
        int numColumns = styleManager.getNumberOfColumns(group);
        numColumns = numColumns < 1 ? 1 : numColumns;
        layout.numColumns = numColumns;
        layout.makeColumnsEqualWidth = styleManager.getEqualWidth(group);
        layout.verticalSpacing = styleManager.getVerticalSpacing(group);
        layout.horizontalSpacing = styleManager.getHorizantalSpacing(group);
        client.setLayout(layout);
        // Util.highlight(client, SWT.COLOR_YELLOW);
        return control;
    }

    /**
     * Creates an Image Hyperlink from an IntroLink. Model object is NOT cached.
     * 
     * @param body
     * @param link
     */
    private Control createImageHyperlink(Composite parent, IntroLink link) {
        Control control;
        Hyperlink linkControl;
        boolean showLinkDescription = styleManager.getShowLinkDescription();
        Image linkImage = styleManager.getImage(link, "link-icon", //$NON-NLS-1$
            ImageUtil.DEFAULT_LINK);

        if (showLinkDescription && link.getText() != null) {
            Composite container = toolkit.createComposite(parent);
            TableWrapLayout layout = new TableWrapLayout();
            layout.leftMargin = layout.rightMargin = 0;
            layout.topMargin = layout.bottomMargin = 0;
            layout.verticalSpacing = 0;
            layout.numColumns = 2;
            container.setLayout(layout);

            //Label ilabel = toolkit.createLabel(container, null);
            ImageHyperlink ilabel = toolkit.createImageHyperlink(container, SWT.NULL);
            ilabel.setImage(linkImage);
            ilabel.setHoverImage(styleManager.getImage(link, "hover-icon", //$NON-NLS-1$
                null));
            ilabel.setHref(link.getUrl());
            ilabel.addHyperlinkListener(hyperlinkAdapter);
            TableWrapData td = new TableWrapData();
            td.valign = TableWrapData.TOP;
            td.rowspan = 2;
            ilabel.setLayoutData(td);

            linkControl = toolkit.createHyperlink(container, null, SWT.WRAP);
            td = new TableWrapData(TableWrapData.LEFT, TableWrapData.BOTTOM);
            td.grabVertical = true;
            linkControl.setLayoutData(td);
            // Util.highlight(linkControl, SWT.COLOR_RED);
            // Util.highlight(container, SWT.COLOR_DARK_YELLOW);

            Control desc = createText(container, link.getIntroText());
            td = new TableWrapData(TableWrapData.FILL, TableWrapData.TOP);
            td.grabHorizontal = true;
            td.grabVertical = true;
            desc.setLayoutData(td);
            control = container;
        } else {
            ImageHyperlink imageLink = toolkit.createImageHyperlink(parent,
                SWT.WRAP | SWT.CENTER);
            imageLink.setImage(linkImage);
            imageLink.setHoverImage(styleManager.getImage(link, "hover-icon", //$NON-NLS-1$
                null));
            TableWrapData td = new TableWrapData();
            td.grabHorizontal = true;
            imageLink.setLayoutData(td);
            linkControl = imageLink;
            control = linkControl;
        }
        linkControl.setText(link.getLabel());
        linkControl.setFont(PageStyleManager.getBannerFont());
        colorControl(linkControl, link);
        linkControl.setHref(link.getUrl());
        linkControl.addHyperlinkListener(hyperlinkAdapter);
        // Util.highlight(linkControl, SWT.COLOR_DARK_YELLOW);
        return control;
    }

    /**
     * Creates a forms Text or FormattedText.
     * 
     * @param body
     * @param link
     */
    protected Control createText(Composite parent, IntroText text) {
        Color fg = styleManager.getColor(toolkit, text);
        boolean isBold = styleManager.isBold(text);
        // formatted case. If text is alredy formatted, the bold property is
        // ignored.
        if (text.isFormatted())
            return createFormText(parent, generateFormText(text.getText()), fg);

        // non formatted case.
        if (isBold)
            return createFormText(parent, generateBoldFormText(text.getText()),
                fg);
        return createText(parent, StringUtil.normalizeWhiteSpace(text.getText()), fg);
    }

    private Control createFormText(Composite parent, String text, Color fg) {
        FormText formText = toolkit.createFormText(parent, false);
        formText.addHyperlinkListener(hyperlinkAdapter);
        try {
            formText.setText(text, true, true);
        } catch (SWTError e) {
            Log.error(e.getMessage(), e);
            return createText(parent, text, fg);
        }
        if (fg != null)
            formText.setForeground(fg);
        return formText;
    }


    private Control createText(Composite parent, String text, Color fg) {
        Label label = toolkit.createLabel(parent, text, SWT.WRAP);
        if (fg != null)
            label.setForeground(fg);
        return label;
    }



    protected Control createImage(Composite parent, IntroImage image) {
        Label ilabel = null;
        Image imageFile = styleManager.getImage(image);
        if (imageFile != null) {
            ilabel = toolkit.createLabel(parent, null, SWT.LEFT);
            ilabel.setImage(imageFile);
            if (image.getAlt() != null)
                ilabel.setToolTipText(image.getAlt());
        }
        // for images, do not use default layout. Grab horizontal is not what we
        // want.
        if (ilabel!=null) {
        	TableWrapData td = new TableWrapData();
        	ilabel.setLayoutData(td);
        }
        return ilabel;
    }
    
    public Control createContentProvider(Composite parent,
            IntroContentProvider provider) {
        // If we've already loaded the content provider for this element,
        // retrieve it, otherwise load the class.
        // Create parent composite to hold dynamic content, and set layout
        // accordingly.
        Composite container = toolkit.createComposite(parent);
        TableWrapLayout layout = new TableWrapLayout();
        layout.topMargin = 0;
        layout.bottomMargin = 0;
        layout.leftMargin = 0;
        layout.rightMargin = 0;
        container.setLayout(layout);
        container.setData(provider);


        IIntroContentProvider providerClass = ContentProviderManager.getInst()
            .getContentProvider(provider);
        if (providerClass == null)
            // content provider never created before, create it.
            providerClass = ContentProviderManager.getInst()
                .createContentProvider(provider, site);

        if (providerClass != null) {
            try {
                providerClass.createContent(provider.getId(), container,
                    toolkit);
            } catch (Exception e) {
                Log.error(
                    "Failed to create the content of Intro model content provider: " //$NON-NLS-1$
                            + provider.getClassName(), e);
                // null provider.
                providerClass = null;
            }
        }

        if (providerClass == null) {
            // we failed to create a provider class, create the embedded text.
            IntroText text = provider.getIntroText();
            if (text != null)
                createText(container, text);
        }
        return container;
    }
    
    protected Control createSeparator(Composite parent, IntroSeparator sep) {
    	String key = sep.getParentPage().getId()+".separator.fg"; //$NON-NLS-1$
        Color fg = styleManager.getColor(toolkit, key);
        //Composite l = toolkit.createCompositeSeparator(parent);
        Composite l = new Composite(parent, SWT.NULL);
        if (fg!=null)
        	l.setBackground(fg);
        else
        	l.setBackground(toolkit.getColors().getColor(IFormColors.SEPARATOR));
        TableWrapData td = new TableWrapData(TableWrapData.FILL,
                TableWrapData.FILL);
        td.grabHorizontal = true;
        td.maxHeight = 1;
        l.setLayoutData(td);
        return l;
    }

    private void colorControl(Control elementControl,
            AbstractBaseIntroElement element) {
        Color fg = styleManager.getColor(toolkit, element);
        if (fg != null)
            elementControl.setForeground(fg);
        Color bg = styleManager.getBackgrond(toolkit, element);
        if (bg != null)
            elementControl.setBackground(bg);
    }


    /*
     * creates form text on a formatted string. A formatted string is any string
     * that has a " <" in it. If it starts with a <p> then it is assumed that
     * the text if a proper UI forms formatted text. If not, the <p> tag is
     * added.
     */
    private String generateFormText(String text) {
        StringBuffer sbuf = new StringBuffer();
        sbuf.append("<form>"); //$NON-NLS-1$
        if (text.startsWith("<p>")) //$NON-NLS-1$
            sbuf.append(text);
        else {
            sbuf.append("<p>"); //$NON-NLS-1$
            sbuf.append(text);
            sbuf.append("</p>"); //$NON-NLS-1$
        }
        sbuf.append("</form>"); //$NON-NLS-1$
        return sbuf.toString();
    }

    /**
     * Will be only called for non formatted text.
     * 
     * @param text
     * @return
     */
    private String generateBoldFormText(String text) {
        StringBuffer sbuf = new StringBuffer();
        sbuf.append("<form>"); //$NON-NLS-1$
        sbuf.append("<p>"); //$NON-NLS-1$
        sbuf.append("<b>"); //$NON-NLS-1$
        sbuf.append(text);
        sbuf.append("</b>"); //$NON-NLS-1$
        sbuf.append("</p>"); //$NON-NLS-1$
        sbuf.append("</form>"); //$NON-NLS-1$
        return sbuf.toString();
    }

    /**
     * Check the filter state of the element. Only base elements have the filter
     * attribute.
     * 
     * @param element
     * @return
     */
    private boolean getFilterState(AbstractIntroElement element) {
        if (element.isOfType(AbstractIntroElement.BASE_ELEMENT))
            return ((AbstractBaseIntroElement) element).isFiltered();
        return false;
    }


}

Back to the top