litesoft
@ 547
litesoft / trunk / GWT_Sandbox / MultiModule / common / src / org / litesoft / sandbox / infrastructure / client / PlaceActivityManager.java
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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 |
package org.litesoft.sandbox.infrastructure.client; import java.util.*; import org.litesoft.logger.*; import org.litesoft.sandbox.anywhere.util.*; import com.google.gwt.event.logical.shared.*; import com.google.gwt.event.shared.*; import com.google.gwt.place.shared.*; import com.google.gwt.user.client.*; import com.google.gwt.user.client.ui.*; public class PlaceActivityManager<CommonActivityParam extends CommonActivityParameter> extends ActivityFactoryRegistry implements PlaceChanger { private static Logger LOGGER = LoggerFactory.getLogger( PlaceActivityManager.class ); private static final Activity NULL_ACTIVITY = new AbstractActivity() { @Override public void start( AcceptsOneWidget panel ) { panel.setWidget( new Label( "!" ) ); } }; private final AcceptsOneWidget mPanel; private final CommonActivityParam mCommonActivityParam; private final PlaceHistoryMapper mMapper; private final WindowClose mWindowClose; private final Historian mHistorian; private Place mCurrentPlace = Place.NOWHERE; private Place mPendingCodePlace, mPendingAsyncPlace; private Activity mCurrentActivity = NULL_ACTIVITY; private Activity mPendingActivity; public PlaceActivityManager( AcceptsOneWidget pPanel, PlaceHistoryMapper pMapper, WindowClose pWindowClose, Historian pHistorian, CommonActivityParam pCommonActivityParam ) { mPanel = pPanel; mCommonActivityParam = (pCommonActivityParam != null) ? pCommonActivityParam : DefaultCommonActivityParam.<CommonActivityParam>createInstance(); mMapper = (pMapper != null) ? pMapper : new PlaceRegistry( mCommonActivityParam ); mWindowClose = (pWindowClose != null) ? pWindowClose : new WindowCloseImpl(); mWindowClose.addWindowClosingHandler( new Window.ClosingHandler() { @Override public void onWindowClosing( Window.ClosingEvent event ) { String zWarning = getLeavingCurrentPlaceWarningText(); if ( zWarning != null ) { event.setMessage( zWarning ); } } } ); mHistorian = (pHistorian != null) ? pHistorian : new HistorianImpl(); mHistorian.addValueChangeHandler( new ValueChangeHandler<String>() { @Override public void onValueChange( ValueChangeEvent<String> event ) { handleHistoryToken( event.getValue() ); } } ); } public PlaceActivityManager( AcceptsOneWidget pPanel, CommonActivityParam pCommonActivityParam ) { this( pPanel, null, null, null, pCommonActivityParam ); } public PlaceActivityManager( AcceptsOneWidget pPanel ) { this( pPanel, null, null, null, null ); } /** * Handle the current history token. Typically called at application start, to * ensure bookmark launches work. */ public void handleCurrentHistory() { handleHistoryToken( mHistorian.getToken() ); } /** * Returns the current place. * * @return a {@link Place} instance */ @Override public Place getWhere() { return mCurrentPlace; } /** * Request a change to a new place. It is not a given that we'll actually get * there. If there is an ActivityFactory for the Place, and the user does not * reject leaving the current Place, then the URL is adjusted to have the user * taken to the new place. * * @param pNewPlace a {@link Place} instance * * @return null if going to pNewPlace, !null means NOT going and why */ @Override public GoToPlace goTo( Place pNewPlace ) { pNewPlace = UtilsCommon.deNull( pNewPlace, mCurrentPlace ); if ( pNewPlace.equals( mCurrentPlace ) ) { return GoToPlace.AlreadyThere; } if ( pNewPlace.equals( mPendingCodePlace ) || pNewPlace.equals( mPendingAsyncPlace ) ) { return null; } if ( null == get( PlaceIdExtractor.getPlaceId( pNewPlace ) ) ) // Check for Factory { return GoToPlace.NoActivity; } if ( userRejectedLeavingCurrentPlace( pNewPlace ) ) { return GoToPlace.CurrentActivityRejectedLeaving; // From Code, maybe the code shouldn't have allowed it! } mPendingAsyncPlace = null; mPendingCodePlace = pNewPlace; mHistorian.newItem( mMapper.getToken( pNewPlace ), true ); return null; } private void handleHistoryToken( String pToken ) { mCommonActivityParam.getMessageUserSink().clearMessage(); Place zNewPlace = null; if ( null == (pToken = UtilsCommon.noEmpty( pToken )) ) { zNewPlace = PlaceRegistry.getDefaultPlace(); } else { try { if ( null == (zNewPlace = mMapper.getPlace( pToken )) ) { LOGGER.info.log( "Unrecognized history token: ", pToken ); } } catch ( RuntimeException e ) { LOGGER.warn.log( e, "Unable to parse history token: ", pToken ); } } zNewPlace = UtilsCommon.deNull( zNewPlace, mCurrentPlace ); if ( zNewPlace.equals( mCurrentPlace ) ) { mPendingCodePlace = null; mPendingAsyncPlace = null; return; } if ( (mPendingCodePlace != null) && !zNewPlace.equals( mPendingCodePlace ) && userRejectedLeavingCurrentPlace( zNewPlace ) ) { mPendingCodePlace = null; mPendingAsyncPlace = null; mHistorian.newItem( mMapper.getToken( mCurrentPlace ), false ); // reset the URL back to the Current Place return; } mPendingCodePlace = null; if ( zNewPlace.equals( mPendingAsyncPlace ) ) { return; } ActivityFactory zFactory = get( zNewPlace ); if ( zFactory == null ) { mPendingAsyncPlace = null; mCommonActivityParam.getMessageUserSink().setErrorMessage( "No Activity Factory for '" + UtilsCommon.justSimpleName( zNewPlace ) + "' from: " + pToken ); mHistorian.newItem( mMapper.getToken( mCurrentPlace ), false ); // reset the URL back to the Current Place return; } if ( zFactory instanceof ActivityFactory.Asynchronous ) // Un-Happy Case! { loadSyncFactory( (ActivityFactory.Asynchronous) zFactory, mPendingAsyncPlace = zNewPlace ); return; } mPendingAsyncPlace = null; activate( zNewPlace, (ActivityFactory.Synchronous) zFactory ); } /** * Activate the New Place and it's associated Activity (deactivating the current activity. * <p/> * Note: * The current activity's widget will be hidden immediately, which can cause * flicker if the next activity provides its widget asynchronously. That can * be minimized by decent caching. Perenially slow activities might mitigate * this by providing a widget immediately, with some kind of "loading" * treatment. */ protected void activate( Place pNewPlace, ActivityFactory.Synchronous pSyncFactory ) { Set<Throwable> zIssues = new LinkedHashSet<Throwable>(); mCurrentPlace = pNewPlace; Activity zNewActivity = UtilsCommon.deNull( createActivity( zIssues, pSyncFactory, pNewPlace ), NULL_ACTIVITY ); if ( (mPendingActivity != null) && (mPendingActivity != zNewActivity) ) // The place changed again before the new Activity showed its widget { tryCancel( zIssues, mPendingActivity ); } mPendingActivity = zNewActivity; // Wrap the actual panel with a per-call instance that protects the actual panel from canceled or stopped activities, and // which handles the Pending Activity becoming the Current Activity. AcceptsOneWidget zPanel = new ProtectedDisplay( zIssues, mPendingActivity ); if ( !canStart( zIssues, mPendingActivity, zPanel ) ) { NULL_ACTIVITY.start( new ProtectedDisplay( zIssues, mPendingActivity = NULL_ACTIVITY ) ); // Can NOT fail! } } @SuppressWarnings({"unchecked"}) protected Activity createActivity( Collection<Throwable> pIssues, ActivityFactory.Synchronous pFactory, Place pPlace ) { try { return pFactory.createActivity( mCommonActivityParam, pFactory.getView(), pPlace ); } catch ( Exception e ) { pIssues.add( e ); } return null; } private boolean canStart( Set<Throwable> pIssues, Activity pActivity, AcceptsOneWidget pPanel ) { try { pActivity.start( pPanel ); return true; } catch ( Exception e ) { pIssues.add( e ); } return false; } private void tryCancel( Collection<Throwable> pIssues, Activity pActivity ) { try { pActivity.onCancel(); } catch ( Exception e ) { pIssues.add( e ); } } private void tryStop( Collection<Throwable> pIssues, Activity pActivity ) { try { pActivity.onStop(); } catch ( Exception e ) { pIssues.add( e ); } } private void possiblyUpdateActivityAndShowWidget( Set<Throwable> pIssues, Activity pActivity, IsWidget pWidget ) { if ( mPendingActivity == null ) // Normal? Happy case when No Pending Activity and request is from Current Activity { if ( pActivity == mCurrentActivity ) { showWidget( pWidget ); } return; } if ( pActivity != mPendingActivity ) // Is a Pending Activity, but the request is NOT from it! { return; } tryStop( pIssues, mCurrentActivity ); mCurrentActivity = mPendingActivity; mPendingActivity = null; showWidget( pWidget ); if ( !pIssues.isEmpty() ) { throw new UmbrellaException( pIssues ); } } private void showWidget( IsWidget pWidget ) { if ( mPanel != null ) { mPanel.setWidget( pWidget ); } } @SuppressWarnings({"unchecked"}) private void loadSyncFactory( final ActivityFactory.Asynchronous pAsyncFactory, final Place pPlace ) { pAsyncFactory.load( new ActivityFactory.Asynchronous.Callback<CommonActivityParam, IsWidget, Place>() { @Override public void loaded( ActivityFactory.Synchronous<CommonActivityParam, IsWidget, Place> pSynchronousFactory ) { switchAsyncToSync( pPlace, pAsyncFactory, pSynchronousFactory ); } } ); } private void switchAsyncToSync( Place pPlace, ActivityFactory.Asynchronous pAsyncFactory, ActivityFactory.Synchronous pSyncFactory ) { boolean zStillGoing = pPlace.equals( mPendingAsyncPlace ); mPendingAsyncPlace = null; if ( replace( pAsyncFactory, pSyncFactory ) && zStillGoing ) { activate( pPlace, pSyncFactory ); } } protected ActivityFactory get( Place pPlace ) { return get( PlaceIdExtractor.getPlaceId( pPlace ) ); } protected boolean userRejectedLeavingCurrentPlace( Place newPlace ) { String zWarning = getLeavingCurrentPlaceWarningText(); return (zWarning != null) && !mWindowClose.confirm( zWarning ); } protected String getLeavingCurrentPlaceWarningText() { return mCurrentActivity.mayStop(); } /** * Default implementation of CommonActivityParam, based on {@link Window}. */ private static class DefaultCommonActivityParam implements MessageUserSinkAccessor { @Override public MessageUserSink getMessageUserSink() { return DialogMessageUserSink.INSTANCE; } @SuppressWarnings({"unchecked"}) public static <CommonActivityParam extends MessageUserSinkAccessor> CommonActivityParam createInstance() { return (CommonActivityParam) new DefaultCommonActivityParam(); } } /** * Wraps our real display to prevent an Activity from taking it over if it is * not the currentActivity. */ private class ProtectedDisplay implements AcceptsOneWidget { private final Set<Throwable> mIssues; private final Activity mExpectedActivity; ProtectedDisplay( Set<Throwable> pIssues, Activity pExpectedActivity ) { mIssues = pIssues; mExpectedActivity = pExpectedActivity; } @Override public void setWidget( IsWidget pWidget ) { possiblyUpdateActivityAndShowWidget( mIssues, mExpectedActivity, pWidget ); } } } |