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

import org.litesoft.logger.*;
import org.litesoft.sandbox.anywhere.util.*;
import org.litesoft.sandbox.infrastructure.client.internal.*;

import com.google.gwt.event.logical.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> implements PlaceChangerWithCurrentUrlSupport
{
    private static final Logger LOGGER = LoggerFactory.getLogger( PlaceActivityManager.class );

    private final PanelPlaceActivityHelper<CommonActivityParam> mActivityHelper;

    private final CommonActivityParam mCommonActivityParam;

    private final PlaceHistoryMapper mMapper;
    private final WindowClose mWindowClose;
    private final Historian mHistorian;
    private Place mPendingCodePlace;

    public PlaceActivityManager( AcceptsOneWidget pPanel, PlaceHistoryMapper pMapper, WindowClose pWindowClose, Historian pHistorian, CommonActivityParam pCommonActivityParam )
    {
        mCommonActivityParam = (pCommonActivityParam != null) ? pCommonActivityParam : createDefaultCommonActivityParam();
        mActivityHelper = new PanelPlaceActivityHelper<CommonActivityParam>( pPanel, mCommonActivityParam );
        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 zWarning = getLeavingCurrentPlaceWarningText();
                if ( zWarning != null )
                {
                    event.setMessage( zWarning );
                }
            }
        } );
        mHistorian = (pHistorian != null) ? pHistorian : new HistorianImpl();
        mHistorian.addValueChangeHandler( new ValueChangeHandler<String>()
        {
            @Override
            public void onValueChange( ValueChangeEvent<String> event )
            {
                handleHistoryToken( event.getValue() );
            }
        } );
    }

    public PlaceActivityManager( AcceptsOneWidget pPanel, CommonActivityParam pCommonActivityParam )
    {
        this( pPanel, null, null, null, pCommonActivityParam );
    }

    public PlaceActivityManager( AcceptsOneWidget pPanel )
    {
        this( pPanel, null, null, null, null );
    }

    /**
     * Go to the Current URL (current history token).
     * <p/>
     * Typically called at application start, to ensure bookmark launches work.
     */
    @Override
    public void goToCurrentUrl()
    {
        handleHistoryToken( mHistorian.getToken() );
    }

    /**
     * Returns the current place.
     *
     * @return a {@link Place} instance
     */
    @Override
    public Place getWhere()
    {
        return mActivityHelper.getCurrentPlace();
    }

    /**
     * Request a change to a new place. It is not a given that we'll actually get
     * there. If there is an ActivityFactory for the Place, and the user does not
     * reject leaving the current Place, then the URL is adjusted to have the user
     * taken to the new place.
     *
     * @param pNewPlace a {@link Place} instance
     *
     * @return null if going to pNewPlace, !null means NOT going and why
     */
    @Override
    public GoToPlace goTo( Place pNewPlace )
    {
        Place zCurrentPlace = mActivityHelper.getCurrentPlace();
        pNewPlace = UtilsCommon.deNull( pNewPlace, zCurrentPlace );
        if ( pNewPlace.equals( zCurrentPlace ) )
        {
            return GoToPlace.AlreadyThere;
        }
        if ( pNewPlace.equals( mPendingCodePlace ) || mActivityHelper.isPendingAsync( pNewPlace ) )
        {
            return null;
        }
        if ( null == mActivityHelper.get( pNewPlace ) ) // Check for Factory
        {
            return GoToPlace.NoActivity;
        }
        if ( userRejectedLeavingCurrentPlace( pNewPlace ) )
        {
            return GoToPlace.CurrentActivityRejectedLeaving; // From Code, maybe the code shouldn't have allowed it!
        }
        mActivityHelper.clearPendingAsync();
        mPendingCodePlace = pNewPlace;
        mHistorian.newItem( mMapper.getToken( pNewPlace ), true );
        return null;
    }

    private void handleHistoryToken( String pToken )
    {
        Place zWasGoingToCodePlace = mPendingCodePlace;
        mPendingCodePlace = null;
        mCommonActivityParam.getMessageUserSink().clearMessage();

        Place zNewPlace = null;

        if ( null == (pToken = UtilsCommon.noEmpty( pToken )) )
        {
            zNewPlace = PlaceRegistry.getDefaultPlace();
        }
        else
        {
            try
            {
                if ( null == (zNewPlace = mMapper.getPlace( pToken )) )
                {
                    LOGGER.info.log( "Unrecognized history token: ", pToken );
                }
            }
            catch ( RuntimeException e )
            {
                LOGGER.warn.log( e, "Unable to parse history token: ", pToken );
            }
        }
        Place zCurrentPlace = mActivityHelper.getCurrentPlace();
        zNewPlace = UtilsCommon.deNull( zNewPlace, zCurrentPlace );
        if ( zNewPlace.equals( zCurrentPlace ) )
        {
            mActivityHelper.clearPendingAsync();
            return;
        }
        if ( (zWasGoingToCodePlace != null) && !zNewPlace.equals( zWasGoingToCodePlace ) && userWantsToStopUrlChange( zNewPlace ) )
        {
            mActivityHelper.clearPendingAsync();
            resetURLbackTo( zCurrentPlace );
            return;
        }
        if ( !mActivityHelper.activate( zNewPlace ) )
        {
            mCommonActivityParam.getMessageUserSink().setErrorMessage( "No Activity Factory for '" + UtilsCommon.justSimpleName( zNewPlace ) + "' from: " + pToken );
            resetURLbackTo( zCurrentPlace );
        }
    }

    protected boolean userWantsToStopUrlChange( Place pNewPlace )
    {
        return userRejectedLeavingCurrentPlace( pNewPlace );
    }

    protected void resetURLbackTo( Place pCurrentPlace )
    {
        mHistorian.newItem( mMapper.getToken( pCurrentPlace ), false ); // reset the URL back to the Current Place
    }

    protected boolean userRejectedLeavingCurrentPlace( Place newPlace )
    {
        String zWarning = getLeavingCurrentPlaceWarningText();
        return (zWarning != null) && !mWindowClose.confirm( zWarning );
    }

    protected String getLeavingCurrentPlaceWarningText()
    {
        return mActivityHelper.getCurrentActivityMayStop();
    }

    @SuppressWarnings({"unchecked"})
    protected CommonActivityParam createDefaultCommonActivityParam()
    {
        return (CommonActivityParam) new DefaultCommonActivityParam();
    }

    /**
     * Default implementation of CommonActivityParameter, based on {@link Window}.
     */
    private class DefaultCommonActivityParam implements CommonActivityParameter
    {
        @Override
        public MessageUserSink getMessageUserSink()
        {
            return DialogMessageUserSink.INSTANCE;
        }

        @Override
        public Place getWhere()
        {
            return PlaceActivityManager.this.getWhere();
        }

        @Override
        public GoToPlace goTo( Place pNewPlace )
        {
            return PlaceActivityManager.this.goTo( pNewPlace );
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/MultiModule/common/src/org/litesoft/sandbox/infrastructure/client/PlaceActivityManager.java

Diff revisions: vs.
Revision Author Commited Message
561 Diff Diff GeorgeS picture GeorgeS Thu 13 Oct, 2011 22:28:36 +0000
559 Diff Diff GeorgeS picture GeorgeS Tue 11 Oct, 2011 21:32:40 +0000
557 Diff Diff GeorgeS picture GeorgeS Tue 11 Oct, 2011 21:12:30 +0000
548 Diff Diff GeorgeS picture GeorgeS Mon 10 Oct, 2011 19:08:21 +0000
547 Diff Diff GeorgeS picture GeorgeS Mon 10 Oct, 2011 02:22:01 +0000
546 Diff Diff GeorgeS picture GeorgeS Sun 09 Oct, 2011 02:25:08 +0000
544 GeorgeS picture GeorgeS Fri 07 Oct, 2011 12:28:12 +0000