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
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
package org.litesoft.GWT.client.pavsupport.internal;

import com.google.gwt.event.shared.*;
import com.google.gwt.place.shared.*;
import com.google.gwt.user.client.ui.*;
import org.litesoft.GWT.client.pavsupport.*;
import org.litesoft.commonfoundation.typeutils.Objects;

import java.util.*;

public class PanelPlaceActivityHelper<CommonActivityParam extends CommonActivityParameter> extends ActivityFactoryRegistry
{
    private static final Activity NULL_ACTIVITY = new Activity()
    {
        @Override
        public String mayStop()
        {
            return null;
        }

        @Override
        public void onCancel()
        {
        }

        @Override
        public void onStop()
        {
        }

        @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;
    private PlaceChangeListener.Source mPlaceChangerRequestedPlaceSource;
    private Place mPlaceChangerRequestedPlace;
    private PlaceChangeListener mPlaceChangeListener;

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

    public void add( PlaceChangeListener pPlaceChangeListener )
    {
        mPlaceChangeListener = pPlaceChangeListener;
    }

    public void placeChangeRequested( Place pPlace, PlaceChangeListener.Source pSource )
    {
        mPlaceChangerRequestedPlace = pPlace;
        mPlaceChangerRequestedPlaceSource = pSource;
    }

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

        notifyPlaceChangeListener( pNewPlace );
        mCurrentPlace = pNewPlace;

        Activity zNewActivity = Objects.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!
        }
    }

    private void notifyPlaceChangeListener( Place pNewPlace )
    {
        if ( mPlaceChangeListener != null )
        {
            if ( (mPlaceChangerRequestedPlaceSource == null) || //
                 ((mPlaceChangerRequestedPlaceSource == PlaceChangeListener.Source.Forward) && //
                  !Objects.areNonArraysEqual( mPlaceChangerRequestedPlace, pNewPlace )) )
            {
                mPlaceChangerRequestedPlaceSource = PlaceChangeListener.Source.Unexpected;
            }
            mPlaceChangeListener.placeChange( mPlaceChangerRequestedPlaceSource, mCurrentPlace, pNewPlace );
        }
        mPlaceChangerRequestedPlaceSource = null;
        mPlaceChangerRequestedPlace = null;
    }

    @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 " + Objects.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/Java/GWT/Client/src/org/litesoft/GWT/client/pavsupport/internal/PanelPlaceActivityHelper.java

Diff revisions: vs.
Revision Author Commited Message
939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

917 Diff Diff GeorgeS picture GeorgeS Sun 08 Dec, 2013 20:49:56 +0000

1.7 prep & VersionedStaticContentFilter upgrade to new “/ver” model!

811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
737 Diff Diff GeorgeS picture GeorgeS Sun 24 Jun, 2012 16:36:32 +0000

!

735 Diff Diff GeorgeS picture GeorgeS Sat 23 Jun, 2012 15:05:41 +0000

!

720 Diff Diff GeorgeS picture GeorgeS Sun 10 Jun, 2012 16:20:42 +0000
712 GeorgeS picture GeorgeS Sat 09 Jun, 2012 22:46:04 +0000

Move PAV stuff into LiteSoft