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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
package com.temp.client.foundation.widget.input;

import com.google.gwt.user.client.ui.*;
import com.temp.client.foundation.widget.*;
import com.temp.client.foundation.widget.input.fieldsupport.*;
import com.temp.client.foundation.widget.input.support.*;
import com.temp.client.foundation.widget.HelpWidget;
import com.temp.shared.externalization.*;
import com.temp.shared.utils.*;

/**
 * An Input Field that can be instantiated directly or as a widget in a UI Binding file.
 * <p/>
 * It operates in two distinct states:
 * <p/>
 * <li>Builder Mode - Where properties of the final widget are specified (and parts of the widget are NOT accessable)</li>
 * <li>Run Mode - Whare the parts of the Widget are accessable (and the specification properties are NOT changeable)</li>
 * <p/>
 * The transition between the states is one way ('Builder' to 'Run') and is a result of the call to 'init'.
 *
 * @param <T> the data type returned by this InputField
 */
public class InputField<T> extends Composite implements HasName
{
    // Required to be set
    private String name;

    // Optionally set/changed
    private InputType inputType = InputType.String;
    private boolean required = false;
    private boolean errorLabel = true;
    private boolean extendedLabel = false;
    private boolean example = false;
    private LabelPosition labelPosition = LabelPosition.Left;

    // Optionally set
    private Integer maxLength;
    private String helpLink;

    // The above must be set/changed BEFORE the widget is attached!

    private HorizontalPanel container = new HorizontalPanel();
    private CompleteInputFieldAccessor<T> completeInputFieldAccessor;

    public InputField()
    {
        container.add( new Label( "!?!" ) );
        initWidget( container );
    }

    // Configuration Methods

    @Override
    public String getName()
    {
        return name;
    }

    @Override
    public void setName( String name )
    {
        assertBuildMode();
        this.name = Validate.noEmptyIdentifier( "Name", name );
    }

    public InputField<T> name( String name )
    {
        setName( name );
        return this;
    }

    public String getType()
    {
        return (inputType != null) ? inputType.name() : null;
    }

    public void setType( String type )
    {
        assertBuildMode();
        type = StringUtils.noEmpty( type );
        InputType inputType = ObjectUtils.oneOfToStringIgnoreCase( type, InputType.values() );
        if ( inputType == null )
        {
            throw new IllegalArgumentException( "Unacceptable InputType of: " + type );
        }
        setInputType( inputType );
    }

    public InputType getInputType()
    {
        return inputType;
    }

    public void setInputType( InputType inputType )
    {
        assertBuildMode();
        Validate.notNull( "InputType", this.inputType = inputType );
    }

    public InputField<T> inputType( InputType inputType )
    {
        setInputType( inputType );
        return this;
    }

    public String getLabel()
    {
        return labelPosition.name();
    }

    public void setLabel( String labelPosition )
    {
        assertBuildMode();
        labelPosition = StringUtils.noEmpty( labelPosition );
        LabelPosition position = ObjectUtils.oneOfToStringIgnoreCase( labelPosition, LabelPosition.values() );
        if ( position == null )
        {
            throw new IllegalArgumentException( "Unacceptable LabelPosition of: " + labelPosition );
        }
        setLabelPosition( position );
    }

    public LabelPosition getLabelPosition()
    {
        return labelPosition;
    }

    public void setLabelPosition( LabelPosition labelPosition )
    {
        assertBuildMode();
        Validate.notNull( "LabelPosition", this.labelPosition = labelPosition );
    }

    public InputField<T> labelPosition( LabelPosition labelPosition )
    {
        setLabelPosition( labelPosition );
        return this;
    }

    public boolean isRequired()
    {
        return required;
    }

    public void setRequired( boolean required )
    {
        assertBuildMode();
        this.required = required;
        if ( required )
        {
            setErrorLabel( true );
        }
    }

    public InputField<T> required()
    {
        setRequired( true );
        return this;
    }

    public boolean isErrorLabel()
    {
        return errorLabel;
    }

    public void setErrorLabel( boolean pErrorLabel )
    {
        assertBuildMode();
        errorLabel = pErrorLabel;
    }

    public InputField<T> errorLabel()
    {
        setErrorLabel( true );
        return this;
    }

    public boolean isExtendedLabel()
    {
        return extendedLabel;
    }

    public void setExtendedLabel( boolean pExtendedLabel )
    {
        assertBuildMode();
        extendedLabel = pExtendedLabel;
    }

    public InputField<T> extendedLabel()
    {
        setExtendedLabel( true );
        return this;
    }

    public boolean isExample()
    {
        return example;
    }

    public void setExample( boolean pExample )
    {
        assertBuildMode();
        example = pExample;
    }

    public InputField<T> example()
    {
        setExample( true );
        return this;
    }

    public Integer getMaxLength()
    {
        return maxLength;
    }

    public void setMaxLength( Integer maxLength )
    {
        assertBuildMode();
        this.maxLength = Validate.optionalLength( "MaxLength", maxLength );
    }

    public InputField<T> maxLength( int maxLength )
    {
        setMaxLength( maxLength );
        return this;
    }

    public String getHelpLink()
    {
        return helpLink;
    }

    public void setHelpLink( String helpLink )
    {
        assertBuildMode();
        this.helpLink = StringUtils.noEmpty( helpLink );
    }

