aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/tickets/Ticket34.java
blob: 85c39c6842590077ca2915cef2b41d82c466bd8b (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
98
99
100
101
102
103
104
105
106
package com.vaadin.tests.tickets;

import java.util.HashMap;
import java.util.Map;

import com.vaadin.LegacyApplication;
import com.vaadin.server.Page;
import com.vaadin.server.Page.FragmentChangedEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI.LegacyWindow;
import com.vaadin.ui.VerticalLayout;

public class Ticket34 extends LegacyApplication {

    private Map<String, Component> views = new HashMap<String, Component>();
    private VerticalLayout mainLayout;
    private Component currentView;

    @Override
    public void init() {

        buildViews(new String[] { "main", "view2", "view3" });

        mainLayout = new VerticalLayout();
        mainLayout.setSizeFull();
        final LegacyWindow mainWin = new LegacyWindow(
                "Test app for URI fragment management/reading", mainLayout);
        setMainWindow(mainWin);

        mainWin.getPage().addListener(new Page.FragmentChangedListener() {

            @Override
            public void fragmentChanged(FragmentChangedEvent event) {
                getMainWindow().showNotification(
                        "Fragment now: " + event.getFragment());
                // try to change to view mapped by fragment string
                setView(event.getFragment());
            }
        });

        setView("main");

    }

    private void setView(String string) {
        Component component = views.get(string);
        if (component == null) {
            getMainWindow().showNotification(
                    "View called " + string + " not found!");
        } else if (component != currentView) {
            if (currentView != null) {
                mainLayout.replaceComponent(currentView, component);
            } else {
                mainLayout.addComponent(component);
            }
            // give all extra space for view
            mainLayout.setExpandRatio(component, 1);
            currentView = component;
        }
    }

    private void buildViews(String[] strings) {
        for (String string : strings) {
            Panel p = new Panel(string);
            p.setSizeFull();
            ((VerticalLayout) p.getContent()).setSpacing(true);
            p.addComponent(new Label("This is a simple test case for "
                    + "UriFragmentReader that can be used for"
                    + " adding linking, back/forward button "
                    + "and history support for ajax application. "));
            StringBuffer sb = new StringBuffer();
            sb.append("Available views : ");
            for (String key : strings) {
                sb.append(key);
                sb.append(" ");
            }
            sb.append("Application will change to them from uri "
                    + "fragment or server initiated via textfield below.");
            p.addComponent(new Label(sb.toString()));

            final TextField tf = new TextField(
                    "Type view name (will change to that "
                            + "view and change the uri fragment)");
            p.addComponent(tf);
            Button b = new Button("Go!");
            p.addComponent(b);
            b.addListener(new Button.ClickListener() {

                @Override
                public void buttonClick(ClickEvent event) {
                    String viewName = tf.getValue().toString();
                    // fragmentChangedListener will change the view if possible
                    event.getButton().getUI().getPage().setFragment(viewName);
                }
            });

            views.put(string, p);
        }
    }

}