Subversion Repository Public Repository

litesoft

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.GWT.forms.client.components.impls.input;

import java.util.*;

import org.litesoft.GWT.client.*;
import org.litesoft.GWT.client.widgets.*;
import org.litesoft.GWT.forms.client.components.*;
import org.litesoft.GWT.forms.client.components.nonpublic.*;
import org.litesoft.core.simpletypes.*;
import org.litesoft.core.typeutils.*;
import org.litesoft.ui.*;
import org.litesoft.uispecification.*;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.user.client.ui.*;

public class FormComboBox extends AbstractFocusWidgetErrorableFormElement
{
    public FormComboBox( String pFieldLabel, UiFont pLabelFont, String pTooltip, FormComboBoxFocusWidget pComboBox, boolean pAddStyle )
    {
        super( pComboBox, pTooltip, "frmCmbBox", pAddStyle );
        initialize( pFieldLabel, pLabelFont, pTooltip );
    }

    public FormComboBox( String pFieldLabel, UiFont pLabelFont, String pTooltip, SimpleKeyValuePair pOptionZero )
    {
        this( pFieldLabel, pLabelFont, pTooltip, new NativeFormComboBox(), false );
        addItem( pOptionZero );
    }

    public FormComboBox( String pFieldLabel, UiFont pLabelFont, String pTooltip, String pOptionZero )
    {
        this( pFieldLabel, pLabelFont, pTooltip, convertToKeyValuePair( pOptionZero ) );
    }

    public FormComboBox( String pFieldLabel, UiFont pLabelFont, String pTooltip )
    {
        this( pFieldLabel, pLabelFont, pTooltip, (SimpleKeyValuePair) null );
    }

    @Override
    protected void initialize( String pFieldLabel, UiFont pLabelFont, String pTooltip )
    {
        super.initialize( pFieldLabel, pLabelFont, pTooltip );

        getComboBox().addChangeHandler( new ChangeHandler()
        {
            @Override
            public void onChange( ChangeEvent event )
            {
                fireChangeListeners();
            }
        } );
    }

    // SizeManaged

    @Override
    public void relayout()
    {
        Widget parent = getParent();
        if ( parent instanceof SizeManaged )
        {
            ((SizeManaged) parent).relayout();
        }
    }

    // IFormComponent

    @Override
    public Object getCurrentValue()
    {
        return getSelectedKeyValuePair();
    }

    @Override
    public void setCurrentValue( Object pNewValue )
    {
        if ( pNewValue != null )
        {
            if ( pNewValue instanceof String )
            {
                setSelected( (String) pNewValue );
                return;
            }
            if ( pNewValue instanceof SimpleKeyValuePair )
            {
                setSelected( (SimpleKeyValuePair) pNewValue );
                return;
            }
            throw new IllegalArgumentException( "Not a String or a SimpleKeyValuePair, but a '" + pNewValue.getClass() + "': " + pNewValue );
        }
    }

    // Support...

    @Override
    public void simulateUserTextEntry( String pText )
    {
        int zCurrentSelected = getSelectedIndex();
        setSelectedIndex( determineItemToSelect( Strings.deNull( pText ).trim() ) );
        if ( zCurrentSelected != getSelectedIndex() )
        {
            fireChangeListeners();
        }
        pullFocusTo( (Focusable) getComboBox() );
    }

    private int determineItemToSelect( String pText )
    {
        FormComboBoxFocusWidget zCB = getComboBox();
        for ( int zIndex = zCB.getItemCount(); --zIndex >= 0; )
        {
            String zValue = zCB.getValue( zIndex );
            String zKeyPairValue = mManager.getPairBySubmitValue( zValue ).toStringValue();
            if ( pText.equals( Strings.deNull( zKeyPairValue ).trim() ) )
            {
                return zIndex;
            }
        }
        throw new IllegalArgumentException( "simulateUserTextEntry on '" + mFieldLabel + "' to:" + pText );
    }

