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
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.GWT.forms.client;

import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

import org.litesoft.GWT.forms.client.components.impls.input.*;
import org.litesoft.GWT.forms.client.components.nonpublic.*;
import org.litesoft.bo.*;
import org.litesoft.bo.attributes.*;
import org.litesoft.bo.change.*;
import org.litesoft.bo.views.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.core.simpletypes.temporal.*;
import org.litesoft.commonfoundation.typeutils.Objects;
import org.litesoft.core.util.*;

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

public class FormBinder<T extends IViewObject>
{
    public static final String LABEL_EXTENSION_REQUIRED = " *";
    public static final String LABEL_EXTENSION_UNIQUE = " **";

    private T mObject = null;
    private FormEngine mFE;
    private VoMetaData<T> mVoMetaData;
    private SSMap mTooltipMap = new SSMap();
    private static final String NO_CASE_SPECIFICATION = "NoCaseSpecification";

    public FormBinder( FormEngine pFE, VoMetaData<T> pVoMetaData )
    {
        Objects.assertNotNull( "FormEngine", mFE = pFE );
        Objects.assertNotNull( "VoMetaData", mVoMetaData = pVoMetaData );
    }

    public T getObject()
    {
        return mObject;
    }

    public void setExistingObject( T pObject, FormEngine.Mode pMode )
    {
        mFE.setMode( pMode );
        updateExistingObject( pObject );
    }

    public void updateExistingObject( T pObject )
    {
        Objects.assertNotNull( "Object", mObject = pObject );
        if ( FormEngine.Mode.NewFromSearch.equals( mFE.getMode() ) )
        {
            mFE.setMode( FormEngine.Mode.EditFromSearch );
        }
        populateForm();
    }

    public void setNewObject( T pObject )
    {
        Objects.assertNotNull( "Object", mObject = pObject );
        mFE.setMode( FormEngine.Mode.NewFromSearch );
    }

    private void populateForm()
    {
        List<String> zNames = getAppropriateNames( true );
        for ( String zName : zNames )
        {
            Object zValue = mObject.getAttributeValue( zName );
            mFE.setNamedComponentValue( zName, zValue );
        }
        mFE.resetForm();
    }

    public void commitForm()
    {
        if ( mObject == null )
        {
            throw new IllegalStateException( "No Object currently bound to Form" );
        }
        List<String> zNames = getAppropriateNames( false );
        for ( String zName : zNames )
        {
            Object zValue = mFE.getNamedComponentValue( zName );
            mObject.setAttributeValue( zName, zValue );
        }
    }

    private List<String> getAppropriateNames( boolean pIncludeUnSetables )
    {
        List<String> zNames = new ArrayList<String>();
        Set<String> zFormNames = mFE.getNamedComponentNames();
        for ( BoAttribute zAttribute : mObject.getBoMetaData().getBoAttributes() )
        {
            String zName = zAttribute.getName();
            if ( zFormNames.contains( zName ) )
            {
                if ( pIncludeUnSetables || isSetable( zAttribute ) )
                {
                    zNames.add( zName );
                }
            }
        }
        return zNames;
    }

    private boolean isSetable( BoAttribute pAttribute )
    {
        return (pAttribute instanceof ChangeControlledBoAttribute) && ((ChangeControlledBoAttribute) pAttribute).isSetable();
    }

    public Widget add( String pAttibuteName )
    {
        return add( pAttibuteName, null, null );
    }

    public Widget add( String pAttibuteName, boolean pViewOnly )
    {
        return add( pAttibuteName, pViewOnly, null, null );
    }

    public Widget add( String pAttibuteName, String pLabelOveride )
    {
        return add( pAttibuteName, null, pLabelOveride );
    }

    public Widget add( String pAttibuteName, boolean pViewOnly, String pLabelOveride )
    {
        return add( pAttibuteName, pViewOnly, null, pLabelOveride );
    }

    public Widget add( String pAttibuteName, Integer pDisplayWidthOveride )
    {
        return add( pAttibuteName, pDisplayWidthOveride, null );
    }

