| 1 | |
package org.kuali.student.common.ui.client.widgets.table.scroll; |
| 2 | |
|
| 3 | |
import java.util.ArrayList; |
| 4 | |
import java.util.List; |
| 5 | |
|
| 6 | |
import org.kuali.student.common.ui.client.widgets.notification.LoadingDiv; |
| 7 | |
|
| 8 | |
import com.google.gwt.core.client.GWT; |
| 9 | |
import com.google.gwt.event.dom.client.ChangeEvent; |
| 10 | |
import com.google.gwt.event.dom.client.ChangeHandler; |
| 11 | |
import com.google.gwt.event.dom.client.ClickEvent; |
| 12 | |
import com.google.gwt.event.dom.client.ClickHandler; |
| 13 | |
import com.google.gwt.event.dom.client.HasChangeHandlers; |
| 14 | |
import com.google.gwt.event.dom.client.HasClickHandlers; |
| 15 | |
import com.google.gwt.event.dom.client.ScrollEvent; |
| 16 | |
import com.google.gwt.event.dom.client.ScrollHandler; |
| 17 | |
import com.google.gwt.event.shared.HandlerRegistration; |
| 18 | |
import com.google.gwt.resources.client.CssResource; |
| 19 | |
import com.google.gwt.uibinder.client.UiBinder; |
| 20 | |
import com.google.gwt.uibinder.client.UiField; |
| 21 | |
import com.google.gwt.uibinder.client.UiHandler; |
| 22 | |
import com.google.gwt.user.client.DOM; |
| 23 | |
import com.google.gwt.user.client.Element; |
| 24 | |
import com.google.gwt.user.client.ui.Composite; |
| 25 | |
import com.google.gwt.user.client.ui.FlexTable; |
| 26 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
| 27 | |
import com.google.gwt.user.client.ui.ScrollPanel; |
| 28 | |
import com.google.gwt.user.client.ui.Widget; |
| 29 | |
import com.google.gwt.user.client.ui.HTMLTable.Cell; |
| 30 | |
import com.google.gwt.user.client.ui.PopupPanel.PositionCallback; |
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | 0 | public class Table extends Composite implements HasRetrieveAdditionalDataHandlers{ |
| 36 | |
|
| 37 | 0 | private static TableUiBinder uiBinder = GWT.create(TableUiBinder.class); |
| 38 | 0 | private List<RetrieveAdditionalDataHandler> retrieveDataHandlers = new ArrayList<RetrieveAdditionalDataHandler>(); |
| 39 | |
|
| 40 | |
interface TableUiBinder extends UiBinder<Widget, Table> { |
| 41 | |
} |
| 42 | |
|
| 43 | |
interface SelectionStyle extends CssResource { |
| 44 | |
String selectedRow(); |
| 45 | |
String columnAscending(); |
| 46 | |
String columnDescending(); |
| 47 | |
} |
| 48 | |
|
| 49 | |
@UiField |
| 50 | |
FlexTable header; |
| 51 | |
@UiField |
| 52 | |
MouseHoverFlexTable table; |
| 53 | |
@UiField |
| 54 | |
SelectionStyle selectionStyle; |
| 55 | |
@UiField |
| 56 | |
ScrollPanel scrollPanel; |
| 57 | |
@UiField |
| 58 | |
HTMLPanel panel; |
| 59 | |
|
| 60 | |
private TableModel tableModel; |
| 61 | 0 | private LoadingDiv loading = new LoadingDiv(); |
| 62 | |
|
| 63 | 0 | public Table() { |
| 64 | 0 | initWidget(uiBinder.createAndBindUi(this)); |
| 65 | |
|
| 66 | 0 | scrollPanel.addScrollHandler(new ScrollHandler(){ |
| 67 | |
@Override |
| 68 | |
public void onScroll(ScrollEvent event) { |
| 69 | 0 | int position = scrollPanel.getScrollPosition(); |
| 70 | 0 | int size = scrollPanel.getWidget().getOffsetHeight(); |
| 71 | 0 | int diff = size - scrollPanel.getOffsetHeight(); |
| 72 | 0 | if(position == diff){ |
| 73 | 0 | for(int i = 0; i < retrieveDataHandlers.size(); i++){ |
| 74 | 0 | retrieveDataHandlers.get(i).onAdditionalDataRequest(); |
| 75 | |
} |
| 76 | |
} |
| 77 | 0 | } |
| 78 | |
}); |
| 79 | |
|
| 80 | |
|
| 81 | 0 | } |
| 82 | |
public FlexTable getHeader(){ |
| 83 | 0 | return header; |
| 84 | |
} |
| 85 | |
public FlexTable getContent(){ |
| 86 | 0 | return table; |
| 87 | |
} |
| 88 | |
public ScrollPanel getScrollPanel() { |
| 89 | 0 | return scrollPanel; |
| 90 | |
} |
| 91 | |
|
| 92 | |
public void setTableModel(TableModel m) { |
| 93 | 0 | tableModel = m; |
| 94 | 0 | table.setModel(tableModel); |
| 95 | 0 | if(m instanceof AbstractTableModel){ |
| 96 | 0 | ((AbstractTableModel)tableModel).addTableModelListener(new TableModelListener() { |
| 97 | |
@Override |
| 98 | |
public void tableChanged(TableModelEvent e) { |
| 99 | 0 | updateTable(e); |
| 100 | 0 | } |
| 101 | |
}); |
| 102 | 0 | ((AbstractTableModel)tableModel).fireTableStructureChanged(); |
| 103 | |
} |
| 104 | 0 | } |
| 105 | |
|
| 106 | |
@UiHandler("table") |
| 107 | |
void onTableClicked(ClickEvent event) { |
| 108 | 0 | Cell cell = table.getCellForEvent(event); |
| 109 | |
|
| 110 | 0 | if (cell == null) { |
| 111 | 0 | return; |
| 112 | |
} |
| 113 | 0 | int cellIndex = cell.getCellIndex(); |
| 114 | 0 | int rowIndex = cell.getRowIndex(); |
| 115 | 0 | Row row = tableModel.getRow(rowIndex); |
| 116 | |
|
| 117 | 0 | if(tableModel.isMultipleSelectable() == false){ |
| 118 | 0 | for (int r = 0; r < tableModel.getRowCount(); r++) { |
| 119 | 0 | if(r != rowIndex){ |
| 120 | 0 | tableModel.getRow(r).setSelected(false); |
| 121 | |
} |
| 122 | |
} |
| 123 | 0 | row.setSelected(!row.isSelected()); |
| 124 | 0 | updateTableSelection(); |
| 125 | 0 | for (int r = 0; r < tableModel.getRowCount(); r++) { |
| 126 | 0 | updateTableCell(r, 0); |
| 127 | |
} |
| 128 | |
}else{ |
| 129 | 0 | if(cellIndex == 0){ |
| 130 | 0 | return; |
| 131 | |
} |
| 132 | 0 | row.setSelected(!row.isSelected()); |
| 133 | 0 | updateTableSelection(); |
| 134 | 0 | updateTableCell(rowIndex, 0); |
| 135 | |
} |
| 136 | 0 | } |
| 137 | |
|
| 138 | |
@UiHandler("header") |
| 139 | |
void onTableHeaderClicked(ClickEvent event) { |
| 140 | 0 | Cell cell = header.getCellForEvent(event); |
| 141 | 0 | if (cell != null) { |
| 142 | 0 | Column col = tableModel.getColumn(cell.getCellIndex()); |
| 143 | 0 | tableModel.sort(col); |
| 144 | |
} |
| 145 | 0 | } |
| 146 | |
|
| 147 | |
private void onTableClicked(int row, String columnId, |
| 148 | |
TableCellWidget cellWidget) { |
| 149 | 0 | onTableCellChanged(row, columnId, cellWidget); |
| 150 | 0 | } |
| 151 | |
|
| 152 | |
private void onTableCellChanged(int rowIndex, String columnId, |
| 153 | |
TableCellWidget cellWidget) { |
| 154 | 0 | Row row =tableModel.getRow(rowIndex); |
| 155 | 0 | if ("RowHeader".equals(columnId)) { |
| 156 | 0 | row.setSelected(!row.isSelected()); |
| 157 | 0 | updateTableSelection(); |
| 158 | |
} |
| 159 | 0 | row.setCellData(columnId, |
| 160 | |
cellWidget.getCellEditorValue()); |
| 161 | 0 | } |
| 162 | |
|
| 163 | |
private void updateTableSelection() { |
| 164 | 0 | int count = tableModel.getRowCount(); |
| 165 | 0 | for (int i = 0; i < count; i++) { |
| 166 | 0 | Element tr = table.getRowFormatter().getElement(i); |
| 167 | 0 | if (tableModel.getRow(i).isSelected()) { |
| 168 | 0 | DOM.setStyleAttribute(tr, "backgroundColor", "#C6D9FF"); |
| 169 | |
}else{ |
| 170 | 0 | DOM.setStyleAttribute(tr, "backgroundColor", "#FFFFFF"); |
| 171 | |
} |
| 172 | |
} |
| 173 | 0 | } |
| 174 | |
|
| 175 | |
public void updateTable(TableModelEvent event) { |
| 176 | 0 | if (event.getType() == TableModelEvent.TableStructure) { |
| 177 | 0 | updateTableStructure(); |
| 178 | 0 | updateTableData(); |
| 179 | 0 | } else if (event.getType() == TableModelEvent.TableData) { |
| 180 | 0 | updateTableData(); |
| 181 | 0 | } else if (event.getType() == TableModelEvent.CellUpdate) { |
| 182 | 0 | updateTableCell(event.getFirstRow(), event.getColumn()); |
| 183 | |
} |
| 184 | 0 | } |
| 185 | |
|
| 186 | |
private void updateTableData() { |
| 187 | 0 | for (int r = 0; r < tableModel.getRowCount(); r++) { |
| 188 | 0 | int columnCount = tableModel.getColumnCount(); |
| 189 | 0 | for (int c = 0; c < columnCount; c++) { |
| 190 | 0 | updateTableCell(r, c); |
| 191 | |
} |
| 192 | |
} |
| 193 | 0 | updateTableSelection(); |
| 194 | 0 | } |
| 195 | |
|
| 196 | |
private void updateTableCell(final int r, final int c) { |
| 197 | 0 | int columnCount = tableModel.getColumnCount(); |
| 198 | 0 | for (int i = 0; i < columnCount - 1; i++) { |
| 199 | 0 | Column col = tableModel.getColumn(i); |
| 200 | 0 | table.getColumnFormatter().setWidth(i, col.getWidth()); |
| 201 | |
} |
| 202 | 0 | final String columnId = tableModel.getColumn(c).getId(); |
| 203 | 0 | Row row = tableModel.getRow(r ); |
| 204 | 0 | Object v = null; |
| 205 | 0 | if("RowHeader".equals(columnId)){ |
| 206 | 0 | v = row.isSelected() ; |
| 207 | |
}else{ |
| 208 | 0 | v = row.getCellData(columnId); |
| 209 | |
} |
| 210 | 0 | if("RowHeader".equals(columnId) == false){ |
| 211 | 0 | if(v != null){ |
| 212 | 0 | table.setText(r, c, v.toString()); |
| 213 | |
} |
| 214 | |
else{ |
| 215 | 0 | table.setHTML(r, c, " "); |
| 216 | |
} |
| 217 | 0 | return; |
| 218 | |
} |
| 219 | 0 | final TableCellWidget widget = new TableCellWidget(v); |
| 220 | 0 | widget.setCellEditorValue(v); |
| 221 | 0 | if (widget instanceof HasClickHandlers) { |
| 222 | 0 | ((HasClickHandlers) widget) |
| 223 | 0 | .addClickHandler(new ClickHandler() { |
| 224 | |
@Override |
| 225 | |
public void onClick(ClickEvent event) { |
| 226 | 0 | onTableClicked(r, columnId, widget); |
| 227 | 0 | } |
| 228 | |
}); |
| 229 | |
} |
| 230 | 0 | if (widget instanceof HasChangeHandlers) { |
| 231 | 0 | ((HasChangeHandlers) widget) |
| 232 | 0 | .addChangeHandler(new ChangeHandler() { |
| 233 | |
@Override |
| 234 | |
public void onChange(ChangeEvent event) { |
| 235 | 0 | onTableCellChanged(r, columnId, widget); |
| 236 | |
|
| 237 | 0 | } |
| 238 | |
}); |
| 239 | |
} |
| 240 | 0 | table.setWidget(r, c, widget); |
| 241 | 0 | } |
| 242 | |
|
| 243 | |
private void updateTableStructure() { |
| 244 | 0 | int columnCount = tableModel.getColumnCount(); |
| 245 | 0 | for (int i = 0; i < columnCount; i++) { |
| 246 | 0 | Column col = tableModel.getColumn(i); |
| 247 | 0 | header.setWidget(0, i, col.getColumnTitleWidget()); |
| 248 | |
} |
| 249 | 0 | for (int i = 0; i < columnCount - 1; i++) { |
| 250 | 0 | Column col = tableModel.getColumn(i); |
| 251 | 0 | header.getColumnFormatter().setWidth(i, col.getWidth()); |
| 252 | |
} |
| 253 | 0 | for (int i = 0; i < columnCount - 1; i++) { |
| 254 | 0 | Column col = tableModel.getColumn(i); |
| 255 | |
|
| 256 | 0 | header.getCellFormatter().removeStyleName(0, i, selectionStyle.columnAscending()); |
| 257 | 0 | header.getCellFormatter().removeStyleName(0, i, selectionStyle.columnDescending()); |
| 258 | |
|
| 259 | 0 | if(col.getSortDirection() == Column.Ascending){ |
| 260 | 0 | header.getCellFormatter().addStyleName(0, i, selectionStyle.columnAscending()); |
| 261 | 0 | }else if(col.getSortDirection() == Column.Descending){ |
| 262 | 0 | header.getCellFormatter().addStyleName(0, i, selectionStyle.columnDescending()); |
| 263 | |
}else{ |
| 264 | |
|
| 265 | |
} |
| 266 | 0 | header.getColumnFormatter().setWidth(i, col.getWidth()); |
| 267 | |
} |
| 268 | 0 | } |
| 269 | |
@Override |
| 270 | |
public HandlerRegistration addRetrieveAdditionalDataHandler( |
| 271 | |
final RetrieveAdditionalDataHandler handler) { |
| 272 | 0 | retrieveDataHandlers.add(handler); |
| 273 | 0 | HandlerRegistration result = new HandlerRegistration() { |
| 274 | |
@Override |
| 275 | |
public void removeHandler() { |
| 276 | 0 | retrieveDataHandlers.remove(handler); |
| 277 | 0 | } |
| 278 | |
}; |
| 279 | 0 | return result; |
| 280 | |
} |
| 281 | |
|
| 282 | |
public void displayLoading(boolean isLoading){ |
| 283 | |
|
| 284 | 0 | final int x = scrollPanel.getAbsoluteLeft() + scrollPanel.getOffsetWidth(); |
| 285 | 0 | final int y = scrollPanel.getAbsoluteTop() + scrollPanel.getOffsetHeight(); |
| 286 | 0 | if(isLoading){ |
| 287 | 0 | loading.setPopupPositionAndShow(new PositionCallback(){ |
| 288 | |
|
| 289 | |
@Override |
| 290 | |
public void setPosition(int offsetWidth, int offsetHeight) { |
| 291 | 0 | loading.setPopupPosition(x - offsetWidth, y + 1); |
| 292 | 0 | } |
| 293 | |
}); |
| 294 | |
} |
| 295 | |
else{ |
| 296 | 0 | loading.hide(); |
| 297 | |
} |
| 298 | 0 | } |
| 299 | |
} |