|
@@ -4,163 +4,128 @@ |
4 |
4 |
|
import com.google.gwt.event.logical.shared.*; |
5 |
5 |
|
import com.google.gwt.event.shared.*; |
6 |
6 |
|
import com.google.gwt.place.shared.*; |
7 |
|
- |
import com.google.gwt.user.client.*; |
8 |
7 |
|
|
9 |
8 |
|
public class CopyPlaceHistoryHandler |
10 |
9 |
|
{ |
|
10 |
+ |
private final Historian historian; |
|
11 |
+ |
|
|
12 |
+ |
private final PlaceHistoryMapper mapper; |
|
13 |
+ |
|
|
14 |
+ |
private PlaceChanger mPlaceChanger; |
|
15 |
+ |
|
|
16 |
+ |
private Place defaultPlace = Place.NOWHERE; |
|
17 |
+ |
|
11 |
18 |
|
/** |
12 |
|
- |
* Optional delegate in charge of History related events. Provides nice |
13 |
|
- |
* isolation for unit testing, and allows pre- or post-processing of tokens. |
14 |
|
- |
* Methods correspond to the like named methods on {@link History}. |
|
19 |
+ |
* Create a new PlaceHistoryHandler with a {@link HistorianImpl}. The |
|
20 |
+ |
* HistorianImpl is created via a call to GWT.create(), so an alternative |
|
21 |
+ |
* default implementation can be provided through <replace-with> rules |
|
22 |
+ |
* in a {@code gwt.xml} file. |
|
23 |
+ |
* |
|
24 |
+ |
* @param mapper a {@link PlaceHistoryMapper} instance |
15 |
25 |
|
*/ |
16 |
|
- |
public interface Historian { |
17 |
|
- |
/** |
18 |
|
- |
* Adds a {@link com.google.gwt.event.logical.shared.ValueChangeEvent} |
19 |
|
- |
* handler to be informed of changes to the browser's history stack. |
20 |
|
- |
* |
21 |
|
- |
* @param handler the handler |
22 |
|
- |
* @return the registration used to remove this value change handler |
23 |
|
- |
*/ |
24 |
|
- |
HandlerRegistration addValueChangeHandler( |
25 |
|
- |
ValueChangeHandler<String> valueChangeHandler); |
26 |
|
- |
|
27 |
|
- |
/** |
28 |
|
- |
* @return the current history token. |
29 |
|
- |
*/ |
30 |
|
- |
String getToken(); |
31 |
|
- |
|
32 |
|
- |
/** |
33 |
|
- |
* Adds a new browser history entry. Calling this method will cause |
34 |
|
- |
* {@link ValueChangeHandler#onValueChange(com.google.gwt.event.logical.shared.ValueChangeEvent)} |
35 |
|
- |
* to be called as well. |
36 |
|
- |
*/ |
37 |
|
- |
void newItem(String token, boolean issueEvent); |
38 |
|
- |
} |
39 |
|
- |
|
40 |
|
- |
/** |
41 |
|
- |
* Default implementation of {@link Historian}, based on {@link History}. |
42 |
|
- |
*/ |
43 |
|
- |
public static class DefaultHistorian implements Historian { |
44 |
|
- |
@Override public HandlerRegistration addValueChangeHandler( |
45 |
|
- |
ValueChangeHandler<String> valueChangeHandler) { |
46 |
|
- |
return History.addValueChangeHandler(valueChangeHandler); |
47 |
|
- |
} |
48 |
|
- |
|
49 |
|
- |
@Override public String getToken() { |
50 |
|
- |
return History.getToken(); |
51 |
|
- |
} |
52 |
|
- |
|
53 |
|
- |
@Override public void newItem(String token, boolean issueEvent) { |
54 |
|
- |
History.newItem(token, issueEvent); |
55 |
|
- |
} |
56 |
|
- |
} |
57 |
|
- |
|
58 |
|
- |
|
59 |
|
- |
private final Historian historian; |
60 |
|
- |
|
61 |
|
- |
private final PlaceHistoryMapper mapper; |
62 |
|
- |
|
63 |
|
- |
private PlaceChanger mPlaceChanger; |
64 |
|
- |
|
65 |
|
- |
private Place defaultPlace = Place.NOWHERE; |
66 |
|
- |
|
67 |
|
- |
/** |
68 |
|
- |
* Create a new PlaceHistoryHandler with a {@link DefaultHistorian}. The |
69 |
|
- |
* DefaultHistorian is created via a call to GWT.create(), so an alternative |
70 |
|
- |
* default implementation can be provided through <replace-with> rules |
71 |
|
- |
* in a {@code gwt.xml} file. |
72 |
|
- |
* |
73 |
|
- |
* @param mapper a {@link PlaceHistoryMapper} instance |
74 |
|
- |
*/ |
75 |
|
- |
public CopyPlaceHistoryHandler(PlaceHistoryMapper mapper) { |
76 |
|
- |
this(mapper, (Historian) GWT.create( DefaultHistorian.class )); |
77 |
|
- |
} |
78 |
|
- |
|
79 |
|
- |
/** |
80 |
|
- |
* Create a new PlaceHistoryHandler. |
81 |
|
- |
* |
82 |
|
- |
* @param mapper a {@link PlaceHistoryMapper} instance |
83 |
|
- |
* @param historian a {@link Historian} instance |
84 |
|
- |
*/ |
85 |
|
- |
public CopyPlaceHistoryHandler(PlaceHistoryMapper mapper, Historian historian) { |
86 |
|
- |
this.mapper = mapper; |
87 |
|
- |
this.historian = historian; |
88 |
|
- |
} |
89 |
|
- |
|
90 |
|
- |
/** |
91 |
|
- |
* Handle the current history token. Typically called at application start, to |
92 |
|
- |
* ensure bookmark launches work. |
93 |
|
- |
*/ |
94 |
|
- |
public void handleCurrentHistory() { |
95 |
|
- |
handleHistoryToken(historian.getToken()); |
96 |
|
- |
} |
97 |
|
- |
|
98 |
|
- |
/** |
99 |
|
- |
* Initialize this place history handler. |
100 |
|
- |
* |
101 |
|
- |
* @return a registration object to de-register the handler |
102 |
|
- |
*/ |
103 |
|
- |
public HandlerRegistration register(PlaceChanger pPlaceChanger, |
104 |
|
- |
EventBus eventBus, Place defaultPlace) { |
105 |
|
- |
this.mPlaceChanger = pPlaceChanger; |
106 |
|
- |
this.defaultPlace = defaultPlace; |
107 |
|
- |
|
108 |
|
- |
final HandlerRegistration placeReg = eventBus.addHandler( |
109 |
|
- |
PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() { |
110 |
|
- |
@Override public void onPlaceChange(PlaceChangeEvent event) { |
111 |
|
- |
Place newPlace = event.getNewPlace(); |
112 |
|
- |
historian.newItem(tokenForPlace(newPlace), false); |
113 |
|
- |
} |
114 |
|
- |
}); |
115 |
|
- |
|
116 |
|
- |
final HandlerRegistration historyReg = historian.addValueChangeHandler(new ValueChangeHandler<String>() { |
117 |
|
- |
@Override public void onValueChange(ValueChangeEvent<String> event) { |
118 |
|
- |
String token = event.getValue(); |
119 |
|
- |
handleHistoryToken(token); |
120 |
|
- |
} |
121 |
|
- |
}); |
122 |
|
- |
|
123 |
|
- |
return new HandlerRegistration() { |
124 |
|
- |
@Override public void removeHandler() { |
125 |
|
- |
CopyPlaceHistoryHandler.this.defaultPlace = Place.NOWHERE; |
126 |
|
- |
CopyPlaceHistoryHandler.this.mPlaceChanger = null; |
127 |
|
- |
placeReg.removeHandler(); |
128 |
|
- |
historyReg.removeHandler(); |
129 |
|
- |
} |
130 |
|
- |
}; |
131 |
|
- |
} |
132 |
|
- |
|
133 |
|
- |
private void handleHistoryToken(String token) { |
134 |
|
- |
|
135 |
|
- |
Place newPlace = null; |
136 |
|
- |
|
137 |
|
- |
if ("".equals(token)) { |
138 |
|
- |
newPlace = defaultPlace; |
|
26 |
+ |
public CopyPlaceHistoryHandler( PlaceHistoryMapper mapper ) |
|
27 |
+ |
{ |
|
28 |
+ |
this( mapper, (Historian) GWT.create( HistorianImpl.class ) ); |
139 |
29 |
|
} |
140 |
30 |
|
|
141 |
|
- |
if (newPlace == null) { |
142 |
|
- |
newPlace = mapper.getPlace(token); |
|
31 |
+ |
/** |
|
32 |
+ |
* Create a new PlaceHistoryHandler. |
|
33 |
+ |
* |
|
34 |
+ |
* @param mapper a {@link PlaceHistoryMapper} instance |
|
35 |
+ |
* @param historian a {@link Historian} instance |
|
36 |
+ |
*/ |
|
37 |
+ |
public CopyPlaceHistoryHandler( PlaceHistoryMapper mapper, Historian historian ) |
|
38 |
+ |
{ |
|
39 |
+ |
this.mapper = mapper; |
|
40 |
+ |
this.historian = historian; |
143 |
41 |
|
} |
144 |
42 |
|
|
145 |
|
- |
if (newPlace == null) { |
146 |
|
- |
// log().warning("Unrecognized history token: " + token); |
147 |
|
- |
newPlace = defaultPlace; |
|
43 |
+ |
/** |
|
44 |
+ |
* Handle the current history token. Typically called at application start, to |
|
45 |
+ |
* ensure bookmark launches work. |
|
46 |
+ |
*/ |
|
47 |
+ |
public void handleCurrentHistory() |
|
48 |
+ |
{ |
|
49 |
+ |
handleHistoryToken( historian.getToken() ); |
148 |
50 |
|
} |
149 |
51 |
|
|
150 |
|
- |
mPlaceChanger.goTo(newPlace); |
151 |
|
- |
} |
|
52 |
+ |
/** |
|
53 |
+ |
* Initialize this place history handler. |
|
54 |
+ |
* |
|
55 |
+ |
* @return a registration object to de-register the handler |
|
56 |
+ |
*/ |
|
57 |
+ |
public HandlerRegistration register( PlaceChanger pPlaceChanger, EventBus eventBus, Place defaultPlace ) |
|
58 |
+ |
{ |
|
59 |
+ |
this.mPlaceChanger = pPlaceChanger; |
|
60 |
+ |
this.defaultPlace = defaultPlace; |
|
61 |
+ |
|
|
62 |
+ |
final HandlerRegistration placeReg = eventBus.addHandler( PlaceChangeEvent.TYPE, new PlaceChangeEvent.Handler() |
|
63 |
+ |
{ |
|
64 |
+ |
@Override public void onPlaceChange( PlaceChangeEvent event ) |
|
65 |
+ |
{ |
|
66 |
+ |
Place newPlace = event.getNewPlace(); |
|
67 |
+ |
historian.newItem( tokenForPlace( newPlace ), false ); |
|
68 |
+ |
} |
|
69 |
+ |
} ); |
|
70 |
+ |
|
|
71 |
+ |
historian.addValueChangeHandler( new ValueChangeHandler<String>() |
|
72 |
+ |
{ |
|
73 |
+ |
@Override public void onValueChange( ValueChangeEvent<String> event ) |
|
74 |
+ |
{ |
|
75 |
+ |
String token = event.getValue(); |
|
76 |
+ |
handleHistoryToken( token ); |
|
77 |
+ |
} |
|
78 |
+ |
} ); |
|
79 |
+ |
|
|
80 |
+ |
return new HandlerRegistration() |
|
81 |
+ |
{ |
|
82 |
+ |
@Override public void removeHandler() |
|
83 |
+ |
{ |
|
84 |
+ |
CopyPlaceHistoryHandler.this.defaultPlace = Place.NOWHERE; |
|
85 |
+ |
CopyPlaceHistoryHandler.this.mPlaceChanger = null; |
|
86 |
+ |
placeReg.removeHandler(); |
|
87 |
+ |
} |
|
88 |
+ |
}; |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
private void handleHistoryToken( String token ) |
|
92 |
+ |
{ |
|
93 |
+ |
|
|
94 |
+ |
Place newPlace = null; |
|
95 |
+ |
|
|
96 |
+ |
if ( "".equals( token ) ) |
|
97 |
+ |
{ |
|
98 |
+ |
newPlace = defaultPlace; |
|
99 |
+ |
} |
|
100 |
+ |
|
|
101 |
+ |
if ( newPlace == null ) |
|
102 |
+ |
{ |
|
103 |
+ |
newPlace = mapper.getPlace( token ); |
|
104 |
+ |
} |
152 |
105 |
|
|
153 |
|
- |
private String tokenForPlace(Place newPlace) { |
154 |
|
- |
if (defaultPlace.equals(newPlace)) { |
155 |
|
- |
return ""; |
156 |
|
- |
} |
|
106 |
+ |
if ( newPlace == null ) |
|
107 |
+ |
{ |
|
108 |
+ |
// log().warning("Unrecognized history token: " + token); |
|
109 |
+ |
newPlace = defaultPlace; |
|
110 |
+ |
} |
157 |
111 |
|
|
158 |
|
- |
String token = mapper.getToken(newPlace); |
159 |
|
- |
if (token != null) { |
160 |
|
- |
return token; |
|
112 |
+ |
mPlaceChanger.goTo( newPlace ); |
161 |
113 |
|
} |
162 |
114 |
|
|
|
115 |
+ |
private String tokenForPlace( Place newPlace ) |
|
116 |
+ |
{ |
|
117 |
+ |
if ( defaultPlace.equals( newPlace ) ) |
|
118 |
+ |
{ |
|
119 |
+ |
return ""; |
|
120 |
+ |
} |
|
121 |
+ |
|
|
122 |
+ |
String token = mapper.getToken( newPlace ); |
|
123 |
+ |
if ( token != null ) |
|
124 |
+ |
{ |
|
125 |
+ |
return token; |
|
126 |
+ |
} |
|
127 |
+ |
|
163 |
128 |
|
// log().warning("Place not mapped to a token: " + newPlace); |
164 |
|
- |
return ""; |
165 |
|
- |
} |
|
129 |
+ |
return ""; |
|
130 |
+ |
} |
166 |
131 |
|
} |