litesoft
@ 513
litesoft / trunk / GWT_Sandbox / MultiModule / common / src / org / litesoft / sandbox / multimodule / common / client / infrastructure / PlaceActivityMapper.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 |
package org.litesoft.sandbox.multimodule.common.client.infrastructure; import org.litesoft.logger.*; import com.google.gwt.activity.shared.*; import com.google.gwt.event.shared.*; import com.google.gwt.place.shared.*; import com.google.gwt.user.client.*; public class PlaceActivityMapper<CommonActivityParam extends MessageUserSinkAccessor> extends ActivityFactoryRegistry implements PlaceChanger, ActivityMapper { private static Logger LOGGER = LoggerFactory.getLogger( PlaceActivityMapper.class ); private final CommonActivityParam mCommonActivityParam; private final EventBus mEventBus; private final Delegate mDelegate; private Place mCurrentPlace = Place.NOWHERE; private Place mPendingAsyncPlace; public PlaceActivityMapper( EventBus pEventBus, Delegate pDelegate, CommonActivityParam pCommonActivityParam ) { mCommonActivityParam = (pCommonActivityParam != null) ? pCommonActivityParam : DefaultCommonActivityParam.<CommonActivityParam>createInstance(); mEventBus = pEventBus; mDelegate = (pDelegate != null) ? pDelegate : new DefaultDelegate(); mDelegate.addWindowClosingHandler( new Window.ClosingHandler() { @Override public void onWindowClosing( Window.ClosingEvent event ) { String warning = maybeGoTo( Place.NOWHERE ); if ( warning != null ) { event.setMessage( warning ); } } } ); } public PlaceActivityMapper( EventBus pEventBus, CommonActivityParam pCommonActivityParam ) { this( pEventBus, null, pCommonActivityParam ); } public PlaceActivityMapper( EventBus pEventBus ) { this( pEventBus, null, null ); } @Override public Place getWhere() { return mCurrentPlace; } @Override public GoToPlace goTo( Place pNewPlace ) { if ( (pNewPlace == null) || pNewPlace.equals( mCurrentPlace ) || pNewPlace.equals( mPendingAsyncPlace ) ) { return GoToPlace.AlreadyThere; } ActivityFactory zFactory = get( PlaceIdExtractor.getPlaceId( pNewPlace ) ); if ( zFactory == null ) { return GoToPlace.NoActivity; } String zWarning = maybeGoTo( pNewPlace ); if ( zWarning != null && !mDelegate.confirm( zWarning ) ) { return GoToPlace.CurrentActivityRejectedLeaving; } if ( zFactory instanceof ActivityFactory.Synchronous ) // Happy Case! { mCurrentPlace = pNewPlace; mEventBus.fireEvent( new PlaceChangeEvent( pNewPlace ) ); 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; return null; //To change body of implemented methods use File | Settings | File Templates. } 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 ); } @Override 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 String maybeGoTo( Place newPlace ) { PlaceChangeRequestEvent willChange = new PlaceChangeRequestEvent( newPlace ); mEventBus.fireEvent( willChange ); return willChange.getWarning(); } /** * 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(); } } /** * Default implementation of {@link Delegate}, based on {@link Window}. */ private static class DefaultDelegate implements Delegate { @Override public HandlerRegistration addWindowClosingHandler( Window.ClosingHandler handler ) { return Window.addWindowClosingHandler( handler ); } @Override public boolean confirm( String message ) { return Window.confirm( message ); } } } |
Commits for litesoft/trunk/GWT_Sandbox/MultiModule/common/src/org/litesoft/sandbox/multimodule/common/client/infrastructure/PlaceActivityMapper.java
Revision | Author | Commited | Message |
---|---|---|---|
513 | GeorgeS | Thu 22 Sep, 2011 04:04:07 +0000 | Multi |