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
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
package com.test.uidesign.client;

import java.util.*;

import org.litesoft.GWT.client.widgets.*;

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

public class AccordionVerticalPairPanel extends Composite
{
    public static class WidgetPair
    {
        private Widget mPrime, mSubordinate;
        private boolean mShowing;
        private PairPanel mPairPanel;

        public WidgetPair( Widget pPrime, Widget pSubordinate )
        {
            mPrime = pPrime;
            mSubordinate = pSubordinate;
        }

        public Widget getPrime()
        {
            return mPrime;
        }

        public Widget getSubordinate()
        {
            return mSubordinate;
        }

        public boolean isSubordinateShowing()
        {
            if ( mShowing )
            {
                mShowing = checkPrimeAncestry( mPrime ) && checkSubordinateAncestry( mSubordinate );
            }
            return mShowing;
        }

        private boolean isShowing()
        {
            return mShowing;
        }

        private void setShowing( boolean pShowing )
        {
            mShowing = pShowing;
        }

        private static boolean hasContent( WidgetPair pPair )
        {
            return (pPair != null) && (pPair.mPrime != null);
        }

        private PairPanel getPairPanel()
        {
            return mPairPanel;
        }

        private void setPairPanel( PairPanel pPairPanel )
        {
            mPairPanel = pPairPanel;
        }
    }

    private VerticalPanel mContainer = new VerticalPanel();
    private List<WidgetPair> mMembers = new ArrayList<WidgetPair>();

    public AccordionVerticalPairPanel()
    {
        initWidget( mContainer );
    }

    public boolean isEmpty()
    {
        return mMembers.isEmpty();
    }

    public int size()
    {
        return mMembers.size();
    }

    /**
     * Add the two widgets as a WidgetPair (if pPrimeWidget is not null).
     *
     * @return true if the widgets were actually added.
     */
    public boolean add( Widget pPrimeWidget, Widget pSubordinateWidget )
    {
        return add( new WidgetPair( pPrimeWidget, pSubordinateWidget ) );
    }

    /**
     * Add the WidgetPair (if either of the widgets that make up the pair are not null).
     *
     * @return true if the WidgetPair was actually added.
     */
    public boolean add( WidgetPair pWidgetPair )
    {
        if ( WidgetPair.hasContent( pWidgetPair ) )
        {
            mMembers.add( pWidgetPair );
            mContainer.add( new PairPanel( this, pWidgetPair ) );
            return true;
        }
        return false;
    }

    public int indexOf( WidgetPair pWidgetPair )
    {
        return mMembers.indexOf( pWidgetPair );
    }

    public int indexOfSubordinateWidget( Widget pSubordinateWidget )
    {
        for ( int i = 0; i < mMembers.size(); i++ )
        {
            WidgetPair zMember = mMembers.get( i );
            if ( pSubordinateWidget == zMember.getSubordinate() )
            {
                return i;
            }
        }
        return -1;
    }

    public int indexOfPrimeWidget( Widget pPrimeWidget )
    {
        for ( int i = 0; i < mMembers.size(); i++ )
        {
            WidgetPair zMember = mMembers.get( i );
            if ( pPrimeWidget == zMember.getPrime() )
            {
                return i;
            }
        }
        return -1;
    }

    /**
     * @return true if contents changed (i.e. actually removed)
     */
    public boolean remove( WidgetPair pWidgetPair )
    {
        int zIndex = indexOf( pWidgetPair );
        return (zIndex != -1) && (null != remove( zIndex ));
    }

    /**
     * @return WidgetPair that "was" at pIndex.
     */
    public WidgetPair remove( int pIndex )
            throws IndexOutOfBoundsException
    {
        WidgetPair zRemoved = mMembers.remove( pIndex );
        if ( zRemoved != null )
        {
            PairPanel zPairPanel = zRemoved.getPairPanel();
            if ( zPairPanel != null )
            {
                zPairPanel.clear();
            }
            zRemoved.setShowing( false );
            mContainer.remove( pIndex );
        }
        return zRemoved;
    }

    /**
     * Removes all widgets (both the Prime and the Subordinate).
     */
    public void clear()
    {
        mContainer.clear();
        for ( WidgetPair zMember : mMembers )
        {
            PairPanel zPairPanel = zMember.getPairPanel();
            if ( zPairPanel != null )
            {
                zPairPanel.clear();
            }
            zMember.setShowing( false );
        }
        mMembers.clear();
    }

