aboutsummaryrefslogtreecommitdiffstats
path: root/uitest/src/com/vaadin/tests/minitutorials/v7b9/SettingsView.java
blob: 74c4e68b93467d71042705c7b698872466136d03 (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
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
package com.vaadin.tests.minitutorials.v7b9;

import java.util.Date;

import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.data.util.ObjectProperty;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.DateField;
import com.vaadin.ui.InlineDateField;
import com.vaadin.ui.Layout;
import com.vaadin.ui.Notification;
import com.vaadin.ui.Notification.Type;
import com.vaadin.ui.Panel;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.themes.Reindeer;

public class SettingsView extends Panel implements View {

    public static String NAME = "settings";

    Navigator navigator;
    DateField date;
    Button apply;
    Button cancel;

    String pendingViewAndParameters = null;

    public SettingsView(final Navigator navigator) {
        this.navigator = navigator;
        Layout layout = new VerticalLayout();

        date = new InlineDateField("Birth date");
        date.setImmediate(true);
        layout.addComponent(date);
        // pretend we have a datasource:
        date.setPropertyDataSource(new ObjectProperty<Date>(new Date()));
        date.setBuffered(true);
        // show buttons when date is changed
        date.addValueChangeListener(new ValueChangeListener() {
            @Override
            public void valueChange(ValueChangeEvent event) {
                hideOrShowButtons();
                pendingViewAndParameters = null;
            }
        });

        // commit the TextField changes when "Save" is clicked
        apply = new Button("Apply", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                date.commit();
                hideOrShowButtons();
                processPendingView();
            }
        });
        layout.addComponent(apply);

        // Discard the TextField changes when "Cancel" is clicked
        cancel = new Button("Cancel", new Button.ClickListener() {
            @Override
            public void buttonClick(ClickEvent event) {
                date.discard();
                hideOrShowButtons();
                processPendingView();
            }
        });
        cancel.setStyleName(Reindeer.BUTTON_LINK);
        layout.addComponent(cancel);

        // attach a listener so that we'll get asked isViewChangeAllowed?
        navigator.addViewChangeListener(new ViewChangeListener() {
            @Override
            public boolean beforeViewChange(ViewChangeEvent event) {
                if (event.getOldView() == SettingsView.this
                        && date.isModified()) {

                    // save the View where the user intended to go
                    pendingViewAndParameters = event.getViewName();
                    if (event.getParameters() != null) {
                        pendingViewAndParameters += "/";
                        pendingViewAndParameters += event.getParameters();
                    }

                    // Prompt the user to save or cancel if the name is changed
                    Notification.show("Please apply or cancel your changes",
                            Type.WARNING_MESSAGE);

                    return false;
                } else {
                    return true;
                }
            }

            @Override
            public void afterViewChange(ViewChangeEvent event) {
                pendingViewAndParameters = null;
            }
        });

        setContent(layout);

    }

    // Hide or show buttons depending on whether date is modified or not
    private void hideOrShowButtons() {
        apply.setVisible(date.isModified());
        cancel.setVisible(date.isModified());
    }

    // if there is a pending view change, do it now
    private void processPendingView() {
        if (pendingViewAndParameters != null) {
            navigator.navigateTo(pendingViewAndParameters);
            pendingViewAndParameters = null;
        }
    }

    public void navigateTo(String fragmentParameters) {
        hideOrShowButtons();
    }

    @Override
    public void enter(ViewChangeEvent event) {
        // TODO Auto-generated method stub

    }

}