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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
|
/*
@VaadinApache2LicenseForJavaFiles@
*/
package com.vaadin.tests.navigator;
import com.vaadin.navigator.Navigator;
import com.vaadin.navigator.View;
import com.vaadin.navigator.ViewChangeListener;
import com.vaadin.navigator.ViewChangeListener.ViewChangeEvent;
import com.vaadin.server.VaadinRequest;
import com.vaadin.tests.util.Log;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.Layout;
import com.vaadin.ui.RichTextArea;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
public class NavigatorTest extends UI {
private Log log = new Log(5);
private Layout naviLayout = new VerticalLayout();
private TextField params = new TextField("Parameters");
private Navigator navi;
class ListView extends Table implements View {
public ListView() {
addContainerProperty("name", String.class, "");
addContainerProperty("value", String.class, "");
}
@Override
public void enter(ViewChangeEvent event) {
String params = event.getParameters();
log.log("Navigated to ListView "
+ (params.isEmpty() ? "without params" : "with params "
+ params));
removeAllItems();
for (String arg : params.split(",")) {
addItem(arg.split("=|$", 2), arg);
}
}
}
class EditView extends RichTextArea implements View {
@Override
public void enter(ViewChangeEvent event) {
String params = event.getParameters();
log.log("Navigated to EditView "
+ (params.isEmpty() ? "without params" : "with params "
+ params));
setValue("Displaying edit view with parameters " + params);
}
}
class SpecialCharsView extends Label implements View {
@Override
public void enter(ViewChangeEvent event) {
log.log("Navigated to SpecialCharsView: " + event.getViewName()
+ "; fragment: " + getPage().getUriFragment());
setValue(event.getViewName());
}
}
class DefaultView extends Label implements View {
@Override
public void enter(ViewChangeEvent event) {
String params = event.getParameters();
log.log("Navigated to DefaultView "
+ (params.isEmpty() ? "without params" : "with params "
+ params));
setValue("Default view: " + event.getParameters());
}
}
class ForbiddenView implements View {
@Override
public void enter(ViewChangeEvent event) {
log.log("Navigated to ForbiddenView - this should not happen");
}
}
class ErrorView extends Label implements View {
@Override
public void enter(ViewChangeEvent event) {
log.log("View '" + event.getViewName() + "' not found!");
setValue("Tried to navigate to " + event.getViewName()
+ " but such a view could not be found :(");
}
}
class NaviListener implements ViewChangeListener {
@Override
public boolean beforeViewChange(ViewChangeEvent event) {
if (event.getNewView() instanceof ForbiddenView) {
log.log("Prevent navigation to ForbiddenView");
return false;
}
return true;
}
@Override
public void afterViewChange(ViewChangeEvent event) {
}
};
class NaviButton extends Button {
public NaviButton(final String path) {
super("Navigate to " + path, new ClickListener() {
@Override
public void buttonClick(ClickEvent event) {
navi.navigateTo(path + "/" + params.getValue());
}
});
}
}
@Override
protected void init(VaadinRequest req) {
try {
VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
setContent(layout);
navi = new Navigator(this, naviLayout);
navi.addView("", new DefaultView());
navi.addView("list", new ListView());
navi.addView("edit", new EditView());
navi.addView("öääö !%&/()=", new SpecialCharsView());
navi.addView("forbidden", new ForbiddenView());
navi.addViewChangeListener(new NaviListener());
navi.setErrorView(new ErrorView());
layout.addComponent(new NaviButton("list"));
layout.addComponent(new NaviButton("edit"));
layout.addComponent(new NaviButton("forbidden"));
layout.addComponent(new NaviButton("öääö !%&/()="));
layout.addComponent(params);
layout.addComponent(log);
layout.addComponent(naviLayout);
} catch (Exception e) {
e.printStackTrace();
log.log("Exception: " + e);
}
}
}
|