    public void collapseAll()
    {
        for ( WidgetPair zMember : mMembers )
        {
            PairPanel zPairPanel = zMember.getPairPanel();
            if ( zPairPanel != null )
            {
                zPairPanel.collapse();
            }
            zMember.setShowing( false );
        }
    }

    /**
     * Expand (show) or Collapse (!show) the pSubordinateWidget (if found) according to the pShow value requested.
     *
     * @param pSubordinateWidget the Subordinate Widget to locate
     * @param pShow              what to do with the Subordinate Widget if located
     *
     * @return true if the Subordinate Widget was located
     */
    public boolean show( Widget pSubordinateWidget, boolean pShow )
    {
        return showSubordinateWidgetOf( getPairFor( pSubordinateWidget ), pShow );
    }

    /**
     * Expand (show) or Collapse (!show) the Subordinate Widget (at pIndex) according to the pShow value requested.
     *
     * @param pIndex Index of the the Subordinate Widget
     * @param pShow  what to do with the Subordinate Widget
     */
    public void showSubordinateWidgetAt( int pIndex, boolean pShow )
            throws IndexOutOfBoundsException
    {
        showSubordinateWidgetOf( mMembers.get( pIndex ), pShow );
    }

    /**
     * Expand (show) or Collapse (!show) the Subordinate Widget of the WidgetPair (if found) according to the pShow value requested.
     *
     * @param pWidgetPair the  Widget Pair to locate
     * @param pShow       what to do with the Subordinate Widget of the Widget Pair if located
     *
     * @return true if the Widget Pair was located
     */
    public boolean showSubordinateWidgetOf( WidgetPair pWidgetPair, boolean pShow )
    {
        if ( mMembers.contains( pWidgetPair ) )
        {
            PairPanel zPairPanel = pWidgetPair.getPairPanel();
            if ( zPairPanel != null )
            {
                if ( pShow )
                {
                    zPairPanel.expand();
                }
                else
                {
                    zPairPanel.collapse();
                }
                pWidgetPair.setShowing( pShow );
                return true;
            }
        }
        return false;
    }

    public boolean isShowing( Widget pSubordinateWidget )
    {
        return isSubordinateWidgetShowing( getPairFor( pSubordinateWidget ) );
    }

    public boolean isSubordinateWidgetShowingAt( int pIndex )
            throws IndexOutOfBoundsException
    {
        return isSubordinateWidgetShowing( mMembers.get( pIndex ) );
    }

    public boolean isSubordinateWidgetShowing( WidgetPair pWidgetPair )
    {
        return mMembers.contains( pWidgetPair ) && pWidgetPair.isShowing();
    }

    private WidgetPair getPairFor( Widget pSubordinateWidget )
    {
        for ( WidgetPair zMember : mMembers )
        {
            if ( pSubordinateWidget == zMember.getSubordinate() )
            {
                return zMember;
            }
        }
        return null;
    }

    /**
     * Only called by the PairPanel that should be removed because the PairPanel has lost one of its widgets.
     */
    private void removePair( PairPanel pPairPanel )
    {
        for ( int i = 0; i < mMembers.size(); i++ )
        {
            WidgetPair zMember = mMembers.get( i );
            PairPanel zPairPanel = zMember.getPairPanel();
            if ( pPairPanel == zPairPanel )
            {
                remove( i );
                return;
            }
        }
    }

    private static boolean checkSubordinateAncestry( Widget pWidget )
    {
        return (pWidget == null) || //
               (null != parentClassIs( PairPanel.class, //
                                       parentClassIs( ClippingPanel.class, //
                                                      parentClassIs( SubordinatePanel.class, //
                                                                     pWidget ) ) ));
    }

    private static boolean checkPrimeAncestry( Widget pWidget )
    {
        return (null != parentClassIs( PairPanel.class, pWidget ));
    }

    private static Widget parentClassIs( Class<?> pClass, Widget pWidget )
    {
        if ( pWidget != null )
        {
            if ( null != (pWidget = pWidget.getParent()) )
            {
                if ( pWidget.getClass() == pClass )
                {
                    return pWidget;
                }
            }
        }
        return null;
    }

    private static class PairPanel extends VerticalPanel
    {
        private AccordionVerticalPairPanel mParent;
        private ClippingPanel mClippingPanel;

        private PairPanel( AccordionVerticalPairPanel pParent, WidgetPair pWidgetPair )
        {
            mParent = pParent;
            add( pWidgetPair.getPrime() );
            add( mClippingPanel = new ClippingPanel( this, pWidgetPair.getSubordinate() ) );
            pWidgetPair.setPairPanel( this );
        }