    public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride )
    {
        return add( pAttibuteName, pViewOnly, pDisplayWidthOveride, null );
    }

    public Widget add( String pAttibuteName, Integer pDisplayWidthOveride, String pLabelOveride )
    {
        return add( pAttibuteName, false, pDisplayWidthOveride, pLabelOveride );
    }

    public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabelOveride )
    {
        String zLabel = Strings.noEmpty( (pLabelOveride != null) ? pLabelOveride : DeCamelizer.resolve( pAttibuteName ) );

        BoAttribute zAttribute = mVoMetaData.getBoAttribute( pAttibuteName );

        if ( zAttribute instanceof SimpleVoAttribute )
        {
            SimpleVoAttribute zSA = (SimpleVoAttribute) zAttribute;
            AttributeMetaData zAMD = zSA.getMetaData();
            switch ( zAMD.getType() )
            {
                case Boolean:
                    return addBoolean( zSA, pViewOnly, zLabel );
                case Float:
                    break;
                case Integer:
                    return addInteger( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case Long:
                    return addLong( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case Password:
                    return addPassword( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case Primitive_boolean:
                    break;
                case Primitive_int:
                    break;
                case CalendarYMD:
                    return addSimpleDate( zSA, pViewOnly, zLabel );
                case SimpleFixedPointLong:
                    break;
                case SimpleFloatingPointLong:
                    break;
                case SimpleMoney:
                    return addSimpleMoney( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case SimpleTime:
                    break;
                case SimpleTimestamp:
                    break;
                case String:
                    return addString( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case Text:
                    return addText( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case TextLines:
                    break;
                case ValidOptions:
                    return addValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                case OpenValidOptions:
                    return addOpenValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel );
                default:
                    break;
            }
            throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAMD.getType() );
        }

        throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAttribute );
    }

    private Widget addBoolean( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel )
    {
        return add( false, pAttribute, pViewOnly, //
                    new FormCheckBox( adjustLabelUnique( pAttribute, pLabel, pViewOnly ), null, //
                                      mTooltipMap.get( pAttribute.getName() ), false ) );
    }

    private Widget addInteger( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        return add( pAttribute, pViewOnly, //
                    new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), //
                                                 positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                                 positiveOnly( MaxLength.get( zAMD ) ) ) );
    }

    private Widget addLong( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        return add( pAttribute, pViewOnly, //
                    new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), //
                                                 positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                                 positiveOnly( MaxLength.get( zAMD ) ) ) );
    }

    protected Widget addPassword( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        return add( pAttribute, pViewOnly, //
                    new FormPasswordField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), //
                                           positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                           positiveOnly( MaxLength.get( zAMD ) ) ) );
    }

    private static DateFormat sDF = null;

    private Widget addSimpleDate( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel )
    {
        String zFieldLabel = adjustLabel( pAttribute, pLabel, pViewOnly );
        String zTooltip = mTooltipMap.get( pAttribute.getName() );

        if ( pViewOnly )
        {
            return add( pAttribute, pViewOnly, new FormTextField( zFieldLabel, null, zTooltip, 12, 12 ) );
        }
        if ( sDF == null )
        {
            sDF = DateFormat.DEFAULT_YMD;
        }
        return add( pAttribute, pViewOnly, new FormDatePicker( zFieldLabel, null, zTooltip, "Cancel", "Today", sDF ) );
    }

    private Widget addSimpleMoney( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        return add( pAttribute, pViewOnly, //
                    new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), //
                                                 positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                                 positiveOnly( MaxLength.get( zAMD ) ) ) );
    }

    private Widget addText( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        return add( pAttribute, pViewOnly, //
                    new FormTextarea( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), true, // todo: switch the true to use a SSMap of Name Values...
                                      positiveOverride( pDisplayWidthOveride, DisplayWidthInitial.get( zAMD ) ), false, //
                                      positiveOnly( DisplayHeightInitial.get( zAMD ) ), false ) );
    }

    private Widget addString( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        String zName = pAttribute.getName();

        FormTextField zTextField = new FormTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( zName ), //
                                                      positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                                      positiveOnly( MaxLength.get( zAMD ) ) );

        if ( !pViewOnly && Case.Form.AsEntered.equals( Case.get( zAMD ) ) && mFE.isSearchFilter( zName ) )
        {
            zTextField.addStyleName( NO_CASE_SPECIFICATION );
        }
        return add( pAttribute, pViewOnly, zTextField );
    }

    private Widget addOpenValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        String[] zOptions = Options.get( zAMD );
        FormSuggestBox zSuggestBox = new FormSuggestBox( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), //
                                                         positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), //
                                                         positiveOnly( MaxLength.get( zAMD ) ), zOptions );
        // todo ? Case Management?
        return add( pAttribute, pViewOnly, zSuggestBox );
    }

    private Widget addValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        AttributeMetaData zAMD = pAttribute.getMetaData();
        String zName = pAttribute.getName();
        if ( pViewOnly )
        {
            return mFE.add( zName, disable( new FormTextField( pLabel, null, mTooltipMap.get( zName ), //
                                                               positiveOverride( pDisplayWidthOveride, Options.getLength( zAMD ) ), //
                                                               Options.getLength( zAMD ) ) ) );
        }
        String[] zOptions = Options.get( zAMD );
        pLabel = adjustLabelUnique( pAttribute, pLabel, false );
        if ( !pAttribute.isRequired() )
        {
            return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ), "" ).addItems( zOptions ) );
        }
        if ( zOptions.length <= 3 )
        {
            return mFE.add( zName, new FormRadioGroup( pLabel, null, mTooltipMap.get( zName ), zOptions ) );
        }
        return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ) ).addItems( zOptions ) );
    }

    private Widget add( SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement )
    {
        return add( pAttribute.isRequired(), pAttribute, pViewOnly, pFormElement );
    }

    private Widget add( boolean pRequired, SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement )
    {
        if ( pViewOnly )
        {
            pRequired = false;
            pFormElement = disable( pFormElement );
        }
        return mFE.add( pAttribute.getName(), pRequired, pFormElement );
    }

    private AbstractFormElement disable( AbstractFormElement pField )
    {
        pField.setEnabled( false );
        return pField;
    }

    public FormBinder addTooltip( String pAttributeName, String pTooltip )
    {
        return Strings.isNotNullOrEmpty( pTooltip ) ? LLaddTooltip( pAttributeName, pTooltip ) : this;
    }

    private FormBinder LLaddTooltip( String pAttributeName, String pTooltip )
    {
        mTooltipMap.put( pAttributeName, pTooltip );
        return this;
    }

    private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly )
    {
        return adjustLabel( pAttribute, pLabel, pViewOnly, true );
    }

    private String adjustLabelUnique( VoAttribute pAttribute, String pLabel, boolean pViewOnly )
    {
        return adjustLabel( pAttribute, pLabel, pViewOnly, false );
    }

    private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly, boolean pAllowRequired )
    {
        if ( !pViewOnly && (pLabel != null) )
        {
            if ( mFE.isUnique( pAttribute.getName() ) )
            {
                return pLabel + LABEL_EXTENSION_UNIQUE;
            }
            if ( pAllowRequired && (pAttribute.isRequired() || mFE.isEditRequired( pAttribute.getName() )) )
            {
                return pLabel + LABEL_EXTENSION_REQUIRED;
            }
        }
        return pLabel;
    }

    private Integer positiveOverride( Integer pOveride, Integer pAttribute )
    {
        return positiveOnly( (pOveride != null) ? pOveride : pAttribute );
    }

    private Integer positiveOnly( Integer pValue )
    {
        return ((pValue != null) && (pValue > 0)) ? pValue : null;
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/FormBinder.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!

863 Diff Diff GeorgeS picture GeorgeS Fri 16 Nov, 2012 22:14:29 +0000
851 Diff Diff GeorgeS picture GeorgeS Mon 08 Oct, 2012 00:05:32 +0000

Breaking the code as Temporal changes are implemented...

804 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:48:51 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
228 Diff Diff GeorgeS picture GeorgeS Sun 22 May, 2011 22:40:21 +0000
225 Diff Diff GeorgeS picture GeorgeS Sun 22 May, 2011 20:18:55 +0000
221 GeorgeS picture GeorgeS Fri 20 May, 2011 16:39:20 +0000