Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/client/pavsupport/PlaceActivityManager.java

Diff revisions: vs.
  @@ -1,211 +1,210 @@
1 - package org.litesoft.GWT.client.pavsupport;
2 -
3 - import org.litesoft.GWT.client.historian.*;
4 - import org.litesoft.GWT.client.pavsupport.internal.*;
5 - import org.litesoft.GWT.client.util.*;
6 - import org.litesoft.commonfoundation.base.*;
7 - import org.litesoft.commonfoundation.typeutils.*;
8 - import org.litesoft.logger.*;
9 -
10 - import com.google.gwt.event.logical.shared.*;
11 - import com.google.gwt.place.shared.*;
12 - import com.google.gwt.user.client.*;
13 - import com.google.gwt.user.client.ui.*;
14 -
15 - public class PlaceActivityManager<CommonActivityParam extends CommonActivityParameter> implements PlaceChangerWithCurrentUrlSupport {
16 - private static final Logger LOGGER = LoggerFactory.getLogger( PlaceActivityManager.class );
17 -
18 - private final PanelPlaceActivityHelper<CommonActivityParam> mActivityHelper;
19 -
20 - private final CommonActivityParam mCommonActivityParam;
21 -
22 - private final PlaceHistoryMapper mMapper;
23 - private final WindowClose mWindowClose;
24 - private final Historian mHistorian;
25 - private Place mPendingCodePlace;
26 -
27 - public PlaceActivityManager( AcceptsOneWidget pPanel, PlaceHistoryMapper pMapper, WindowClose pWindowClose, Historian pHistorian,
28 - CommonActivityParam pCommonActivityParam ) {
29 - mCommonActivityParam = (pCommonActivityParam != null) ? pCommonActivityParam : createDefaultCommonActivityParam();
30 - mActivityHelper = new PanelPlaceActivityHelper<CommonActivityParam>( pPanel, mCommonActivityParam );
31 - mMapper = (pMapper != null) ? pMapper : new PlaceRegistry( mCommonActivityParam );
32 - mWindowClose = (pWindowClose != null) ? pWindowClose : new WindowCloseImpl();
33 - mWindowClose.addWindowClosingHandler( new Window.ClosingHandler() {
34 - @Override
35 - public void onWindowClosing( Window.ClosingEvent event ) {
36 - String zWarning = getLeavingCurrentPlaceWarningText();
37 - if ( zWarning != null ) {
38 - event.setMessage( zWarning );
39 - }
40 - }
41 - } );
42 - mHistorian = (pHistorian != null) ? pHistorian : new HistorianImpl();
43 - mHistorian.addValueChangeHandler( new ValueChangeHandler<String>() {
44 - @Override
45 - public void onValueChange( ValueChangeEvent<String> event ) {
46 - handleHistoryToken( event.getValue() );
47 - }
48 - } );
49 - }
50 -
51 - public PlaceActivityManager( AcceptsOneWidget pPanel, CommonActivityParam pCommonActivityParam ) {
52 - this( pPanel, null, null, null, pCommonActivityParam );
53 - }
54 -
55 - public PlaceActivityManager( AcceptsOneWidget pPanel ) {
56 - this( pPanel, null, null, null, null );
57 - }
58 -
59 - public void add( PlaceChangeListener pPlaceChangeListener ) {
60 - mActivityHelper.add( pPlaceChangeListener );
61 - }
62 -
63 - /**
64 - * Go to the Current URL (current history token).
65 - * <p/>
66 - * Typically called at application start, to ensure bookmark launches work.
67 - */
68 - @Override
69 - public void goToCurrentUrl() {
70 - handleHistoryToken( mHistorian.getToken() );
71 - }
72 -
73 - /**
74 - * Returns the current place.
75 - *
76 - * @return a {@link Place} instance
77 - */
78 - @Override
79 - public Place getWhere() {
80 - return mActivityHelper.getCurrentPlace();
81 - }
82 -
83 - /**
84 - * Request a change to the previous place on the browser's history stack.
85 - * If there is a previous place on the browser's history stack, the location
86 - * changes and a {@link PlaceChangeEvent} is posted announcing the previous place.
87 - */
88 - @Override
89 - public void goBack() {
90 - mActivityHelper.placeChangeRequested( null, PlaceChangeListener.Source.Back );
91 - mHistorian.back();
92 - }
93 -
94 - /**
95 - * Request a change to a new place. It is not a given that we'll actually get
96 - * there. If there is an ActivityFactory for the Place, and the user does not
97 - * reject leaving the current Place, then the URL is adjusted to have the user
98 - * taken to the new place.
99 - *
100 - * @param pNewPlace a {@link Place} instance
101 - *
102 - * @return null if going to pNewPlace, !null means NOT going and why
103 - */
104 - @Override
105 - public GoToPlace goForwardTo( Place pNewPlace ) {
106 - Place zCurrentPlace = mActivityHelper.getCurrentPlace();
107 - pNewPlace = Objects.deNull( pNewPlace, zCurrentPlace );
108 - if ( pNewPlace.equals( zCurrentPlace ) ) {
109 - return GoToPlace.AlreadyThere;
110 - }
111 - if ( pNewPlace.equals( mPendingCodePlace ) || mActivityHelper.isPendingAsync( pNewPlace ) ) {
112 - return null;
113 - }
114 - if ( null == mActivityHelper.get( pNewPlace ) ) // Check for Factory
115 - {
116 - return GoToPlace.NoActivity;
117 - }
118 - if ( userRejectedLeavingCurrentPlace( pNewPlace ) ) {
119 - return GoToPlace.CurrentActivityRejectedLeaving; // From Code, maybe the code shouldn't have allowed it!
120 - }
121 - mActivityHelper.placeChangeRequested( pNewPlace, PlaceChangeListener.Source.Forward );
122 - mActivityHelper.clearPendingAsync();
123 - mPendingCodePlace = pNewPlace;
124 - mHistorian.newItem( mMapper.getToken( pNewPlace ), true );
125 - return null;
126 - }
127 -
128 - private void handleHistoryToken( String pToken ) {
129 - Place zWasGoingToCodePlace = mPendingCodePlace;
130 - mPendingCodePlace = null;
131 - mCommonActivityParam.getMessageUserSink().clearMessage();
132 -
133 - Place zNewPlace = null;
134 -
135 - if ( null == (pToken = Strings.noEmpty( pToken )) ) {
136 - zNewPlace = PlaceRegistry.getDefaultPlace();
137 - } else {
138 - try {
139 - if ( null == (zNewPlace = mMapper.getPlace( pToken )) ) {
140 - LOGGER.info.log( "Unrecognized history token: ", pToken );
141 - }
142 - }
143 - catch ( RuntimeException e ) {
144 - LOGGER.warn.log( e, "Unable to parse history token: ", pToken );
145 - }
146 - }
147 - Place zCurrentPlace = mActivityHelper.getCurrentPlace();
148 - zNewPlace = Objects.deNull( zNewPlace, zCurrentPlace );
149 - if ( zNewPlace.equals( zCurrentPlace ) ) {
150 - mActivityHelper.clearPendingAsync();
151 - return;
152 - }
153 - if ( (zWasGoingToCodePlace != null) && !zNewPlace.equals( zWasGoingToCodePlace ) && userWantsToStopUrlChange( zNewPlace ) ) {
154 - mActivityHelper.clearPendingAsync();
155 - resetURLbackTo( zCurrentPlace );
156 - return;
157 - }
158 - if ( !mActivityHelper.activate( zNewPlace ) ) {
159 - mCommonActivityParam.getMessageUserSink().setErrorMessage( "No Activity Factory for '" + ClassName.simple( zNewPlace ) + "' from: " + pToken );
160 - resetURLbackTo( zCurrentPlace );
161 - }
162 - }
163 -
164 - protected boolean userWantsToStopUrlChange( Place pNewPlace ) {
165 - return userRejectedLeavingCurrentPlace( pNewPlace );
166 - }
167 -
168 - protected void resetURLbackTo( Place pCurrentPlace ) {
169 - mHistorian.newItem( mMapper.getToken( pCurrentPlace ), false ); // reset the URL back to the Current Place
170 - }
171 -
172 - @SuppressWarnings("UnusedParameters")
173 - protected boolean userRejectedLeavingCurrentPlace( Place newPlace ) {
174 - String zWarning = getLeavingCurrentPlaceWarningText();
175 - return (zWarning != null) && !mWindowClose.confirm( zWarning );
176 - }
177 -
178 - protected String getLeavingCurrentPlaceWarningText() {
179 - return mActivityHelper.getCurrentActivityMayStop();
180 - }
181 -
182 - @SuppressWarnings({"unchecked"})
183 - protected CommonActivityParam createDefaultCommonActivityParam() {
184 - return (CommonActivityParam) new DefaultCommonActivityParam();
185 - }
186 -
187 - /**
188 - * Default implementation of CommonActivityParameter, based on {@link Window}.
189 - */
190 - private class DefaultCommonActivityParam implements CommonActivityParameter {
191 - @Override
192 - public MessageUserSink getMessageUserSink() {
193 - return DialogMessageUserSink.INSTANCE;
194 - }
195 -
196 - @Override
197 - public Place getWhere() {
198 - return PlaceActivityManager.this.getWhere();
199 - }
200 -
201 - @Override
202 - public void goBack() {
203 - PlaceActivityManager.this.goBack();
204 - }
205 -
206 - @Override
207 - public GoToPlace goForwardTo( Place pNewPlace ) {
208 - return PlaceActivityManager.this.goForwardTo( pNewPlace );
209 - }
210 - }
211 - }
1 + package org.litesoft.GWT.client.pavsupport;
2 +
3 + import org.litesoft.GWT.client.historian.*;
4 + import org.litesoft.GWT.client.pavsupport.internal.*;
5 + import org.litesoft.GWT.client.util.*;
6 + import org.litesoft.commonfoundation.base.*;
7 + import org.litesoft.logger.*;
8 +
9 + import com.google.gwt.event.logical.shared.*;
10 + import com.google.gwt.place.shared.*;
11 + import com.google.gwt.user.client.*;
12 + import com.google.gwt.user.client.ui.*;
13 +
14 + public class PlaceActivityManager<CommonActivityParam extends CommonActivityParameter> implements PlaceChangerWithCurrentUrlSupport {
15 + private static final Logger LOGGER = LoggerFactory.getLogger( PlaceActivityManager.class );
16 +
17 + private final PanelPlaceActivityHelper<CommonActivityParam> mActivityHelper;
18 +
19 + private final CommonActivityParam mCommonActivityParam;
20 +
21 + private final PlaceHistoryMapper mMapper;
22 + private final WindowClose mWindowClose;
23 + private final Historian mHistorian;
24 + private Place mPendingCodePlace;
25 +
26 + public PlaceActivityManager( AcceptsOneWidget pPanel, PlaceHistoryMapper pMapper, WindowClose pWindowClose, Historian pHistorian,
27 + CommonActivityParam pCommonActivityParam ) {
28 + mCommonActivityParam = (pCommonActivityParam != null) ? pCommonActivityParam : createDefaultCommonActivityParam();
29 + mActivityHelper = new PanelPlaceActivityHelper<CommonActivityParam>( pPanel, mCommonActivityParam );
30 + mMapper = (pMapper != null) ? pMapper : new PlaceRegistry( mCommonActivityParam );
31 + mWindowClose = (pWindowClose != null) ? pWindowClose : new WindowCloseImpl();
32 + mWindowClose.addWindowClosingHandler( new Window.ClosingHandler() {
33 + @Override
34 + public void onWindowClosing( Window.ClosingEvent event ) {
35 + String zWarning = getLeavingCurrentPlaceWarningText();
36 + if ( zWarning != null ) {
37 + event.setMessage( zWarning );
38 + }
39 + }
40 + } );
41 + mHistorian = (pHistorian != null) ? pHistorian : new HistorianImpl();
42 + mHistorian.addValueChangeHandler( new ValueChangeHandler<String>() {
43 + @Override
44 + public void onValueChange( ValueChangeEvent<String> event ) {
45 + handleHistoryToken( event.getValue() );
46 + }
47 + } );
48 + }
49 +
50 + public PlaceActivityManager( AcceptsOneWidget pPanel, CommonActivityParam pCommonActivityParam ) {
51 + this( pPanel, null, null, null, pCommonActivityParam );
52 + }
53 +
54 + public PlaceActivityManager( AcceptsOneWidget pPanel ) {
55 + this( pPanel, null, null, null, null );
56 + }
57 +
58 + public void add( PlaceChangeListener pPlaceChangeListener ) {
59 + mActivityHelper.add( pPlaceChangeListener );
60 + }
61 +
62 + /**
63 + * Go to the Current URL (current history token).
64 + * <p/>
65 + * Typically called at application start, to ensure bookmark launches work.
66 + */
67 + @Override
68 + public void goToCurrentUrl() {
69 + handleHistoryToken( mHistorian.getToken() );
70 + }
71 +
72 + /**
73 + * Returns the current place.
74 + *
75 + * @return a {@link Place} instance
76 + */
77 + @Override
78 + public Place getWhere() {
79 + return mActivityHelper.getCurrentPlace();
80 + }
81 +
82 + /**
83 + * Request a change to the previous place on the browser's history stack.
84 + * If there is a previous place on the browser's history stack, the location
85 + * changes and a {@link PlaceChangeEvent} is posted announcing the previous place.
86 + */
87 + @Override
88 + public void goBack() {
89 + mActivityHelper.placeChangeRequested( null, PlaceChangeListener.Source.Back );
90 + mHistorian.back();
91 + }
92 +
93 + /**
94 + * Request a change to a new place. It is not a given that we'll actually get
95 + * there. If there is an ActivityFactory for the Place, and the user does not
96 + * reject leaving the current Place, then the URL is adjusted to have the user
97 + * taken to the new place.
98 + *
99 + * @param pNewPlace a {@link Place} instance
100 + *
101 + * @return null if going to pNewPlace, !null means NOT going and why
102 + */
103 + @Override
104 + public GoToPlace goForwardTo( Place pNewPlace ) {
105 + Place zCurrentPlace = mActivityHelper.getCurrentPlace();
106 + pNewPlace = ConstrainTo.notNull( pNewPlace, zCurrentPlace );
107 + if ( pNewPlace.equals( zCurrentPlace ) ) {
108 + return GoToPlace.AlreadyThere;
109 + }
110 + if ( pNewPlace.equals( mPendingCodePlace ) || mActivityHelper.isPendingAsync( pNewPlace ) ) {
111 + return null;
112 + }
113 + if ( null == mActivityHelper.get( pNewPlace ) ) // Check for Factory
114 + {
115 + return GoToPlace.NoActivity;
116 + }
117 + if ( userRejectedLeavingCurrentPlace( pNewPlace ) ) {
118 + return GoToPlace.CurrentActivityRejectedLeaving; // From Code, maybe the code shouldn't have allowed it!
119 + }
120 + mActivityHelper.placeChangeRequested( pNewPlace, PlaceChangeListener.Source.Forward );
121 + mActivityHelper.clearPendingAsync();
122 + mPendingCodePlace = pNewPlace;
123 + mHistorian.newItem( mMapper.getToken( pNewPlace ), true );
124 + return null;
125 + }
126 +
127 + private void handleHistoryToken( String pToken ) {
128 + Place zWasGoingToCodePlace = mPendingCodePlace;
129 + mPendingCodePlace = null;
130 + mCommonActivityParam.getMessageUserSink().clearMessage();
131 +
132 + Place zNewPlace = null;
133 +
134 + if ( null == (pToken = ConstrainTo.significantOrNull( pToken )) ) {
135 + zNewPlace = PlaceRegistry.getDefaultPlace();
136 + } else {
137 + try {
138 + if ( null == (zNewPlace = mMapper.getPlace( pToken )) ) {
139 + LOGGER.info.log( "Unrecognized history token: ", pToken );
140 + }
141 + }
142 + catch ( RuntimeException e ) {
143 + LOGGER.warn.log( e, "Unable to parse history token: ", pToken );
144 + }
145 + }
146 + Place zCurrentPlace = mActivityHelper.getCurrentPlace();
147 + zNewPlace = ConstrainTo.notNull( zNewPlace, zCurrentPlace );
148 + if ( zNewPlace.equals( zCurrentPlace ) ) {
149 + mActivityHelper.clearPendingAsync();
150 + return;
151 + }
152 + if ( (zWasGoingToCodePlace != null) && !zNewPlace.equals( zWasGoingToCodePlace ) && userWantsToStopUrlChange( zNewPlace ) ) {
153 + mActivityHelper.clearPendingAsync();
154 + resetURLbackTo( zCurrentPlace );
155 + return;
156 + }
157 + if ( !mActivityHelper.activate( zNewPlace ) ) {
158 + mCommonActivityParam.getMessageUserSink().setErrorMessage( "No Activity Factory for '" + ClassName.simple( zNewPlace ) + "' from: " + pToken );
159 + resetURLbackTo( zCurrentPlace );
160 + }
161 + }
162 +
163 + protected boolean userWantsToStopUrlChange( Place pNewPlace ) {
164 + return userRejectedLeavingCurrentPlace( pNewPlace );
165 + }
166 +
167 + protected void resetURLbackTo( Place pCurrentPlace ) {
168 + mHistorian.newItem( mMapper.getToken( pCurrentPlace ), false ); // reset the URL back to the Current Place
169 + }
170 +
171 + @SuppressWarnings("UnusedParameters")
172 + protected boolean userRejectedLeavingCurrentPlace( Place newPlace ) {
173 + String zWarning = getLeavingCurrentPlaceWarningText();
174 + return (zWarning != null) && !mWindowClose.confirm( zWarning );
175 + }
176 +
177 + protected String getLeavingCurrentPlaceWarningText() {
178 + return mActivityHelper.getCurrentActivityMayStop();
179 + }
180 +
181 + @SuppressWarnings({"unchecked"})
182 + protected CommonActivityParam createDefaultCommonActivityParam() {
183 + return (CommonActivityParam) new DefaultCommonActivityParam();
184 + }
185 +
186 + /**
187 + * Default implementation of CommonActivityParameter, based on {@link Window}.
188 + */
189 + private class DefaultCommonActivityParam implements CommonActivityParameter {
190 + @Override
191 + public MessageUserSink getMessageUserSink() {
192 + return DialogMessageUserSink.INSTANCE;
193 + }
194 +
195 + @Override
196 + public Place getWhere() {
197 + return PlaceActivityManager.this.getWhere();
198 + }
199 +
200 + @Override
201 + public void goBack() {
202 + PlaceActivityManager.this.goBack();
203 + }
204 +
205 + @Override
206 + public GoToPlace goForwardTo( Place pNewPlace ) {
207 + return PlaceActivityManager.this.goForwardTo( pNewPlace );
208 + }
209 + }
210 + }