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
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.gen2.table.client;

import com.google.gwt.dom.client.*;
import com.google.gwt.gen2.event.shared.*;
import com.google.gwt.gen2.table.event.client.*;
import com.google.gwt.gen2.table.event.client.TableEvent.*;
import com.google.gwt.user.client.*;
import com.google.gwt.user.client.Element;

import java.util.*;

/**
 * A variation of the {@link Grid} that supports row or cell highlight and row
 * selection.
 * <p/>
 * <h3>CSS Style Rules</h3>
 * <p/>
 * <ul class="css">
 * <p/>
 * <li>tr.selected { applied to selected rows }</li>
 * <p/>
 * <li>tr.highlighted { applied to row currently being highlighted }</li>
 * <p/>
 * <li>td.highlighted { applied to cell currently being highlighted }</li>
 * <p/>
 * </ul>
 */
public class SelectionGrid extends Grid implements HasRowHighlightHandlers,
                                                   HasRowUnhighlightHandlers,
                                                   HasCellHighlightHandlers,
                                                   HasCellUnhighlightHandlers,
                                                   HasRowSelectionHandlers {
    /**
     * This class contains methods used to format a table's cells.
     */
    public class SelectionGridCellFormatter extends CellFormatter {
        @Override
        protected Element getRawElement( int row, int column ) {
            if ( selectionPolicy.hasInputColumn() ) {
                column += 1;
            }
            return super.getRawElement( row, column );
        }
    }

    /**
     * This class contains methods used to format a table's rows.
     */
    public class SelectionGridRowFormatter extends RowFormatter {
        @Override
        protected Element getRawElement( int row ) {
            return super.getRawElement( row );
        }
    }

    /**
     * Selection policies.
     * <p/>
     * <ul>
     * <li>ONE_ROW - one row can be selected at a time</li>
     * <li>MULTI_ROW - multiple rows can be selected at a time</li>
     * <li>CHECKBOX - multiple rows can be selected using checkboxes</li>
     * <li>RADIO - one row can be selected using radio buttons</li>
     * </ul>
     */
    public static enum SelectionPolicy {
        ONE_ROW( null ), MULTI_ROW( null ), CHECKBOX( "<input type='checkbox'/>" ), RADIO( "<input name='%NAME%' type='radio'/>" );

        private String inputHtml;

        private SelectionPolicy( String inputHtml ) {
            this.inputHtml = inputHtml;
        }

        /**
         * @return true if the policy requires a selection column
         */
        public boolean hasInputColumn() {
            return inputHtml != null;
        }

        /**
         * @return the HTML string used for this policy
         */
        private String getInputHtml() {
            return inputHtml;
        }
    }

    /**
     * Unique IDs assigned to the grids.
     */
    private static int uniqueID = 0;

    /**
     * The cell element currently being highlight.
     */
    private Element highlightedCellElem = null;

    /**
     * The index of the cell currently being highlight.
     */
    private int highlightedCellIndex = -1;

    /**
     * The row element currently being highlight.
     */
    private Element highlightedRowElem = null;

    /**
     * The index of the row currently being highlight.
     */
    private int highlightedRowIndex = -1;

    /**
     * Unique ID of this grid.
     */
    private int id;

    /**
     * The index of the row that the user selected last.
     */
    private int lastSelectedRowIndex = -1;

    /**
     * The rows that are currently selected.
     */
    private Map<Integer, Element> selectedRows = new HashMap<Integer, Element>();

    /**
     * A boolean indicating if selection is enabled disabled.
     */
    private boolean selectionEnabled = true;

    /**
     * The selection policy determines if the user can select zero, one, or many
     * rows.
     */
    private SelectionPolicy selectionPolicy = SelectionPolicy.MULTI_ROW;

    /**
     * Construct a new {@link SelectionGrid}.
     */
    public SelectionGrid() {
        super();
        id = uniqueID++;
        setCellFormatter( new SelectionGridCellFormatter() );
        setRowFormatter( new SelectionGridRowFormatter() );

        // Sink highlight and selection events
        sinkEvents( Event.ONMOUSEOVER | Event.ONMOUSEOUT | Event.ONMOUSEDOWN | Event.ONCLICK );
    }

    /**
     * Constructs a {@link SelectionGrid} with the requested size.
     *
     * @param rows    the number of rows
     * @param columns the number of columns
     *
     * @throws IndexOutOfBoundsException
     */
    public SelectionGrid( int rows, int columns ) {
        this();
        resize( rows, columns );
    }

    @Override
    public HandlerRegistration addCellHighlightHandler( CellHighlightHandler handler ) {
        return addHandler( CellHighlightEvent.TYPE, handler );
    }

    @Override
    public HandlerRegistration addCellUnhighlightHandler( CellUnhighlightHandler handler ) {
        return addHandler( CellUnhighlightEvent.TYPE, handler );
    }

    @Override
    public HandlerRegistration addRowHighlightHandler( RowHighlightHandler handler ) {
        return addHandler( RowHighlightEvent.TYPE, handler );
    }

    @Override
    public HandlerRegistration addRowSelectionHandler( RowSelectionHandler handler ) {
        return addHandler( RowSelectionEvent.TYPE, handler );
    }

    @Override
    public HandlerRegistration addRowUnhighlightHandler( RowUnhighlightHandler handler ) {
        return addHandler( RowUnhighlightEvent.TYPE, handler );
    }

    /**
     * Deselect all selected rows in the data table.
     */
    public void deselectAllRows() {
        deselectAllRows( true );
    }

    /**
     * Deselect a row in the grid. This method is safe to call even if the row is
     * not selected, or doesn't exist (out of bounds).
     *
     * @param row the row index
     */
    public void deselectRow( int row ) {
        deselectRow( row, true );
    }

    /**
     * @return the set of selected row indexes
     */
    public Set<Integer> getSelectedRows() {
        return selectedRows.keySet();
    }

    /**
     * Explicitly gets the {@link SelectionGridCellFormatter}. The results of
     * {@link HTMLTable#getCellFormatter()}
     * may also be downcast to a {@link SelectionGridCellFormatter}.
     *
     * @return the FlexTable's cell formatter
     */
    public SelectionGridCellFormatter getSelectionGridCellFormatter() {
        return (SelectionGridCellFormatter) getCellFormatter();
    }

    /**
     * Explicitly gets the {@link SelectionGridRowFormatter}. The results of
     * {@link HTMLTable#getRowFormatter()}
     * may also be downcast to a {@link SelectionGridRowFormatter}.
     *
     * @return the FlexTable's cell formatter
     */
    public SelectionGridRowFormatter getSelectionGridRowFormatter() {
        return (SelectionGridRowFormatter) getRowFormatter();
    }

    /**
     * @return the selection policy
     */
    public SelectionPolicy getSelectionPolicy() {
        return selectionPolicy;
    }

    @Override
    public int insertRow( int beforeRow ) {
        deselectAllRows();
        return super.insertRow( beforeRow );
    }

    /**
     * @param row the row index
     *
     * @return true if the row is selected, false if not
     */
    public boolean isRowSelected( int row ) {
        return selectedRows.containsKey( new Integer( row ) );
    }

    /**
     * @return true if selection is enabled
     */
    public boolean isSelectionEnabled() {
        return selectionEnabled;
    }

    @Override
    public void onBrowserEvent( Event event ) {
        super.onBrowserEvent( event );
        Element targetRow = null;
        Element targetCell = null;

        switch ( DOM.eventGetType( event ) ) {
            // Highlight the cell on mouse over
            case Event.ONMOUSEOVER:
                Element cellElem = getEventTargetCell( event );
                if ( cellElem != null ) {
                    highlightCell( cellElem );
                }
                break;

            // Unhighlight on mouse out
            case Event.ONMOUSEOUT:
                Element toElem = DOM.eventGetToElement( event );
                if ( highlightedRowElem != null && (toElem == null || !highlightedRowElem.isOrHasChild( toElem )) ) {
                    // Check that the coordinates are not directly over the cell
                    int clientX = event.getClientX() + Window.getScrollLeft();
                    int clientY = event.getClientY() + Window.getScrollTop();
                    int rowLeft = highlightedRowElem.getAbsoluteLeft();
                    int rowTop = highlightedRowElem.getAbsoluteTop();
                    int rowWidth = highlightedRowElem.getOffsetWidth();
                    int rowHeight = highlightedRowElem.getOffsetHeight();
                    int rowBottom = rowTop + rowHeight;
                    int rowRight = rowLeft + rowWidth;
                    if ( clientX > rowLeft && clientX < rowRight && clientY > rowTop && clientY < rowBottom ) {
                        return;
                    }

                    // Unhighlight the current cell
                    highlightCell( null );
                }
                break;

            // Select a row on click
            case Event.ONMOUSEDOWN: {
                // Ignore if selection is disabled
                if ( !selectionEnabled ) {
                    return;
                }

                // Get the target row
                targetCell = getEventTargetCell( event );
                if ( targetCell == null ) {
                    return;
                }
                targetRow = DOM.getParent( targetCell );
                int targetRowIndex = getRowIndex( targetRow );

                // Select the row
                if ( selectionPolicy == SelectionPolicy.MULTI_ROW ) {
                    boolean shiftKey = DOM.eventGetShiftKey( event );
                    boolean ctrlKey = DOM.eventGetCtrlKey( event ) || DOM.eventGetMetaKey( event );

                    // Prevent default text selection
                    if ( ctrlKey || shiftKey ) {
                        event.preventDefault();
                    }

                    // Select the rows
                    selectRow( targetRowIndex, ctrlKey, shiftKey );
                } else if ( selectionPolicy == SelectionPolicy.ONE_ROW ||
                            (selectionPolicy == SelectionPolicy.RADIO && targetCell == targetRow.getFirstChild()) ) {
                    selectRow( -1, targetRow, true, true );
                    lastSelectedRowIndex = targetRowIndex;
                }
            }
            break;

            // Prevent native inputs from being checked
            case Event.ONCLICK: {
                // Ignore if selection is disabled
                if ( !selectionEnabled ) {
                    return;
                }

                // Get the target row
                targetCell = getEventTargetCell( event );
                if ( targetCell == null ) {
                    return;
                }
                targetRow = DOM.getParent( targetCell );
                int targetRowIndex = getRowIndex( targetRow );

                // Select the row
                if ( selectionPolicy == SelectionPolicy.CHECKBOX && targetCell == targetRow.getFirstChild() ) {
                    selectRow( targetRowIndex, true, DOM.eventGetShiftKey( event ) );
                }
            }
            break;
        }
    }

    @Override
    public void removeRow( int row ) {
        deselectAllRows();
        super.removeRow( row );
    }

    /**
     * Select all rows in the table.
     */
    public void selectAllRows() {
        // Get the currently selected rows
        Set<Row> oldRowSet = getSelectedRowsSet();

        // Select all rows
        RowFormatter rowFormatter = getRowFormatter();
        int rowCount = getRowCount();
        for ( int i = 0; i < rowCount; i++ ) {
            if ( !selectedRows.containsKey( i ) ) {
                selectRow( i, rowFormatter.getElement( i ), false, false );
            }
        }

        // Trigger the event
        fireRowSelectionEvent( oldRowSet );
    }

    /**
     * Select a row in the data table.
     *
     * @param row         the row index
     * @param unselectAll unselect all other rows first
     *
     * @throws IndexOutOfBoundsException
     */
    public void selectRow( int row, boolean unselectAll ) {
        selectRow( row, getRowFormatter().getElement( row ), unselectAll, true );
    }

    /**
     * Select a row in the data table. Simulate the effect of a shift click and/or
     * control click. This method ignores the selection policy, which only applies
     * to user selection via mouse events.
     *
     * @param row      the row index
     * @param ctrlKey  true to simulate a control click
     * @param shiftKey true to simulate a shift selection
     *
     * @throws IndexOutOfBoundsException
     */
    public void selectRow( int row, boolean ctrlKey, boolean shiftKey ) {
        // Check the row bounds
        checkRowBounds( row );

        // Get the old list of selected rows
        Set<Row> oldRowList = getSelectedRowsSet();

        // Deselect all rows
        if ( !ctrlKey ) {
            deselectAllRows( false );
        }

        boolean isSelected = selectedRows.containsKey( new Integer( row ) );
        if ( shiftKey && (lastSelectedRowIndex > -1) ) {
            // Shift+select rows
            SelectionGridRowFormatter formatter = getSelectionGridRowFormatter();
            int firstRow = Math.min( row, lastSelectedRowIndex );
            int lastRow = Math.max( row, lastSelectedRowIndex );
            lastRow = Math.min( lastRow, getRowCount() - 1 );
            for ( int curRow = firstRow; curRow <= lastRow; curRow++ ) {
                if ( isSelected ) {
                    deselectRow( curRow, false );
                } else {
                    selectRow( curRow, formatter.getRawElement( curRow ), false, false );
                }
            }

            // Fire Event
            lastSelectedRowIndex = row;
            fireRowSelectionEvent( oldRowList );
        } else if ( isSelected ) {
            // Ctrl+unselect a selected row
            deselectRow( row, false );
            lastSelectedRowIndex = row;
            fireRowSelectionEvent( oldRowList );
        } else {
            // Select the row
            SelectionGridRowFormatter formatter = getSelectionGridRowFormatter();
            selectRow( row, formatter.getRawElement( row ), false, false );
            lastSelectedRowIndex = row;
            fireRowSelectionEvent( oldRowList );
        }
    }

    /**
     * Enable or disable row selection.
     *
     * @param enabled true to enable, false to disable
     */
    public void setSelectionEnabled( boolean enabled ) {
        selectionEnabled = enabled;

        // Update the input elements
        if ( selectionPolicy.hasInputColumn() ) {
            SelectionGridCellFormatter formatter = getSelectionGridCellFormatter();
            int rowCount = getRowCount();
            for ( int i = 0; i < rowCount; i++ ) {
                Element td = formatter.getRawElement( i, -1 );
                setInputEnabled( selectionPolicy, td, enabled );
            }
        }
    }

    /**
     * Set the selection policy, which determines if the user can select zero,
     * one, or multiple rows.
     *
     * @param selectionPolicy the selection policy
     */
    public void setSelectionPolicy( SelectionPolicy selectionPolicy ) {
        if ( this.selectionPolicy == selectionPolicy ) {
            return;
        }
        deselectAllRows();

        // Update the input column
        if ( selectionPolicy.hasInputColumn() ) {
            if ( this.selectionPolicy.hasInputColumn() ) {
                // Update the existing input column
                String inputHtml = getInputHtml( selectionPolicy );
                for ( int i = 0; i < numRows; i++ ) {
                    Element tr = getRowFormatter().getElement( i );
                    tr.getFirstChildElement().setInnerHTML( inputHtml );
                }
            } else {
                // Add an input column to every row
                String inputHtml = getInputHtml( selectionPolicy );
                Element td = createCell();
                td.setInnerHTML( inputHtml );
                for ( int i = 0; i < numRows; i++ ) {
                    Element tr = getRowFormatter().getElement( i );
                    tr.insertBefore( td.cloneNode( true ), tr.getFirstChildElement() );
                }
            }
        } else if ( this.selectionPolicy.hasInputColumn() ) {
            // Remove the input column from every row
            for ( int i = 0; i < numRows; i++ ) {
                Element tr = getRowFormatter().getElement( i );
                tr.removeChild( tr.getFirstChildElement() );
            }
        }
        this.selectionPolicy = selectionPolicy;

        // Update the enabled state
        setSelectionEnabled( selectionEnabled );
    }

    @Override
    protected Element createRow() {
        Element tr = super.createRow();
        if ( selectionPolicy.hasInputColumn() ) {
            Element td = createCell();
            td.setPropertyString( "align", "center" );
            td.setInnerHTML( getInputHtml( selectionPolicy ) );
            DOM.insertChild( tr, td, 0 );
            if ( !selectionEnabled ) {
                setInputEnabled( selectionPolicy, td, false );
            }
        }
        return tr;
    }

    /**
     * Deselect all selected rows in the data table.
     *
     * @param fireEvent true to fire events
     */
    protected void deselectAllRows( boolean fireEvent ) {
        // Get the old list of selected rows
        Set<Row> oldRows = null;
        if ( fireEvent ) {
            oldRows = getSelectedRowsSet();
        }

        // Deselect all rows
        boolean hasInputColumn = selectionPolicy.hasInputColumn();
        for ( Element rowElem : selectedRows.values() ) {
            setStyleName( rowElem, "selected", false );
            if ( hasInputColumn ) {
                setInputSelected( getSelectionPolicy(), (Element) rowElem.getFirstChildElement(), false );
            }
        }

        // Clear out the rows
        selectedRows.clear();

        // Fire event
        if ( fireEvent ) {
            fireRowSelectionEvent( oldRows );
        }
    }

    /**
     * Deselect a row in the grid. This method is safe to call even if the row is
     * not selected, or doesn't exist (out of bounds).
     *
     * @param row       the row index
     * @param fireEvent true to fire events
     */
    protected void deselectRow( int row, boolean fireEvent ) {
        Element rowElem = selectedRows.remove( new Integer( row ) );
        if ( rowElem != null ) {
            // Get the old list of selected rows
            Set<Row> oldRows = null;
            if ( fireEvent ) {
                oldRows = getSelectedRowsSet();
            }

            // Deselect the row
            setStyleName( rowElem, "selected", false );
            if ( selectionPolicy.hasInputColumn() ) {
                setInputSelected( getSelectionPolicy(), (Element) rowElem.getFirstChildElement(), false );
            }

            // Fire Event
            if ( fireEvent ) {
                fireRowSelectionEvent( oldRows );
            }
        }
    }

    /**
     * Fire a {@link RowSelectionEvent}. This method will automatically add the
     * currently selected rows.
     *
     * @param oldRowSet the set of previously selected rows
     */
    protected void fireRowSelectionEvent( Set<Row> oldRowSet ) {
        Set<Row> newRowList = getSelectedRowsSet();
        if ( newRowList.equals( oldRowSet ) ) {
            return;
        }
        fireEvent( new RowSelectionEvent( oldRowSet, newRowList ) );
    }

    @Override
    protected int getCellIndex( Element rowElem, Element cellElem ) {
        int index = super.getCellIndex( rowElem, cellElem );
        if ( selectionPolicy.hasInputColumn() ) {
            index--;
        }
        return index;
    }

    @Override
    protected int getDOMCellCount( int row ) {
        int count = super.getDOMCellCount( row );
        if ( getSelectionPolicy().hasInputColumn() ) {
            count--;
        }
        return count;
    }

    /**
     * Get the html used to create the native input selection element.
     *
     * @param selectionPolicy the associated {@link SelectionPolicy}
     *
     * @return the html representation of the input element
     */
    protected String getInputHtml( SelectionPolicy selectionPolicy ) {
        String inputHtml = selectionPolicy.getInputHtml();
        if ( inputHtml != null ) {
            inputHtml = inputHtml.replace( "%NAME%", "__gwtSelectionGrid" + id );
        }
        return inputHtml;
    }

    /**
     * @return a map or selected row indexes to their elements
     */
    protected Map<Integer, Element> getSelectedRowsMap() {
        return selectedRows;
    }

    /**
     * @return a list of selected rows to pass into a {@link RowSelectionEvent}
     */
    protected Set<Row> getSelectedRowsSet() {
        Set<Row> rowSet = new TreeSet<Row>();
        for ( Integer rowIndex : selectedRows.keySet() ) {
            rowSet.add( new Row( rowIndex.intValue() ) );
        }
        return rowSet;
    }

    /**
     * Set the current highlighted cell.
     *
     * @param cellElem the cell element
     */
    protected void highlightCell( Element cellElem ) {
        // Ignore if the cell is already being highlighted
        if ( cellElem == highlightedCellElem ) {
            return;
        }

        // Get the row element
        Element rowElem = null;
        if ( cellElem != null ) {
            rowElem = DOM.getParent( cellElem );
        }

        // Unhighlight the current cell
        if ( highlightedCellElem != null ) {
            setStyleName( highlightedCellElem, "highlighted", false );
            fireEvent( new CellUnhighlightEvent( highlightedRowIndex, highlightedCellIndex ) );
            highlightedCellElem = null;
            highlightedCellIndex = -1;

            // Unhighlight the current row if it changed
            if ( rowElem != highlightedRowElem ) {
                setStyleName( highlightedRowElem, "highlighted", false );
                fireEvent( new RowUnhighlightEvent( highlightedRowIndex ) );
                highlightedRowElem = null;
                highlightedRowIndex = -1;
            }
        }

        // Highlight the cell
        if ( cellElem != null ) {
            setStyleName( cellElem, "highlighted", true );
            highlightedCellElem = cellElem;
            highlightedCellIndex = OverrideDOM.getCellIndex( cellElem );

            // Highlight the row if it changed
            if ( highlightedRowElem == null ) {
                setStyleName( rowElem, "highlighted", true );
                highlightedRowElem = rowElem;
                highlightedRowIndex = getRowIndex( highlightedRowElem );
                fireEvent( new RowHighlightEvent( highlightedRowIndex ) );
            }

            // Fire listeners
            fireEvent( new CellHighlightEvent( highlightedRowIndex, highlightedCellIndex ) );
        }
    }

    /**
     * Select a row in the data table.
     *
     * @param row         the row index, or -1 if unknown
     * @param rowElem     the row element
     * @param unselectAll true to unselect all currently selected rows
     * @param fireEvent   true to fire the select event to listeners
     */
    protected void selectRow( int row, Element rowElem, boolean unselectAll, boolean fireEvent ) {
        // Get the row index if needed
        if ( row < 0 ) {
            row = getRowIndex( rowElem );
        }

        // Ignore request if row already selected
        Integer rowI = new Integer( row );
        if ( selectedRows.containsKey( rowI ) ) {
            return;
        }

        // Get the old list of selected rows
        Set<Row> oldRowSet = null;
        if ( fireEvent ) {
            oldRowSet = getSelectedRowsSet();
        }

        // Deselect current rows
        if ( unselectAll ) {
            deselectAllRows( false );
        }

        // Select the new row
        selectedRows.put( rowI, rowElem );
        setStyleName( rowElem, "selected", true );
        if ( selectionPolicy.hasInputColumn() ) {
            setInputSelected( getSelectionPolicy(), (Element) rowElem.getFirstChildElement(), true );
        }

        // Fire grid listeners
        if ( fireEvent ) {
            fireRowSelectionEvent( oldRowSet );
        }
    }

    @Override
    protected void setBodyElement( Element element ) {
        super.setBodyElement( element );
        if ( !selectionEnabled ) {
            setSelectionEnabled( selectionEnabled );
        }
    }

    /**
     * Enabled or disabled the native input element in the given cell. This method
     * should correspond with the HTML returned from {@link #getInputHtml}.
     *
     * @param selectionPolicy the associated {@link SelectionPolicy}
     * @param td              the cell containing the element
     * @param enabled         true to enable, false to disable
     */
    protected void setInputEnabled( SelectionPolicy selectionPolicy, Element td, boolean enabled ) {
        ((InputElement) td.getFirstChild()).setDisabled( !enabled );
    }

    /**
     * Select the native input element in the given cell. This method should
     * correspond with the HTML returned from {@link #getInputHtml}.
     *
     * @param selectionPolicy the associated {@link SelectionPolicy}
     * @param td              the cell containing the element
     * @param selected        true to select, false to deselect
     */
    protected void setInputSelected( SelectionPolicy selectionPolicy, Element td, boolean selected ) {
        ((InputElement) td.getFirstChild()).setChecked( selected );
    }
}

Commits for litesoft/trunk/Java/GWT/Client/src/com/google/gwt/gen2/table/client/SelectionGrid.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

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

Extracting commonfoundation

475 Diff Diff GeorgeS picture GeorgeS Sat 03 Sep, 2011 13:54:51 +0000
282 GeorgeS picture GeorgeS Fri 17 Jun, 2011 13:54:39 +0000