summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/demo/featurebrowser/RichTextExample.java
blob: 2bced566a13ed58f96fd8f0853537f92c3f3a545 (plain)
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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.demo.featurebrowser;

import com.itmill.toolkit.ui.Alignment;
import com.itmill.toolkit.ui.Button;
import com.itmill.toolkit.ui.CustomComponent;
import com.itmill.toolkit.ui.Label;
import com.itmill.toolkit.ui.RichTextArea;
import com.itmill.toolkit.ui.VerticalLayout;
import com.itmill.toolkit.ui.Button.ClickEvent;

/**
 * An example using a RichTextArea to edit a Label in XHTML-mode.
 * 
 */
public class RichTextExample extends CustomComponent {

    public static final String txt = "<h1>RichText editor example</h1>"
            + "To edit this text, press the <b>Edit</b> button below."
            + "<br/>"
            + "See the <A href=\"http://www.itmill.com/documentation/itmill-toolkit-5-reference-manual/\">manual</a> "
            + "for more information.";

    private final VerticalLayout main;
    private final Label l;
    private final RichTextArea editor = new RichTextArea();
    private final Button b;

    public RichTextExample() {
        // main layout
        main = new VerticalLayout();
        main.setMargin(true);
        setCompositionRoot(main);

        editor.setWidth("100%");

        // Add the label
        l = new Label(txt);
        l.setContentMode(Label.CONTENT_XHTML);
        main.addComponent(l);
        // Edit button with inline click-listener
        b = new Button("Edit", new Button.ClickListener() {
            public void buttonClick(ClickEvent event) {
                // swap Label <-> RichTextArea
                if (main.getComponentIterator().next() == l) {
                    editor.setValue(l.getValue());
                    main.replaceComponent(l, editor);
                    b.setCaption("Save");
                } else {
                    l.setValue(editor.getValue());
                    main.replaceComponent(editor, l);
                    b.setCaption("Edit");
                }
            }
        });
        main.addComponent(b);
        main.setComponentAlignment(b, Alignment.MIDDLE_RIGHT);
    }

}