Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/KeyHole/src/org/litesoft/aokeyhole/objects/support/PropertyManager.java

Diff revisions: vs.
  @@ -1,209 +1,209 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.aokeyhole.objects.support;
3 -
4 - import org.litesoft.aokeyhole.objects.*;
5 - import org.litesoft.aokeyhole.persist.*;
6 - import org.litesoft.aokeyhole.toolkit.*;
7 -
8 - import java.util.*;
9 -
10 - import static org.litesoft.aokeyhole.objects.support.AbstractBase.*;
11 -
12 - public class PropertyManager {
13 - public static final PropertyManager NONE = new PropertyManager( null, false, PropertyMetaData.EMPTY_ARRAY );
14 -
15 - private Changeable mChangeable;
16 - private Property[] mProperties;
17 - private Map<String, Property> mNameToProperty;
18 -
19 - public PropertyManager( Changeable pChangeable, boolean pEditable, PropertyMetaData[] pPropertyMetaDatas ) {
20 - mChangeable = pChangeable;
21 - mNameToProperty = new HashMap<String, Property>( pPropertyMetaDatas.length );
22 - mProperties = new Property[pPropertyMetaDatas.length];
23 - for ( int i = 0; i < pPropertyMetaDatas.length; i++ ) {
24 - Property property = new Property( pPropertyMetaDatas[i], pEditable );
25 - mProperties[i] = property;
26 - if ( mNameToProperty.put( property.getName(), property ) != null ) {
27 - throw new IllegalStateException( "Duplicate Property: " + property.getName() );
28 - }
29 - }
30 - }
31 -
32 - public Property[] getProperties() {
33 - return mProperties;
34 - }
35 -
36 - public Property getProperty( String pName ) {
37 - return mNameToProperty.get( pName );
38 - }
39 -
40 - public void unpersistProperty( String pName, String pValueAsString ) {
41 - Property property = getProperty( pName );
42 - if ( property != null ) // Ignoring not founds - as we assume that the model has changed...
43 - {
44 - property.unpersistValue( property.getMetaData().validateAndNormalize( pValueAsString ) );
45 - }
46 - }
47 -
48 - /**
49 - * @return null or Error
50 - */
51 - public String updatePropertyValue( String pName, Object pValue ) {
52 - Property property = getProperty( pName );
53 - if ( property == null ) {
54 - return "No Property Named: " + pName;
55 - }
56 - Object newNormalizedValue;
57 - try {
58 - newNormalizedValue = property.getMetaData().validateAndNormalize( pValue );
59 - }
60 - catch ( ParseException e ) {
61 - return e.getMessage();
62 - }
63 - if ( property.setValue( newNormalizedValue ) && (mChangeable != null) ) {
64 - mChangeable.changed();
65 - }
66 - return null;
67 - }
68 -
69 - public void addTo( PropertyableBuilder pBuilder ) {
70 - for ( Property property : mProperties ) {
71 - property.addTo( pBuilder );
72 - }
73 - }
74 -
75 - public void setRehydrated() {
76 - for ( Property property : mProperties ) {
77 - property.setRehydrated();
78 - }
79 - }
80 -
81 - /**
82 - * @return null or Error
83 - */
84 - public String validatePersistable() {
85 - String error = null;
86 - for ( int i = 0; (i < mProperties.length) && (error == null); i++ ) {
87 - error = mProperties[i].validatePersistable();
88 - }
89 - return error;
90 - }
91 -
92 - public void setUpView( NameValuesComponent pNameValuesComponent ) {
93 - for ( Property property : mProperties ) {
94 - pNameValuesComponent.setValue( property.getName(), property.getValueAsString() );
95 - }
96 - }
97 -
98 - public void saveFromView( ChangeHandler pChanges, NameValuesComponent pNameValuesComponent ) {
99 - for ( Property property : mProperties ) {
100 - String zName = property.getName();
101 - if ( pNameValuesComponent.hasComponentFor( zName ) ) {
102 - pChanges.add( checkPropertyChange( property, pNameValuesComponent.getValue( zName ) ) );
103 - }
104 - }
105 - }
106 -
107 - private ChangeFragment checkPropertyChange( Property pProperty, String pNewValue ) {
108 - PropertyMetaData zPMD = pProperty.getMetaData();
109 - String zNewValue = zPMD.valueToString( zPMD.validateAndNormalize( pNewValue ) );
110 - String zCurValue = pProperty.getValueAsString();
111 - return !areEqual( zCurValue, zNewValue ) ? new ChangeProperty( pProperty, zNewValue ) : null;
112 - }
113 -
114 - public String get_String( String pName, String pDefault ) {
115 - Property zProperty = getProperty( pName );
116 - if ( zProperty != null ) {
117 - Object zValue = zProperty.getValue();
118 - if ( zValue != null ) {
119 - String rv = zValue.toString();
120 - if ( rv.length() != 0 ) {
121 - return rv;
122 - }
123 - }
124 - }
125 - return pDefault;
126 - }
127 -
128 - public boolean get_boolean( String pName ) {
129 - Property zProperty = getProperty( pName );
130 - if ( zProperty != null ) {
131 - Object zValue = zProperty.getValue();
132 - if ( zValue instanceof Boolean ) {
133 - return Boolean.TRUE.equals( zValue );
134 - }
135 - if ( zValue != null ) {
136 - String rv = zValue.toString();
137 - if ( rv.length() != 0 ) {
138 - return Boolean.parseBoolean( rv );
139 - }
140 - }
141 - }
142 - return false;
143 - }
144 -
145 - public int get_int( String pName, int pDefault ) {
146 - Property zProperty = getProperty( pName );
147 - if ( zProperty != null ) {
148 - Object zValue = zProperty.getValue();
149 - if ( zValue instanceof Integer ) {
150 - return ((Integer) zValue);
151 - }
152 - if ( zValue != null ) {
153 - String rv = zValue.toString();
154 - if ( rv.length() != 0 ) {
155 - return Integer.parseInt( rv );
156 - }
157 - }
158 - }
159 - return pDefault;
160 - }
161 -
162 - public Integer get_Integer( String pName, Integer pDefault ) {
163 - Property zProperty = getProperty( pName );
164 - if ( zProperty != null ) {
165 - Object zValue = zProperty.getValue();
166 - if ( zValue instanceof Integer ) {
167 - return ((Integer) zValue);
168 - }
169 - if ( zValue != null ) {
170 - String rv = zValue.toString();
171 - if ( rv.length() != 0 ) {
172 - return Integer.parseInt( rv );
173 - }
174 - }
175 - }
176 - return pDefault;
177 - }
178 -
179 - public String[] get_StringArray( String pName ) {
180 - Property zProperty = getProperty( pName );
181 - if ( zProperty != null ) {
182 - Object zValue = zProperty.getValue();
183 - if ( zValue instanceof String[] ) {
184 - return ((String[]) zValue);
185 - }
186 - if ( zValue != null ) {
187 - throw new IllegalArgumentException( "Property '" + pName + "' not a String Array, value: " + zValue );
188 - }
189 - }
190 - return new String[0];
191 - }
192 -
193 - protected class ChangeProperty implements ChangeFragment {
194 - private Property mProperty;
195 - private String mNewValue;
196 -
197 - public ChangeProperty( Property pProperty, String pNewValue ) {
198 - mProperty = pProperty;
199 - mNewValue = pNewValue;
200 - }
201 -
202 - @Override
203 - public void commitChange() {
204 - if ( mProperty.setValue( mNewValue ) ) {
205 - mChangeable.changed();
206 - }
207 - }
208 - }
209 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.aokeyhole.objects.support;
3 +
4 + import org.litesoft.aokeyhole.objects.*;
5 + import org.litesoft.aokeyhole.persist.*;
6 + import org.litesoft.aokeyhole.toolkit.*;
7 +
8 + import java.util.*;
9 +
10 + import static org.litesoft.aokeyhole.objects.support.AbstractBase.*;
11 +
12 + public class PropertyManager {
13 + public static final PropertyManager NONE = new PropertyManager( null, false, PropertyMetaData.EMPTY_ARRAY );
14 +
15 + private Changeable mChangeable;
16 + private Property[] mProperties;
17 + private Map<String, Property> mNameToProperty;
18 +
19 + public PropertyManager( Changeable pChangeable, boolean pEditable, PropertyMetaData[] pPropertyMetaDatas ) {
20 + mChangeable = pChangeable;
21 + mNameToProperty = new HashMap<String, Property>( pPropertyMetaDatas.length );
22 + mProperties = new Property[pPropertyMetaDatas.length];
23 + for ( int i = 0; i < pPropertyMetaDatas.length; i++ ) {
24 + Property property = new Property( pPropertyMetaDatas[i], pEditable );
25 + mProperties[i] = property;
26 + if ( mNameToProperty.put( property.getName(), property ) != null ) {
27 + throw new IllegalStateException( "Duplicate Property: " + property.getName() );
28 + }
29 + }
30 + }
31 +
32 + public Property[] getProperties() {
33 + return mProperties;
34 + }
35 +
36 + public Property getProperty( String pName ) {
37 + return mNameToProperty.get( pName );
38 + }
39 +
40 + public void unpersistProperty( String pName, String pValueAsString ) {
41 + Property property = getProperty( pName );
42 + if ( property != null ) // Ignoring not founds - as we assume that the model has changed...
43 + {
44 + property.unpersistValue( property.getMetaData().validateAndNormalize( pValueAsString ) );
45 + }
46 + }
47 +
48 + /**
49 + * @return null or Error
50 + */
51 + public String updatePropertyValue( String pName, Object pValue ) {
52 + Property property = getProperty( pName );
53 + if ( property == null ) {
54 + return "No Property Named: " + pName;
55 + }
56 + Object newNormalizedValue;
57 + try {
58 + newNormalizedValue = property.getMetaData().validateAndNormalize( pValue );
59 + }
60 + catch ( ParseException e ) {
61 + return e.getMessage();
62 + }
63 + if ( property.setValue( newNormalizedValue ) && (mChangeable != null) ) {
64 + mChangeable.changed();
65 + }
66 + return null;
67 + }
68 +
69 + public void addTo( PropertyableBuilder pBuilder ) {
70 + for ( Property property : mProperties ) {
71 + property.addTo( pBuilder );
72 + }
73 + }
74 +
75 + public void setRehydrated() {
76 + for ( Property property : mProperties ) {
77 + property.setRehydrated();
78 + }
79 + }
80 +
81 + /**
82 + * @return null or Error
83 + */
84 + public String validatePersistable() {
85 + String error = null;
86 + for ( int i = 0; (i < mProperties.length) && (error == null); i++ ) {
87 + error = mProperties[i].validatePersistable();
88 + }
89 + return error;
90 + }
91 +
92 + public void setUpView( NameValuesComponent pNameValuesComponent ) {
93 + for ( Property property : mProperties ) {
94 + pNameValuesComponent.setValue( property.getName(), property.getValueAsString() );
95 + }
96 + }
97 +
98 + public void saveFromView( ChangeHandler pChanges, NameValuesComponent pNameValuesComponent ) {
99 + for ( Property property : mProperties ) {
100 + String zName = property.getName();
101 + if ( pNameValuesComponent.hasComponentFor( zName ) ) {
102 + pChanges.add( checkPropertyChange( property, pNameValuesComponent.getValue( zName ) ) );
103 + }
104 + }
105 + }
106 +
107 + private ChangeFragment checkPropertyChange( Property pProperty, String pNewValue ) {
108 + PropertyMetaData zPMD = pProperty.getMetaData();
109 + String zNewValue = zPMD.valueToString( zPMD.validateAndNormalize( pNewValue ) );
110 + String zCurValue = pProperty.getValueAsString();
111 + return !areEqual( zCurValue, zNewValue ) ? new ChangeProperty( pProperty, zNewValue ) : null;
112 + }
113 +
114 + public String get_String( String pName, String pDefault ) {
115 + Property zProperty = getProperty( pName );
116 + if ( zProperty != null ) {
117 + Object zValue = zProperty.getValue();
118 + if ( zValue != null ) {
119 + String rv = zValue.toString();
120 + if ( rv.length() != 0 ) {
121 + return rv;
122 + }
123 + }
124 + }
125 + return pDefault;
126 + }
127 +
128 + public boolean get_boolean( String pName ) {
129 + Property zProperty = getProperty( pName );
130 + if ( zProperty != null ) {
131 + Object zValue = zProperty.getValue();
132 + if ( zValue instanceof Boolean ) {
133 + return Boolean.TRUE.equals( zValue );
134 + }
135 + if ( zValue != null ) {
136 + String rv = zValue.toString();
137 + if ( rv.length() != 0 ) {
138 + return Boolean.parseBoolean( rv );
139 + }
140 + }
141 + }
142 + return false;
143 + }
144 +
145 + public int get_int( String pName, int pDefault ) {
146 + Property zProperty = getProperty( pName );
147 + if ( zProperty != null ) {
148 + Object zValue = zProperty.getValue();
149 + if ( zValue instanceof Integer ) {
150 + return ((Integer) zValue);
151 + }
152 + if ( zValue != null ) {
153 + String rv = zValue.toString();
154 + if ( rv.length() != 0 ) {
155 + return Integer.parseInt( rv );
156 + }
157 + }
158 + }
159 + return pDefault;
160 + }
161 +
162 + public Integer get_Integer( String pName, Integer pDefault ) {
163 + Property zProperty = getProperty( pName );
164 + if ( zProperty != null ) {
165 + Object zValue = zProperty.getValue();
166 + if ( zValue instanceof Integer ) {
167 + return ((Integer) zValue);
168 + }
169 + if ( zValue != null ) {
170 + String rv = zValue.toString();
171 + if ( rv.length() != 0 ) {
172 + return Integer.parseInt( rv );
173 + }
174 + }
175 + }
176 + return pDefault;
177 + }
178 +
179 + public String[] get_StringArray( String pName ) {
180 + Property zProperty = getProperty( pName );
181 + if ( zProperty != null ) {
182 + Object zValue = zProperty.getValue();
183 + if ( zValue instanceof String[] ) {
184 + return ((String[]) zValue);
185 + }
186 + if ( zValue != null ) {
187 + throw new IllegalArgumentException( "Property '" + pName + "' not a String Array, value: " + zValue );
188 + }
189 + }
190 + return new String[0];
191 + }
192 +
193 + protected class ChangeProperty implements ChangeFragment {
194 + private Property mProperty;
195 + private String mNewValue;
196 +
197 + public ChangeProperty( Property pProperty, String pNewValue ) {
198 + mProperty = pProperty;
199 + mNewValue = pNewValue;
200 + }
201 +
202 + @Override
203 + public void commitChange() {
204 + if ( mProperty.setValue( mNewValue ) ) {
205 + mChangeable.changed();
206 + }
207 + }
208 + }
209 + }