|
@@ -1,70 +1,70 @@ |
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.toolkit.*; |
6 |
|
- |
|
7 |
|
- |
public abstract class PropertyStringMetaData extends PropertyMetaData { |
8 |
|
- |
private String[] mValidOptions; |
9 |
|
- |
|
10 |
|
- |
public PropertyStringMetaData( String pName, Required pRequired, String pInitialValue ) { |
11 |
|
- |
this( pName, pName, pRequired, pInitialValue ); |
12 |
|
- |
} |
13 |
|
- |
|
14 |
|
- |
public PropertyStringMetaData( String pName, String pDisplayLabel, Required pRequired, String pInitialValue ) { |
15 |
|
- |
this( pName, pDisplayLabel, pRequired, pInitialValue, (String[]) null ); |
16 |
|
- |
} |
17 |
|
- |
|
18 |
|
- |
public PropertyStringMetaData( String pName, Required pRequired, String pInitialValue, String... pValidOptions ) { |
19 |
|
- |
this( pName, pName, pRequired, pInitialValue, pValidOptions ); |
20 |
|
- |
} |
21 |
|
- |
|
22 |
|
- |
public PropertyStringMetaData( String pName, String pDisplayLabel, Required pRequired, String pInitialValue, String... pValidOptions ) { |
23 |
|
- |
super( pName, pDisplayLabel, DataType.STRING, pRequired, parse( pInitialValue, pValidOptions ) ); |
24 |
|
- |
|
25 |
|
- |
mValidOptions = validateValidOptions( pValidOptions ); |
26 |
|
- |
} |
27 |
|
- |
|
28 |
|
- |
private String[] validateValidOptions( String[] pValidOptions ) { |
29 |
|
- |
if ( pValidOptions != null ) { |
30 |
|
- |
for ( int i = 0; i < pValidOptions.length; i++ ) { |
31 |
|
- |
String option = trimEmptyToNull( pValidOptions[i] ); |
32 |
|
- |
if ( option == null ) { |
33 |
|
- |
throw new IllegalStateException( "Null not allowed as a Valid Option" ); |
34 |
|
- |
} |
35 |
|
- |
pValidOptions[i] = option; |
36 |
|
- |
} |
37 |
|
- |
} |
38 |
|
- |
return pValidOptions; |
39 |
|
- |
} |
40 |
|
- |
|
41 |
|
- |
@Override |
42 |
|
- |
public String[] getValidOptions() { |
43 |
|
- |
return mValidOptions; |
44 |
|
- |
} |
45 |
|
- |
|
46 |
|
- |
@Override |
47 |
|
- |
public Object validateAndNormalize( Object pValue ) |
48 |
|
- |
throws ParseException { |
49 |
|
- |
return parse( pValue, mValidOptions ); |
50 |
|
- |
} |
51 |
|
- |
|
52 |
|
- |
public static Object parse( Object pValue, String[] pValidOptions ) |
53 |
|
- |
throws ParseException { |
54 |
|
- |
if ( pValue != null ) { |
55 |
|
- |
String value = trimEmptyToNull( pValue.toString() ); |
56 |
|
- |
if ( value != null ) { |
57 |
|
- |
if ( pValidOptions == null ) { |
58 |
|
- |
return value; |
59 |
|
- |
} |
60 |
|
- |
for ( String option : pValidOptions ) { |
61 |
|
- |
if ( value.equals( option ) ) { |
62 |
|
- |
return value; |
63 |
|
- |
} |
64 |
|
- |
} |
65 |
|
- |
throw new ParseException( value + " is not a valid option" ); |
66 |
|
- |
} |
67 |
|
- |
} |
68 |
|
- |
return pValue; |
69 |
|
- |
} |
70 |
|
- |
} |
|
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.toolkit.*; |
|
6 |
+ |
|
|
7 |
+ |
public abstract class PropertyStringMetaData extends PropertyMetaData { |
|
8 |
+ |
private String[] mValidOptions; |
|
9 |
+ |
|
|
10 |
+ |
public PropertyStringMetaData( String pName, Required pRequired, String pInitialValue ) { |
|
11 |
+ |
this( pName, pName, pRequired, pInitialValue ); |
|
12 |
+ |
} |
|
13 |
+ |
|
|
14 |
+ |
public PropertyStringMetaData( String pName, String pDisplayLabel, Required pRequired, String pInitialValue ) { |
|
15 |
+ |
this( pName, pDisplayLabel, pRequired, pInitialValue, (String[]) null ); |
|
16 |
+ |
} |
|
17 |
+ |
|
|
18 |
+ |
public PropertyStringMetaData( String pName, Required pRequired, String pInitialValue, String... pValidOptions ) { |
|
19 |
+ |
this( pName, pName, pRequired, pInitialValue, pValidOptions ); |
|
20 |
+ |
} |
|
21 |
+ |
|
|
22 |
+ |
public PropertyStringMetaData( String pName, String pDisplayLabel, Required pRequired, String pInitialValue, String... pValidOptions ) { |
|
23 |
+ |
super( pName, pDisplayLabel, DataType.STRING, pRequired, parse( pInitialValue, pValidOptions ) ); |
|
24 |
+ |
|
|
25 |
+ |
mValidOptions = validateValidOptions( pValidOptions ); |
|
26 |
+ |
} |
|
27 |
+ |
|
|
28 |
+ |
private String[] validateValidOptions( String[] pValidOptions ) { |
|
29 |
+ |
if ( pValidOptions != null ) { |
|
30 |
+ |
for ( int i = 0; i < pValidOptions.length; i++ ) { |
|
31 |
+ |
String option = trimEmptyToNull( pValidOptions[i] ); |
|
32 |
+ |
if ( option == null ) { |
|
33 |
+ |
throw new IllegalStateException( "Null not allowed as a Valid Option" ); |
|
34 |
+ |
} |
|
35 |
+ |
pValidOptions[i] = option; |
|
36 |
+ |
} |
|
37 |
+ |
} |
|
38 |
+ |
return pValidOptions; |
|
39 |
+ |
} |
|
40 |
+ |
|
|
41 |
+ |
@Override |
|
42 |
+ |
public String[] getValidOptions() { |
|
43 |
+ |
return mValidOptions; |
|
44 |
+ |
} |
|
45 |
+ |
|
|
46 |
+ |
@Override |
|
47 |
+ |
public Object validateAndNormalize( Object pValue ) |
|
48 |
+ |
throws ParseException { |
|
49 |
+ |
return parse( pValue, mValidOptions ); |
|
50 |
+ |
} |
|
51 |
+ |
|
|
52 |
+ |
public static Object parse( Object pValue, String[] pValidOptions ) |
|
53 |
+ |
throws ParseException { |
|
54 |
+ |
if ( pValue != null ) { |
|
55 |
+ |
String value = trimEmptyToNull( pValue.toString() ); |
|
56 |
+ |
if ( value != null ) { |
|
57 |
+ |
if ( pValidOptions == null ) { |
|
58 |
+ |
return value; |
|
59 |
+ |
} |
|
60 |
+ |
for ( String option : pValidOptions ) { |
|
61 |
+ |
if ( value.equals( option ) ) { |
|
62 |
+ |
return value; |
|
63 |
+ |
} |
|
64 |
+ |
} |
|
65 |
+ |
throw new ParseException( value + " is not a valid option" ); |
|
66 |
+ |
} |
|
67 |
+ |
} |
|
68 |
+ |
return pValue; |
|
69 |
+ |
} |
|
70 |
+ |
} |