        public void collapse()
        {
            mClippingPanel.collapse();
        }

        public void expand()
        {
            mClippingPanel.expand();
        }

        /**
         * Clear/Release our Widgets (this is called under two circumstances: our Parent is clearing and calling us to clean up, or
         * one of our children has been removed so we are no longer attached to our parent and are cleaning up).
         */
        @Override
        public void clear()
        {
            mParent = null; // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
            super.clear();
            if ( mClippingPanel != null )
            {
                mClippingPanel.clear();
                mClippingPanel = null;
            }
        }

        /**
         * Here we handle the case when either the Prime Widget is removed (by being attached somewhere else) or the Clipping Panel removes itself.
         * <p/>
         * We Automatically remove ourselves from our Parent (AccordionVerticalPairPanel).
         */
        @Override
        public boolean remove( Widget w )
        {
            boolean zRemoved = super.remove( w );
            if ( zRemoved )
            {
                if ( mParent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    mParent.removePair( this );
                    clear();
                }
            }
            return zRemoved;
        }
    }

    private static class ClippingPanel extends SimplePanel
    {
        private PairPanel mParent;
        private SubordinatePanel mSubordinatePanel;
        private AnimatedVisibility mVisibility = AnimatedVisibility.Collapsed;
        private AnimatedVisibility mTargetVisibility;
        private int mCurrentHeight = 1;
        private Integer mMaxHeight;
        private HeightAnimator mAnimator;

        private ClippingPanel( PairPanel pParent, Widget pSubordinateWidget )
        {
            mParent = pParent;
            setWidget( mSubordinatePanel = new SubordinatePanel( this, pSubordinateWidget ) );
            getStyleElement().getStyle().setOverflow( Style.Overflow.HIDDEN );
            setHeight( "1px" );
        }

        /**
         * Clear/Release our Widget (this is called under two circumstances: our Parent is clearing and calling us to clean up, or
         * our child has been removed so we are no longer attached to our parent and are cleaning up).
         */
        @Override
        public void clear()
        {
            mParent = null; // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
            super.clear();
            if ( mSubordinatePanel != null )
            {
                mSubordinatePanel.clear();
                mSubordinatePanel = null;
            }
        }

        /**
         * Here we handle the case when the Subordinate Panel removes itself.
         * <p/>
         * We Automatically remove ourselves from our Parent (PairPanel).
         */
        @Override
        public boolean remove( Widget w )
        {
            boolean zRemoved = super.remove( w );
            if ( zRemoved )
            {
                if ( mParent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    mParent.remove( this );
                    clear();
                }
            }
            return zRemoved;
        }

        public AnimatedVisibility getVisibility()
        {
            return mVisibility;
        }

        public void collapse()
        {
            if ( sizingIsReadyWithNotReadyTarget( AnimatedVisibility.Collapsed ) && mVisibility.isExpandingOrExpanded() )
            {
                transition( AnimatedVisibility.Collapsing, 1 );
            }
        }

        public void expand()
        {
            if ( sizingIsReadyWithNotReadyTarget( AnimatedVisibility.Expanded ) && mVisibility.isCollapsingOrCollapsed() )
            {
                transition( AnimatedVisibility.Expanding, mMaxHeight );
            }
        }

        /**
         * Some browsers implement incremental rendering, which means that when an element is added to the DOM,
         * it can take some time until it is actually rendered and hence actually has a size (of course if any
         * element along the path to the root is display=none, then a size might NEVER arrive).
         */
        private boolean sizingIsReadyWithNotReadyTarget( AnimatedVisibility pTargetVisibility )
        {
            if ( mMaxHeight == null )
            {
                mTargetVisibility = pTargetVisibility;
                return false;
            }
            mTargetVisibility = null;
            return true;
        }

        /**
         * Called to indicate that Sizing is Ready!
         */
        private void setMaxHeight( int pHeight )
        {
            mMaxHeight = pHeight;
            // Now dispatch the last request (if any) that occured before Sizing was Ready!
            if ( AnimatedVisibility.Collapsed == mTargetVisibility )
            {
                collapse();
            }
            if ( AnimatedVisibility.Expanded == mTargetVisibility )
            {
                expand();
            }
        }

