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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt
package org.litesoft.GWT.forms.client.components.impls.input;

import org.litesoft.commonfoundation.html.*;
import org.litesoft.commonfoundation.typeutils.*;

import java.util.*;

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

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

public class FormRadioGroup extends AbstractFormElement implements ChangeHandler
{
    protected final InfoIndicator mInfoIndicator = new InfoIndicator();

    public FormRadioGroup( String pGroupLabel, UiFont pLabelFont, String pTooltip, SimpleKeyValuePair... pOptions )
    {
        super( DOM.createFieldSet() );
        mFieldLabel = pGroupLabel;
        mTooltip = pTooltip;
        int zInstanceNumber = StaticSimpleIdSource.getNext();

        mInfoIndicator.setTooltip( Strings.deNull( pTooltip ).trim() );

        pGroupLabel = Strings.noEmpty( pGroupLabel );

        String zGroupName = "RadioGroup" + zInstanceNumber;
        String legendId = "lgnd" + zInstanceNumber;
        String legendHTML = (pGroupLabel == null) ? "" : "<legend id=" + legendId + " class='litesoft-FormComponentRadioGroupLegend'" + ApplyFont.toHtmlStyle( pLabelFont ) + "><img src='common/images/misc/TransparentSpacer_h18.gif'>" + pGroupLabel + "</legend>";
        String html = legendHTML + "<div id='" + zGroupName + "'></div>";

        initializeHTML( html );
        if ( (legendHTML.length() != 0) && mInfoIndicator.mCanShow )
        {
            mInnerHTMLpanel.add( mInfoIndicator, legendId );
        }

        if ( pOptions != null )
        {
            for ( SimpleKeyValuePair option : pOptions )
            {
                if ( option != null )
                {
                    RadioButtonProxy radioButtonProxy = new RadioButtonProxy( zGroupName, option, this );
                    mRadioButtons.add( radioButtonProxy );
                    mInnerHTMLpanel.add( radioButtonProxy, zGroupName );
                }
            }
        }
        if ( mRadioButtons.size() < 2 )
        {
            throw new IllegalArgumentException( "Radio Groups Must have at least two options" );
        }
        setDefaultSelection();
    }

    public FormRadioGroup( String pGroupLabel, UiFont pLabelFont, String pTooltip, String... pOptions )
    {
        this( pGroupLabel, pLabelFont, pTooltip, convertToKeyValuePairs( pOptions ) );
    }

    private ArrayList<RadioButtonProxy> mRadioButtons = new ArrayList<RadioButtonProxy>();

    private RadioButtonProxy getRadioButon( int pI )
    {
        return mRadioButtons.get( pI );
    }

    // Changehandler

    @Override
    public void onChange( ChangeEvent event ) // possibleStateChange
    {
        if ( mDelayedStateChecker == null )
        {
            mDelayedStateChecker = new DelayedStateChecker();
        }
        Scheduler.get().scheduleDeferred( mDelayedStateChecker.getCommandToDefer() );
    }

    // SizeManaged

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

    // IFormComponent

    @Override
    public boolean setFocus()
    {
        pullFocusTo( getRadioButon( 0 ) );
        return true;
    }

    @Override
    public Object getCurrentValue()
    {
        return (mLastSelection != null) ? mLastSelection.getKeyValuePair() : setDefaultSelection();
    }

    @Override
    public void setCurrentValue( Object pNewValue )
    {
        if ( pNewValue != null )
        {
            setSelected( getProxyFor( pNewValue ) );
        }
    }

    @Override
    public void setEnabled( boolean pEnabled )
    {
        mEnabled = pEnabled;
        for ( int i = 0; i < mRadioButtons.size(); i++ )
        {
            RadioButtonProxy radioButtonProxy = getRadioButon( i );
            radioButtonProxy.setEnabled( pEnabled );
        }
        stateChanged();
    }

    // AbstractFormElement

    @Override
    protected void setEnabledStyles()
    {
        super.setEnabledStyles();
        mInfoIndicator.setVisible( hasFocus() );
    }

