|
@@ -1,150 +1,150 @@ |
1 |
|
- |
// This Source Code is in the Public Domain per: http://unlicense.org |
2 |
|
- |
package org.litesoft.GWT.forms.client.components.factories.input; |
3 |
|
- |
|
4 |
|
- |
import org.litesoft.GWT.forms.client.components.factories.*; |
5 |
|
- |
import org.litesoft.GWT.forms.client.components.impls.input.*; |
6 |
|
- |
import org.litesoft.GWT.forms.client.nonpublic.*; |
7 |
|
- |
import org.litesoft.core.simpletypes.*; |
8 |
|
- |
import org.litesoft.ui.def.nonpublic.*; |
9 |
|
- |
import org.litesoft.ui.def.nonpublic.support.*; |
10 |
|
- |
import org.litesoft.uispecification.*; |
11 |
|
- |
|
12 |
|
- |
import com.google.gwt.user.client.ui.*; |
13 |
|
- |
|
14 |
|
- |
import java.io.*; |
15 |
|
- |
import java.util.*; |
16 |
|
- |
|
17 |
|
- |
public class UiComboBoxWidgetFactory implements InputWidgetFactory { |
18 |
|
- |
public Widget create( FormInstanceComponentHandler pComponentHandler, // |
19 |
|
- |
UiInputDef pUiDef, AttributeMetaData pMetaData, |
20 |
|
- |
boolean pHasHorizontalPeer ) { |
21 |
|
- |
ComboBoxMetaData zMD = (ComboBoxMetaData) pMetaData; |
22 |
|
- |
|
23 |
|
- |
String zLabelText = !pUiDef.isLabelLess() ? zMD.getExternalText() : null; |
24 |
|
- |
UiFont zLabelFont = pUiDef.getLabelFont(); |
25 |
|
- |
String zTooltip = zMD.getTooltip(); |
26 |
|
- |
|
27 |
|
- |
return new MyFormAdapter( pComponentHandler, zMD, |
28 |
|
- |
createFormComboBox( zLabelText, zLabelFont, zTooltip ) ).init(); |
29 |
|
- |
} |
30 |
|
- |
|
31 |
|
- |
protected FormComboBox createFormComboBox( String pLabelText, UiFont pLabelFont, String pTooltip ) { |
32 |
|
- |
return new FormComboBox( pLabelText, pLabelFont, pTooltip ); |
33 |
|
- |
} |
34 |
|
- |
|
35 |
|
- |
private static final class MyFormAdapter extends AbstractFormRequireableAttributeAdapter { |
36 |
|
- |
private FormComboBox mFC; |
37 |
|
- |
private SimpleKeyValuePair mOptionZero; |
38 |
|
- |
private SimpleKeyValuePair[] mValidOptions; |
39 |
|
- |
private List<Serializable> mNewValidOptions = new ArrayList<Serializable>(); |
40 |
|
- |
private boolean mHasFocus = false; |
41 |
|
- |
|
42 |
|
- |
public MyFormAdapter( FormInstanceComponentHandler pComponentHandler, // |
43 |
|
- |
ComboBoxMetaData pMD, FormComboBox pFC ) { |
44 |
|
- |
super( pComponentHandler, pMD, pMD, pFC ); |
45 |
|
- |
mFC = pFC; |
46 |
|
- |
mFC.addItem( mOptionZero = pMD.getNoSendOptionZero() ); |
47 |
|
- |
mFC.addItems( mValidOptions = pMD.getValidOptions() ); |
48 |
|
- |
} |
49 |
|
- |
|
50 |
|
- |
public void updateValidOptions( Serializable[] pValidOptions ) { |
51 |
|
- |
convertOptions( pValidOptions ); |
52 |
|
- |
if ( !mHasFocus || mFC.areOptionsAlwaysUpdatable() ) { |
53 |
|
- |
LLupdateValidOptions(); |
54 |
|
- |
} |
55 |
|
- |
} |
56 |
|
- |
|
57 |
|
- |
private void convertOptions( Serializable[] pValidOptions ) { |
58 |
|
- |
mNewValidOptions.clear(); |
59 |
|
- |
if ( pValidOptions != null ) { |
60 |
|
- |
for ( Serializable zValidOption : pValidOptions ) { |
61 |
|
- |
SimpleKeyValuePair option = convertOption( zValidOption ); |
62 |
|
- |
if ( option != null ) { |
63 |
|
- |
mNewValidOptions.add( option ); |
64 |
|
- |
} |
65 |
|
- |
} |
66 |
|
- |
} |
67 |
|
- |
} |
68 |
|
- |
|
69 |
|
- |
private void LLupdateValidOptions() { |
70 |
|
- |
if ( !mNewValidOptions.isEmpty() ) { |
71 |
|
- |
// save the currently select & null it if it is the default option zero |
72 |
|
- |
SimpleKeyValuePair selected = convertOption( mFC.getSelectedKeyValuePair() ); |
73 |
|
- |
|
74 |
|
- |
mFC.setSelectedIndex( -1 ); |
75 |
|
- |
|
76 |
|
- |
// Remove all the old options |
77 |
|
- |
for ( SimpleKeyValuePair zValuePair : mValidOptions ) { |
78 |
|
- |
mFC.removeItem( zValuePair ); |
79 |
|
- |
} |
80 |
|
- |
// Add all the new ones & note if old current selected was found |
81 |
|
- |
mValidOptions = new SimpleKeyValuePair[mNewValidOptions.size()]; |
82 |
|
- |
boolean found = false; |
83 |
|
- |
for ( int i = 0; i < mNewValidOptions.size(); i++ ) { |
84 |
|
- |
SimpleKeyValuePair option = (SimpleKeyValuePair) mNewValidOptions.get( i ); |
85 |
|
- |
found |= option.equals( selected ); |
86 |
|
- |
mFC.addItem( mValidOptions[i] = option ); |
87 |
|
- |
} |
88 |
|
- |
mNewValidOptions.clear(); |
89 |
|
- |
|
90 |
|
- |
setComponentCurrentValue( found ? selected : null ); |
91 |
|
- |
} |
92 |
|
- |
} |
93 |
|
- |
|
94 |
|
- |
protected Object getComponentCurrentValue() { |
95 |
|
- |
Object rv = super.getComponentCurrentValue(); |
96 |
|
- |
return !areEqual( rv, mOptionZero ) ? rv : null; |
97 |
|
- |
} |
98 |
|
- |
|
99 |
|
- |
protected void setComponentCurrentValue( Object pCurrentValue ) { |
100 |
|
- |
if ( pCurrentValue == null ) { |
101 |
|
- |
pCurrentValue = mOptionZero; |
102 |
|
- |
} |
103 |
|
- |
super.setComponentCurrentValue( pCurrentValue ); |
104 |
|
- |
} |
105 |
|
- |
|
106 |
|
- |
protected Serializable convertComponentValueToSendable( Object pValue ) { |
107 |
|
- |
return !areEqual( pValue, mOptionZero ) ? (Serializable) pValue : null; |
108 |
|
- |
} |
109 |
|
- |
|
110 |
|
- |
protected Object convertSendableToComponentValue( Serializable pValue ) { |
111 |
|
- |
return convertOption( pValue ); |
112 |
|
- |
} |
113 |
|
- |
|
114 |
|
- |
protected String convertComponentValueToString( Object pValue ) { |
115 |
|
- |
if ( pValue instanceof SimpleKeyValuePair ) { |
116 |
|
- |
return "@" + ((SimpleKeyValuePair) pValue).getKey(); |
117 |
|
- |
} |
118 |
|
- |
return (pValue == null) ? "null" : "?" + pValue; |
119 |
|
- |
} |
120 |
|
- |
|
121 |
|
- |
protected Object convertStringToComponentValue( String pValue ) { |
122 |
|
- |
if ( (pValue == null) || pValue.equals( "null" ) ) { |
123 |
|
- |
return null; |
124 |
|
- |
} |
125 |
|
- |
return pValue.startsWith( "@" ) ? pValue.substring( 1 ) : pValue; |
126 |
|
- |
} |
127 |
|
- |
|
128 |
|
- |
public void focusOccured() { |
129 |
|
- |
super.focusOccured(); |
130 |
|
- |
mHasFocus = true; |
131 |
|
- |
} |
132 |
|
- |
|
133 |
|
- |
public void blurOccurred() { |
134 |
|
- |
mHasFocus = false; |
135 |
|
- |
LLupdateValidOptions(); |
136 |
|
- |
super.blurOccurred(); |
137 |
|
- |
} |
138 |
|
- |
|
139 |
|
- |
private SimpleKeyValuePair convertOption( Serializable pValue ) { |
140 |
|
- |
if ( pValue == null ) { |
141 |
|
- |
return null; |
142 |
|
- |
} |
143 |
|
- |
SimpleKeyValuePair rv = (pValue instanceof SimpleKeyValuePair) ? // |
144 |
|
- |
(SimpleKeyValuePair) pValue : // |
145 |
|
- |
new StringKeyValuePair( pValue.toString() ); |
146 |
|
- |
|
147 |
|
- |
return !areEqual( rv, mOptionZero ) ? rv : null; |
148 |
|
- |
} |
149 |
|
- |
} |
150 |
|
- |
} |
|
1 |
+ |
// This Source Code is in the Public Domain per: http://unlicense.org |
|
2 |
+ |
package org.litesoft.GWT.forms.client.components.factories.input; |
|
3 |
+ |
|
|
4 |
+ |
import org.litesoft.GWT.forms.client.components.factories.*; |
|
5 |
+ |
import org.litesoft.GWT.forms.client.components.impls.input.*; |
|
6 |
+ |
import org.litesoft.GWT.forms.client.nonpublic.*; |
|
7 |
+ |
import org.litesoft.core.simpletypes.*; |
|
8 |
+ |
import org.litesoft.ui.def.nonpublic.*; |
|
9 |
+ |
import org.litesoft.ui.def.nonpublic.support.*; |
|
10 |
+ |
import org.litesoft.uispecification.*; |
|
11 |
+ |
|
|
12 |
+ |
import com.google.gwt.user.client.ui.*; |
|
13 |
+ |
|
|
14 |
+ |
import java.io.*; |
|
15 |
+ |
import java.util.*; |
|
16 |
+ |
|
|
17 |
+ |
public class UiComboBoxWidgetFactory implements InputWidgetFactory { |
|
18 |
+ |
public Widget create( FormInstanceComponentHandler pComponentHandler, // |
|
19 |
+ |
UiInputDef pUiDef, AttributeMetaData pMetaData, |
|
20 |
+ |
boolean pHasHorizontalPeer ) { |
|
21 |
+ |
ComboBoxMetaData zMD = (ComboBoxMetaData) pMetaData; |
|
22 |
+ |
|
|
23 |
+ |
String zLabelText = !pUiDef.isLabelLess() ? zMD.getExternalText() : null; |
|
24 |
+ |
UiFont zLabelFont = pUiDef.getLabelFont(); |
|
25 |
+ |
String zTooltip = zMD.getTooltip(); |
|
26 |
+ |
|
|
27 |
+ |
return new MyFormAdapter( pComponentHandler, zMD, |
|
28 |
+ |
createFormComboBox( zLabelText, zLabelFont, zTooltip ) ).init(); |
|
29 |
+ |
} |
|
30 |
+ |
|
|
31 |
+ |
protected FormComboBox createFormComboBox( String pLabelText, UiFont pLabelFont, String pTooltip ) { |
|
32 |
+ |
return new FormComboBox( pLabelText, pLabelFont, pTooltip ); |
|
33 |
+ |
} |
|
34 |
+ |
|
|
35 |
+ |
private static final class MyFormAdapter extends AbstractFormRequireableAttributeAdapter { |
|
36 |
+ |
private FormComboBox mFC; |
|
37 |
+ |
private SimpleKeyValuePair mOptionZero; |
|
38 |
+ |
private SimpleKeyValuePair[] mValidOptions; |
|
39 |
+ |
private List<Serializable> mNewValidOptions = new ArrayList<Serializable>(); |
|
40 |
+ |
private boolean mHasFocus = false; |
|
41 |
+ |
|
|
42 |
+ |
public MyFormAdapter( FormInstanceComponentHandler pComponentHandler, // |
|
43 |
+ |
ComboBoxMetaData pMD, FormComboBox pFC ) { |
|
44 |
+ |
super( pComponentHandler, pMD, pMD, pFC ); |
|
45 |
+ |
mFC = pFC; |
|
46 |
+ |
mFC.addItem( mOptionZero = pMD.getNoSendOptionZero() ); |
|
47 |
+ |
mFC.addItems( mValidOptions = pMD.getValidOptions() ); |
|
48 |
+ |
} |
|
49 |
+ |
|
|
50 |
+ |
public void updateValidOptions( Serializable[] pValidOptions ) { |
|
51 |
+ |
convertOptions( pValidOptions ); |
|
52 |
+ |
if ( !mHasFocus || mFC.areOptionsAlwaysUpdatable() ) { |
|
53 |
+ |
LLupdateValidOptions(); |
|
54 |
+ |
} |
|
55 |
+ |
} |
|
56 |
+ |
|
|
57 |
+ |
private void convertOptions( Serializable[] pValidOptions ) { |
|
58 |
+ |
mNewValidOptions.clear(); |
|
59 |
+ |
if ( pValidOptions != null ) { |
|
60 |
+ |
for ( Serializable zValidOption : pValidOptions ) { |
|
61 |
+ |
SimpleKeyValuePair option = convertOption( zValidOption ); |
|
62 |
+ |
if ( option != null ) { |
|
63 |
+ |
mNewValidOptions.add( option ); |
|
64 |
+ |
} |
|
65 |
+ |
} |
|
66 |
+ |
} |
|
67 |
+ |
} |
|
68 |
+ |
|
|
69 |
+ |
private void LLupdateValidOptions() { |
|
70 |
+ |
if ( !mNewValidOptions.isEmpty() ) { |
|
71 |
+ |
// save the currently select & null it if it is the default option zero |
|
72 |
+ |
SimpleKeyValuePair selected = convertOption( mFC.getSelectedKeyValuePair() ); |
|
73 |
+ |
|
|
74 |
+ |
mFC.setSelectedIndex( -1 ); |
|
75 |
+ |
|
|
76 |
+ |
// Remove all the old options |
|
77 |
+ |
for ( SimpleKeyValuePair zValuePair : mValidOptions ) { |
|
78 |
+ |
mFC.removeItem( zValuePair ); |
|
79 |
+ |
} |
|
80 |
+ |
// Add all the new ones & note if old current selected was found |
|
81 |
+ |
mValidOptions = new SimpleKeyValuePair[mNewValidOptions.size()]; |
|
82 |
+ |
boolean found = false; |
|
83 |
+ |
for ( int i = 0; i < mNewValidOptions.size(); i++ ) { |
|
84 |
+ |
SimpleKeyValuePair option = (SimpleKeyValuePair) mNewValidOptions.get( i ); |
|
85 |
+ |
found |= option.equals( selected ); |
|
86 |
+ |
mFC.addItem( mValidOptions[i] = option ); |
|
87 |
+ |
} |
|
88 |
+ |
mNewValidOptions.clear(); |
|
89 |
+ |
|
|
90 |
+ |
setComponentCurrentValue( found ? selected : null ); |
|
91 |
+ |
} |
|
92 |
+ |
} |
|
93 |
+ |
|
|
94 |
+ |
protected Object getComponentCurrentValue() { |
|
95 |
+ |
Object rv = super.getComponentCurrentValue(); |
|
96 |
+ |
return !areEqual( rv, mOptionZero ) ? rv : null; |
|
97 |
+ |
} |
|
98 |
+ |
|
|
99 |
+ |
protected void setComponentCurrentValue( Object pCurrentValue ) { |
|
100 |
+ |
if ( pCurrentValue == null ) { |
|
101 |
+ |
pCurrentValue = mOptionZero; |
|
102 |
+ |
} |
|
103 |
+ |
super.setComponentCurrentValue( pCurrentValue ); |
|
104 |
+ |
} |
|
105 |
+ |
|
|
106 |
+ |
protected Serializable convertComponentValueToSendable( Object pValue ) { |
|
107 |
+ |
return !areEqual( pValue, mOptionZero ) ? (Serializable) pValue : null; |
|
108 |
+ |
} |
|
109 |
+ |
|
|
110 |
+ |
protected Object convertSendableToComponentValue( Serializable pValue ) { |
|
111 |
+ |
return convertOption( pValue ); |
|
112 |
+ |
} |
|
113 |
+ |
|
|
114 |
+ |
protected String convertComponentValueToString( Object pValue ) { |
|
115 |
+ |
if ( pValue instanceof SimpleKeyValuePair ) { |
|
116 |
+ |
return "@" + ((SimpleKeyValuePair) pValue).getKey(); |
|
117 |
+ |
} |
|
118 |
+ |
return (pValue == null) ? "null" : "?" + pValue; |
|
119 |
+ |
} |
|
120 |
+ |
|
|
121 |
+ |
protected Object convertStringToComponentValue( String pValue ) { |
|
122 |
+ |
if ( (pValue == null) || pValue.equals( "null" ) ) { |
|
123 |
+ |
return null; |
|
124 |
+ |
} |
|
125 |
+ |
return pValue.startsWith( "@" ) ? pValue.substring( 1 ) : pValue; |
|
126 |
+ |
} |
|
127 |
+ |
|
|
128 |
+ |
public void focusOccured() { |
|
129 |
+ |
super.focusOccured(); |
|
130 |
+ |
mHasFocus = true; |
|
131 |
+ |
} |
|
132 |
+ |
|
|
133 |
+ |
public void blurOccurred() { |
|
134 |
+ |
mHasFocus = false; |
|
135 |
+ |
LLupdateValidOptions(); |
|
136 |
+ |
super.blurOccurred(); |
|
137 |
+ |
} |
|
138 |
+ |
|
|
139 |
+ |
private SimpleKeyValuePair convertOption( Serializable pValue ) { |
|
140 |
+ |
if ( pValue == null ) { |
|
141 |
+ |
return null; |
|
142 |
+ |
} |
|
143 |
+ |
SimpleKeyValuePair rv = (pValue instanceof SimpleKeyValuePair) ? // |
|
144 |
+ |
(SimpleKeyValuePair) pValue : // |
|
145 |
+ |
new StringKeyValuePair( pValue.toString() ); |
|
146 |
+ |
|
|
147 |
+ |
return !areEqual( rv, mOptionZero ) ? rv : null; |
|
148 |
+ |
} |
|
149 |
+ |
} |
|
150 |
+ |
} |