    public InputField<T> helpLink( String helpLink )
    {
        setHelpLink( Validate.noEmpty( "HelpLink", helpLink ) );
        return this;
    }

    /**
     * The 'Builder' to 'Run' state transition method fully constructs/populates the InputField's Widget / container.
     *
     * @return this for method chaining
     */
    public InputField<T> init( E13nResolver resolver )
    {
        Validate.notNull( "resolver", resolver );
        if ( name == null )
        {
            throw new IllegalStateException( "Unable to init, Name not set" );
        }
        assertBuildMode();
        completeInputFieldAccessor = buildActualWidget( resolver );
        return this;
    }

    // Anytime Methods

    public boolean isEnabled()
    {
        return true; // TODO: XXX
    }

    public void setEnabled( boolean enable )
    {
        // TODO: XXX
    }

    public InputField<T> enable()
    {
        setEnabled( true );
        return this;
    }

    public InputField<T> disable()
    {
        setEnabled( false );
        return this;
    }

    public boolean isHidden()
    {
        return true; // TODO: XXX
    }

    public void setHidden( boolean hidden )
    {
        // TODO: XXX
    }

    public InputField<T> hide()
    {
        setHidden( true );
        return this;
    }

    public InputField<T> show()
    {
        setHidden( false );
        return this;
    }

    // Widget Accessor Methods

    public T getCurrentValue()
    {
        return getInput().getValue();
    }

    public Widget getInputWidget()
    {
        return checkRunMode( "getInputWidget" ).getInputWidget();
    }

    public InputWidgetChangeFilter<T> getInput()
    {
        return checkRunMode( "getInput" ).getInput();
    }

    public InputWidgetValidator<T> getValidator()
    {
        return checkRunMode( "getValidator" ).getValidator();
    }

    public TextLabel getLabelLabel()
    {
        return checkRunMode( "getLabelLabel" ).getLabelLabel();
    }

    public TextLabel getExtendedLabel()
    {
        return checkRunMode( "getExtendedLabel" ).getExtendedLabel();
    }

    public TextLabel getExampleLabel()
    {
        return checkRunMode( "getExampleLabel" ).getExampleLabel();
    }

    public TextLabel getErrorLabel()
    {
        return checkRunMode( "getErrorLabel" ).getErrorLabel();
    }

    public HelpWidget getHelpWidget()
    {
        return checkRunMode( "getHelpWidget" ).getHelpWidget();
    }

    // Support Methods...

    protected final void assertBuildMode()
    {
        if ( completeInputFieldAccessor != null )
        {
            throw new IllegalStateException( "Unable to Mutate InputField Named '" + name + "', because no longer in Build Mode (init called)" );
        }
    }

    protected final CompleteInputFieldAccessor<T> checkRunMode( String method )
    {
        if ( completeInputFieldAccessor == null )
        {
            throw new IllegalStateException( "Unable to access widgets of InputField Named '" + name + "', because not in Run Mode (init NOT called)" );
        }
        return completeInputFieldAccessor;
    }

    protected CompleteInputFieldAccessor<T> buildActualWidget( E13nResolver pResolver )
    {
        InputFieldAccessor<T> accessor = inputType.createAccessor();
        TextLabel labelTextLabel = null;
        TextLabel extendedTextLabel = null;
        if ( !LabelPosition.None.equals( labelPosition ) )
        {
            labelTextLabel = createWrappingLabel( "labelLabel", true );
            extendedTextLabel = createWrappingLabel( "extendedLabel", extendedLabel );
        }
        TextLabel exampleTextLabel = createWrappingLabel( "exampleLabel", example );
        TextLabel errorTextLabel = createClippingLabel( "errorLabel", errorLabel && (accessor.getValidator() != null) );
        HelpWidget helpWidget = (helpLink != null) ? new HelpWidget().link( helpLink ) : null;
        CompleteInputFieldAccessor<T> completeAccessor = new CompleteInputFieldAccessor<T>( labelTextLabel, //
                                                                                            extendedTextLabel,  //
                                                                                            accessor,  //
                                                                                            helpWidget,  //
                                                                                            exampleTextLabel,  //
                                                                                            errorTextLabel );
        populate( container, completeAccessor );
        return completeAccessor;
    }

    protected final TextLabel createNonWrappingLabel( String styleName, boolean create )
    {
        return create ? new NonWrappingLabel().style( styleName ) : null;
    }

    protected final TextLabel createWrappingLabel( String styleName, boolean create )
    {
        return create ? new WrappingLabel().style( styleName ) : null;
    }

    protected final TextLabel createClippingLabel( String styleName, boolean create )
    {
        return create ? new ClippingLabel().style( styleName ) : null;
    }

    protected void populate( HorizontalPanel container, CompleteInputFieldAccessor<T> completeAccessor )
    {

//        completeAccessor.
//        boolean required = false;
//        boolean errorLabel = true;
//        boolean extendedLabel = false;
//        boolean example = false;
//        LabelPosition labelPosition = LabelPosition.Left;
//
//        // Optionally set
//        Integer maxLength;
//        String helpLink;
//
//        E13nResolverAccessor resolverAccessor;
//        CompleteInputFieldAccessor<T> completeInputFieldAccessor;
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/input/InputField.java

Diff revisions: vs.
Revision Author Commited Message
626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000