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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
package com.temp.client.foundation.widget.table.datatableadapter;

import com.google.gwt.cell.client.*;
import com.google.gwt.cell.client.Cell.*;
import com.google.gwt.core.client.*;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.*;
import com.google.gwt.dom.client.Style.*;
import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.shared.*;
import com.google.gwt.safehtml.client.*;
import com.google.gwt.safehtml.shared.*;
import com.google.gwt.user.cellview.client.*;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.*;

import java.util.*;

/**
 * Derived from:
 * https://p4db.amazon.com/source-views/p4db/brazil/fileViewer
 * .cgi?FSPC=//brazil/src/appgroup/webservices/apps/AWSConsole-GWT-Widgets/mainline/src/com/amazonaws/console/gwt/ui/DataTable.java&REV=3
 * <p/>
 * This widget is derived from DataGrid, and behaves very similar to it,
 * except that it adds interactively resize-able columns, and an AWS
 * console look and feel.
 * <p/>
 * <p/>
 * Although this widget is functionally equivalent to a DataGrid, it can't
 * be used in exactly the same way as a DataGrid can be used. The following
 * additional rules must be followed for this table to work correctly.
 * <p/>
 * <p><ol>
 * <li>
 * Since the data area of this widget scrolls and therefore has no predetermined
 * size, this table should be placed inside a LayoutPanel. If it's not, then you
 * need to modify the positioning attributes of <tt>DataGrid.getElement().getStyle()</tt>
 * to make it takes up non-zero vertical space, and call {@link #onResize() onResize}
 * whenever the size of the table area may have changed.
 * <li>
 * For this table to function correctly, developers should not set the width of
 * columns directly using one of the setColumnWidth methods. Instead, they should
 * call setFixedColumnWidth to set the width of fixed-width columns, or
 * setRelativeColumnWidth to set the relative width of columns that should expand
 * to fill whatever space is not used by fixed-width columns. These functions
 * should generally only be used once to set the initial width of columns when
 * they are added to the table, or to show a column that was previously hidden by
 * calling clearColumnWidth, because otherwise user's column width adjustments
 * could be lost.
 * <li>
 * DataGrid always used fixed table layout. A good layout strategy is to call
 * setWidth("100%"), and to have at least one relative width column that will
 * expand to fill any space not used by fixed-width columns. NOTE: this means
 * that you *must* call setFixedColumnWidth or setRelativeColumnWidth, or a
 * column will not be visible.
 * </ol>
 *
 * @param <T> Type of data object stored in the table.
 *
 * @author Stephen Saville (saville@amazon.com)
 */

