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
// This Source Code is in the Public Domain per: http://unlicense.org
package org.litesoft.GWT.client.widgets;

import org.litesoft.GWT.client.widgets.nonpublic.*;
import org.litesoft.commonfoundation.base.*;

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

import java.util.*;

public class SizeableFloatableContainerPanel extends AbstractSizeableComplexPanel implements IndexedPanel,
                                                                                             FloatablesCarrier {
    private static int INITIAL_Z_INDEX = 1000;
    private static int MAX_ALLOWED_Z_INDEX = 9999;
    private SizeableFloater mActive = null;

    private FloatableContainerListener mFloatableContainerListener;

    public SizeableFloatableContainerPanel() {
        initializeElements( getHelper().create_OeSeIeOverflowHiddenSingleDivSandwich(),
                            "litesoft-NoSizeAffectingCSS-FloatablesCarrier" );
        LLstretchable();
        Element containerElement = getContainerElement();

        /*
        * Establish a local stacking context. This keeps the menus and dialogs
        * above the internal windows.
        * http://code.google.com/p/google-web-toolkit/issues/detail?id=1288
        */
        DOM.setStyleAttribute( containerElement, "position", "relative" );
        DOM.setIntStyleAttribute( containerElement, "zIndex", 0 );
    }

    private boolean setActive( SizeableFloater pActive ) {
        if ( pActive != mActive ) {
            if ( mActive != null ) {
                mActive.setActive( false );
                if ( mFloatableContainerListener != null ) {
                    mFloatableContainerListener.clearActive( mActive );
                }
            }
            mActive = pActive;
            if ( mActive != null ) {
                mActive.setActive( true );
                if ( mFloatableContainerListener != null ) {
                    mFloatableContainerListener.setActive( mActive );
                }
            }
            return true;
        }
        return false;
    }

    public void undoCascade() {
        if ( !mStackOfFloatersPositionState.isEmpty() ) {
            SizeableFloater[] floaters = getFloatersSortedByZindex();

            if ( floaters.length == 0 ) {
                mStackOfFloatersPositionState.clear();
            } else {
                FloatersPositionState zFPS = mStackOfFloatersPositionState.pop();

                setActive( zFPS.restoreFloaters( floaters ) );
            }
        }
    }

    private Stack<FloatersPositionState> mStackOfFloatersPositionState = new Stack<FloatersPositionState>();

    public void cascade() {
        SizeableFloater[] floaters = getFloatersSortedByZindex();
        if ( floaters.length == 0 ) {
            mStackOfFloatersPositionState.clear();
        } else {
            FloatersPositionState zFPS = new FloatersPositionState();

            SizeableFloater floater = zFPS.cascadeFloaters( floaters );

            if ( floater != null ) {
                setActive( floater );

                if ( floater.getZindex() > MAX_ALLOWED_Z_INDEX ) {
                    reZindex();
                }

                if ( zFPS.isChanged() ) {
                    mStackOfFloatersPositionState.push( zFPS );
                }
            }
        }
    }

    private SizeableFloater[] getArrayOfChildren() {
        WidgetCollection wc = getChildren();
        int count = wc.size();
        if ( count == 0 ) {
            return SizeableFloater.EMPTY_ARRAY;
        }
        SizeableFloater[] floaters = new SizeableFloater[count];
        for ( int i = 0; i < floaters.length; i++ ) {
            floaters[i] = (SizeableFloater) wc.get( i );
        }
        return floaters;
    }

    public Widget getWidget( int index ) {
        return getChildren().get( index );
    }

    public int getWidgetCount() {
        return getChildren().size();
    }

    public int getWidgetIndex( Widget child ) {
        return getChildren().indexOf( child );
    }

    public boolean remove( int index ) {
        return remove( getWidget( index ) );
    }

    /**
     * Adds a floater to the panel and makes it the top floater.
     *
     * @param w the widget to be added
     */
    public void add( Widget w ) {
        if ( !(w instanceof SizeableFloater) ) {
            throw new IllegalArgumentException( "Only a 'SizeableFloater' can be added" );
        }
        SizeableFloater floater = (SizeableFloater) w;

        floater.setZindex( calcTemporaryNewZindex() );

        int zTop = 0;
        int zLeft = 0;
        int count = 0;

        SizeableFloater[] floaters = getArrayOfChildren();

        if ( floaters.length != 0 ) {
            int delta = Math.max( floaters[0].getTitleBarHeight(), 25 );

            int doubleDelta = (delta * 2);
            int maxWidth = getOffsetWidth() - doubleDelta;
            int maxHeight = getOffsetHeight() - doubleDelta;
            boolean checkOverflow = (maxWidth > doubleDelta && maxHeight > doubleDelta);

            while ( anyAt( floaters, delta, zTop, zLeft ) ) {
                // safety valve -- if things get too jammed, just start plopping stuff at the top left
                if ( count++ > 80 ) {
                    zTop = zLeft = 0;
                    break;
                }

                zTop += delta;
                zLeft += delta;

                // if we roll off the bottom or right, start over with a smaller delta
                if ( checkOverflow && (zTop > maxHeight || zLeft > maxWidth) ) {
                    delta /= 2;
                    if ( delta < 2 ) {
                        delta = 2;
                    }
                    zTop = delta;
                    zLeft = delta;
                }
            }
        }

        add( w, getContainerElement() );
        floater.initializeSize();
        floater.setPosition( zLeft, zTop );
        floaterAdded( floater );
        makeTop( floater );
    }

    public boolean remove( Widget w ) {
        if ( super.remove( w ) ) {
            setActive( (getChildren().size() > 0) ? getCurrentMaxZindexChild() : null );
            if ( w instanceof SizeableFloater ) {
                floaterRemoved( (SizeableFloater) w );
            }
            return true;
        }
        return false;
    }

    private boolean anyAt( SizeableFloater[] pFloaters, int pDelta, int pTop, int pLeft ) {
        //noinspection ForLoopReplaceableByForEach
        for ( int i = 0; i < pFloaters.length; i++ ) {
            SizeableFloater floater = pFloaters[i];
            if ( within( pDelta, pTop, floater.getPosTop() ) ||
                 within( pDelta, pLeft, floater.getPosLeft() ) ) {
                return true;
            }
        }
        return false;
    }

    private boolean within( int pDelta, int pPos1, int pPos2 ) {
        return Math.abs( pPos1 - pPos2 ) < pDelta;
    }

    public SizeableFloater[] getFloatersSortedByTitle() {
        return getFloatersSortedBy( TitleComparator.INSTANCE );
    }

    public SizeableFloater[] getFloatersSortedByZindex() {
        return getFloatersSortedBy( ZindexComparator.INSTANCE );
    }

    private SizeableFloater[] getFloatersSortedBy( Comparator pComparator ) {
        SizeableFloater[] floaters = getArrayOfChildren();
        return sort( floaters, pComparator );
    }

    private static SizeableFloater[] sort( SizeableFloater[] pFloaters, Comparator pComparator ) {
        if ( pFloaters.length != 0 ) {
            //noinspection unchecked
            Arrays.sort( pFloaters, pComparator );
        }
        return pFloaters;
    }

    private void reZindex() {
        reZindex( getFloatersSortedByZindex() );
    }

    private static void reZindex( SizeableFloater[] pFloaters ) {
        int zIndex = INITIAL_Z_INDEX;
        //noinspection ForLoopReplaceableByForEach
        for ( int i = 0; i < pFloaters.length; i++ ) {
            pFloaters[i].setZindex( ++zIndex );
        }
    }

    private SizeableFloater getCurrentMaxZindexChild() {
        WidgetCollection wc = getChildren();
        int count = wc.size();

        if ( count == 0 ) {
            return null;
        }
        SizeableFloater maxChild = (SizeableFloater) wc.get( 0 );
        int maxZ = maxChild.getZindex();

        for ( int i = 1; i < count; i++ ) {
            SizeableFloater floater = (SizeableFloater) wc.get( i );
            int zindex = floater.getZindex();
            if ( zindex > maxZ ) {
                maxChild = floater;
                maxZ = zindex;
            }
        }
        return maxChild;
    }

    /**
     * ONLY called by a current child!
     */
    public void makeTop( SizeableFloater pChild ) {
        SizeableFloatingPanel zFloater = ((SizeableFloatingPanel) pChild);

        if ( zFloater.isHidden() ) {
//            zFloater.setActive( true );
            zFloater.showFloater();
        }

        if ( pChild != mActive ) {
            int zindex = calcTemporaryNewZindex();
            pChild.setZindex( zindex );
            if ( zindex > MAX_ALLOWED_Z_INDEX ) {
                reZindex();
            }
            setActive( pChild );
        }
    }

    private int calcTemporaryNewZindex() {
        return 1 + ((mActive == null) ? INITIAL_Z_INDEX : mActive.getZindex());
    }

    //****** Implementation Code Block to support delegation to AbsoluteSizeHelper

    public SizeableFloatableContainerPanel width( int pPixels ) {
        setWidth( "" + pPixels );
        return this;
    }

    public SizeableFloatableContainerPanel height( int pPixels ) {
        setHeight( "" + pPixels );
        return this;
    }

    public void setRenderedDeferredCommand( Command pCommand ) {
        mRDCHelper.setRenderedDeferredCommand( pCommand );
    }

    public void setWidth( String width ) {
        super.setWidth( width );
        mRDCHelper.chkWidth( width );
    }

    public void setHeight( String height ) {
        super.setHeight( height );
        mRDCHelper.chkHeight( height );
    }

    private RenderedDeferredCommandHelper mRDCHelper = new RenderedDeferredCommandHelper( this );

    protected void distributeToChildrenChangedWidth() {
        getHelper().forceAdjustDimensionOnChild( getHelper().getInnerElement(), getWidthHelper() );
        for ( int i = 0; i < getWidgetCount(); i++ ) {
            SizeableFloater floater = (SizeableFloater) getWidget( i );
            floater.initializeWidth();
            if ( floater.isPositionLocked() ) {
                floater.changedContainerWidth();
            }
        }
    }

    protected void distributeToChildrenChangedHeight() {
        getHelper().forceAdjustDimensionOnChild( getHelper().getInnerElement(), getHeightHelper() );
        for ( int i = 0; i < getWidgetCount(); i++ ) {
            SizeableFloater floater = (SizeableFloater) getWidget( i );
            floater.initializeHeight();
            if ( floater.isPositionLocked() ) {
                floater.changedContainerHeight();
            }
        }
    }

    public void setFloatableContainerListener( FloatableContainerListener pFloatableContainerListener ) {
        mFloatableContainerListener = pFloatableContainerListener;
    }

    private void floaterAdded( SizeableFloater pFloater ) {
        if ( mFloatableContainerListener != null ) {
            mFloatableContainerListener.floaterAdded( pFloater );
        }
    }

    private void floaterRemoved( SizeableFloater pFloater ) {
        if ( mFloatableContainerListener != null ) {
            mFloatableContainerListener.floaterRemoved( pFloater );
        }
    }

    private static class ZindexComparator implements Comparator {
        public static final Comparator INSTANCE = new ZindexComparator();

        public int compare( Object o1, Object o2 ) {
            return compare( (SizeableFloater) o1, (SizeableFloater) o2 );
        }

        /**
         * @return a negative integer, zero, or a positive integer as pThis object
         * is less than, equal to, or greater than the pThem object.
         */
        public int compare( SizeableFloater pThis, SizeableFloater pThem ) {
            return pThis.getZindex() - pThem.getZindex();
        }
    }

    private static class AreaComparator implements Comparator<SizeableFloater> {
        public static final AreaComparator INSTANCE = new AreaComparator();

        /**
         * @return a negative integer, zero, or a positive integer as pThis object
         * is less than, equal to, or greater than the pThem object.
         */
        public int compare( SizeableFloater pThis, SizeableFloater pThem ) {
            // return pThis.getArea() - pThem.getArea();
            return pThem.getArea() - pThis.getArea(); // reverse! (Bigger to Smaller)
        }
    }

    private static class TitleComparator implements Comparator {
        public static final Comparator INSTANCE = new TitleComparator();

        public int compare( Object o1, Object o2 ) {
            return compare( (SizeableFloater) o1, (SizeableFloater) o2 );
        }

        /**
         * @return a negative integer, zero, or a positive integer as pThis object
         * is less than, equal to, or greater than the pThem object.
         */
        public int compare( SizeableFloater pThis, SizeableFloater pThem ) {
            return Compare.nullsOK( pThem.getTitle(), pThis.getTitle() );
        }
    }

    private static class FloaterPosition implements Cascadable {
        private int mCascadeUniqueID, mPosLeft, mPosTop;

        public FloaterPosition( SizeableFloater pFloater ) {
            mCascadeUniqueID = pFloater.getCascadeUniqueID();
            mPosLeft = pFloater.getPosLeft();
            mPosTop = pFloater.getPosTop();
        }

        public boolean isPositionLocked() {
            return true;
        }

        public int getCascadeUniqueID() {
            return mCascadeUniqueID;
        }

        public int getPosLeft() {
            return mPosLeft;
        }

        public int getPosTop() {
            return mPosTop;
        }

        public int getOffsetWidth() {
            throw new UnsupportedOperationException();
        }

        public int getOffsetHeight() {
            throw new UnsupportedOperationException();
        }

        public int minPosLeft() {
            throw new UnsupportedOperationException();
        }

        public int minPosTop() {
            throw new UnsupportedOperationException();
        }

        public int maxPosLeft() {
            throw new UnsupportedOperationException();
        }

        public int maxPosTop() {
            throw new UnsupportedOperationException();
        }

        public void setPosition( int pLeft, int pTop ) {
            throw new UnsupportedOperationException();
        }

        public void setPosLeft( int pPosLeft ) {
            throw new UnsupportedOperationException();
        }

        public void setPosTop( int pPosTop ) {
            throw new UnsupportedOperationException();
        }

        /**
         * Like equals(), but with the contract
         *
         * @param them !null
         */
        public boolean isEquivalent( Cascadable them ) {
            return (this.getCascadeUniqueID() == them.getCascadeUniqueID()) //
                   && (this.getPosLeft() == them.getPosLeft()) //
                   && (this.getPosTop() == them.getPosTop()) //
                    ;
        }

        public boolean shouldMove( int pDesiredLeft, int pDesiredTop ) {
            return (mPosLeft != pDesiredLeft) || (mPosTop != pDesiredTop);
        }
    }

    private static class FloatersPositionState {
        private boolean mChanged = false;
        private FloaterPosition[] mFloaterPositions;

        /**
         * @param pFloaters - current floaters sorted in Current Z-Order Back to Front,
         *                  Array is !null, !empty, & safe to mutate
         *
         * @return - Floater to make Active
         */
        public SizeableFloater restoreFloaters( SizeableFloater[] pFloaters ) {
            SizeableFloater floater = null;

            SizeableFloater[] zNewOrder = new SizeableFloater[pFloaters.length];

            int ndxTo = 0;
            for ( int ndxFrom = 0; ndxFrom < mFloaterPositions.length; ndxFrom++ ) {
                FloaterPosition zPosition = mFloaterPositions[ndxFrom];
                int zAt = locate( zPosition.getCascadeUniqueID(), pFloaters );
                if ( zAt != -1 ) {
                    floater = zNewOrder[ndxTo++] = pFloaters[zAt];
                    pFloaters[zAt] = null;
                    if ( !floater.isPositionLocked() ) {
                        floater.setPosition( zPosition.getPosLeft(), zPosition.getPosTop() );
                    }
                }
            }
            for ( int i = 0; i < pFloaters.length; i++ ) {
                SizeableFloater zFloater = pFloaters[i];
                if ( zFloater != null ) {
                    floater = zNewOrder[ndxTo++] = zFloater;
                }
            }

            reZindex( zNewOrder );

            return floater;
        }

        private int locate( int pCascadeUniqueID, SizeableFloater[] pFloaters ) {
            int i = pFloaters.length;
            while ( --i >= 0 ) {
                SizeableFloater zFloater = pFloaters[i];
                if ( (zFloater != null) && (zFloater.getCascadeUniqueID() == pCascadeUniqueID) ) {
                    return i;
                }
            }
            return i;
        }

        private SizeableFloater populate( SizeableFloater[] pFloaters,
                                          List<SizeableFloater> pNonLockedFloaters ) {
            mFloaterPositions = new FloaterPosition[pFloaters.length];

            SizeableFloater zFloater = null;
            for ( int i = 0; i < pFloaters.length; i++ ) {
                zFloater = pFloaters[i];
                mFloaterPositions[i] = new FloaterPosition( zFloater );
                if ( !zFloater.isPositionLocked() ) {
                    pNonLockedFloaters.add( zFloater );
                }
            }
            return pNonLockedFloaters.isEmpty() ? null : zFloater;
        }

        /**
         * @param pFloaters - current floaters sorted in Current Z-Order Back to Front,
         *                  Array is !null, !empty, & safe to mutate
         *
         * @return - Floater to make Active
         */
        public SizeableFloater cascadeFloaters( SizeableFloater[] pFloaters ) {
            List<SizeableFloater> zNonLockedFloaters = new ArrayList<SizeableFloater>( pFloaters.length );

            SizeableFloater zFloater = populate( pFloaters, zNonLockedFloaters );

            if ( zFloater == null ) {
                return null;
            }
            Collections.sort( zNonLockedFloaters, AreaComparator.INSTANCE );

            int pMaxZindex = zFloater.getZindex();

            int zTop = 0;
            int zLeft = 0;

            SizeableFloater floater = cascadeFloater( zNonLockedFloaters, pMaxZindex, 0, zTop, zLeft );

            int delta = Math.max( floater.getTitleBarHeight(), 25 );

            for ( int i = 1; i < zNonLockedFloaters.size(); i++ ) {
                floater = cascadeFloater( zNonLockedFloaters, pMaxZindex, i, zLeft += delta, zTop += delta );
            }
            return floater;
        }

        private SizeableFloater cascadeFloater( List<SizeableFloater> pFloaters, int pMaxZindex, int pIndex,
                                                int pTop, int pLeft ) {
            SizeableFloater floater = pFloaters.get( pIndex );

            floater.setPosition( pLeft, pTop );
            floater.setZindex( pMaxZindex + pIndex + 1 );

            mChanged |= !mFloaterPositions[pIndex].isEquivalent( floater );

            return floater;
        }

        public boolean isChanged() {
            return mChanged;
        }
    }

    // vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv No Additional Style Support vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    public void setStylePrimaryName( String style ) {
        throw new IllegalStateException( "No Style Support" );
    }

    public void addStyleDependentName( String styleSuffix ) {
        throw new IllegalStateException( "No Style Support" );
    }

    public void addStyleName( String style ) {
        throw new IllegalStateException( "No Style Support" );
    }

    protected Element getStyleElement() {
        throw new IllegalStateException( "No Style Support" );
    }
}

Commits for litesoft/trunk/Java/GWT/OldClient/src/org/litesoft/GWT/client/widgets/SizeableFloatableContainerPanel.java

Diff revisions: vs.
Revision Author Commited Message
950 Diff Diff GeorgeS picture GeorgeS Thu 19 Jun, 2014 17:57:04 +0000

New Lines

948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

947 Diff Diff GeorgeS picture GeorgeS Fri 06 Jun, 2014 23:36:56 +0000

Correct Spelling of package!

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

Extracting commonfoundation

805 Diff Diff GeorgeS picture GeorgeS Wed 15 Aug, 2012 12:53:09 +0000
60 Diff Diff GeorgeS picture GeorgeS Tue 24 Aug, 2010 00:37:36 +0000
49 Diff Diff GeorgeS picture GeorgeS Mon 12 Apr, 2010 02:59:10 +0000

License Text

2 GeorgeS picture GeorgeS Sun 07 Feb, 2010 12:50:58 +0000