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
package com.temp.foundation.client.widget;

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

import java.util.*;

/**
 * This class provides for creating a VerticalPanel "like" object that takes widgets as Pairs.
 * <p/>
 * These Widget Pairs are called the Prime Widget and the Subordinate Widget.  All the Prime Widgets are
 * visible, but the Subordinate Widget attached to the Prime Widget is only visible if visibility is requested.
 * <p/>
 * The showing and hiding of the a Subordinate Widget is animated!
 *
 * @author georgs
 */
public class AccordionVerticalPairPanel extends Composite {
    public static class WidgetPair {
        private Widget prime, mSubordinate;
        private boolean showing;
        private PairPanel pairPanel;

        public WidgetPair( Widget prime, Widget subordinate ) {
            this.prime = prime;
            this.mSubordinate = subordinate;
        }

        public Widget getPrime() {
            return prime;
        }

        public Widget getSubordinate() {
            return mSubordinate;
        }

        public boolean isSubordinateShowing() {
            if ( showing ) {
                showing = checkPrimeAncestry( prime ) && checkSubordinateAncestry( mSubordinate );
            }
            return showing;
        }

        private boolean isShowing() {
            return showing;
        }

        private void setShowing( boolean showing ) {
            this.showing = showing;
        }

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

        private PairPanel getPairPanel() {
            return pairPanel;
        }

        private void setPairPanel( PairPanel pairPanel ) {
            this.pairPanel = pairPanel;
        }
    }

    private VerticalPanel container = new VerticalPanel();
    private List<WidgetPair> members = new ArrayList<WidgetPair>();

    public AccordionVerticalPairPanel() {
        initWidget( container );
    }

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

    public int size() {
        return members.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 primeWidget, Widget subordinateWidget ) {
        return add( new WidgetPair( primeWidget, subordinateWidget ) );
    }

    /**
     * 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 widgetPair ) {
        if ( WidgetPair.hasContent( widgetPair ) ) {
            members.add( widgetPair );
            container.add( new PairPanel( this, widgetPair ) );
            return true;
        }
        return false;
    }

    public int indexOf( WidgetPair widgetPair ) {
        return members.indexOf( widgetPair );
    }

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

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

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

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

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

    public void collapseAll() {
        for ( WidgetPair zMember : members ) {
            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 subordinateWidget the Subordinate Widget to locate
     * @param show              what to do with the Subordinate Widget if located
     *
     * @return true if the Subordinate Widget was located
     */
    public boolean show( Widget subordinateWidget, boolean show ) {
        return showSubordinateWidgetOf( getPairFor( subordinateWidget ), show );
    }

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