    @Override
    public void publishAnyPendingChanges()
    {
        FocusSelection zFS = determineFocusSelection();
        RadioButtonProxy prevLastSelection = mLastSelection;
        if ( zFS.getSelected() != null )
        {
            setLastSelected( zFS.getSelected() );
        }
        RadioButtonProxy curLastSelection = mLastSelection;
        if ( prevLastSelection != curLastSelection )
        {
            fireChangeListeners();
        }
        if ( mReportedFocus )
        {
            mReportedFocus = false;
            fireBlurListeners();
        }
        super.publishAnyPendingChanges();
    }

    // Support...

    @Override
    public void simulateUserTextEntry( String pText )
    {
        RadioButtonProxy zCurrentSelection = mLastSelection;
        setLastSelected( determineItemToSelect( Strings.deNull( pText ).trim() ) );
        if ( zCurrentSelection != mLastSelection )
        {
            mLastSelection.setChecked();
            fireChangeListeners();
        }
        pullFocusTo( mLastSelection );
    }

    private RadioButtonProxy determineItemToSelect( String pText )
    {
        for ( int i = 0; i < mRadioButtons.size(); i++ )
        {
            RadioButtonProxy radioButtonProxy = getRadioButon( i );
            if ( pText.equals( radioButtonProxy.getKeyValuePair().toStringValue() ) )
            {
                return radioButtonProxy;
            }
        }
        throw new IllegalArgumentException( "simulateUserTextEntry on '" + mFieldLabel + "' to:" + pText );
    }

    //vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv//

    private RadioButtonProxy getProxyFor( Object pNewValue )
    {
        Object pairKey;
        if ( pNewValue instanceof SimpleKeyValuePair )
        {
            pairKey = ((SimpleKeyValuePair) pNewValue).getKey();
        }
        else if ( pNewValue instanceof String )
        {
            pairKey = pNewValue;
        }
        else
        {
            pairKey = "?" + pNewValue;
        }
        for ( int i = 0; i < mRadioButtons.size(); i++ )
        {
            RadioButtonProxy radioButtonProxy = getRadioButon( i );
            if ( pairKey.equals( radioButtonProxy.getKeyValuePair().getKey() ) )
            {
                return radioButtonProxy;
            }
        }
        throw new IllegalArgumentException( "No Valid Option in Radio Group of: " + pNewValue );
    }

    private SimpleKeyValuePair setDefaultSelection()
    {
        SimpleKeyValuePair rv = getRadioButon( 0 ).getKeyValuePair();
        setCurrentValue( rv );
        return rv;
    }

    private void setSelected( RadioButtonProxy pRadioButtonProxy )
    {
        setInitialSelection( pRadioButtonProxy );
        setLastSelected( pRadioButtonProxy );
        /*
         * Work around for IE bug where you can select multiple radio buttons
         * when not attached (part 1)
         */
        if ( isAttached() )
        {
            pRadioButtonProxy.setChecked();
        }
    }

    @Override
    protected void onLoad()
    {
        super.onLoad();
        /*
         * Work around for IE bug where you can select multiple radio buttons
         * when not attached (part 2)
         */
        if ( mLastSelection != null )
        {
            mLastSelection.setChecked();
        }
    }

    private FocusSelection determineFocusSelection()
    {
        boolean anyFocus = false;
        RadioButtonProxy selected = null;
        for ( int i = 0; i < mRadioButtons.size(); i++ )
        {
            RadioButtonProxy radioButtonProxy = getRadioButon( i );
            if ( radioButtonProxy.isChecked() )
            {
                selected = radioButtonProxy;
            }
            anyFocus |= radioButtonProxy.hasFocus();
        }
        return new FocusSelection( anyFocus, selected );
    }

    private static class FocusSelection
    {
        private boolean mFocused;
        private RadioButtonProxy mSelected;

        public FocusSelection( boolean pFocused, RadioButtonProxy pSelected )
        {
            mFocused = pFocused;
            mSelected = pSelected;
        }

        public boolean isFocused()
        {
            return mFocused;
        }

        public RadioButtonProxy getSelected()
        {
            return mSelected;
        }
    }

