View Javadoc

1   package org.kuali.student.common.ui.client.widgets.table.scroll;
2   
3   import com.google.gwt.event.dom.client.ClickEvent;
4   import com.google.gwt.event.dom.client.ClickHandler;
5   import com.google.gwt.user.client.DOM;
6   import com.google.gwt.user.client.ui.CheckBox;
7   
8   import java.util.ArrayList;
9   import java.util.Collections;
10  import java.util.List;
11  
12  public class DefaultTableModel extends AbstractTableModel {
13      private ArrayList<Column> columnList = new ArrayList<Column>();
14      private ArrayList<Row> rowList = new ArrayList<Row>();
15      private ArrayList<Column> sortedColumnList = new ArrayList<Column>();
16      private int sortableColumnCount = 1;
17      private boolean moreData = true;
18      private boolean multipleSelectable = true;
19      private int currentRowIndex;
20  
21  
22      Column rowHeader = new Column();
23  
24      public void installCheckBoxRowHeaderColumn() {
25          columnList.remove(rowHeader);
26          rowHeader.setId("RowHeader");
27          rowHeader.setName("RowHeader");
28          final CheckBox checkBox = new CheckBox();
29          checkBox.setTabIndex(-1);
30          checkBox.addClickHandler(new ClickHandler() {
31              @Override
32              public void onClick(ClickEvent event) {
33                  int count = getRowCount();
34                  for (int i = 0; i < count; i++) {
35                      getRow(i).setSelected(checkBox.getValue());
36                  }
37                  fireTableDataChanged();
38              }
39          });
40          DOM.setStyleAttribute(checkBox.getElement(), "style", "padding-right: 0.8em");
41          rowHeader.setColumnTitleWidget(checkBox);
42          rowHeader.setWidth("40px");
43          rowHeader.setVisible(true);
44  
45          columnList.add(0, rowHeader);
46      }
47  
48      public void addRow(Row row) {
49          rowList.add(row);
50      }
51  
52      public void insertRow(int index, Row row) {
53          rowList.add(index, row);
54      }
55  
56      public void addColumn(Column col) {
57          columnList.add(col);
58      }
59  
60      public void insertColumn(int index, Column col) {
61          columnList.add(index, col);
62      }
63  
64      public boolean isMultipleSelectable() {
65          return multipleSelectable;
66      }
67  
68      @Override
69      public void setCurrentIndex(int index) {
70          if (index != currentRowIndex && currentRowIndex != -1){
71          	if(currentRowIndex < rowList.size()){
72          		rowList.get(currentRowIndex).setHighlighted(false);
73          	}
74          }
75          currentRowIndex = index;
76      }
77  
78      @Override
79      public int getCurrentIndex() {
80          return currentRowIndex;
81      }
82  
83      @Override
84      public void setSelectedRow(int index) {
85          for (int rowIndex = 0; rowIndex < getRowCount(); rowIndex++) {
86              Row currentRow = rowList.get(rowIndex);
87              if (rowIndex == index) {
88                  currentRow.setSelected(true);
89              } else if (!isMultipleSelectable()) {
90                  currentRow.setSelected(false);
91              }
92          }
93      }
94  
95      public void setMultipleSelectable(boolean value) {
96          multipleSelectable = value;
97      }
98  
99      public void setMoreData(boolean hasMoreData) {
100         moreData = hasMoreData;
101     }
102 
103     public boolean getMoreData() {
104         return moreData;
105     }
106 
107     public int getRowCount() {
108         return rowList.size();
109     }
110 
111     public void sort(Column column) {
112         if (column.isSortable() == false) {
113             return;
114         }
115         if (sortedColumnList.contains(column) == false) {
116             if (sortedColumnList.size() >= sortableColumnCount) {
117                 Column removed = sortedColumnList.remove(0);
118                 removed.setSortDirection(Column.NotOrdered);
119             }
120             sortedColumnList.add(column);
121 
122         }
123 
124         column.changeSortDirection();
125         RowComparator comparator = column.getComparator();
126         if (comparator != null) {
127             Collections.sort(rowList, comparator);
128         }
129         super.fireTableStructureChanged();
130     }
131 
132     public Row getRow(int rowIndex) {
133         return rowList.get(rowIndex);
134     }
135 
136     public List<Row> getSelectedRows() {
137         ArrayList<Row> selectedRows = new ArrayList<Row>();
138         for (Row row : rowList) {
139             if (row.isSelected()) {
140                 selectedRows.add(row);
141             }
142         }
143         return selectedRows;
144     }
145 
146     public Column getColumn(int columnIndex) {
147         ArrayList<Column> list = new ArrayList<Column>();
148         for (Column col : columnList) {
149             if (col.isVisible()) {
150                 list.add(col);
151             }
152         }
153         return list.get(columnIndex);
154     }
155 
156     public int getColumnCount() {
157         int count = 0;
158         for (Column col : columnList) {
159             if (col.isVisible()) {
160                 count++;
161             }
162         }
163         return count;
164     }
165 
166     public void clearRows() {
167         rowList = new ArrayList<Row>();
168         super.fireTableDataChanged();
169     }
170 
171     public void selectFirstRow() {
172         if (!rowList.isEmpty()) {
173             currentRowIndex = 0;
174             rowList.get(0).setHighlighted(true);
175         }
176     }
177 }