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
|
/*
@ITMillApache2LicenseForJavaFiles@
*/
package com.vaadin.ui;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;
import com.vaadin.terminal.PaintException;
import com.vaadin.terminal.PaintTarget;
import com.vaadin.terminal.gwt.client.ui.VUriFragmentUtility;
import com.vaadin.ui.ClientWidget.LoadStyle;
/**
* Experimental web browser dependent component for URI fragment (part after
* hash mark "#") reading and writing.
*
* Component can be used to workaround common ajax web applications pitfalls:
* bookmarking a program state and back button.
*
*/
@SuppressWarnings("serial")
@ClientWidget(value = VUriFragmentUtility.class, loadStyle = LoadStyle.EAGER)
public class UriFragmentUtility extends AbstractComponent {
/**
* Listener that listens changes in URI fragment.
*/
public interface FragmentChangedListener extends Serializable {
public void fragmentChanged(FragmentChangedEvent source);
}
/**
* Event fired when uri fragment changes.
*/
public class FragmentChangedEvent extends Component.Event {
/**
* Creates a new instance of UriFragmentReader change event.
*
* @param source
* the Source of the event.
*/
public FragmentChangedEvent(Component source) {
super(source);
}
/**
* Gets the UriFragmentReader where the event occurred.
*
* @return the Source of the event.
*/
public UriFragmentUtility getUriFragmentUtility() {
return (UriFragmentUtility) getSource();
}
}
private static final Method FRAGMENT_CHANGED_METHOD;
static {
try {
FRAGMENT_CHANGED_METHOD = FragmentChangedListener.class
.getDeclaredMethod("fragmentChanged",
new Class[] { FragmentChangedEvent.class });
} catch (final java.lang.NoSuchMethodException e) {
// This should never happen
throw new java.lang.RuntimeException(
"Internal error finding methods in FragmentChangedListener");
}
}
public void addListener(FragmentChangedListener listener) {
addListener(FragmentChangedEvent.class, listener,
FRAGMENT_CHANGED_METHOD);
}
public void removeListener(FragmentChangedListener listener) {
removeListener(FragmentChangedEvent.class, listener,
FRAGMENT_CHANGED_METHOD);
}
private String fragment;
public UriFragmentUtility() {
// immediate by default
setImmediate(true);
}
@Override
public void paintContent(PaintTarget target) throws PaintException {
super.paintContent(target);
target.addVariable(this, "fragment", fragment);
}
@Override
public void changeVariables(Object source, Map<String, Object> variables) {
super.changeVariables(source, variables);
fragment = (String) variables.get("fragment");
fireEvent(new FragmentChangedEvent(this));
}
/**
* Gets currently set URI fragment.
* <p>
* To listen changes in fragment, hook a {@link FragmentChangedListener}.
* <p>
* Note that initial URI fragment that user used to enter the application
* will be read after application init. It fires FragmentChangedEvent only
* if it is not the same as on server side.
*
* @return the current fragment in browser uri or null if not known
*/
public String getFragment() {
return fragment;
}
/**
* Sets URI fragment. Optionally fires a {@link FragmentChangedEvent}
*
* @param newFragment
* id of the new fragment
* @param fireEvent
* true to fire event
* @see FragmentChangedEvent
* @see FragmentChangedListener
*/
public void setFragment(String newFragment, boolean fireEvent) {
if ((newFragment == null && fragment != null)
|| (newFragment != null && !newFragment.equals(fragment))) {
fragment = newFragment;
if (fireEvent) {
fireEvent(new FragmentChangedEvent(this));
}
requestRepaint();
}
}
/**
* Sets URI fragment. This method fires a {@link FragmentChangedEvent}
*
* @param newFragment
* id of the new fragment
* @see FragmentChangedEvent
* @see FragmentChangedListener
*/
public void setFragment(String newFragment) {
setFragment(newFragment, true);
}
}
|