        private void setCurrentHeight( int pHeight )
        {
            if ( pHeight < 2 )
            {
                mVisibility = AnimatedVisibility.Collapsed;
                pHeight = 1;
            }
            if ( mMaxHeight <= pHeight )
            {
                mVisibility = AnimatedVisibility.Expanded;
                pHeight = mMaxHeight;
            }
            mCurrentHeight = pHeight;
            setHeight( pHeight + "px" );
        }

        private void transition( AnimatedVisibility pNewVisibility, int pTargetHeight )
        {
            if ( mAnimator != null )
            {
                mAnimator.cancel();
                mAnimator = null;
            }
            mVisibility = pNewVisibility;
            mAnimator = new HeightAnimator( this, mCurrentHeight, pTargetHeight );
            mAnimator.start();
        }
    }

    private static class SubordinatePanel extends VerticalPanel
    {
        private OurSizingDeterminer mSizingDeterminer = new OurSizingDeterminer();
        private ClippingPanel mParent;

        private SubordinatePanel( ClippingPanel pParent, Widget pSubordinateWidget )
        {
            mParent = pParent;
            add( new TransparentImage() );
            add( (null != pSubordinateWidget) ? pSubordinateWidget : new TransparentImage() );
        }

        /**
         * Clear/Release our Widgets (this is called under two circumstances: our Parent is clearing and calling us to clean up, or
         * one of our children has been removed so we are no longer attached to our parent and are cleaning up).
         */
        @Override
        public void clear()
        {
            mParent = null; // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
            super.clear();
        }

        /**
         * Here we handle the case when the Subordinate Widget is removed by being attached somewhere else.
         * <p/>
         * We Automatically remove ourselves from our Parent (ClippingPanel).
         */
        @Override
        public boolean remove( Widget w )
        {
            boolean zRemoved = super.remove( w );
            if ( zRemoved )
            {
                if ( mParent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    mParent.remove( this );
                    clear();
                }
            }
            return zRemoved;
        }

        @Override
        protected void onAttach()
        {
            super.onAttach();
            mSizingDeterminer.start();
        }

        @Override
        protected void onDetach()
        {
            super.onDetach();
            mSizingDeterminer.stop();
        }

        /**
         * Some browsers implement incremental rendering, which means that when an element is added to the DOM,
         * it can take some time until it is actually rendered and hence actually has a size (of course if any
         * element along the path to the root is display=none, then a size might NEVER arrive).
         */
        private class OurSizingDeterminer extends com.google.gwt.user.client.Timer
        {
            private Boolean mSizingDetermined;
            private int mHeight = 1;

            @Override
            public void run()
            {
                int zHeight = getOffsetHeight();
                if ( (1 < zHeight) && (zHeight == mHeight) )
                {
                    mParent.setMaxHeight( zHeight );
                    mSizingDetermined = true;
                    return;
                }
                mHeight = zHeight;
                if ( mSizingDetermined != null )
                {
                    schedule( 50 );
                }
            }

            public void start()
            {
                if ( !Boolean.TRUE.equals( mSizingDetermined ) )
                {
                    mSizingDetermined = false;
                    schedule( 50 );
                }
            }

            public void stop()
            {
                if ( Boolean.FALSE.equals( mSizingDetermined ) )
                {
                    mSizingDetermined = null;
                    cancel();
                }
            }
        }
    }

    private static class HeightAnimator extends Animation
    {
        private ClippingPanel mClippingPanel;
        private int mFromHeight;
        private int mToHeight;
        private int mDelta;
        private boolean mCompleted = true;

        public HeightAnimator( ClippingPanel pClippingPanel, int pFromHeight, int pToHeight )
        {
            mClippingPanel = pClippingPanel;
            mFromHeight = pFromHeight;
            mToHeight = pToHeight;
            mDelta = pToHeight - pFromHeight;
        }

        public void start()
        {
            mCompleted = false;
            run( Math.min( 700, Math.abs( 7 * mDelta ) ) );
        }

        @Override
        protected void onUpdate( double progress )
        {
            if ( !mCompleted )
            {
                mClippingPanel.setCurrentHeight( mFromHeight + (int) (progress * mDelta) );
            }
        }

        @Override
        protected void onComplete()
        {
            mCompleted = true;
            super.onComplete();
            mClippingPanel.setCurrentHeight( mToHeight );
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/UIdesign/src/com/test/uidesign/client/AccordionVerticalPairPanel.java

Diff revisions: vs.
Revision Author Commited Message
587 GeorgeS picture GeorgeS Fri 02 Dec, 2011 11:55:12 +0000

Accordion...