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
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
package org.litesoft.sandbox.multimodule.client.foundation.internal;

import java.util.*;

import org.litesoft.sandbox.multimodule.anywhere.util.*;
import org.litesoft.sandbox.multimodule.client.foundation.pavsupport.*;

import com.google.gwt.event.shared.*;
import com.google.gwt.place.shared.*;
import com.google.gwt.user.client.ui.*;

public class PanelPlaceActivityHelper<CommonActivityParam extends CommonActivityParameter> extends ActivityFactoryRegistry
{
    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 Activity mCurrentActivity = NULL_ACTIVITY;
    private Activity mPendingActivity;
    private Place mCurrentPlace = Place.NOWHERE;
    private Place mPendingAsyncPlace;

    public PanelPlaceActivityHelper( AcceptsOneWidget pPanel, CommonActivityParam pCommonActivityParam )
    {
        mPanel = pPanel;
        mCommonActivityParam = pCommonActivityParam;
    }

    public Place getCurrentPlace()
    {
        return mCurrentPlace;
    }

    public boolean isPendingAsync( Place pPlace )
    {
        return pPlace.equals( mPendingAsyncPlace );
    }

    public void clearPendingAsync()
    {
        mPendingAsyncPlace = null;
    }

    public ActivityFactory get( Place pPlace )
    {
        return get( PlaceIdExtractor.getPlaceId( pPlace ) );
    }

    public String getCurrentActivityMayStop()
    {
        return mCurrentActivity.mayStop();
    }

    public boolean activate( Place pNewPlace )
    {
        if ( !isPendingAsync( pNewPlace ) )
        {
            mPendingAsyncPlace = null;
            ActivityFactory zFactory = get( pNewPlace );
            if ( zFactory == null )
            {
                return false;
            }
            if ( zFactory instanceof ActivityFactory.Asynchronous ) // Un-Happy Case!
            {
                loadSyncFactory( (ActivityFactory.Asynchronous) zFactory, mPendingAsyncPlace = pNewPlace );
            }
            else
            {
                activate( pNewPlace, (ActivityFactory.Synchronous) zFactory );
            }
        }
        return true;
    }

    /**
     * Activate the New Place and it's associated Activity (deactivating the current activity.
     */
    public 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( mCommonActivityParam, 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 )
    {
        System.out.println( "Switched " + UtilsCommon.justSimpleName( pPlace ) + " from Async to Sync" );
        boolean zStillGoing = pPlace.equals( mPendingAsyncPlace );
        mPendingAsyncPlace = null;
        if ( replace( pAsyncFactory, pSyncFactory ) && zStillGoing )
        {
            activate( pPlace, pSyncFactory );
        }
    }

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

Commits for litesoft/trunk/GWT_Sandbox/MultiModuleSingleSrc/main/src/org/litesoft/sandbox/multimodule/client/common/internal/PanelPlaceActivityHelper.java

Diff revisions: vs.
Revision Author Commited Message
585 Diff Diff GeorgeS picture GeorgeS Fri 18 Nov, 2011 17:55:26 +0000
582 GeorgeS picture GeorgeS Mon 14 Nov, 2011 19:55:23 +0000

Single Dir Based MultiModule