    public void publishAnyDelayedChanges()
    {
        if ( mPendingReentries++ != 0 )
        {
            return;
        }
        do
        {
            FocusSelection zFS = determineFocusSelection();
            boolean shouldIssueBlur = false;
            if ( zFS.isFocused() != mReportedFocus )
            {
                if ( zFS.isFocused() )
                {
                    mReportedFocus = true;
                    fireFocusListeners();
                }
                else
                {
                    shouldIssueBlur = true;
                }
            }
            RadioButtonProxy prevLastSelection = mLastSelection;
            if ( zFS.getSelected() != null )
            {
                setLastSelected( zFS.getSelected() );
            }
            RadioButtonProxy curLastSelection = mLastSelection;
            if ( prevLastSelection != curLastSelection )
            {
                fireChangeListeners();
            }
            if ( shouldIssueBlur )
            {
                mReportedFocus = false;
                fireBlurListeners();
            }
        }
        while ( --mPendingReentries > 0 );
    }

    private int mPendingReentries = 0;
    private boolean mReportedFocus = false;
    private RadioButtonProxy mInitialSelection = null;
    private RadioButtonProxy mLastSelection = null;

    private void setInitialSelection( RadioButtonProxy pInitialSelection )
    {
        RadioButtonProxy prev = mInitialSelection;
        switchChangeFromTo( prev, mInitialSelection = pInitialSelection );
    }

    private RadioButtonProxy setLastSelected( RadioButtonProxy pSelected )
    {
        return mLastSelection = switchChangeFromTo( mLastSelection, pSelected );
    }

    private RadioButtonProxy switchChangeFromTo( RadioButtonProxy pPrev, RadioButtonProxy pNew )
    {
        if ( pPrev != pNew )
        {
            if ( pPrev != null )
            {
                pPrev.removeChangeIndicator();
            }
            if ( pNew != null )
            {
                if ( pNew != mInitialSelection )
                {
                    mInitialSelection.addChangeIndicator();
                    pNew.addChangeIndicator();
                }
                else
                {
                    mInitialSelection.removeChangeIndicator();
                    pNew.removeChangeIndicator();
                }
            }
        }
        return pNew;
    }

    //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^//

    private DelayedStateChecker mDelayedStateChecker = null;

    private class DelayedStateChecker implements Command
    {
        private int mPendingDelays = 0;

        public Command getCommandToDefer()
        {
            mPendingDelays++;
            return this;
        }

        @Override
        public void execute()
        {
            if ( --mPendingDelays == 0 )
            {
                publishAnyDelayedChanges();
            }
        }
    }

    private static class RadioButtonProxy extends Composite implements Focusable,
                                                                       FocusHandler,
                                                                       BlurHandler,
                                                                       ClickHandler
    {
        private SimpleKeyValuePair mKeyValuePair;
        private ChangeHandler mChangeHandler;
        private RadioButton mRadioButton;
        private boolean mHasFocus = false;
        private boolean mChangeIndicated = false;

        public RadioButtonProxy( String pRadioGroupName, SimpleKeyValuePair pKeyValuePair, ChangeHandler pChangeHandler )
        {
            mKeyValuePair = pKeyValuePair;
            mChangeHandler = pChangeHandler;
            String zLabel = HTMLize.escapeNoWrap( pKeyValuePair.toStringValue() );
            mRadioButton = new RadioButton( pRadioGroupName, zLabel, true );
            //noinspection GWTStyleCheck
            mRadioButton.setStyleName( "frmRadioBtn" );

            initWidget( buildComponentPanel() );

            mRadioButton.addFocusHandler( this );
            mRadioButton.addBlurHandler( this );
            mRadioButton.addClickHandler( this );

//            setStyleName( BASE_STYLE_NAME );
        }

        private HTMLPanel buildComponentPanel()
        {
            String inputId = "input" + StaticSimpleIdSource.getNext();
            String html = "<nowrap><table cellpadding='0' cellspacing='0'><tr><td id='" + inputId + "'></td></tr></table></nowrap>";

            HTMLPanel panel = new HTMLPanel( html );
            panel.add( mRadioButton, inputId );
            return panel;
        }

        @Override
        public int getTabIndex()
        {
            return mRadioButton.getTabIndex();
        }

        @Override
        public void setTabIndex( int index )
        {
            mRadioButton.setTabIndex( index );
        }

        @Override
        public void setAccessKey( char key )
        {
            mRadioButton.setAccessKey( key );
        }

        @Override
        public void setFocus( boolean pFocus )
        {
            mRadioButton.setFocus( pFocus );
        }

        @Override
        public void onFocus( FocusEvent event )
        {
            // System.out.println( "RadioButtonProxy.onFocus: " + mKeyValuePair );
            mHasFocus = true;
            mChangeHandler.onChange( null );
        }

        @Override
        public void onBlur( BlurEvent event )
        {
            // System.out.println( "RadioButtonProxy.onLostFocus: " + mKeyValuePair );
            mHasFocus = false;
            mChangeHandler.onChange( null );
        }

        @Override
        public void onClick( ClickEvent event )
        {
            // System.out.println( "RadioButtonProxy.onClick: " + mKeyValuePair );
            mChangeHandler.onChange( null );
        }

        public SimpleKeyValuePair getKeyValuePair()
        {
            return mKeyValuePair;
        }

        public void setChecked()
        {
            mRadioButton.setValue( true );
        }

        public void setEnabled( boolean pEnabled )
        {
            mRadioButton.setEnabled( pEnabled );
        }

        public boolean isChecked()
        {
            return mRadioButton.getValue();
        }

        public boolean hasFocus()
        {
            return mHasFocus;
        }

        public void addChangeIndicator()
        {
            if ( !mChangeIndicated )
            {
                mChangeIndicated = true;
                //noinspection GWTStyleCheck
                mRadioButton.addStyleName( "frmRadioBtn-Changed" );
            }
        }

        public void removeChangeIndicator()
        {
            if ( mChangeIndicated )
            {
                mChangeIndicated = false;
                //noinspection GWTStyleCheck
                mRadioButton.removeStyleName( "frmRadioBtn-Changed" );
            }
        }

        @Override
        public String toString()
        {
            return "RadioButtonProxy: " + mKeyValuePair.toStringValue();
        }
    }