public class EIS_DataTable<T> extends DataGrid<T> implements RequiresResize,
                                                             MouseDownHandler,
                                                             MouseMoveHandler,
                                                             MouseUpHandler {
    public static final int DEFAULT_PAGE_SIZE = 15;

    public static final String RESIZE_HANDLE_ATTRIBUTE = "resize-col";

    public static final String TABLE_CONTAINER_CLASS_NAME = "aws-DataTableContainer";

    interface Templates extends SafeHtmlTemplates {
        @Template("<div class=\"{0}\">")
        SafeHtml resizableHeaderCellPrefix( String headerClassName );

        @Template("<div " + RESIZE_HANDLE_ATTRIBUTE + "=\"{0}\" class=\"{1}\"> </div></div>")
        SafeHtml resizableHeaderCellSuffix( int col, String resizeHandleClassName );
    }

    private static final Templates TEMPLATES = GWT.create( Templates.class );

    public class DataTableOverlayHeaderCell<V> implements Cell<V> {
        private final Cell<V> delegate;

        public DataTableOverlayHeaderCell( Cell<V> delegate ) {
            this.delegate = delegate;
        }

        public boolean dependsOnSelection() {
            return delegate.dependsOnSelection();
        }

        public Set<String> getConsumedEvents() {
            return delegate.getConsumedEvents();
        }

        public boolean handlesSelection() {
            return delegate.handlesSelection();
        }

        public boolean isEditing(
                com.google.gwt.cell.client.Cell.Context context,
                Element parent, V value ) {
            return delegate.isEditing( context, parent, value );
        }

        public void onBrowserEvent(
                com.google.gwt.cell.client.Cell.Context context,
                Element parent, V value, NativeEvent event,
                ValueUpdater<V> valueUpdater ) {
            delegate.onBrowserEvent( context, parent, value, event, valueUpdater );
        }

        public void render( com.google.gwt.cell.client.Cell.Context context,
                            V value, SafeHtmlBuilder sb ) {
            sb.append( TEMPLATES.resizableHeaderCellPrefix(
                    styles.dataTableHeader() ) );
            delegate.render( context, value, sb );
            sb.append( TEMPLATES.resizableHeaderCellSuffix(
                    context.getColumn(), styles.dataTableResizeHandle() ) );
        }

        public boolean resetFocus(
                com.google.gwt.cell.client.Cell.Context context,
                Element parent, V value ) {
            return delegate.resetFocus( context, parent, value );
        }

        public void setValue( com.google.gwt.cell.client.Cell.Context context,
                              Element parent, V value ) {
            delegate.setValue( context, parent, value );
        }
    }

    public class DataTableOverlayHeader<V> extends Header<V> {
        private final Header<V> delegate;

        public DataTableOverlayHeader( Header<V> delegate ) {
            super( new DataTableOverlayHeaderCell<V>( delegate.getCell() ) );
            this.delegate = delegate;
        }

        public Object getKey() {
            return delegate.getKey();
        }

        public V getValue() {
            return delegate.getValue();
        }

        public void onBrowserEvent( Context context, Element elem,
                                    NativeEvent event ) {
            delegate.onBrowserEvent( context, elem, event );
        }

        public void setUpdater( ValueUpdater<V> updater ) {
            delegate.setUpdater( updater );
        }
    }

    public <V> DataTableOverlayHeader<V> overlayHeader( Header<V> header ) {
        return new DataTableOverlayHeader<V>( header );
    }

    public class DataTableSafeHtmlHeaderCell extends AbstractCell<SafeHtml> {
        @Override
        public void render( com.google.gwt.cell.client.Cell.Context context,
                            SafeHtml value, SafeHtmlBuilder sb ) {
            sb.append( TEMPLATES.resizableHeaderCellPrefix(
                    styles.dataTableHeader() ) );
            sb.append( value );
            sb.append( TEMPLATES.resizableHeaderCellSuffix(
                    context.getColumn(), styles.dataTableResizeHandle() ) );
        }
    }

    public class DataTableSafeHtmlHeader extends Header<SafeHtml> {
        private SafeHtml value;

        public DataTableSafeHtmlHeader( SafeHtml html ) {
            super( new DataTableSafeHtmlHeaderCell() );
            this.value = html;
        }

        public DataTableSafeHtmlHeader( String text ) {
            this( SafeHtmlUtils.fromString( text ) );
        }

        @Override
        public SafeHtml getValue() {
            return value;
        }
    }

    private final Map<Column<T, ?>, Double> relativeWidthColumns =
            new LinkedHashMap<Column<T, ?>, Double>();

    private final Set<Column<T, ?>> fixedWidthColumns =
            new HashSet<Column<T, ?>>();

    private final EIS_DataTableStyle styles;

    private static final int MIN_RESIZED_WIDTH = 10;

    private boolean resizingColumns = false;
    private Column<T, ?> resizeLeftColumn, resizeRightColumn;
    private int resizeLeftX, resizeRightX, resizeTotalWidth, borderOverhead;
    private HandlerRegistration mouseMoveRegistration, mouseUpRegistration;

    private static BrowserTableResizeSupport RESIZE_SUPPORT;

    public EIS_DataTable() {
        this( DEFAULT_PAGE_SIZE );
    }

    public EIS_DataTable( int pageSize, ProvidesKey<T> keyProvider ) {
        this( pageSize, GWT.<EIS_DataTableResources>create( EIS_DataTableResources.class ), keyProvider );
    }

    public EIS_DataTable( int pageSize ) {
        this( pageSize, new SimpleKeyProvider<T>() );
    }

    public EIS_DataTable( ProvidesKey<T> keyProvider ) {
        this( DEFAULT_PAGE_SIZE, keyProvider );
    }

    public EIS_DataTable( int pageSize, EIS_DataTableResources resources, ProvidesKey<T> keyProvider ) {
        super( pageSize, resources, keyProvider );
        styles = resources.dataGridStyle();
        borderOverhead = styles.cellBorderWidth();
        finiInit();
    }

    public EIS_DataTable( int pageSize, EIS_DataTableResources resources, ProvidesKey<T> keyProvider, Widget loadingIndicator ) {
        super( pageSize, resources, keyProvider, loadingIndicator );
        styles = resources.dataGridStyle();
        borderOverhead = styles.cellBorderWidth();
        finiInit();
    }

    private void finiInit() {
        if ( RESIZE_SUPPORT == null ) {
            RESIZE_SUPPORT = GWT.create( BrowserTableResizeSupport.class );
        }
        addDomHandler( this, MouseDownEvent.getType() );
    }

    @Override
    public void addColumn( Column<T, ?> col ) {
        super.addColumn( col, new DataTableSafeHtmlHeader( SafeHtmlUtils.EMPTY_SAFE_HTML ) );
    }

    @Override
    public void addColumn( Column<T, ?> col, SafeHtml headerHtml ) {
        super.addColumn( col, new DataTableSafeHtmlHeader( headerHtml ) );
    }

    @Override
    public void addColumn( Column<T, ?> col, String headerString ) {
        super.addColumn( col, new DataTableSafeHtmlHeader( headerString ) );
    }

    @Override
    public void addColumn( Column<T, ?> col, Header<?> header ) {
        super.addColumn( col, overlayHeader( header ) );
    }

    @Override
    public void addColumn( Column<T, ?> col, Header<?> header, Header<?> footer ) {
        super.addColumn( col, overlayHeader( header ), footer );
    }

    @Override
    public void addColumn( Column<T, ?> col, String headerString, String footerString ) {
        super.addColumn( col, new DataTableSafeHtmlHeader( headerString ), new TextHeader( footerString ) );
    }

    @Override
    public void addColumn( Column<T, ?> col, SafeHtml headerHtml, SafeHtml footerHtml ) {
        super.addColumn( col, new DataTableSafeHtmlHeader( headerHtml ), new SafeHtmlHeader( footerHtml ) );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, Header<?> header ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, header );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, SafeHtml headerHtml ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, headerHtml );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, SafeHtml headerHtml, SafeHtml footerHtml ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, headerHtml, footerHtml );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, String headerString ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, headerString );
    }

    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, String headerString, String footerString ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, headerString, footerString );
    }

    /**
     * Everything goes thru here!
     */
    @Override
    public void insertColumn( int beforeIndex, Column<T, ?> col, Header<?> header, Header<?> footer ) {
        // todo: we need to proxy so that we can control all this!
        super.insertColumn( beforeIndex, col, header, footer );
    }

    @Override
    public void removeColumn( int index ) {
        removeColumn( getColumn( index ) );
    }

    @Override
    public void removeColumn( Column<T, ?> col ) {
        super.removeColumn( col );
        removeTrackedColumnWidths( col );
    }

    @Override
    public void setColumnWidth( Column<T, ?> column, String width ) {
        if ( !width.endsWith( Unit.PCT.getType() ) ) {
            setFixedColumnWidth( column, width );
        } else {
            setRelativeColumnWidth( column, Double.parseDouble( width.substring( 0, width.length() - Unit.PCT.getType().length() ) ) );
        }
    }

    protected void superSetColumnWidth( Column<T, ?> column, String width ) {
        super.setColumnWidth( column, width );
    }

    protected void setOnResizeColumnWidthInPixels( Column<T, ?> column, int resizedWidth ) {
        superSetColumnWidth( column, "" + RESIZE_SUPPORT.adjustColumnWidthInPixelsForBorder( borderOverhead, resizedWidth ) + Unit.PX );
    }

    private void setFixedColumnWidth( Column<T, ?> column, String fixedWidth ) {
        removeTrackedColumnWidths( column );
        if ( -1 != getColumnIndex( column ) ) {
            fixedWidthColumns.add( column );
        }
        superSetColumnWidth( column, fixedWidth );
    }

    /**
     * Set the relative width of a column that should expand to take up any
     * space not used by fixed-width columns. Relative widths are normalized
     * across relative width columns, so, for example, if you add two columns
     * with relatives widths of 1 and 2, then the first column will take up
     * 33% of the available space, and the second column will take up 66% of
     * the available space. If you were to add a third column with a relative
     * width of 1, then the columns would take up 25%, 50% and 25% of the
     * available space, respectively.
     *
     * @param column        Column to set the relative width of.
     * @param relativeWidth Relative column width.
     */
    public void setRelativeColumnWidth( Column<T, ?> column, double relativeWidth ) {
        clearColumnWidth( column );
        if ( -1 != getColumnIndex( column ) ) {
            relativeWidthColumns.put( column, relativeWidth );
        }
    }

    /**
     * Set the width of a fixed-width column.
     *
     * @param column     Column to set the width of.
     * @param fixedWidth Fixed width in the given units.
     * @param unit       Fixed width unit, such as {@link Unit#PX}. Must <it>not</it> be {@link Unit#PCT}.
     */
    public void setFixedColumnWidth( Column<T, ?> column, double fixedWidth, Unit unit ) {
        if ( Unit.PCT.equals( unit ) ) {
            throw new IllegalArgumentException( "PCT (percent) is NOT applicable to Fixed Column Width" );
        }
        setFixedColumnWidth( column, "" + fixedWidth + unit );
    }

    /**
     * Set the width of a fixed-width column.
     *
     * @param column     Column to set the width of.
     * @param pixelWidth Fixed width in pixels.
     */
    public void setFixedColumnWidth( Column<T, ?> column, int pixelWidth ) {
        setFixedColumnWidth( column, "" + pixelWidth + Unit.PX );
    }

    /**
     * Clear the width of a column. This will make the column disappear.
     *
     * @param column Column to clear the width of.
     */
    @Override
    public void clearColumnWidth( Column<T, ?> column ) {
        removeTrackedColumnWidths( column );

        // clear the width of this column
        super.clearColumnWidth( column );
    }

    private void removeTrackedColumnWidths( Column<T, ?> col ) {
        relativeWidthColumns.remove( col );
        fixedWidthColumns.remove( col );
    }

    @Override
    public void onResize() {
        super.onResize();
        if ( !resizePending ) {
            if ( !isAttached() ) {
                resizePending = true;
            } else if ( !resizeColumns() ) {
                resizePending = true;
                launchResizer();
            }
        }
    }

    @Override
    protected void onAttach() {
        super.onAttach();
        if ( resizePending ) {
            launchResizer();
        }
    }

    @Override
    protected void onDetach() {
        super.onDetach();
        onMouseUp( null ); // Cleanup dynamic Handlers
        killResizer();
    }

    private static final int INITIAL_RESIZE_DELAY_MILLIS = 100;

    private boolean resizePending;
    private Timer resizeTimer;
    private int currentResizeDelay;

    private void killResizer() {
        if ( resizeTimer != null ) {
            resizeTimer.cancel();
            resizeTimer = null;
        }
    }

    private void launchResizer() {
        killResizer();
        (resizeTimer = new Timer() {
            @Override
            public void run() {
                if ( isAttached() && resizePending ) {
                    if ( resizeColumns() ) {
                        resizePending = false;
                        resizeTimer = null;
                        return;
                    }
                    currentResizeDelay += currentResizeDelay;
                    Timer timer = resizeTimer; // Snag a reference
                    if ( (timer != null) && (currentResizeDelay < 12800) ) {
                        timer.schedule( currentResizeDelay );
                        return;
                    }
                }
                resizeTimer = null;
            }
        }).schedule( currentResizeDelay = INITIAL_RESIZE_DELAY_MILLIS );
    }

    /**
     * @return true is resizing was successful!
     */
    protected boolean resizeColumns() {
        Element headerRow = getTableHeadElement().getRows().getItem( 0 );
        if ( headerRow == null ) {
            return false; // No Header Row?
        }
        NodeList<Node> headerRowChildren = headerRow.getChildNodes();
        int headerRowChildCount = headerRowChildren.getLength();
        if ( headerRowChildCount == 0 ) {
            return false; // No Columns?
        }
        int availableWidth = getElement().getClientWidth();
        if ( availableWidth < (headerRowChildCount * 10) ) {
            return false; // Overall Sizing is probably not Working Yet or ?
        }

        // find the width of all fixed-width columns, in pixels
        Set<Column<T, ?>> unspecifiedColumnWidths = new HashSet<Column<T, ?>>();
        int totalWidthOfFixedColumns = 0;
        for ( int i = 0, col = 0; i < headerRowChildCount; i++, col++ ) {
            Node child = headerRowChildren.getItem( i );
            if ( Element.is( child ) ) {
                Element childElement = Element.as( child );
                int columnWidth = RESIZE_SUPPORT.getColumnWidthInPixels( borderOverhead, childElement );
                if ( columnWidth < 1 ) {
                    return false; // Sizing Not Working Yet
                }
                Column<T, ?> column = getColumn( col );
                if ( !relativeWidthColumns.containsKey( column ) ) {
                    if ( fixedWidthColumns.contains( column ) ) {
                        totalWidthOfFixedColumns += columnWidth;
                    } else {
                        unspecifiedColumnWidths.add( column );
                    }
                }
            }
        }

        if ( relativeWidthColumns.isEmpty() && unspecifiedColumnWidths.isEmpty() ) {
            System.out.println( "No Column Adjustability Allowed *** Probable Specification Error ***" );
            return true; // Nothing to do!
        }
        if ( !unspecifiedColumnWidths.isEmpty() ) {
            double relativeWidth = getCurrentRelativeMinimumWidth();
            for ( Column<T, ?> column : unspecifiedColumnWidths ) {
                setRelativeColumnWidth( column, relativeWidth );
            }
        }
        distributeToRelativeWidthColumns( availableWidth - totalWidthOfFixedColumns );

        // finally adjust child column widths
        redraw();
        return true;
    }

    private double getCurrentRelativeMinimumWidth() {
        double lowest = 100; // 100%
        for ( Double value : relativeWidthColumns.values() ) {
            if ( value < lowest ) {
                lowest = value;
            }
        }
        return lowest;
    }

    protected void applyColumnResize( final int clientX ) {
        if ( ((resizeLeftX + MIN_RESIZED_WIDTH) < clientX) && (clientX < (resizeRightX - MIN_RESIZED_WIDTH)) ) {
            int leftWidth = clientX - resizeLeftX;
            int rightWidth = resizeRightX - clientX;

            // fix up new widths in case they don't add up correctly
            int totalWidth = leftWidth + rightWidth;
            if ( totalWidth != resizeTotalWidth ) {
                int totalAdjustment = resizeTotalWidth - totalWidth;
                int leftAdjustment = totalAdjustment / 2;
                int rightAdjustment = totalAdjustment - leftAdjustment;
                leftWidth += leftAdjustment;
                rightWidth += rightAdjustment;
            }

            setFixedColumnWidth( resizeLeftColumn, leftWidth, Unit.PX );
            setFixedColumnWidth( resizeRightColumn, rightWidth, Unit.PX );
        }
        redraw();
    }

    protected void startColumnResize( final int col, final Element target, final int clientX ) {
        resizeLeftColumn = getColumn( col );
        resizeRightColumn = getColumn( col + 1 );

        NodeList<TableCellElement> headerCells = getTableHeadElement().getRows().getItem( 0 ).getCells();
        TableCellElement leftTableHeader = headerCells.getItem( col );
        TableCellElement rightTableHeader = headerCells.getItem( col + 1 );

        int leftWidth = RESIZE_SUPPORT.getColumnWidthInPixels( borderOverhead, leftTableHeader );
        int rightWidth = RESIZE_SUPPORT.getColumnWidthInPixels( borderOverhead, rightTableHeader );
        resizeLeftX = clientX - leftWidth;
        resizeRightX = clientX + rightWidth;
        resizeTotalWidth = leftWidth + rightWidth;
    }

    @Override
    public void onMouseDown( MouseDownEvent event ) {
        if ( !resizingColumns ) {
            EventTarget eventTarget = event.getNativeEvent().getEventTarget();
            if ( !Element.is( eventTarget ) ) {
                return;
            }
            final Element target = Element.as( eventTarget );

            if ( target.hasAttribute( RESIZE_HANDLE_ATTRIBUTE ) ) {
                // figure out which column this event is for
                int col = Integer.parseInt( target.getAttribute( RESIZE_HANDLE_ATTRIBUTE ) );
                if ( col < getColumnCount() - 1 ) {
                    startColumnResize( col, target, event.getClientX() );
                    resizingColumns = true;
                    RootLayoutPanel root = RootLayoutPanel.get();
                    if ( mouseMoveRegistration == null ) {
                        mouseMoveRegistration = root.addDomHandler( this, MouseMoveEvent.getType() );
                    }
                    if ( mouseUpRegistration == null ) {
                        mouseUpRegistration = root.addDomHandler( this, MouseUpEvent.getType() );
                    }
                }
            }
        }
    }

    @Override
    public void onMouseMove( MouseMoveEvent event ) {
        if ( resizingColumns ) {
            applyColumnResize( event.getClientX() );
        }
    }

    public void onMouseUp( com.google.gwt.event.dom.client.MouseUpEvent event ) {
        if ( resizingColumns ) {
            resizingColumns = false;
            if ( mouseMoveRegistration != null ) {
                mouseMoveRegistration.removeHandler();
                mouseMoveRegistration = null;
            }
            if ( mouseUpRegistration != null ) {
                mouseUpRegistration.removeHandler();
                mouseUpRegistration = null;
            }
        }
    }

    private static class RelativeWidthColumnControl<T> implements Comparable<RelativeWidthColumnControl<T>> {
        private Column<T, ?> column;
        private double width;
        private int desiredPixelWidth;

        public RelativeWidthColumnControl( Column<T, ?> column, double width ) {
            this.column = column;
            this.width = width;
        }

        public Column<T, ?> getColumn() {
            return column;
        }

        public double getWidth() {
            return width;
        }

        public int getDesiredPixelWidth() {
            return desiredPixelWidth;
        }

        /**
         * Note CompareTo NOT compatible with equals()!
         */
        @Override
        public int compareTo( RelativeWidthColumnControl<T> them ) {
            if ( them.width > this.width ) {
                return 1;
            }
            if ( them.width < this.width ) {
                return -1;
            }
            return 0;
        }

        public int distribute( int available, int overColumnsRemaining, double remainingTotalWidths ) {
            if ( overColumnsRemaining == 1 ) {
                desiredPixelWidth = Math.max( available, MIN_RESIZED_WIDTH );
                return 0;
            }
            double proportionatePixels = available * (width / remainingTotalWidths); // percentage
            if ( overColumnsRemaining < 10 ) { // favor the end...
                proportionatePixels += (10 - overColumnsRemaining) / 10f;
            }
            int pixels = Math.min( (int) proportionatePixels, available );
            desiredPixelWidth = Math.max( pixels, MIN_RESIZED_WIDTH );
            return available - pixels;
        }
    }

    private void distributeToRelativeWidthColumns( int pixelsToDistribute ) {
        // create List & sum Relative Widths
        List<RelativeWidthColumnControl<T>> controls = new ArrayList<RelativeWidthColumnControl<T>>( relativeWidthColumns.size() );
        double totalWidths = 0;
        for ( Map.Entry<Column<T, ?>, Double> entry : relativeWidthColumns.entrySet() ) {
            Double proportionateWidth = entry.getValue();
            controls.add( new RelativeWidthColumnControl<T>( entry.getKey(), proportionateWidth ) );
            totalWidths += proportionateWidth;
        }
        // Distribute the Pixels
        for ( int i = 0; i < controls.size(); i++ ) {
            RelativeWidthColumnControl<T> control = controls.get( i );
            pixelsToDistribute = control.distribute( pixelsToDistribute, controls.size() - i, totalWidths );
            setOnResizeColumnWidthInPixels( control.getColumn(),
                                            RESIZE_SUPPORT.adjustColumnWidthInPixelsForBorder( borderOverhead, control.getDesiredPixelWidth() ) );
            totalWidths -= control.getWidth();
        }
    }
}

Commits for litesoft/trunk/GWT_Sandbox/FormEngine/src/com/temp/client/foundation/widget/table/datatableadapter/EIS_DataTable.java

Diff revisions: vs.
Revision Author Commited Message
948 Diff Diff GeorgeS picture GeorgeS Sat 07 Jun, 2014 23:42:39 +0000

Jusefuls Formatter Updated to New Code Format

626 GeorgeS picture GeorgeS Wed 11 Apr, 2012 19:39:41 +0000