summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/automatedtests/featurebrowser/JavaScriptAPIExample.java
blob: 5956db9853f390dd29f93196b2a2388fc3630f89 (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
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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.automatedtests.featurebrowser;

import java.util.Date;

import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.CustomComponent;
import com.vaadin.ui.Label;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.Button.ClickEvent;

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

    public static final String txt = "<p>For advanced client side programmers Toolkit offers a simple method which can be used to force sync client with server. This may be needed for example if another part of a mashup changes things on server.</p> (more examples will be added here as the APIs are made public)<br/><br/><A href=\"javascript:vaadin.forceSync();\">javascript:itmill.forceSync();</A>";

    private final VerticalLayout main;
    private final Label l;
    private final TextField editor = new TextField();

    public JavaScriptAPIExample() {
        // main layout
        main = new VerticalLayout();
        main.setMargin(true);
        setCompositionRoot(main);
        editor.setRows(7);
        editor.setColumns(50);
        // Add the label
        l = new Label(txt);
        l.setContentMode(Label.CONTENT_XHTML);
        main.addComponent(l);
        // Edit button with inline click-listener
        Button 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);
                    event.getButton().setCaption("Save");
                } else {
                    l.setValue(editor.getValue());
                    main.replaceComponent(editor, l);
                    event.getButton().setCaption("Edit");
                }
            }
        });
        main.addComponent(b);
        main.setComponentAlignment(b, Alignment.MIDDLE_RIGHT);

        // 
        Label l = new Label(
                "This label will update it's server-side value AFTER it's rendered to the client-side. "
                        + "The client will be synchronized on reload, when you click a button, "
                        + "or when vaadin.forceSync() is called.") {

            @Override
            public void paintContent(PaintTarget target) throws PaintException {

                super.paintContent(target);
                Delay d = new Delay(this);
                d.start();
            }

        };
        main.addComponent(l);

    }

    private class Delay extends Thread {
        Label label;

        public Delay(Label l) {
            label = l;
        }

        @Override
        public void run() {
            try {
                Thread.sleep(500);
                label.setValue(new Date().toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }
}