    public FormComboBox addItems( String... pKeyAndDisplayValues )
    {
        if ( pKeyAndDisplayValues != null )
        {
            for ( String zKeyAndDisplayValue : pKeyAndDisplayValues )
            {
                addItem( zKeyAndDisplayValue );
            }
        }
        return this;
    }

    public FormComboBox addItems( SimpleKeyValuePair... pKeyValuePairs )
    {
        if ( pKeyValuePairs != null )
        {
            for ( SimpleKeyValuePair zKeyValuePair : pKeyValuePairs )
            {
                addItem( zKeyValuePair );
            }
        }
        return this;
    }

    public void setSelected( String pKeyAndDisplayValue )
    {
        setSelected( convertToKeyValuePair( pKeyAndDisplayValue ) );
    }

    public void addItem( String pKeyAndDisplayValue )
    {
        addItem( convertToKeyValuePair( pKeyAndDisplayValue ) );
    }

    public void removeItem( String pKeyAndDisplayValue )
    {
        removeItem( convertToKeyValuePair( pKeyAndDisplayValue ) );
    }

    public void setSelected( String pKey, String pDisplayValue )
    {
        setSelected( convertToKeyValuePair( pKey, pDisplayValue ) );
    }

    public void addItem( String pKey, String pDisplayValue )
    {
        addItem( convertToKeyValuePair( pKey, pDisplayValue ) );
    }

    public void removeItem( String pKey, String pDisplayValue )
    {
        removeItem( convertToKeyValuePair( pKey, pDisplayValue ) );
    }

    public void setSelected( SimpleKeyValuePair pNewSelectedKeyValuePair )
    {
        int zIx = -1;
        if ( pNewSelectedKeyValuePair != null )
        {
            if ( -1 == (zIx = indexOf( pNewSelectedKeyValuePair )) )
            {
                addItem( pNewSelectedKeyValuePair );
                zIx = indexOf( pNewSelectedKeyValuePair );
            }
        }
        setSelectedIndex( zIx );
    }

    public void addItem( SimpleKeyValuePair pKeyValuePair )
    {
        if ( pKeyValuePair != null )
        {
            String zComboBoxDisplayValue = pKeyValuePair.toStringValue();
            String zComboBoxSubmitValue = "" + (++mLastComboBoxSubmitValue);
            addItemToComboBox( zComboBoxDisplayValue, zComboBoxSubmitValue );
            mManager.add( zComboBoxSubmitValue, pKeyValuePair );
        }
    }

    public void removeItem( SimpleKeyValuePair pKeyValuePair )
    {
        int at = indexOf( pKeyValuePair );
        if ( at != -1 )
        {
            removeItemFromComboBox( at );
            mManager.remove( pKeyValuePair );
        }
    }

    public SimpleKeyValuePair getSelectedKeyValuePair()
    {
        int zIx = getSelectedIndex();
        if ( zIx == -1 )
        {
            return null;
        }
        String zSelectedSubmitValue = getSubmitValueFromComboBox( zIx );
        return mManager.getPairBySubmitValue( zSelectedSubmitValue );
    }

    private int mLastComboBoxSubmitValue = 0;
    private PairSubmitValueManager mManager = new PairSubmitValueManager();

    private int indexOf( SimpleKeyValuePair pKeyValuePair )
    {
        if ( pKeyValuePair != null )
        {
            String zComboBoxSubmitValue = mManager.getSubmitValueByPair( pKeyValuePair );
            if ( zComboBoxSubmitValue != null )
            {
                int zItemCount = getItemCountFromComboBox();
                for ( int i = 0; i < zItemCount; i++ )
                {
                    String zSubmitValue = getSubmitValueFromComboBox( i );
                    if ( zComboBoxSubmitValue.equals( zSubmitValue ) )
                    {
                        return i;
                    }
                }
            }
        }
        return -1;
    }

