litesoft
@ 546
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 390 391 392 393 394 395 |
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 ) { } }; private final AcceptsOneWidget display; private final CommonActivityParam mCommonActivityParam; private final PlaceHistoryMapper mMapper; private final WindowClose mWindowClose; private final Historian mHistorian; private Place mCurrentPlace = Place.NOWHERE; private Place mPendingAsyncPlace; private Activity currentActivity = NULL_ACTIVITY; private boolean startingNext = false; public PlaceActivityManager( AcceptsOneWidget pPanel, PlaceHistoryMapper pMapper, WindowClose pWindowClose, Historian pHistorian, CommonActivityParam pCommonActivityParam ) { display = 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 warning = maybeGoTo( Place.NOWHERE ); if ( warning != null ) { event.setMessage( warning ); } } } ); mHistorian = (pHistorian != null) ? pHistorian : new HistorianImpl(); mHistorian.addValueChangeHandler( new ValueChangeHandler<String>() { @Override public void onValueChange( ValueChangeEvent<String> event ) { System.out.println( "PlaceActivityManager.onValueChange: " + event.getValue() ); // todo... } } ); } 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() ); } @Override public Place getWhere() { return mCurrentPlace; } @Override public GoToPlace goTo( Place pNewPlace ) { return goTo( pNewPlace, false ); } private GoToPlace goTo( Place pNewPlace, boolean pFromHistory ) { if ( (pNewPlace == null) || pNewPlace.equals( mCurrentPlace ) ) { return GoToPlace.AlreadyThere; } if ( pNewPlace.equals( mPendingAsyncPlace ) ) { return null; } ActivityFactory zFactory = get( PlaceIdExtractor.getPlaceId( pNewPlace ) ); if ( zFactory == null ) { return GoToPlace.NoActivity; } String zWarning = maybeGoTo( pNewPlace ); if ( zWarning != null && !mWindowClose.confirm( zWarning ) ) { return GoToPlace.CurrentActivityRejectedLeaving; // todo: if from Code, then why didn't the code not allow it, if from History, then the URL no longer matches the View!!! } if ( zFactory instanceof ActivityFactory.Asynchronous ) // Un-Happy Case! { return LoadSyncFactory( (ActivityFactory.Asynchronous) zFactory, mPendingAsyncPlace = pNewPlace ); } mPendingAsyncPlace = null; // todo: Historian? return justGo( pNewPlace, (ActivityFactory.Synchronous) zFactory ); } private String maybeGoTo( Place newPlace ) { return currentActivity.mayStop(); } private void switchAsyncToSync( Place pPlace, ActivityFactory.Asynchronous pAsyncFactory, ActivityFactory.Synchronous pSyncFactory ) { boolean zStillGoing = pPlace.equals( mPendingAsyncPlace ); mPendingAsyncPlace = null; if ( replace( pAsyncFactory, pSyncFactory ) && zStillGoing ) { justGo( pPlace, pSyncFactory ); } } private GoToPlace justGo( Place pPlace, ActivityFactory.Synchronous pSyncFactory ) { mCurrentPlace = pPlace; Activity zActivity = createActivity( pSyncFactory, pPlace ); onPlaceChange( pPlace, zActivity ); return null; } /** * Deactivate the current activity, find the next one from our ActivityMapper, * and start it. * <p/> * 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. */ private void onPlaceChange( Place pPlace, Activity nextActivity ) { Throwable caughtOnStop = null; Throwable caughtOnCancel = null; Throwable caughtOnStart = null; if ( nextActivity == null ) { nextActivity = NULL_ACTIVITY; } if ( currentActivity.equals( nextActivity ) ) { return; } if ( startingNext ) { // The place changed again before the new current activity showed its // widget caughtOnCancel = tryStopOrCancel( false ); currentActivity = NULL_ACTIVITY; startingNext = false; } else if ( !currentActivity.equals( NULL_ACTIVITY ) ) { showWidget( null ); caughtOnStop = tryStopOrCancel( true ); } currentActivity = nextActivity; if ( currentActivity.equals( NULL_ACTIVITY ) ) { showWidget( null ); } else { startingNext = true; caughtOnStart = tryStart(); } if ( caughtOnStart != null || caughtOnCancel != null || caughtOnStop != null ) { Set<Throwable> causes = new LinkedHashSet<Throwable>(); if ( caughtOnStop != null ) { causes.add( caughtOnStop ); } if ( caughtOnCancel != null ) { causes.add( caughtOnCancel ); } if ( caughtOnStart != null ) { causes.add( caughtOnStart ); } throw new UmbrellaException( causes ); } } private Throwable tryStopOrCancel( boolean stop ) { Throwable caughtOnStop = null; try { if ( stop ) { currentActivity.onStop(); } else { currentActivity.onCancel(); } } catch ( Throwable t ) { caughtOnStop = t; } return caughtOnStop; } private Throwable tryStart() { Throwable caughtOnStart = null; try { /* * Wrap the actual display with a per-call instance that protects the * display from canceled or stopped activities, and which maintains our * startingNext state. */ currentActivity.start( new ProtectedDisplay( currentActivity ) ); } catch ( Throwable t ) { caughtOnStart = t; } return caughtOnStart; } private void showWidget( IsWidget view ) { if ( display != null ) { display.setWidget( view ); } } @SuppressWarnings({"unchecked"}) private GoToPlace 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 ); } } ); return null; } protected String createMessage( String pPlaceId ) { return "Unable to locate 'View' for '" + pPlaceId + "'"; } @SuppressWarnings({"unchecked"}) protected Activity createActivity( ActivityFactory.Synchronous pFactory, Place pPlace ) { return pFactory.createActivity( mCommonActivityParam, pFactory.getView(), pPlace ); } public Activity getActivity( Place pPlace ) { String zPlaceId = PlaceIdExtractor.getPlaceId( pPlace ); if ( zPlaceId != null ) { ActivityFactory zFactory = get( zPlaceId ); if ( zFactory instanceof ActivityFactory.Synchronous ) { try { return createActivity( (ActivityFactory.Synchronous) zFactory, pPlace ); } catch ( RuntimeException e ) { String zMessage = createMessage( zPlaceId ); LOGGER.error.log( zMessage, ": ", pPlace.getClass().getName() ); mCommonActivityParam.getMessageUserSink().setErrorMessage( zMessage + "!" ); return null; } } if ( zFactory instanceof ActivityFactory.Asynchronous ) { String zMessage = createMessage( zPlaceId ); LOGGER.error.log( zMessage, " - ", zFactory.getClass().getName() ); mCommonActivityParam.getMessageUserSink().setErrorMessage( zMessage + "?" ); return null; } mCommonActivityParam.getMessageUserSink().setErrorMessage( createMessage( zPlaceId ) ); } return null; } private void handleHistoryToken( String token ) { Place newPlace = null; if ( null != (token = UtilsCommon.noEmpty( token )) ) { try { if ( null == (newPlace = mMapper.getPlace( token )) ) { LOGGER.info.log( "Unrecognized history token: ", token ); } } catch ( RuntimeException e ) { LOGGER.warn.log( e, "Unable to parse history token: ", token ); } } if ( newPlace == null ) { newPlace = PlaceRegistry.getDefaultPlace(); } goTo( newPlace, true ); } /** * 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 Activity activity; ProtectedDisplay( Activity activity ) { this.activity = activity; } @Override public void setWidget( IsWidget view ) { if ( this.activity == currentActivity ) { startingNext = false; showWidget( view ); } } } } |