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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
package org.litesoft.GWT.forms.client.components.nonpublic;

import java.util.*;

import org.litesoft.GWT.client.*;
import org.litesoft.GWT.forms.client.components.*;
import org.litesoft.core.delayed.*;

import com.google.gwt.user.client.*;
import com.google.gwt.user.client.ui.*;

public abstract class AbstractFormElement extends Composite implements IFormComponent
{
    public static final String BASE_STYLE_NAME = "litesoft-FormComponent";
    public static final String FORM_COMPONENT_LABEL_STYLE = "litesoft-FormComponentLabel";
    public static final String FORM_COMPONENT_LABEL_LEFT_STYLE = "litesoft-FormComponentLabelLeft";

    protected String mCurStyleState = "";
    protected boolean mChanged = false;
    protected boolean mFocused = false;
    protected boolean mEnabled = true;
    private Set<Listener> mFormComponentListeners = new HashSet<Listener>();
    protected FormHTMLpanel mInnerHTMLpanel; // Element is either a DIV / FieldSet
    protected String mFieldLabel, mTooltip;

    protected AbstractFormElement( Element pElement )
    {
        VerticalPanel zConstrainingTable = new VerticalPanel();
        initWidget( zConstrainingTable );
        zConstrainingTable.add( mInnerHTMLpanel = new FormHTMLpanel( pElement ) );
        super.setStyleName( BASE_STYLE_NAME );
    }

    protected void initializeHTML( String pHTML )
    {
        mInnerHTMLpanel.setHTML( pHTML );
    }

    // UIObject

    public void setStyleName( String style )
    {
        throw new IllegalStateException( "Can not use setStyleName, use addStyleName & removeStyleName" );
    }

    protected Element getStyleElement()
    {
        return mInnerHTMLpanel.getElement();
    }

    abstract public void simulateUserTextEntry( String pText );

    public String getFieldLabel()
    {
        return mFieldLabel;
    }

    public String getTooltip()
    {
        return mTooltip;
    }

    /**
     * Not supported
     *
     * @throws UnsupportedOperationException
     */
    public void setTitle( String title )
            throws UnsupportedOperationException
    {
        throw new UnsupportedOperationException();
    }

    // IFormComponent

    public boolean hasError()
    {
        return false;
    }

    public void setError( String pError )
            throws UnsupportedOperationException
    {
        if ( UtilsGwt.isNotNullOrEmpty( pError ) )
        {
            throw new UnsupportedOperationException();
        }
    }

    public void setFocused( boolean pFocused )
    {
        mFocused = pFocused;
        stateChanged();
    }

    public void setValueChanged( boolean pChanged )
    {
        mChanged = pChanged;
        stateChanged();
    }

    public boolean isEnabled()
    {
        return mEnabled;
    }

    public void publishAnyPendingChanges()
    {
        // Do nothing as most FormElements do not do defferred processing
        if ( mFocused )
        {
            LOGGER.error.log( "AbstractFormElement.publishAnyPendingChanges: Focused - ", simpleClassname( this ) );
        }
    }

    public IFormComponent addFormComponentListener( Listener pFormComponentListener )
    {
        if ( pFormComponentListener != null )
        {
            pFormComponentListener.add( this );
            mFormComponentListeners.add( pFormComponentListener );
        }
        return this;
    }

    public IFormComponent removeFormComponentListener( Listener pFormComponentListener )
    {
        if ( pFormComponentListener != null )
        {
            pFormComponentListener.remove( this );
            mFormComponentListeners.remove( pFormComponentListener );
        }
        return this;
    }

    // Support...

    protected static String simpleClassname( Object pObject )
    {
        return UtilsGwt.justClassNameOf( pObject );
    }

    protected void fireBlurListeners()
    {
        trace( "fireBlurListeners" );
        for ( Listener zListener : mFormComponentListeners )
        {
            zListener.blurOccurred();
        }
    }

    protected void fireChangeListeners()
    {
        trace( "fireChangeListeners" );
        for ( Listener zListener : mFormComponentListeners )
        {
            zListener.changeOccurred();
        }
    }

    protected void fireFocusListeners()
    {
        trace( "fireFocusListeners" );
        for ( Listener zListener : mFormComponentListeners )
        {
            zListener.focusOccured();
        }
    }

