|
@@ -1,126 +1,126 @@ |
1 |
|
- |
package com.temp.client.foundation.widget.input.fieldsupport; |
2 |
|
- |
|
3 |
|
- |
import org.litesoft.core.util.externalization.*; |
4 |
|
- |
|
5 |
|
- |
import com.google.gwt.event.dom.client.*; |
6 |
|
- |
import com.google.gwt.event.shared.*; |
7 |
|
- |
import com.temp.client.foundation.widget.input.support.*; |
8 |
|
- |
import com.temp.client.foundation.widget.input.support.events.*; |
9 |
|
- |
import com.temp.shared.*; |
10 |
|
- |
|
11 |
|
- |
import java.util.*; |
12 |
|
- |
|
13 |
|
- |
public class InputWidgetValidator<T> implements InputWidgetValueAdapter<T> { |
14 |
|
- |
protected final NonDomHandlerManager<ChangeHandler> handlerManager; |
15 |
|
- |
protected final InputWidgetValueAdapter<T> delegate; |
16 |
|
- |
protected final List<ValueValidator<T>> validators = new ArrayList<ValueValidator<T>>(); |
17 |
|
- |
protected E13nData errorData; |
18 |
|
- |
|
19 |
|
- |
public InputWidgetValidator( InputWidgetValueAdapter<T> delegate ) { |
20 |
|
- |
handlerManager = new NonDomHandlerManager<ChangeHandler>( this ); |
21 |
|
- |
this.delegate = delegate; |
22 |
|
- |
delegate.addChangeHandler( new ChangeHandler() { |
23 |
|
- |
@Override |
24 |
|
- |
public void onChange( ChangeEvent event ) { |
25 |
|
- |
validateWithErrorStateChangeNotification(); |
26 |
|
- |
} |
27 |
|
- |
} ); |
28 |
|
- |
} |
29 |
|
- |
|
30 |
|
- |
public InputWidgetValidator( InputWidgetValueAdapter<T> delegate, ValueValidator<T> validator1, ValueValidator<T>... additionalValidators ) { |
31 |
|
- |
this( delegate ); |
32 |
|
- |
add( validator1 ); |
33 |
|
- |
if ( additionalValidators != null ) { |
34 |
|
- |
for ( ValueValidator<T> validator : additionalValidators ) { |
35 |
|
- |
add( validator ); |
36 |
|
- |
} |
37 |
|
- |
} |
38 |
|
- |
if ( !validators.isEmpty() ) { |
39 |
|
- |
validate(); |
40 |
|
- |
} |
41 |
|
- |
} |
42 |
|
- |
|
43 |
|
- |
protected boolean add( ValueValidator<T> validator ) { |
44 |
|
- |
if ( validator != null ) { |
45 |
|
- |
validators.add( validator ); |
46 |
|
- |
return true; |
47 |
|
- |
} |
48 |
|
- |
return false; |
49 |
|
- |
} |
50 |
|
- |
|
51 |
|
- |
public void addValidator( ValueValidator<T> validator ) { |
52 |
|
- |
if ( add( validator ) ) { |
53 |
|
- |
validateWithErrorStateChangeNotification(); |
54 |
|
- |
} |
55 |
|
- |
} |
56 |
|
- |
|
57 |
|
- |
public void addValidators( ValueValidator<T>... additionalValidators ) { |
58 |
|
- |
if ( additionalValidators != null ) { |
59 |
|
- |
boolean anyAdded = false; |
60 |
|
- |
for ( ValueValidator<T> validator : additionalValidators ) { |
61 |
|
- |
anyAdded |= add( validator ); |
62 |
|
- |
} |
63 |
|
- |
if ( anyAdded ) { |
64 |
|
- |
validateWithErrorStateChangeNotification(); |
65 |
|
- |
} |
66 |
|
- |
} |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
/** |
70 |
|
- |
* @return null if currently valid, otherwise the data to generate the Error Text |
71 |
|
- |
*/ |
72 |
|
- |
public E13nData validate() { |
73 |
|
- |
T value = getValue(); |
74 |
|
- |
errorData = null; |
75 |
|
- |
for ( ValueValidator<T> validator : validators ) { |
76 |
|
- |
errorData = validator.checkValue( value ); |
77 |
|
- |
if ( errorData != null ) { |
78 |
|
- |
break; |
79 |
|
- |
} |
80 |
|
- |
} |
81 |
|
- |
return getErrorData(); |
82 |
|
- |
} |
83 |
|
- |
|
84 |
|
- |
/** |
85 |
|
- |
* @return null if currently valid, otherwise the data to generate the Error Text |
86 |
|
- |
*/ |
87 |
|
- |
public E13nData getErrorData() { |
88 |
|
- |
return errorData; |
89 |
|
- |
} |
90 |
|
- |
|
91 |
|
- |
public HandlerRegistration addErrorStateChangeHandler( ChangeHandler handler ) { |
92 |
|
- |
return handler == null ? null : registerErrorStateChangeHandler( handler ); |
93 |
|
- |
} |
94 |
|
- |
|
95 |
|
- |
@Override |
96 |
|
- |
public HandlerRegistration addChangeHandler( ChangeHandler handler ) { |
97 |
|
- |
return delegate.addChangeHandler( handler ); |
98 |
|
- |
} |
99 |
|
- |
|
100 |
|
- |
@Override |
101 |
|
- |
public T getValue() { |
102 |
|
- |
return delegate.getValue(); |
103 |
|
- |
} |
104 |
|
- |
|
105 |
|
- |
@Override |
106 |
|
- |
public void setValue( T value ) { |
107 |
|
- |
delegate.setValue( value ); |
108 |
|
- |
validateWithErrorStateChangeNotification(); |
109 |
|
- |
} |
110 |
|
- |
|
111 |
|
- |
protected void validateWithErrorStateChangeNotification() { |
112 |
|
- |
boolean hadError = (null != getErrorData()); |
113 |
|
- |
boolean hasError = (null != validate()); |
114 |
|
- |
if ( hadError != hasError ) { |
115 |
|
- |
notifyErrorStateChangeHandlers(); |
116 |
|
- |
} |
117 |
|
- |
} |
118 |
|
- |
|
119 |
|
- |
protected void notifyErrorStateChangeHandlers() { |
120 |
|
- |
handlerManager.fireEvent( new NonDomChangeEvent() ); |
121 |
|
- |
} |
122 |
|
- |
|
123 |
|
- |
protected HandlerRegistration registerErrorStateChangeHandler( ChangeHandler handler ) { |
124 |
|
- |
return handlerManager.addHandler( handler ); |
125 |
|
- |
} |
126 |
|
- |
} |
|
1 |
+ |
package com.temp.client.foundation.widget.input.fieldsupport; |
|
2 |
+ |
|
|
3 |
+ |
import org.litesoft.core.util.externalization.*; |
|
4 |
+ |
|
|
5 |
+ |
import com.google.gwt.event.dom.client.*; |
|
6 |
+ |
import com.google.gwt.event.shared.*; |
|
7 |
+ |
import com.temp.client.foundation.widget.input.support.*; |
|
8 |
+ |
import com.temp.client.foundation.widget.input.support.events.*; |
|
9 |
+ |
import com.temp.shared.*; |
|
10 |
+ |
|
|
11 |
+ |
import java.util.*; |
|
12 |
+ |
|
|
13 |
+ |
public class InputWidgetValidator<T> implements InputWidgetValueAdapter<T> { |
|
14 |
+ |
protected final NonDomHandlerManager<ChangeHandler> handlerManager; |
|
15 |
+ |
protected final InputWidgetValueAdapter<T> delegate; |
|
16 |
+ |
protected final List<ValueValidator<T>> validators = new ArrayList<ValueValidator<T>>(); |
|
17 |
+ |
protected E13nData errorData; |
|
18 |
+ |
|
|
19 |
+ |
public InputWidgetValidator( InputWidgetValueAdapter<T> delegate ) { |
|
20 |
+ |
handlerManager = new NonDomHandlerManager<ChangeHandler>( this ); |
|
21 |
+ |
this.delegate = delegate; |
|
22 |
+ |
delegate.addChangeHandler( new ChangeHandler() { |
|
23 |
+ |
@Override |
|
24 |
+ |
public void onChange( ChangeEvent event ) { |
|
25 |
+ |
validateWithErrorStateChangeNotification(); |
|
26 |
+ |
} |
|
27 |
+ |
} ); |
|
28 |
+ |
} |
|
29 |
+ |
|
|
30 |
+ |
public InputWidgetValidator( InputWidgetValueAdapter<T> delegate, ValueValidator<T> validator1, ValueValidator<T>... additionalValidators ) { |
|
31 |
+ |
this( delegate ); |
|
32 |
+ |
add( validator1 ); |
|
33 |
+ |
if ( additionalValidators != null ) { |
|
34 |
+ |
for ( ValueValidator<T> validator : additionalValidators ) { |
|
35 |
+ |
add( validator ); |
|
36 |
+ |
} |
|
37 |
+ |
} |
|
38 |
+ |
if ( !validators.isEmpty() ) { |
|
39 |
+ |
validate(); |
|
40 |
+ |
} |
|
41 |
+ |
} |
|
42 |
+ |
|
|
43 |
+ |
protected boolean add( ValueValidator<T> validator ) { |
|
44 |
+ |
if ( validator != null ) { |
|
45 |
+ |
validators.add( validator ); |
|
46 |
+ |
return true; |
|
47 |
+ |
} |
|
48 |
+ |
return false; |
|
49 |
+ |
} |
|
50 |
+ |
|
|
51 |
+ |
public void addValidator( ValueValidator<T> validator ) { |
|
52 |
+ |
if ( add( validator ) ) { |
|
53 |
+ |
validateWithErrorStateChangeNotification(); |
|
54 |
+ |
} |
|
55 |
+ |
} |
|
56 |
+ |
|
|
57 |
+ |
public void addValidators( ValueValidator<T>... additionalValidators ) { |
|
58 |
+ |
if ( additionalValidators != null ) { |
|
59 |
+ |
boolean anyAdded = false; |
|
60 |
+ |
for ( ValueValidator<T> validator : additionalValidators ) { |
|
61 |
+ |
anyAdded |= add( validator ); |
|
62 |
+ |
} |
|
63 |
+ |
if ( anyAdded ) { |
|
64 |
+ |
validateWithErrorStateChangeNotification(); |
|
65 |
+ |
} |
|
66 |
+ |
} |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
/** |
|
70 |
+ |
* @return null if currently valid, otherwise the data to generate the Error Text |
|
71 |
+ |
*/ |
|
72 |
+ |
public E13nData validate() { |
|
73 |
+ |
T value = getValue(); |
|
74 |
+ |
errorData = null; |
|
75 |
+ |
for ( ValueValidator<T> validator : validators ) { |
|
76 |
+ |
errorData = validator.checkValue( value ); |
|
77 |
+ |
if ( errorData != null ) { |
|
78 |
+ |
break; |
|
79 |
+ |
} |
|
80 |
+ |
} |
|
81 |
+ |
return getErrorData(); |
|
82 |
+ |
} |
|
83 |
+ |
|
|
84 |
+ |
/** |
|
85 |
+ |
* @return null if currently valid, otherwise the data to generate the Error Text |
|
86 |
+ |
*/ |
|
87 |
+ |
public E13nData getErrorData() { |
|
88 |
+ |
return errorData; |
|
89 |
+ |
} |
|
90 |
+ |
|
|
91 |
+ |
public HandlerRegistration addErrorStateChangeHandler( ChangeHandler handler ) { |
|
92 |
+ |
return handler == null ? null : registerErrorStateChangeHandler( handler ); |
|
93 |
+ |
} |
|
94 |
+ |
|
|
95 |
+ |
@Override |
|
96 |
+ |
public HandlerRegistration addChangeHandler( ChangeHandler handler ) { |
|
97 |
+ |
return delegate.addChangeHandler( handler ); |
|
98 |
+ |
} |
|
99 |
+ |
|
|
100 |
+ |
@Override |
|
101 |
+ |
public T getValue() { |
|
102 |
+ |
return delegate.getValue(); |
|
103 |
+ |
} |
|
104 |
+ |
|
|
105 |
+ |
@Override |
|
106 |
+ |
public void setValue( T value ) { |
|
107 |
+ |
delegate.setValue( value ); |
|
108 |
+ |
validateWithErrorStateChangeNotification(); |
|
109 |
+ |
} |
|
110 |
+ |
|
|
111 |
+ |
protected void validateWithErrorStateChangeNotification() { |
|
112 |
+ |
boolean hadError = (null != getErrorData()); |
|
113 |
+ |
boolean hasError = (null != validate()); |
|
114 |
+ |
if ( hadError != hasError ) { |
|
115 |
+ |
notifyErrorStateChangeHandlers(); |
|
116 |
+ |
} |
|
117 |
+ |
} |
|
118 |
+ |
|
|
119 |
+ |
protected void notifyErrorStateChangeHandlers() { |
|
120 |
+ |
handlerManager.fireEvent( new NonDomChangeEvent() ); |
|
121 |
+ |
} |
|
122 |
+ |
|
|
123 |
+ |
protected HandlerRegistration registerErrorStateChangeHandler( ChangeHandler handler ) { |
|
124 |
+ |
return handlerManager.addHandler( handler ); |
|
125 |
+ |
} |
|
126 |
+ |
} |