Subversion Repository Public Repository

litesoft

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
package org.litesoft.sandbox.infrastructure.client;

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.*;
import com.google.gwt.user.client.ui.*;

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 ) )
        {
            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 && !mDelegate.confirm( zWarning ) )
        {
            return GoToPlace.CurrentActivityRejectedLeaving;
        }
        if ( zFactory instanceof ActivityFactory.Asynchronous ) // Un-Happy Case!
        {
            return LoadSyncFactory( (ActivityFactory.Asynchronous) zFactory, mPendingAsyncPlace = pNewPlace );
        }
        mPendingAsyncPlace = null;
        return justGo( pNewPlace );
    }

    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 );
        }
    }

    private GoToPlace justGo( Place pPlace )
    {
        mCurrentPlace = pPlace;
        mEventBus.fireEvent( new PlaceChangeEvent( pPlace ) );
        return null;
    }

    @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 );
    }

    @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/infrastructure/client/PlaceActivityMapper.java

Diff revisions: vs.
Revision Author Commited Message
540 GeorgeS picture GeorgeS Mon 03 Oct, 2011 04:22:28 +0000