    private static class InfoIndicator extends Widget
    {
        private Element iconImg = DOM.createImg();
        private boolean mCanShow = false;

        public InfoIndicator()
        {
            Element span = DOM.createSpan();
//            DOM.setElementProperty( span, "className", "litesoft-FormComponentIndicatorContainer" );
            setElement( span );

            CommonElementHelper.setImgSrc( iconImg, TransparentImage.URL );
            CommonElementHelper.setStyleClass( iconImg, "litesoft-FormComponentIndicator" );
            DOM.appendChild( span, iconImg );

            setVisible( false );
        }

        public void setTooltip( String pTooltip )
        {
            mCanShow = (pTooltip.length() != 0);
            DOM.setElementProperty( iconImg, "title", pTooltip );
        }

        @Override
        public void setVisible( boolean pVisible )
        {
            getElement().getStyle().setDisplay( (mCanShow && pVisible) ? Style.Display.INLINE : Style.Display.NONE );
        }
    }

    private static SimpleKeyValuePair[] convertToKeyValuePairs( String[] pOptions )
    {
        if ( pOptions == null )
        {
            return SimpleKeyValuePair.EMPTY_ARRAY;
        }
        SimpleKeyValuePair[] rv = new SimpleKeyValuePair[pOptions.length];
        for ( int i = 0; i < pOptions.length; i++ )
        {
            rv[i] = convertToKeyValuePair( pOptions[i] );
        }
        return rv;
    }

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

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

Diff revisions: vs.
Revision Author Commited Message
942 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 23:41:46 +0000

Extracting commonfoundation

939 Diff Diff GeorgeS picture GeorgeS Mon 02 Jun, 2014 21:30:31 +0000

Extracting commonfoundation

802 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 04:04:47 +0000
801 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 03:59:02 +0000
390 Diff Diff GeorgeS picture GeorgeS Sun 14 Aug, 2011 19:33:48 +0000
243 Diff Diff GeorgeS picture GeorgeS Sun 05 Jun, 2011 20:29:12 +0000
154 Diff Diff GeorgeS picture GeorgeS Tue 22 Mar, 2011 20:46:40 +0000
151 Diff Diff GeorgeS picture GeorgeS Thu 17 Mar, 2011 04:16:22 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

23 GeorgeS picture GeorgeS Wed 24 Feb, 2010 00:34:32 +0000