|
@@ -1,335 +1,333 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.forms.client; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.GWT.forms.client.components.impls.input.*; |
5 |
|
- |
import org.litesoft.GWT.forms.client.components.nonpublic.*; |
6 |
|
- |
import org.litesoft.bo.*; |
7 |
|
- |
import org.litesoft.bo.attributes.*; |
8 |
|
- |
import org.litesoft.bo.change.*; |
9 |
|
- |
import org.litesoft.bo.views.*; |
10 |
|
- |
import org.litesoft.commonfoundation.base.*; |
11 |
|
- |
import org.litesoft.commonfoundation.typeutils.Objects; |
12 |
|
- |
import org.litesoft.commonfoundation.typeutils.*; |
13 |
|
- |
import org.litesoft.core.simpletypes.*; |
14 |
|
- |
import org.litesoft.core.simpletypes.temporal.*; |
15 |
|
- |
|
16 |
|
- |
import com.google.gwt.user.client.ui.*; |
17 |
|
- |
|
18 |
|
- |
import java.util.*; |
19 |
|
- |
|
20 |
|
- |
public class FormBinder<T extends IViewObject> { |
21 |
|
- |
public static final String LABEL_EXTENSION_REQUIRED = " *"; |
22 |
|
- |
public static final String LABEL_EXTENSION_UNIQUE = " **"; |
23 |
|
- |
|
24 |
|
- |
private T mObject = null; |
25 |
|
- |
private FormEngine mFE; |
26 |
|
- |
private VoMetaData<T> mVoMetaData; |
27 |
|
- |
private SSMap mTooltipMap = new SSMap(); |
28 |
|
- |
private static final String NO_CASE_SPECIFICATION = "NoCaseSpecification"; |
29 |
|
- |
|
30 |
|
- |
public FormBinder( FormEngine pFE, VoMetaData<T> pVoMetaData ) { |
31 |
|
- |
Objects.assertNotNull( "FormEngine", mFE = pFE ); |
32 |
|
- |
Objects.assertNotNull( "VoMetaData", mVoMetaData = pVoMetaData ); |
33 |
|
- |
} |
34 |
|
- |
|
35 |
|
- |
public T getObject() { |
36 |
|
- |
return mObject; |
37 |
|
- |
} |
38 |
|
- |
|
39 |
|
- |
public void setExistingObject( T pObject, FormEngine.Mode pMode ) { |
40 |
|
- |
mFE.setMode( pMode ); |
41 |
|
- |
updateExistingObject( pObject ); |
42 |
|
- |
} |
43 |
|
- |
|
44 |
|
- |
public void updateExistingObject( T pObject ) { |
45 |
|
- |
Objects.assertNotNull( "Object", mObject = pObject ); |
46 |
|
- |
if ( FormEngine.Mode.NewFromSearch.equals( mFE.getMode() ) ) { |
47 |
|
- |
mFE.setMode( FormEngine.Mode.EditFromSearch ); |
48 |
|
- |
} |
49 |
|
- |
populateForm(); |
50 |
|
- |
} |
51 |
|
- |
|
52 |
|
- |
public void setNewObject( T pObject ) { |
53 |
|
- |
Objects.assertNotNull( "Object", mObject = pObject ); |
54 |
|
- |
mFE.setMode( FormEngine.Mode.NewFromSearch ); |
55 |
|
- |
} |
56 |
|
- |
|
57 |
|
- |
private void populateForm() { |
58 |
|
- |
List<String> zNames = getAppropriateNames( true ); |
59 |
|
- |
for ( String zName : zNames ) { |
60 |
|
- |
Object zValue = mObject.getAttributeValue( zName ); |
61 |
|
- |
mFE.setNamedComponentValue( zName, zValue ); |
62 |
|
- |
} |
63 |
|
- |
mFE.resetForm(); |
64 |
|
- |
} |
65 |
|
- |
|
66 |
|
- |
public void commitForm() { |
67 |
|
- |
if ( mObject == null ) { |
68 |
|
- |
throw new IllegalStateException( "No Object currently bound to Form" ); |
69 |
|
- |
} |
70 |
|
- |
List<String> zNames = getAppropriateNames( false ); |
71 |
|
- |
for ( String zName : zNames ) { |
72 |
|
- |
Object zValue = mFE.getNamedComponentValue( zName ); |
73 |
|
- |
mObject.setAttributeValue( zName, zValue ); |
74 |
|
- |
} |
75 |
|
- |
} |
76 |
|
- |
|
77 |
|
- |
private List<String> getAppropriateNames( boolean pIncludeUnSetables ) { |
78 |
|
- |
List<String> zNames = new ArrayList<String>(); |
79 |
|
- |
Set<String> zFormNames = mFE.getNamedComponentNames(); |
80 |
|
- |
for ( BoAttribute zAttribute : mObject.getBoMetaData().getBoAttributes() ) { |
81 |
|
- |
String zName = zAttribute.getName(); |
82 |
|
- |
if ( zFormNames.contains( zName ) ) { |
83 |
|
- |
if ( pIncludeUnSetables || isSetable( zAttribute ) ) { |
84 |
|
- |
zNames.add( zName ); |
85 |
|
- |
} |
86 |
|
- |
} |
87 |
|
- |
} |
88 |
|
- |
return zNames; |
89 |
|
- |
} |
90 |
|
- |
|
91 |
|
- |
private boolean isSetable( BoAttribute pAttribute ) { |
92 |
|
- |
return (pAttribute instanceof ChangeControlledBoAttribute) && ((ChangeControlledBoAttribute) pAttribute).isSetable(); |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
public Widget add( String pAttibuteName ) { |
96 |
|
- |
return add( pAttibuteName, null, null ); |
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
public Widget add( String pAttibuteName, boolean pViewOnly ) { |
100 |
|
- |
return add( pAttibuteName, pViewOnly, null, null ); |
101 |
|
- |
} |
102 |
|
- |
|
103 |
|
- |
public Widget add( String pAttibuteName, String pLabelOveride ) { |
104 |
|
- |
return add( pAttibuteName, null, pLabelOveride ); |
105 |
|
- |
} |
106 |
|
- |
|
107 |
|
- |
public Widget add( String pAttibuteName, boolean pViewOnly, String pLabelOveride ) { |
108 |
|
- |
return add( pAttibuteName, pViewOnly, null, pLabelOveride ); |
109 |
|
- |
} |
110 |
|
- |
|
111 |
|
- |
public Widget add( String pAttibuteName, Integer pDisplayWidthOveride ) { |
112 |
|
- |
return add( pAttibuteName, pDisplayWidthOveride, null ); |
113 |
|
- |
} |
114 |
|
- |
|
115 |
|
- |
public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride ) { |
116 |
|
- |
return add( pAttibuteName, pViewOnly, pDisplayWidthOveride, null ); |
117 |
|
- |
} |
118 |
|
- |
|
119 |
|
- |
public Widget add( String pAttibuteName, Integer pDisplayWidthOveride, String pLabelOveride ) { |
120 |
|
- |
return add( pAttibuteName, false, pDisplayWidthOveride, pLabelOveride ); |
121 |
|
- |
} |
122 |
|
- |
|
123 |
|
- |
public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabelOveride ) { |
124 |
|
- |
String zLabel = Strings.noEmpty( (pLabelOveride != null) ? pLabelOveride : DeCamelizer.resolve( pAttibuteName ) ); |
125 |
|
- |
|
126 |
|
- |
BoAttribute zAttribute = mVoMetaData.getBoAttribute( pAttibuteName ); |
127 |
|
- |
|
128 |
|
- |
if ( zAttribute instanceof SimpleVoAttribute ) { |
129 |
|
- |
SimpleVoAttribute zSA = (SimpleVoAttribute) zAttribute; |
130 |
|
- |
AttributeMetaData zAMD = zSA.getMetaData(); |
131 |
|
- |
switch ( zAMD.getType() ) { |
132 |
|
- |
case Boolean: |
133 |
|
- |
return addBoolean( zSA, pViewOnly, zLabel ); |
134 |
|
- |
case Float: |
135 |
|
- |
break; |
136 |
|
- |
case Integer: |
137 |
|
- |
return addInteger( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
138 |
|
- |
case Long: |
139 |
|
- |
return addLong( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
140 |
|
- |
case Password: |
141 |
|
- |
return addPassword( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
142 |
|
- |
case Primitive_boolean: |
143 |
|
- |
break; |
144 |
|
- |
case Primitive_int: |
145 |
|
- |
break; |
146 |
|
- |
case CalendarYMD: |
147 |
|
- |
return addSimpleDate( zSA, pViewOnly, zLabel ); |
148 |
|
- |
case SimpleFixedPointLong: |
149 |
|
- |
break; |
150 |
|
- |
case SimpleFloatingPointLong: |
151 |
|
- |
break; |
152 |
|
- |
case SimpleMoney: |
153 |
|
- |
return addSimpleMoney( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
154 |
|
- |
case SimpleTime: |
155 |
|
- |
break; |
156 |
|
- |
case SimpleTimestamp: |
157 |
|
- |
break; |
158 |
|
- |
case String: |
159 |
|
- |
return addString( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
160 |
|
- |
case Text: |
161 |
|
- |
return addText( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
162 |
|
- |
case TextLines: |
163 |
|
- |
break; |
164 |
|
- |
case ValidOptions: |
165 |
|
- |
return addValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
166 |
|
- |
case OpenValidOptions: |
167 |
|
- |
return addOpenValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
168 |
|
- |
default: |
169 |
|
- |
break; |
170 |
|
- |
} |
171 |
|
- |
throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAMD.getType() ); |
172 |
|
- |
} |
173 |
|
- |
|
174 |
|
- |
throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAttribute ); |
175 |
|
- |
} |
176 |
|
- |
|
177 |
|
- |
private Widget addBoolean( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel ) { |
178 |
|
- |
return add( false, pAttribute, pViewOnly, // |
179 |
|
- |
new FormCheckBox( adjustLabelUnique( pAttribute, pLabel, pViewOnly ), null, // |
180 |
|
- |
mTooltipMap.get( pAttribute.getName() ), false ) ); |
181 |
|
- |
} |
182 |
|
- |
|
183 |
|
- |
private Widget addInteger( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
184 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
185 |
|
- |
return add( pAttribute, pViewOnly, // |
186 |
|
- |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
187 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
188 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
189 |
|
- |
} |
190 |
|
- |
|
191 |
|
- |
private Widget addLong( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
192 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
193 |
|
- |
return add( pAttribute, pViewOnly, // |
194 |
|
- |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
195 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
196 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
197 |
|
- |
} |
198 |
|
- |
|
199 |
|
- |
protected Widget addPassword( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
200 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
201 |
|
- |
return add( pAttribute, pViewOnly, // |
202 |
|
- |
new FormPasswordField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
203 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
204 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
205 |
|
- |
} |
206 |
|
- |
|
207 |
|
- |
private static DateFormat sDF = null; |
208 |
|
- |
|
209 |
|
- |
private Widget addSimpleDate( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel ) { |
210 |
|
- |
String zFieldLabel = adjustLabel( pAttribute, pLabel, pViewOnly ); |
211 |
|
- |
String zTooltip = mTooltipMap.get( pAttribute.getName() ); |
212 |
|
- |
|
213 |
|
- |
if ( pViewOnly ) { |
214 |
|
- |
return add( pAttribute, pViewOnly, new FormTextField( zFieldLabel, null, zTooltip, 12, 12 ) ); |
215 |
|
- |
} |
216 |
|
- |
if ( sDF == null ) { |
217 |
|
- |
sDF = DateFormat.DEFAULT_YMD; |
218 |
|
- |
} |
219 |
|
- |
return add( pAttribute, pViewOnly, new FormDatePicker( zFieldLabel, null, zTooltip, "Cancel", "Today", sDF ) ); |
220 |
|
- |
} |
221 |
|
- |
|
222 |
|
- |
private Widget addSimpleMoney( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
223 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
224 |
|
- |
return add( pAttribute, pViewOnly, // |
225 |
|
- |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
226 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
227 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
228 |
|
- |
} |
229 |
|
- |
|
230 |
|
- |
private Widget addText( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
231 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
232 |
|
- |
return add( pAttribute, pViewOnly, // |
233 |
|
- |
new FormTextarea( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), true, |
234 |
|
- |
// todo: switch the true to use a SSMap of Name Values... |
235 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayWidthInitial.get( zAMD ) ), false, // |
236 |
|
- |
positiveOnly( DisplayHeightInitial.get( zAMD ) ), false ) ); |
237 |
|
- |
} |
238 |
|
- |
|
239 |
|
- |
private Widget addString( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
240 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
241 |
|
- |
String zName = pAttribute.getName(); |
242 |
|
- |
|
243 |
|
- |
FormTextField zTextField = new FormTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( zName ), // |
244 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
245 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ) ); |
246 |
|
- |
|
247 |
|
- |
if ( !pViewOnly && Case.Form.AsEntered.equals( Case.get( zAMD ) ) && mFE.isSearchFilter( zName ) ) { |
248 |
|
- |
zTextField.addStyleName( NO_CASE_SPECIFICATION ); |
249 |
|
- |
} |
250 |
|
- |
return add( pAttribute, pViewOnly, zTextField ); |
251 |
|
- |
} |
252 |
|
- |
|
253 |
|
- |
private Widget addOpenValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
254 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
255 |
|
- |
String[] zOptions = Options.get( zAMD ); |
256 |
|
- |
FormSuggestBox zSuggestBox = new FormSuggestBox( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
257 |
|
- |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
258 |
|
- |
positiveOnly( MaxLength.get( zAMD ) ), zOptions ); |
259 |
|
- |
// todo ? Case Management? |
260 |
|
- |
return add( pAttribute, pViewOnly, zSuggestBox ); |
261 |
|
- |
} |
262 |
|
- |
|
263 |
|
- |
private Widget addValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
264 |
|
- |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
265 |
|
- |
String zName = pAttribute.getName(); |
266 |
|
- |
if ( pViewOnly ) { |
267 |
|
- |
return mFE.add( zName, disable( new FormTextField( pLabel, null, mTooltipMap.get( zName ), // |
268 |
|
- |
positiveOverride( pDisplayWidthOveride, Options.getLength( zAMD ) ), // |
269 |
|
- |
Options.getLength( zAMD ) ) ) ); |
270 |
|
- |
} |
271 |
|
- |
String[] zOptions = Options.get( zAMD ); |
272 |
|
- |
pLabel = adjustLabelUnique( pAttribute, pLabel, false ); |
273 |
|
- |
if ( !pAttribute.isRequired() ) { |
274 |
|
- |
return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ), "" ).addItems( zOptions ) ); |
275 |
|
- |
} |
276 |
|
- |
if ( zOptions.length <= 3 ) { |
277 |
|
- |
return mFE.add( zName, new FormRadioGroup( pLabel, null, mTooltipMap.get( zName ), zOptions ) ); |
278 |
|
- |
} |
279 |
|
- |
return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ) ).addItems( zOptions ) ); |
280 |
|
- |
} |
281 |
|
- |
|
282 |
|
- |
private Widget add( SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement ) { |
283 |
|
- |
return add( pAttribute.isRequired(), pAttribute, pViewOnly, pFormElement ); |
284 |
|
- |
} |
285 |
|
- |
|
286 |
|
- |
private Widget add( boolean pRequired, SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement ) { |
287 |
|
- |
if ( pViewOnly ) { |
288 |
|
- |
pRequired = false; |
289 |
|
- |
pFormElement = disable( pFormElement ); |
290 |
|
- |
} |
291 |
|
- |
return mFE.add( pAttribute.getName(), pRequired, pFormElement ); |
292 |
|
- |
} |
293 |
|
- |
|
294 |
|
- |
private AbstractFormElement disable( AbstractFormElement pField ) { |
295 |
|
- |
pField.setEnabled( false ); |
296 |
|
- |
return pField; |
297 |
|
- |
} |
298 |
|
- |
|
299 |
|
- |
public FormBinder addTooltip( String pAttributeName, String pTooltip ) { |
300 |
|
- |
return Strings.isNotNullOrEmpty( pTooltip ) ? LLaddTooltip( pAttributeName, pTooltip ) : this; |
301 |
|
- |
} |
302 |
|
- |
|
303 |
|
- |
private FormBinder LLaddTooltip( String pAttributeName, String pTooltip ) { |
304 |
|
- |
mTooltipMap.put( pAttributeName, pTooltip ); |
305 |
|
- |
return this; |
306 |
|
- |
} |
307 |
|
- |
|
308 |
|
- |
private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly ) { |
309 |
|
- |
return adjustLabel( pAttribute, pLabel, pViewOnly, true ); |
310 |
|
- |
} |
311 |
|
- |
|
312 |
|
- |
private String adjustLabelUnique( VoAttribute pAttribute, String pLabel, boolean pViewOnly ) { |
313 |
|
- |
return adjustLabel( pAttribute, pLabel, pViewOnly, false ); |
314 |
|
- |
} |
315 |
|
- |
|
316 |
|
- |
private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly, boolean pAllowRequired ) { |
317 |
|
- |
if ( !pViewOnly && (pLabel != null) ) { |
318 |
|
- |
if ( mFE.isUnique( pAttribute.getName() ) ) { |
319 |
|
- |
return pLabel + LABEL_EXTENSION_UNIQUE; |
320 |
|
- |
} |
321 |
|
- |
if ( pAllowRequired && (pAttribute.isRequired() || mFE.isEditRequired( pAttribute.getName() )) ) { |
322 |
|
- |
return pLabel + LABEL_EXTENSION_REQUIRED; |
323 |
|
- |
} |
324 |
|
- |
} |
325 |
|
- |
return pLabel; |
326 |
|
- |
} |
327 |
|
- |
|
328 |
|
- |
private Integer positiveOverride( Integer pOveride, Integer pAttribute ) { |
329 |
|
- |
return positiveOnly( (pOveride != null) ? pOveride : pAttribute ); |
330 |
|
- |
} |
331 |
|
- |
|
332 |
|
- |
private Integer positiveOnly( Integer pValue ) { |
333 |
|
- |
return ((pValue != null) && (pValue > 0)) ? pValue : null; |
334 |
|
- |
} |
335 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.forms.client; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.GWT.forms.client.components.impls.input.*; |
|
5 |
+ |
import org.litesoft.GWT.forms.client.components.nonpublic.*; |
|
6 |
+ |
import org.litesoft.bo.*; |
|
7 |
+ |
import org.litesoft.bo.attributes.*; |
|
8 |
+ |
import org.litesoft.bo.change.*; |
|
9 |
+ |
import org.litesoft.bo.views.*; |
|
10 |
+ |
import org.litesoft.commonfoundation.base.*; |
|
11 |
+ |
import org.litesoft.core.simpletypes.*; |
|
12 |
+ |
import org.litesoft.core.simpletypes.temporal.*; |
|
13 |
+ |
|
|
14 |
+ |
import com.google.gwt.user.client.ui.*; |
|
15 |
+ |
|
|
16 |
+ |
import java.util.*; |
|
17 |
+ |
|
|
18 |
+ |
public class FormBinder<T extends IViewObject> { |
|
19 |
+ |
public static final String LABEL_EXTENSION_REQUIRED = " *"; |
|
20 |
+ |
public static final String LABEL_EXTENSION_UNIQUE = " **"; |
|
21 |
+ |
|
|
22 |
+ |
private T mObject = null; |
|
23 |
+ |
private FormEngine mFE; |
|
24 |
+ |
private VoMetaData<T> mVoMetaData; |
|
25 |
+ |
private SSMap mTooltipMap = new SSMap(); |
|
26 |
+ |
private static final String NO_CASE_SPECIFICATION = "NoCaseSpecification"; |
|
27 |
+ |
|
|
28 |
+ |
public FormBinder( FormEngine pFE, VoMetaData<T> pVoMetaData ) { |
|
29 |
+ |
Confirm.isNotNull( "FormEngine", mFE = pFE ); |
|
30 |
+ |
Confirm.isNotNull( "VoMetaData", mVoMetaData = pVoMetaData ); |
|
31 |
+ |
} |
|
32 |
+ |
|
|
33 |
+ |
public T getObject() { |
|
34 |
+ |
return mObject; |
|
35 |
+ |
} |
|
36 |
+ |
|
|
37 |
+ |
public void setExistingObject( T pObject, FormEngine.Mode pMode ) { |
|
38 |
+ |
mFE.setMode( pMode ); |
|
39 |
+ |
updateExistingObject( pObject ); |
|
40 |
+ |
} |
|
41 |
+ |
|
|
42 |
+ |
public void updateExistingObject( T pObject ) { |
|
43 |
+ |
Confirm.isNotNull( "Object", mObject = pObject ); |
|
44 |
+ |
if ( FormEngine.Mode.NewFromSearch.equals( mFE.getMode() ) ) { |
|
45 |
+ |
mFE.setMode( FormEngine.Mode.EditFromSearch ); |
|
46 |
+ |
} |
|
47 |
+ |
populateForm(); |
|
48 |
+ |
} |
|
49 |
+ |
|
|
50 |
+ |
public void setNewObject( T pObject ) { |
|
51 |
+ |
Confirm.isNotNull( "Object", mObject = pObject ); |
|
52 |
+ |
mFE.setMode( FormEngine.Mode.NewFromSearch ); |
|
53 |
+ |
} |
|
54 |
+ |
|
|
55 |
+ |
private void populateForm() { |
|
56 |
+ |
List<String> zNames = getAppropriateNames( true ); |
|
57 |
+ |
for ( String zName : zNames ) { |
|
58 |
+ |
Object zValue = mObject.getAttributeValue( zName ); |
|
59 |
+ |
mFE.setNamedComponentValue( zName, zValue ); |
|
60 |
+ |
} |
|
61 |
+ |
mFE.resetForm(); |
|
62 |
+ |
} |
|
63 |
+ |
|
|
64 |
+ |
public void commitForm() { |
|
65 |
+ |
if ( mObject == null ) { |
|
66 |
+ |
throw new IllegalStateException( "No Object currently bound to Form" ); |
|
67 |
+ |
} |
|
68 |
+ |
List<String> zNames = getAppropriateNames( false ); |
|
69 |
+ |
for ( String zName : zNames ) { |
|
70 |
+ |
Object zValue = mFE.getNamedComponentValue( zName ); |
|
71 |
+ |
mObject.setAttributeValue( zName, zValue ); |
|
72 |
+ |
} |
|
73 |
+ |
} |
|
74 |
+ |
|
|
75 |
+ |
private List<String> getAppropriateNames( boolean pIncludeUnSetables ) { |
|
76 |
+ |
List<String> zNames = new ArrayList<String>(); |
|
77 |
+ |
Set<String> zFormNames = mFE.getNamedComponentNames(); |
|
78 |
+ |
for ( BoAttribute zAttribute : mObject.getBoMetaData().getBoAttributes() ) { |
|
79 |
+ |
String zName = zAttribute.getName(); |
|
80 |
+ |
if ( zFormNames.contains( zName ) ) { |
|
81 |
+ |
if ( pIncludeUnSetables || isSetable( zAttribute ) ) { |
|
82 |
+ |
zNames.add( zName ); |
|
83 |
+ |
} |
|
84 |
+ |
} |
|
85 |
+ |
} |
|
86 |
+ |
return zNames; |
|
87 |
+ |
} |
|
88 |
+ |
|
|
89 |
+ |
private boolean isSetable( BoAttribute pAttribute ) { |
|
90 |
+ |
return (pAttribute instanceof ChangeControlledBoAttribute) && ((ChangeControlledBoAttribute) pAttribute).isSetable(); |
|
91 |
+ |
} |
|
92 |
+ |
|
|
93 |
+ |
public Widget add( String pAttibuteName ) { |
|
94 |
+ |
return add( pAttibuteName, null, null ); |
|
95 |
+ |
} |
|
96 |
+ |
|
|
97 |
+ |
public Widget add( String pAttibuteName, boolean pViewOnly ) { |
|
98 |
+ |
return add( pAttibuteName, pViewOnly, null, null ); |
|
99 |
+ |
} |
|
100 |
+ |
|
|
101 |
+ |
public Widget add( String pAttibuteName, String pLabelOveride ) { |
|
102 |
+ |
return add( pAttibuteName, null, pLabelOveride ); |
|
103 |
+ |
} |
|
104 |
+ |
|
|
105 |
+ |
public Widget add( String pAttibuteName, boolean pViewOnly, String pLabelOveride ) { |
|
106 |
+ |
return add( pAttibuteName, pViewOnly, null, pLabelOveride ); |
|
107 |
+ |
} |
|
108 |
+ |
|
|
109 |
+ |
public Widget add( String pAttibuteName, Integer pDisplayWidthOveride ) { |
|
110 |
+ |
return add( pAttibuteName, pDisplayWidthOveride, null ); |
|
111 |
+ |
} |
|
112 |
+ |
|
|
113 |
+ |
public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride ) { |
|
114 |
+ |
return add( pAttibuteName, pViewOnly, pDisplayWidthOveride, null ); |
|
115 |
+ |
} |
|
116 |
+ |
|
|
117 |
+ |
public Widget add( String pAttibuteName, Integer pDisplayWidthOveride, String pLabelOveride ) { |
|
118 |
+ |
return add( pAttibuteName, false, pDisplayWidthOveride, pLabelOveride ); |
|
119 |
+ |
} |
|
120 |
+ |
|
|
121 |
+ |
public Widget add( String pAttibuteName, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabelOveride ) { |
|
122 |
+ |
String zLabel = ConstrainTo.significantOrNull( (pLabelOveride != null) ? pLabelOveride : DeCamelizer.resolve( pAttibuteName ) ); |
|
123 |
+ |
|
|
124 |
+ |
BoAttribute zAttribute = mVoMetaData.getBoAttribute( pAttibuteName ); |
|
125 |
+ |
|
|
126 |
+ |
if ( zAttribute instanceof SimpleVoAttribute ) { |
|
127 |
+ |
SimpleVoAttribute zSA = (SimpleVoAttribute) zAttribute; |
|
128 |
+ |
AttributeMetaData zAMD = zSA.getMetaData(); |
|
129 |
+ |
switch ( zAMD.getType() ) { |
|
130 |
+ |
case Boolean: |
|
131 |
+ |
return addBoolean( zSA, pViewOnly, zLabel ); |
|
132 |
+ |
case Float: |
|
133 |
+ |
break; |
|
134 |
+ |
case Integer: |
|
135 |
+ |
return addInteger( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
136 |
+ |
case Long: |
|
137 |
+ |
return addLong( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
138 |
+ |
case Password: |
|
139 |
+ |
return addPassword( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
140 |
+ |
case Primitive_boolean: |
|
141 |
+ |
break; |
|
142 |
+ |
case Primitive_int: |
|
143 |
+ |
break; |
|
144 |
+ |
case CalendarYMD: |
|
145 |
+ |
return addSimpleDate( zSA, pViewOnly, zLabel ); |
|
146 |
+ |
case SimpleFixedPointLong: |
|
147 |
+ |
break; |
|
148 |
+ |
case SimpleFloatingPointLong: |
|
149 |
+ |
break; |
|
150 |
+ |
case SimpleMoney: |
|
151 |
+ |
return addSimpleMoney( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
152 |
+ |
case SimpleTime: |
|
153 |
+ |
break; |
|
154 |
+ |
case SimpleTimestamp: |
|
155 |
+ |
break; |
|
156 |
+ |
case String: |
|
157 |
+ |
return addString( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
158 |
+ |
case Text: |
|
159 |
+ |
return addText( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
160 |
+ |
case TextLines: |
|
161 |
+ |
break; |
|
162 |
+ |
case ValidOptions: |
|
163 |
+ |
return addValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
164 |
+ |
case OpenValidOptions: |
|
165 |
+ |
return addOpenValidOptions( zSA, pViewOnly, pDisplayWidthOveride, zLabel ); |
|
166 |
+ |
default: |
|
167 |
+ |
break; |
|
168 |
+ |
} |
|
169 |
+ |
throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAMD.getType() ); |
|
170 |
+ |
} |
|
171 |
+ |
|
|
172 |
+ |
throw new UnsupportedOperationException( "Auto-Component generation not currently supported for " + zAttribute ); |
|
173 |
+ |
} |
|
174 |
+ |
|
|
175 |
+ |
private Widget addBoolean( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel ) { |
|
176 |
+ |
return add( false, pAttribute, pViewOnly, // |
|
177 |
+ |
new FormCheckBox( adjustLabelUnique( pAttribute, pLabel, pViewOnly ), null, // |
|
178 |
+ |
mTooltipMap.get( pAttribute.getName() ), false ) ); |
|
179 |
+ |
} |
|
180 |
+ |
|
|
181 |
+ |
private Widget addInteger( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
182 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
183 |
+ |
return add( pAttribute, pViewOnly, // |
|
184 |
+ |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
|
185 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
186 |
+ |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
|
187 |
+ |
} |
|
188 |
+ |
|
|
189 |
+ |
private Widget addLong( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
190 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
191 |
+ |
return add( pAttribute, pViewOnly, // |
|
192 |
+ |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
|
193 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
194 |
+ |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
|
195 |
+ |
} |
|
196 |
+ |
|
|
197 |
+ |
protected Widget addPassword( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
198 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
199 |
+ |
return add( pAttribute, pViewOnly, // |
|
200 |
+ |
new FormPasswordField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
|
201 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
202 |
+ |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
|
203 |
+ |
} |
|
204 |
+ |
|
|
205 |
+ |
private static DateFormat sDF = null; |
|
206 |
+ |
|
|
207 |
+ |
private Widget addSimpleDate( SimpleVoAttribute pAttribute, boolean pViewOnly, String pLabel ) { |
|
208 |
+ |
String zFieldLabel = adjustLabel( pAttribute, pLabel, pViewOnly ); |
|
209 |
+ |
String zTooltip = mTooltipMap.get( pAttribute.getName() ); |
|
210 |
+ |
|
|
211 |
+ |
if ( pViewOnly ) { |
|
212 |
+ |
return add( pAttribute, pViewOnly, new FormTextField( zFieldLabel, null, zTooltip, 12, 12 ) ); |
|
213 |
+ |
} |
|
214 |
+ |
if ( sDF == null ) { |
|
215 |
+ |
sDF = DateFormat.DEFAULT_YMD; |
|
216 |
+ |
} |
|
217 |
+ |
return add( pAttribute, pViewOnly, new FormDatePicker( zFieldLabel, null, zTooltip, "Cancel", "Today", sDF ) ); |
|
218 |
+ |
} |
|
219 |
+ |
|
|
220 |
+ |
private Widget addSimpleMoney( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
221 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
222 |
+ |
return add( pAttribute, pViewOnly, // |
|
223 |
+ |
new FormRightAlignTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
|
224 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
225 |
+ |
positiveOnly( MaxLength.get( zAMD ) ) ) ); |
|
226 |
+ |
} |
|
227 |
+ |
|
|
228 |
+ |
private Widget addText( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
229 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
230 |
+ |
return add( pAttribute, pViewOnly, // |
|
231 |
+ |
new FormTextarea( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), true, |
|
232 |
+ |
// todo: switch the true to use a SSMap of Name Values... |
|
233 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayWidthInitial.get( zAMD ) ), false, // |
|
234 |
+ |
positiveOnly( DisplayHeightInitial.get( zAMD ) ), false ) ); |
|
235 |
+ |
} |
|
236 |
+ |
|
|
237 |
+ |
private Widget addString( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
238 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
239 |
+ |
String zName = pAttribute.getName(); |
|
240 |
+ |
|
|
241 |
+ |
FormTextField zTextField = new FormTextField( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( zName ), // |
|
242 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
243 |
+ |
positiveOnly( MaxLength.get( zAMD ) ) ); |
|
244 |
+ |
|
|
245 |
+ |
if ( !pViewOnly && Case.Form.AsEntered.equals( Case.get( zAMD ) ) && mFE.isSearchFilter( zName ) ) { |
|
246 |
+ |
zTextField.addStyleName( NO_CASE_SPECIFICATION ); |
|
247 |
+ |
} |
|
248 |
+ |
return add( pAttribute, pViewOnly, zTextField ); |
|
249 |
+ |
} |
|
250 |
+ |
|
|
251 |
+ |
private Widget addOpenValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
252 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
253 |
+ |
String[] zOptions = Options.get( zAMD ); |
|
254 |
+ |
FormSuggestBox zSuggestBox = new FormSuggestBox( adjustLabel( pAttribute, pLabel, pViewOnly ), null, mTooltipMap.get( pAttribute.getName() ), // |
|
255 |
+ |
positiveOverride( pDisplayWidthOveride, DisplayLength.get( zAMD ) ), // |
|
256 |
+ |
positiveOnly( MaxLength.get( zAMD ) ), zOptions ); |
|
257 |
+ |
// todo ? Case Management? |
|
258 |
+ |
return add( pAttribute, pViewOnly, zSuggestBox ); |
|
259 |
+ |
} |
|
260 |
+ |
|
|
261 |
+ |
private Widget addValidOptions( SimpleVoAttribute pAttribute, boolean pViewOnly, Integer pDisplayWidthOveride, String pLabel ) { |
|
262 |
+ |
AttributeMetaData zAMD = pAttribute.getMetaData(); |
|
263 |
+ |
String zName = pAttribute.getName(); |
|
264 |
+ |
if ( pViewOnly ) { |
|
265 |
+ |
return mFE.add( zName, disable( new FormTextField( pLabel, null, mTooltipMap.get( zName ), // |
|
266 |
+ |
positiveOverride( pDisplayWidthOveride, Options.getLength( zAMD ) ), // |
|
267 |
+ |
Options.getLength( zAMD ) ) ) ); |
|
268 |
+ |
} |
|
269 |
+ |
String[] zOptions = Options.get( zAMD ); |
|
270 |
+ |
pLabel = adjustLabelUnique( pAttribute, pLabel, false ); |
|
271 |
+ |
if ( !pAttribute.isRequired() ) { |
|
272 |
+ |
return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ), "" ).addItems( zOptions ) ); |
|
273 |
+ |
} |
|
274 |
+ |
if ( zOptions.length <= 3 ) { |
|
275 |
+ |
return mFE.add( zName, new FormRadioGroup( pLabel, null, mTooltipMap.get( zName ), zOptions ) ); |
|
276 |
+ |
} |
|
277 |
+ |
return mFE.add( zName, new FormComboBox( pLabel, null, mTooltipMap.get( zName ) ).addItems( zOptions ) ); |
|
278 |
+ |
} |
|
279 |
+ |
|
|
280 |
+ |
private Widget add( SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement ) { |
|
281 |
+ |
return add( pAttribute.isRequired(), pAttribute, pViewOnly, pFormElement ); |
|
282 |
+ |
} |
|
283 |
+ |
|
|
284 |
+ |
private Widget add( boolean pRequired, SimpleVoAttribute pAttribute, boolean pViewOnly, AbstractFormElement pFormElement ) { |
|
285 |
+ |
if ( pViewOnly ) { |
|
286 |
+ |
pRequired = false; |
|
287 |
+ |
pFormElement = disable( pFormElement ); |
|
288 |
+ |
} |
|
289 |
+ |
return mFE.add( pAttribute.getName(), pRequired, pFormElement ); |
|
290 |
+ |
} |
|
291 |
+ |
|
|
292 |
+ |
private AbstractFormElement disable( AbstractFormElement pField ) { |
|
293 |
+ |
pField.setEnabled( false ); |
|
294 |
+ |
return pField; |
|
295 |
+ |
} |
|
296 |
+ |
|
|
297 |
+ |
public FormBinder addTooltip( String pAttributeName, String pTooltip ) { |
|
298 |
+ |
return Currently.isNotNullOrEmpty( pTooltip ) ? LLaddTooltip( pAttributeName, pTooltip ) : this; |
|
299 |
+ |
} |
|
300 |
+ |
|
|
301 |
+ |
private FormBinder LLaddTooltip( String pAttributeName, String pTooltip ) { |
|
302 |
+ |
mTooltipMap.put( pAttributeName, pTooltip ); |
|
303 |
+ |
return this; |
|
304 |
+ |
} |
|
305 |
+ |
|
|
306 |
+ |
private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly ) { |
|
307 |
+ |
return adjustLabel( pAttribute, pLabel, pViewOnly, true ); |
|
308 |
+ |
} |
|
309 |
+ |
|
|
310 |
+ |
private String adjustLabelUnique( VoAttribute pAttribute, String pLabel, boolean pViewOnly ) { |
|
311 |
+ |
return adjustLabel( pAttribute, pLabel, pViewOnly, false ); |
|
312 |
+ |
} |
|
313 |
+ |
|
|
314 |
+ |
private String adjustLabel( VoAttribute pAttribute, String pLabel, boolean pViewOnly, boolean pAllowRequired ) { |
|
315 |
+ |
if ( !pViewOnly && (pLabel != null) ) { |
|
316 |
+ |
if ( mFE.isUnique( pAttribute.getName() ) ) { |
|
317 |
+ |
return pLabel + LABEL_EXTENSION_UNIQUE; |
|
318 |
+ |
} |
|
319 |
+ |
if ( pAllowRequired && (pAttribute.isRequired() || mFE.isEditRequired( pAttribute.getName() )) ) { |
|
320 |
+ |
return pLabel + LABEL_EXTENSION_REQUIRED; |
|
321 |
+ |
} |
|
322 |
+ |
} |
|
323 |
+ |
return pLabel; |
|
324 |
+ |
} |
|
325 |
+ |
|
|
326 |
+ |
private Integer positiveOverride( Integer pOveride, Integer pAttribute ) { |
|
327 |
+ |
return positiveOnly( (pOveride != null) ? pOveride : pAttribute ); |
|
328 |
+ |
} |
|
329 |
+ |
|
|
330 |
+ |
private Integer positiveOnly( Integer pValue ) { |
|
331 |
+ |
return ((pValue != null) && (pValue > 0)) ? pValue : null; |
|
332 |
+ |
} |
|
333 |
+ |
} |