    protected void fireEnterPressedListeners( KeyboardKeyModifier pModifiers )
    {
        trace( "fireenterPressedListeners" );
        for ( Listener zListener : mFormComponentListeners )
        {
            zListener.enterPressed( pModifiers );
        }
    }

    protected boolean hasFocus()
    {
        return mFocused;
    }

    protected boolean hasChanged()
    {
        return mChanged;
    }

    protected void stateChanged()
    {
        if ( isEnabled() )
        {
            setEnabledStyles();
        }
        else
        {
            setDisabledStyles();
        }
    }

    protected void setDisabledStyles()
    {
        StringBuilder style = new StringBuilder( "-Disabled" );
        if ( hasError() )
        {
            style.append( "-Error" );
        }
        updateStatefulStyleName( style.toString() );
    }

    protected void setEnabledStyles()
    {
        StringBuilder style = new StringBuilder();
        if ( hasFocus() )
        {
            style.append( "-Focused" );
        }
        if ( hasError() )
        {
            style.append( "-Error" );
        }
        if ( hasChanged() )
        {
            style.append( "-Changed" );
        }
        updateStatefulStyleName( style.toString() );
    }

    private void updateStatefulStyleName( String pNewStyleState )
    {
        if ( !pNewStyleState.equals( mCurStyleState ) )
        {
            if ( !mCurStyleState.equals( "" ) )
            {
                removeStyleName( BASE_STYLE_NAME + mCurStyleState );
            }
            if ( !(mCurStyleState = pNewStyleState).equals( "" ) )
            {
                addStyleName( BASE_STYLE_NAME + mCurStyleState );
            }
        }
    }

    protected static class FormHTMLpanel extends ComplexPanel
    {
        /**
         * Creates an HTML panel with NO specified HTML contents. The element is the wrapping element.
         *
         * @param pElement either the Div or the FieldSet
         */
        public FormHTMLpanel( Element pElement )
        {
            setElement( pElement );
        }

        /**
         * Injects the specified HTML contents into the HTML panel. Any element within
         * this HTML that has a specified id can contain a child widget.
         *
         * @param pHtml the panel's HTML
         */
        public void setHTML( String pHtml )
        {
            DOM.setInnerHTML( getElement(), pHtml );
        }

        /**
         * Adds a child widget to the panel, contained within the HTML element
         * specified by a given id.
         *
         * @param pWidget the widget to be added
         * @param pId     the id of the element within which it will be contained
         */
        public void add( Widget pWidget, String pId )
        {
            Element elem = getElementById( getElement(), pId );
            if ( elem == null )
            {
                throw new NoSuchElementException( pId );
            }

            super.add( pWidget, elem );
        }

        /*
         * Implements getElementById() downward from the given element. We need to do
         * this because {@link #add(Widget, String)} must often be called before the
         * panel is attached to the DOM, so {@link Dom#getElementById} won't yet work.
         */
        private Element getElementById( Element pElement, String pId )
        {
            String elemId = DOM.getElementProperty( pElement, "id" );
            if ( (elemId != null) && elemId.equals( pId ) )
            {
                return pElement;
            }

            Element child = DOM.getFirstChild( pElement );
            while ( child != null )
            {
                Element ret = getElementById( child, pId );
                if ( ret != null )
                {
                    return ret;
                }
                child = DOM.getNextSibling( child );
            }

            return null;
        }
    }

    protected void trace( String pMethod )
    {
        if ( LOGGER.trace.isEnabled() )
        {
            LOGGER.trace.log( pMethod + " - " + simpleClassname( this ) );
        }
    }

    protected void pullFocusTo( final Focusable pFocusable )
    {
        TimedRunnableManager.INSTANCE.runIn( new TimedRunnable()
        {
            public Again runOnce()
            {
                if ( !UtilsGwt.isRecursiveVisible( (Widget) pFocusable ) )
                {
                    return new RunAgainIn( 50 );
                }
                pFocusable.setFocus( true );
                return null;
            }
        }, 10 );
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/AbstractFormElement.java

Diff revisions: vs.
Revision Author Commited Message
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000