    public int getSelectedIndex()
    {
        try
        {
            return getComboBox().getSelectedIndex();
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "getSelectedIndex", e );
            return -1;
        }
    }

    public void setSelectedIndex( int pSelectedIndex )
    {
        try
        {
            getComboBox().setSelectedIndex( pSelectedIndex );
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "setSelectedIndex", e );
        }
    }

    private int getItemCountFromComboBox()
    {
        try
        {
            return getComboBox().getItemCount();
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "getItemCountFromComboBox", e );
            return 0;
        }
    }

    private void addItemToComboBox( String pComboBoxDisplayValue, String pComboBoxSubmitValue )
    {
        try
        {
            getComboBox().addItem( pComboBoxDisplayValue, pComboBoxSubmitValue );
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "addItemToComboBox", e );
        }
    }

    private void removeItemFromComboBox( int pAt )
    {
        try
        {
            getComboBox().removeItem( pAt );
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "removeItemFromComboBox", e );
        }
    }

    private String getSubmitValueFromComboBox( int pAt )
    {
        try
        {
            return getComboBox().getValue( pAt );
        }
        catch ( RuntimeException e )
        {
            errorFromComboBox( "getSubmitValueFromComboBox", e );
            return null;
        }
    }

    public FormComboBoxFocusWidget getComboBox()
    {
        return (FormComboBoxFocusWidget) mInputWidget;
    }

    public boolean areOptionsAlwaysUpdatable()
    {
        return (mInputWidget instanceof ComboBoxOptionsAlwaysUpdatable);
    }

    private void errorFromComboBox( String pWhat, RuntimeException pRTE )
    {
        String zComboBoxType = Objects.justClassNameOf( mInputWidget );
        String zBody = "There was a problem with the FormComboBox\n" +//
                       "    interacting with " + zComboBoxType + "\n" +//
                       "       thru " + pWhat;
        AlertManager.alert( "FormComboBox", "Error", zBody, pRTE );
    }

    private static SimpleKeyValuePair convertToKeyValuePair( String pKeyAndDisplayValue )
    {
        return (pKeyAndDisplayValue != null) ? convertToKeyValuePair( pKeyAndDisplayValue, pKeyAndDisplayValue ) : null;
    }

    private static SimpleKeyValuePair convertToKeyValuePair( String pKey, String pDisplayValue )
    {
        return new StringKeyValuePair( pKey, pDisplayValue );
    }

    private static class PairSubmitValueManager
    {
        private Map mComboBoxSubmitValuesToPairs = new HashMap();
        private Map mPairKeysToComboBoxSubmitValues = new HashMap();

        public void remove( SimpleKeyValuePair pKeyValuePair )
        {
            String zComboBoxSubmitValue = (String) mPairKeysToComboBoxSubmitValues.remove( pKeyValuePair.getKey() );
            mComboBoxSubmitValuesToPairs.remove( zComboBoxSubmitValue );
        }

        public void add( String pComboBoxSubmitValue, SimpleKeyValuePair pKeyValuePair )
        {
            //noinspection unchecked
            mPairKeysToComboBoxSubmitValues.put( pKeyValuePair.getKey(), pComboBoxSubmitValue );
            //noinspection unchecked
            mComboBoxSubmitValuesToPairs.put( pComboBoxSubmitValue, pKeyValuePair );
        }

        public SimpleKeyValuePair getPairBySubmitValue( String pComboBoxSubmitValue )
        {
            return (SimpleKeyValuePair) mComboBoxSubmitValuesToPairs.get( pComboBoxSubmitValue );
        }

        public String getSubmitValueByPair( SimpleKeyValuePair pKeyValuePair )
        {
            return (String) mPairKeysToComboBoxSubmitValues.get( pKeyValuePair.getKey() );
        }
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/org/litesoft/GWT/forms/client/components/impls/input/FormComboBox.java

Diff revisions: vs.
Revision Author Commited Message
811 Diff Diff GeorgeS picture GeorgeS Sat 18 Aug, 2012 13:45:18 +0000
802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

23 Diff Diff GeorgeS picture GeorgeS Wed 24 Feb, 2010 00:34:32 +0000
2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000