summaryrefslogtreecommitdiffstats
path: root/src/com/itmill/toolkit/automatedtests/featurebrowser/EmbeddedBrowserExample.java
blob: cd60dbd2bd64bc10899ad6f7f882948e1b58b5ce (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
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.itmill.toolkit.automatedtests.featurebrowser;

import java.net.MalformedURLException;
import java.net.URL;

import com.itmill.toolkit.data.Property.ValueChangeEvent;
import com.itmill.toolkit.terminal.ExternalResource;
import com.itmill.toolkit.ui.Embedded;
import com.itmill.toolkit.ui.Select;
import com.itmill.toolkit.ui.VerticalLayout;
import com.itmill.toolkit.ui.Window.Notification;

/**
 * Demonstrates the use of Embedded and "suggesting" Select by creating a simple
 * web-browser. Note: does not check for recursion.
 * 
 * @author IT Mill Ltd.
 * @see com.itmill.toolkit.ui.Window
 */
public class EmbeddedBrowserExample extends VerticalLayout implements
        Select.ValueChangeListener {

    private static final long serialVersionUID = -6209869808788169597L;

    // Default URL to open.
    private static final String DEFAULT_URL = "http://www.itmill.com/index_itmill_toolkit.htm";

    // The embedded page
    Embedded emb = new Embedded();

    public EmbeddedBrowserExample() {
        this(new String[] { DEFAULT_URL,
                "http://www.itmill.com/index_developers.htm",
                "http://toolkit.itmill.com/demo/doc/api/",
                "http://www.itmill.com/manual/index.html" });
    }

    public EmbeddedBrowserExample(String[] urls) {
        setSizeFull();

        // create the address combobox
        final Select select = new Select();
        // allow input
        select.setNewItemsAllowed(true);
        // no empty selection
        select.setNullSelectionAllowed(false);
        // no 'go' -button clicking necessary
        select.setImmediate(true);
        // add some pre-configured URLs
        for (int i = 0; i < urls.length; i++) {
            select.addItem(urls[i]);
        }
        // add to layout
        addComponent(select);
        // add listener and select initial URL
        select.addListener(this);
        select.setValue(urls[0]);

        select.setWidth("100%");

        // configure the embedded and add to layout
        emb.setType(Embedded.TYPE_BROWSER);
        emb.setSizeFull();
        addComponent(emb);
        // make the embedded as large as possible
        setExpandRatio(emb, 1);

    }

    public void valueChange(ValueChangeEvent event) {
        final String url = (String) event.getProperty().getValue();
        if (url != null) {
            try {
                // the selected url has changed, let's go there
                @SuppressWarnings("unused")
                URL u = new URL(url);
                emb.setSource(new ExternalResource(url));

            } catch (MalformedURLException e) {
                getWindow().showNotification("Invalid address",
                        e.getMessage() + " (example: http://www.itmill.com)",
                        Notification.TYPE_WARNING_MESSAGE);
            }

        }

    }
}