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

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.views.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.core.simpletypes.temporal.*;
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 )
    {
        UtilsCommon.assertNotNull( "FormEngine", mFE = pFE );
        UtilsCommon.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 )
    {
        UtilsCommon.assertNotNull( "Object", mObject = pObject );
        if ( FormEngine.Mode.NewFromSearch.equals( mFE.getMode() ) )
        {
            mFE.setMode( FormEngine.Mode.EditFromSearch );
        }
        populateForm();
    }

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

    private void populateForm()
    {
        List<String> zNames = getAppropriateNames();
        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();
        for ( String zName : zNames )
        {
            Object zValue = mFE.getNamedComponentValue( zName );
            mObject.setAttributeValue( zName, zValue );
        }
    }

    private List<String> getAppropriateNames()
    {
        List<String> zNames = new ArrayList<String>();
        Set<String> zFormNames = mFE.getNamedComponentNames();
        for ( String zName : mObject.getBoMetaData().getAttributeNames() )
        {
            if ( zFormNames.contains( zName ) )
            {
                zNames.add( zName );
            }
        }
        return zNames;
    }

    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 = UtilsCommon.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 SimpleDate:
                    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 );
                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 )
    {
        String zName = pAttribute.getName();
        AbstractFormElement zFormElement = new FormCheckBox( adjustLabelUnique( pAttribute, pLabel, pViewOnly ), null, //
                                                             mTooltipMap.get( zName ), false );
        if ( pViewOnly )
        {
            zFormElement = disable( zFormElement );
        }
        return mFE.add( zName, zFormElement );
    }

    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 DateFormatControl sDFC = 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 ( sDFC == null )
        {
            sDFC = new DateFormatControl( DateFormatControl.DEFAULT_DATE_FORMAT );
        }
        return add( pAttribute, pViewOnly, new FormDatePicker( zFieldLabel, null, zTooltip, "Cancel", "Today", sDFC ) );
    }

    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 addString( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel )
    {
        String zName = pAttribute.getName();

        AttributeMetaData zAMD = pAttribute.getMetaData();

        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 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() ), false, //
                                      positiveOverride( pDisplayWidthOveride, DisplayWidthInitial.get( zAMD ) ), false, //
                                      positiveOnly( DisplayHeightInitial.get( zAMD ) ), false ) );
    }

    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 )
    {
        boolean zRequired = pAttribute.isRequired();
        if ( pViewOnly )
        {
            zRequired = false;
            pFormElement = disable( pFormElement );
        }
        return mFE.add( pAttribute.getName(), zRequired, pFormElement );
    }

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

    public FormBinder addTooltip( String pAttributeName, String pTooltip )
    {
        return UtilsCommon.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 )
    {
        if ( !pViewOnly && (pLabel != null) )
        {
            if ( isUnique( pAttribute ) )
            {
                return pLabel + LABEL_EXTENSION_UNIQUE;
            }
            if ( pAttribute.isRequired() || mFE.isEditRequired( pAttribute.getName() ) )
            {
                return pLabel + LABEL_EXTENSION_REQUIRED;
            }
        }
        return pLabel;
    }

    private String adjustLabelUnique( BoAttribute pAttribute, String pLabel, boolean pViewOnly )
    {
        return (!pViewOnly && (pLabel != null) && isUnique( pAttribute )) ? pLabel + LABEL_EXTENSION_UNIQUE : pLabel;
    }

    private boolean isUnique( BoAttribute pAttribute )
    {
        return mFE.isUnique( pAttribute.getName() );
    }

    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
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000