Subversion Repository Public Repository

litesoft

Diff Revisions 949 vs 950 for /trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/nonpublic/AbstractFormElement.java

Diff revisions: vs.
  @@ -1,291 +1,290 @@
1 - // This Source Code is in the Public Domain per: http://unlicense.org
2 - package org.litesoft.GWT.forms.client.components.nonpublic;
3 -
4 - import org.litesoft.GWT.client.*;
5 - import org.litesoft.GWT.forms.client.components.*;
6 - import org.litesoft.commonfoundation.base.*;
7 - import org.litesoft.commonfoundation.typeutils.*;
8 - import org.litesoft.core.delayed.*;
9 -
10 - import com.google.gwt.user.client.*;
11 - import com.google.gwt.user.client.ui.*;
12 -
13 - import java.util.*;
14 -
15 - public abstract class AbstractFormElement extends Composite implements IFormComponent {
16 - public static final String BASE_STYLE_NAME = "litesoft-FormComponent";
17 - public static final String FORM_COMPONENT_LABEL_STYLE = "litesoft-FormComponentLabel";
18 - public static final String FORM_COMPONENT_LABEL_LEFT_STYLE = "litesoft-FormComponentLabelLeft";
19 -
20 - protected String mCurStyleState = "";
21 - protected boolean mChanged = false;
22 - protected boolean mFocused = false;
23 - protected boolean mEnabled = true;
24 - private Set<Listener> mFormComponentListeners = new HashSet<Listener>();
25 - protected FormHTMLpanel mInnerHTMLpanel; // Element is either a DIV / FieldSet
26 - protected String mFieldLabel, mTooltip;
27 -
28 - protected AbstractFormElement( Element pElement ) {
29 - VerticalPanel zConstrainingTable = new VerticalPanel();
30 - initWidget( zConstrainingTable );
31 - zConstrainingTable.add( mInnerHTMLpanel = new FormHTMLpanel( pElement ) );
32 - super.setStyleName( BASE_STYLE_NAME );
33 - }
34 -
35 - protected void initializeHTML( String pHTML ) {
36 - mInnerHTMLpanel.setHTML( pHTML );
37 - }
38 -
39 - // UIObject
40 -
41 - @Override
42 - public void setStyleName( String style ) {
43 - throw new IllegalStateException( "Can not use setStyleName, use addStyleName & removeStyleName" );
44 - }
45 -
46 - @Override
47 - protected Element getStyleElement() {
48 - return mInnerHTMLpanel.getElement();
49 - }
50 -
51 - abstract public void simulateUserTextEntry( String pText );
52 -
53 - public String getFieldLabel() {
54 - return mFieldLabel;
55 - }
56 -
57 - public String getTooltip() {
58 - return mTooltip;
59 - }
60 -
61 - /**
62 - * Not supported
63 - *
64 - * @throws UnsupportedOperationException
65 - */
66 - @Override
67 - public void setTitle( String title )
68 - throws UnsupportedOperationException {
69 - throw new UnsupportedOperationException();
70 - }
71 -
72 - // IFormComponent
73 -
74 - @Override
75 - public boolean hasError() {
76 - return false;
77 - }
78 -
79 - @Override
80 - public void setError( String pError )
81 - throws UnsupportedOperationException {
82 - if ( Strings.isNotNullOrEmpty( pError ) ) {
83 - throw new UnsupportedOperationException();
84 - }
85 - }
86 -
87 - @Override
88 - public void setFocused( boolean pFocused ) {
89 - mFocused = pFocused;
90 - stateChanged();
91 - }
92 -
93 - @Override
94 - public void setValueChanged( boolean pChanged ) {
95 - mChanged = pChanged;
96 - stateChanged();
97 - }
98 -
99 - @Override
100 - public boolean isEnabled() {
101 - return mEnabled;
102 - }
103 -
104 - @Override
105 - public void publishAnyPendingChanges() {
106 - // Do nothing as most FormElements do not do defferred processing
107 - if ( mFocused ) {
108 - LOGGER.error.log( "AbstractFormElement.publishAnyPendingChanges: Focused - ", simpleClassname( this ) );
109 - }
110 - }
111 -
112 - @Override
113 - public IFormComponent addFormComponentListener( Listener pFormComponentListener ) {
114 - if ( pFormComponentListener != null ) {
115 - pFormComponentListener.add( this );
116 - mFormComponentListeners.add( pFormComponentListener );
117 - }
118 - return this;
119 - }
120 -
121 - @Override
122 - public IFormComponent removeFormComponentListener( Listener pFormComponentListener ) {
123 - if ( pFormComponentListener != null ) {
124 - pFormComponentListener.remove( this );
125 - mFormComponentListeners.remove( pFormComponentListener );
126 - }
127 - return this;
128 - }
129 -
130 - // Support...
131 -
132 - protected static String simpleClassname( Object pObject ) {
133 - return ClassName.simple( pObject );
134 - }
135 -
136 - protected void fireBlurListeners() {
137 - trace( "fireBlurListeners" );
138 - for ( Listener zListener : mFormComponentListeners ) {
139 - zListener.blurOccurred();
140 - }
141 - }
142 -
143 - protected void fireChangeListeners() {
144 - trace( "fireChangeListeners" );
145 - for ( Listener zListener : mFormComponentListeners ) {
146 - zListener.changeOccurred();
147 - }
148 - }
149 -
150 - protected void fireFocusListeners() {
151 - trace( "fireFocusListeners" );
152 - for ( Listener zListener : mFormComponentListeners ) {
153 - zListener.focusOccured();
154 - }
155 - }
156 -
157 - protected void fireEnterPressedListeners( KeyboardKeyModifier pModifiers ) {
158 - trace( "fireenterPressedListeners" );
159 - for ( Listener zListener : mFormComponentListeners ) {
160 - zListener.enterPressed( pModifiers );
161 - }
162 - }
163 -
164 - protected boolean hasFocus() {
165 - return mFocused;
166 - }
167 -
168 - protected boolean hasChanged() {
169 - return mChanged;
170 - }
171 -
172 - protected void stateChanged() {
173 - if ( isEnabled() ) {
174 - setEnabledStyles();
175 - } else {
176 - setDisabledStyles();
177 - }
178 - }
179 -
180 - protected void setDisabledStyles() {
181 - StringBuilder style = new StringBuilder( "-Disabled" );
182 - if ( hasError() ) {
183 - style.append( "-Error" );
184 - }
185 - updateStatefulStyleName( style.toString() );
186 - }
187 -
188 - protected void setEnabledStyles() {
189 - StringBuilder style = new StringBuilder();
190 - if ( hasFocus() ) {
191 - style.append( "-Focused" );
192 - }
193 - if ( hasError() ) {
194 - style.append( "-Error" );
195 - }
196 - if ( hasChanged() ) {
197 - style.append( "-Changed" );
198 - }
199 - updateStatefulStyleName( style.toString() );
200 - }
201 -
202 - private void updateStatefulStyleName( String pNewStyleState ) {
203 - if ( !pNewStyleState.equals( mCurStyleState ) ) {
204 - if ( !mCurStyleState.equals( "" ) ) {
205 - removeStyleName( BASE_STYLE_NAME + mCurStyleState );
206 - }
207 - if ( !(mCurStyleState = pNewStyleState).equals( "" ) ) {
208 - addStyleName( BASE_STYLE_NAME + mCurStyleState );
209 - }
210 - }
211 - }
212 -
213 - protected static class FormHTMLpanel extends ComplexPanel {
214 - /**
215 - * Creates an HTML panel with NO specified HTML contents. The element is the wrapping element.
216 - *
217 - * @param pElement either the Div or the FieldSet
218 - */
219 - public FormHTMLpanel( Element pElement ) {
220 - setElement( pElement );
221 - }
222 -
223 - /**
224 - * Injects the specified HTML contents into the HTML panel. Any element within
225 - * this HTML that has a specified id can contain a child widget.
226 - *
227 - * @param pHtml the panel's HTML
228 - */
229 - public void setHTML( String pHtml ) {
230 - DOM.setInnerHTML( getElement(), pHtml );
231 - }
232 -
233 - /**
234 - * Adds a child widget to the panel, contained within the HTML element
235 - * specified by a given id.
236 - *
237 - * @param pWidget the widget to be added
238 - * @param pId the id of the element within which it will be contained
239 - */
240 - public void add( Widget pWidget, String pId ) {
241 - Element elem = getElementById( getElement(), pId );
242 - if ( elem == null ) {
243 - throw new NoSuchElementException( pId );
244 - }
245 -
246 - super.add( pWidget, elem );
247 - }
248 -
249 - /*
250 - * Implements getElementById() downward from the given element. We need to do
251 - * this because {@link #add(Widget, String)} must often be called before the
252 - * panel is attached to the DOM, so {@link Dom#getElementById} won't yet work.
253 - */
254 - private Element getElementById( Element pElement, String pId ) {
255 - String elemId = DOM.getElementProperty( pElement, "id" );
256 - if ( (elemId != null) && elemId.equals( pId ) ) {
257 - return pElement;
258 - }
259 -
260 - Element child = DOM.getFirstChild( pElement );
261 - while ( child != null ) {
262 - Element ret = getElementById( child, pId );
263 - if ( ret != null ) {
264 - return ret;
265 - }
266 - child = DOM.getNextSibling( child );
267 - }
268 -
269 - return null;
270 - }
271 - }
272 -
273 - protected void trace( String pMethod ) {
274 - if ( LOGGER.trace.isEnabled() ) {
275 - LOGGER.trace.log( pMethod + " - " + simpleClassname( this ) );
276 - }
277 - }
278 -
279 - protected void pullFocusTo( final Focusable pFocusable ) {
280 - TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() {
281 - @Override
282 - public Again runOnce() {
283 - if ( !UtilsGwt.isRecursiveVisible( (Widget) pFocusable ) ) {
284 - return new RunAgainIn( 50 );
285 - }
286 - pFocusable.setFocus( true );
287 - return null;
288 - }
289 - }, 10 );
290 - }
291 - }
1 + // This Source Code is in the Public Domain per: http://unlicense.org
2 + package org.litesoft.GWT.forms.client.components.nonpublic;
3 +
4 + import org.litesoft.GWT.client.*;
5 + import org.litesoft.GWT.forms.client.components.*;
6 + import org.litesoft.commonfoundation.base.*;
7 + import org.litesoft.core.delayed.*;
8 +
9 + import com.google.gwt.user.client.*;
10 + import com.google.gwt.user.client.ui.*;
11 +
12 + import java.util.*;
13 +
14 + public abstract class AbstractFormElement extends Composite implements IFormComponent {
15 + public static final String BASE_STYLE_NAME = "litesoft-FormComponent";
16 + public static final String FORM_COMPONENT_LABEL_STYLE = "litesoft-FormComponentLabel";
17 + public static final String FORM_COMPONENT_LABEL_LEFT_STYLE = "litesoft-FormComponentLabelLeft";
18 +
19 + protected String mCurStyleState = "";
20 + protected boolean mChanged = false;
21 + protected boolean mFocused = false;
22 + protected boolean mEnabled = true;
23 + private Set<Listener> mFormComponentListeners = new HashSet<Listener>();
24 + protected FormHTMLpanel mInnerHTMLpanel; // Element is either a DIV / FieldSet
25 + protected String mFieldLabel, mTooltip;
26 +
27 + protected AbstractFormElement( Element pElement ) {
28 + VerticalPanel zConstrainingTable = new VerticalPanel();
29 + initWidget( zConstrainingTable );
30 + zConstrainingTable.add( mInnerHTMLpanel = new FormHTMLpanel( pElement ) );
31 + super.setStyleName( BASE_STYLE_NAME );
32 + }
33 +
34 + protected void initializeHTML( String pHTML ) {
35 + mInnerHTMLpanel.setHTML( pHTML );
36 + }
37 +
38 + // UIObject
39 +
40 + @Override
41 + public void setStyleName( String style ) {
42 + throw new IllegalStateException( "Can not use setStyleName, use addStyleName & removeStyleName" );
43 + }
44 +
45 + @Override
46 + protected Element getStyleElement() {
47 + return mInnerHTMLpanel.getElement();
48 + }
49 +
50 + abstract public void simulateUserTextEntry( String pText );
51 +
52 + public String getFieldLabel() {
53 + return mFieldLabel;
54 + }
55 +
56 + public String getTooltip() {
57 + return mTooltip;
58 + }
59 +
60 + /**
61 + * Not supported
62 + *
63 + * @throws UnsupportedOperationException
64 + */
65 + @Override
66 + public void setTitle( String title )
67 + throws UnsupportedOperationException {
68 + throw new UnsupportedOperationException();
69 + }
70 +
71 + // IFormComponent
72 +
73 + @Override
74 + public boolean hasError() {
75 + return false;
76 + }
77 +
78 + @Override
79 + public void setError( String pError )
80 + throws UnsupportedOperationException {
81 + if ( Currently.isNotNullOrEmpty( pError ) ) {
82 + throw new UnsupportedOperationException();
83 + }
84 + }
85 +
86 + @Override
87 + public void setFocused( boolean pFocused ) {
88 + mFocused = pFocused;
89 + stateChanged();
90 + }
91 +
92 + @Override
93 + public void setValueChanged( boolean pChanged ) {
94 + mChanged = pChanged;
95 + stateChanged();
96 + }
97 +
98 + @Override
99 + public boolean isEnabled() {
100 + return mEnabled;
101 + }
102 +
103 + @Override
104 + public void publishAnyPendingChanges() {
105 + // Do nothing as most FormElements do not do defferred processing
106 + if ( mFocused ) {
107 + LOGGER.error.log( "AbstractFormElement.publishAnyPendingChanges: Focused - ", simpleClassname( this ) );
108 + }
109 + }
110 +
111 + @Override
112 + public IFormComponent addFormComponentListener( Listener pFormComponentListener ) {
113 + if ( pFormComponentListener != null ) {
114 + pFormComponentListener.add( this );
115 + mFormComponentListeners.add( pFormComponentListener );
116 + }
117 + return this;
118 + }
119 +
120 + @Override
121 + public IFormComponent removeFormComponentListener( Listener pFormComponentListener ) {
122 + if ( pFormComponentListener != null ) {
123 + pFormComponentListener.remove( this );
124 + mFormComponentListeners.remove( pFormComponentListener );
125 + }
126 + return this;
127 + }
128 +
129 + // Support...
130 +
131 + protected static String simpleClassname( Object pObject ) {
132 + return ClassName.simple( pObject );
133 + }
134 +
135 + protected void fireBlurListeners() {
136 + trace( "fireBlurListeners" );
137 + for ( Listener zListener : mFormComponentListeners ) {
138 + zListener.blurOccurred();
139 + }
140 + }
141 +
142 + protected void fireChangeListeners() {
143 + trace( "fireChangeListeners" );
144 + for ( Listener zListener : mFormComponentListeners ) {
145 + zListener.changeOccurred();
146 + }
147 + }
148 +
149 + protected void fireFocusListeners() {
150 + trace( "fireFocusListeners" );
151 + for ( Listener zListener : mFormComponentListeners ) {
152 + zListener.focusOccured();
153 + }
154 + }
155 +
156 + protected void fireEnterPressedListeners( KeyboardKeyModifier pModifiers ) {
157 + trace( "fireenterPressedListeners" );
158 + for ( Listener zListener : mFormComponentListeners ) {
159 + zListener.enterPressed( pModifiers );
160 + }
161 + }
162 +
163 + protected boolean hasFocus() {
164 + return mFocused;
165 + }
166 +
167 + protected boolean hasChanged() {
168 + return mChanged;
169 + }
170 +
171 + protected void stateChanged() {
172 + if ( isEnabled() ) {
173 + setEnabledStyles();
174 + } else {
175 + setDisabledStyles();
176 + }
177 + }
178 +
179 + protected void setDisabledStyles() {
180 + StringBuilder style = new StringBuilder( "-Disabled" );
181 + if ( hasError() ) {
182 + style.append( "-Error" );
183 + }
184 + updateStatefulStyleName( style.toString() );
185 + }
186 +
187 + protected void setEnabledStyles() {
188 + StringBuilder style = new StringBuilder();
189 + if ( hasFocus() ) {
190 + style.append( "-Focused" );
191 + }
192 + if ( hasError() ) {
193 + style.append( "-Error" );
194 + }
195 + if ( hasChanged() ) {
196 + style.append( "-Changed" );
197 + }
198 + updateStatefulStyleName( style.toString() );
199 + }
200 +
201 + private void updateStatefulStyleName( String pNewStyleState ) {
202 + if ( !pNewStyleState.equals( mCurStyleState ) ) {
203 + if ( !mCurStyleState.equals( "" ) ) {
204 + removeStyleName( BASE_STYLE_NAME + mCurStyleState );
205 + }
206 + if ( !(mCurStyleState = pNewStyleState).equals( "" ) ) {
207 + addStyleName( BASE_STYLE_NAME + mCurStyleState );
208 + }
209 + }
210 + }
211 +
212 + protected static class FormHTMLpanel extends ComplexPanel {
213 + /**
214 + * Creates an HTML panel with NO specified HTML contents. The element is the wrapping element.
215 + *
216 + * @param pElement either the Div or the FieldSet
217 + */
218 + public FormHTMLpanel( Element pElement ) {
219 + setElement( pElement );
220 + }
221 +
222 + /**
223 + * Injects the specified HTML contents into the HTML panel. Any element within
224 + * this HTML that has a specified id can contain a child widget.
225 + *
226 + * @param pHtml the panel's HTML
227 + */
228 + public void setHTML( String pHtml ) {
229 + DOM.setInnerHTML( getElement(), pHtml );
230 + }
231 +
232 + /**
233 + * Adds a child widget to the panel, contained within the HTML element
234 + * specified by a given id.
235 + *
236 + * @param pWidget the widget to be added
237 + * @param pId the id of the element within which it will be contained
238 + */
239 + public void add( Widget pWidget, String pId ) {
240 + Element elem = getElementById( getElement(), pId );
241 + if ( elem == null ) {
242 + throw new NoSuchElementException( pId );
243 + }
244 +
245 + super.add( pWidget, elem );
246 + }
247 +
248 + /*
249 + * Implements getElementById() downward from the given element. We need to do
250 + * this because {@link #add(Widget, String)} must often be called before the
251 + * panel is attached to the DOM, so {@link Dom#getElementById} won't yet work.
252 + */
253 + private Element getElementById( Element pElement, String pId ) {
254 + String elemId = DOM.getElementProperty( pElement, "id" );
255 + if ( (elemId != null) && elemId.equals( pId ) ) {
256 + return pElement;
257 + }
258 +
259 + Element child = DOM.getFirstChild( pElement );
260 + while ( child != null ) {
261 + Element ret = getElementById( child, pId );
262 + if ( ret != null ) {
263 + return ret;
264 + }
265 + child = DOM.getNextSibling( child );
266 + }
267 +
268 + return null;
269 + }
270 + }
271 +
272 + protected void trace( String pMethod ) {
273 + if ( LOGGER.trace.isEnabled() ) {
274 + LOGGER.trace.log( pMethod + " - " + simpleClassname( this ) );
275 + }
276 + }
277 +
278 + protected void pullFocusTo( final Focusable pFocusable ) {
279 + TimedRunnableManager.INSTANCE.runIn( new TimedRunnable() {
280 + @Override
281 + public Again runOnce() {
282 + if ( !UtilsGwt.isRecursiveVisible( (Widget) pFocusable ) ) {
283 + return new RunAgainIn( 50 );
284 + }
285 + pFocusable.setFocus( true );
286 + return null;
287 + }
288 + }, 10 );
289 + }
290 + }