    /**
     * Expand (show) or Collapse (!show) the Subordinate Widget of the
     * WidgetPair (if found) according to the pShow value requested.
     *
     * @param widgetPair the Widget Pair to locate
     * @param show       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 widgetPair, boolean show ) {
        if ( members.contains( widgetPair ) ) {
            PairPanel zPairPanel = widgetPair.getPairPanel();
            if ( zPairPanel != null ) {
                if ( show ) {
                    zPairPanel.expand();
                } else {
                    zPairPanel.collapse();
                }
                widgetPair.setShowing( show );
                return true;
            }
        }
        return false;
    }

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

    public boolean isSubordinateWidgetShowingAt( int index )
            throws IndexOutOfBoundsException {
        return isSubordinateWidgetShowing( members.get( index ) );
    }

    public boolean isSubordinateWidgetShowing( WidgetPair widgetPair ) {
        return members.contains( widgetPair ) && widgetPair.isShowing();
    }

    private WidgetPair getPairFor( Widget pSubordinateWidget ) {
        for ( WidgetPair zMember : members ) {
            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 pairPanel ) {
        for ( int i = 0; i < members.size(); i++ ) {
            WidgetPair zMember = members.get( i );
            PairPanel zPairPanel = zMember.getPairPanel();
            if ( pairPanel == zPairPanel ) {
                remove( i );
                return;
            }
        }
    }

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

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

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

    private static class PairPanel extends VerticalPanel {
        private AccordionVerticalPairPanel parent;
        private ClippingPanel clippingPanel;

        private PairPanel( AccordionVerticalPairPanel pParent, WidgetPair widgetPair ) {
            this.parent = pParent;
            add( widgetPair.getPrime() );
            add( this.clippingPanel = new ClippingPanel( this, widgetPair.getSubordinate() ) );
            widgetPair.setPairPanel( this );
        }

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

        public void expand() {
            clippingPanel.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() {
            parent = null; // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
            super.clear();
            if ( clippingPanel != null ) {
                clippingPanel.clear();
                clippingPanel = 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 ( parent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    parent.removePair( this );
                    clear();
                }
            }
            return zRemoved;
        }
    }

    private enum AnimatedVisibility {
        Collapsing( true ), Collapsed( true ), Expanding( false ), Expanded( false );

        private boolean collapsingOrCollapsed;

        AnimatedVisibility( boolean collapsingOrCollapsed ) {
            this.collapsingOrCollapsed = collapsingOrCollapsed;
        }

        public boolean isCollapsingOrCollapsed() {
            return collapsingOrCollapsed;
        }

        public boolean isExpandingOrExpanded() {
            return !collapsingOrCollapsed;
        }
    }

    private static class ClippingPanel extends SimplePanel {
        private PairPanel parent;
        private SubordinatePanel subordinatePanel;
        private AnimatedVisibility visibility = AnimatedVisibility.Collapsed;
        private AnimatedVisibility targetVisibility;
        private int currentHeight = 1;
        private Integer maxHeight;
        private HeightAnimator animator;

        private ClippingPanel( PairPanel parent, Widget subordinateWidget ) {
            this.parent = parent;
            setWidget( subordinatePanel = new SubordinatePanel( this, subordinateWidget ) );
            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() {
            parent = null; // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
            super.clear();
            if ( subordinatePanel != null ) {
                subordinatePanel.clear();
                subordinatePanel = 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 ( parent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    parent.remove( this );
                    clear();
                }
            }
            return zRemoved;
        }

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

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

        /**
         * 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 targetVisibility ) {
            if ( maxHeight == null ) {
                this.targetVisibility = targetVisibility;
                return false;
            }
            this.targetVisibility = null;
            return true;
        }

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

        private void setCurrentHeight( int height ) {
            if ( height < 2 ) {
                visibility = AnimatedVisibility.Collapsed;
                height = 1;
            }
            if ( maxHeight <= height ) {
                visibility = AnimatedVisibility.Expanded;
                height = maxHeight;
            }
            currentHeight = height;
            setHeight( height + "px" );
        }

        private void transition( AnimatedVisibility newVisibility, int targetHeight ) {
            if ( animator != null ) {
                animator.cancel();
                animator = null;
            }
            visibility = newVisibility;
            animator = new HeightAnimator( this, currentHeight, targetHeight );
            animator.start();
        }
    }

    private static class SubordinatePanel extends VerticalPanel {
        private OurSizingDeterminer sizingDeterminer = new OurSizingDeterminer();
        private ClippingPanel parent;

        private SubordinatePanel( ClippingPanel parent, Widget subordinateWidget ) {
            this.parent = parent;
            add( new Spacer().size( "1px" ) );
            add( (null != subordinateWidget) ? subordinateWidget : 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() {
            parent = 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 ( parent != null ) // in case remove is called as part of the clear operation, then don't generate a potential infinite loop!
                {
                    parent.remove( this );
                    clear();
                }
            }
            return zRemoved;
        }

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

        @Override
        protected void onDetach() {
            super.onDetach();
            sizingDeterminer.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 sizingDetermined;
            private int height = 1;

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

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

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

    private static class HeightAnimator extends Animation {
        private ClippingPanel clippingPanel;
        private int fromHeight;
        private int toHeight;
        private int delta;
        private boolean completed = true;

        public HeightAnimator( ClippingPanel clippingPanel, int fromHeight, int toHeight ) {
            this.clippingPanel = clippingPanel;
            this.fromHeight = fromHeight;
            this.toHeight = toHeight;
            delta = toHeight - fromHeight;
        }

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

        @Override
        protected void onUpdate( double progress ) {
            if ( !completed ) {
                clippingPanel.setCurrentHeight( fromHeight + (int) (progress * delta) );
            }
        }

        @Override
        protected void onComplete() {
            completed = true;
            super.onComplete();
            clippingPanel.setCurrentHeight( toHeight );
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/foundation/client/widget/AccordionVerticalPairPanel.java

Diff revisions: vs.
Revision Author Commited Message
965 GeorgeS picture GeorgeS Fri 01 Aug, 2014 03:20:35 +0000

!