1 package autotest.common.table; 2 3 import autotest.common.table.DynamicTable.DynamicTableListener; 4 import autotest.common.ui.Paginator; 5 import autotest.common.ui.TableActionsPanel; 6 import autotest.common.ui.TableSelectionPanel; 7 import autotest.common.ui.TableActionsPanel.TableActionsListener; 8 import autotest.common.ui.TableActionsPanel.TableActionsWithExportCsvListener; 9 10 import com.google.gwt.json.client.JSONObject; 11 import com.google.gwt.user.client.ui.Composite; 12 import com.google.gwt.user.client.ui.FlexTable; 13 import com.google.gwt.user.client.ui.Widget; 14 15 /** 16 * This class can optionally add any of the following controls around a table (in this order of 17 * layout): 18 * Filter controls 19 * Selection/action menu 20 * Top paginator 21 * Actual data table 22 * Bottom paginator 23 */ 24 public class TableDecorator extends Composite implements DynamicTableListener { 25 private FlexTable enclosingTable = new FlexTable(); 26 private FlexTable filterTable = new FlexTable(); 27 private DynamicTable dataTable; 28 private SelectionManager selectionManager; 29 private int numFilters = 0; 30 31 private static class LayoutRows { 32 public static final int FILTERS = 0, 33 TOP_ACTIONS = 1, 34 TOP_PAGINATOR = 2, 35 TABLE = 3, 36 BOTTOM_PAGINATOR = 4, 37 BOTTOM_ACTIONS = 5; 38 } 39 TableDecorator(DynamicTable dataTable)40 public TableDecorator(DynamicTable dataTable) { 41 this.dataTable = dataTable; 42 setRow(LayoutRows.TABLE, dataTable); 43 setRow(LayoutRows.FILTERS, filterTable); 44 initWidget(enclosingTable); 45 } 46 addPaginators()47 public void addPaginators() { 48 for(int i = 0; i < 2; i++) { 49 Paginator paginator = new Paginator(); 50 dataTable.attachPaginator(paginator); 51 if (i == 0) { // add at top 52 setRow(LayoutRows.TOP_PAGINATOR, paginator); 53 } 54 else { // add at bottom 55 setRow(LayoutRows.BOTTOM_PAGINATOR, paginator); 56 } 57 } 58 } 59 addFilter(String text, Filter filter)60 public void addFilter(String text, Filter filter) { 61 dataTable.addFilter(filter); 62 addControl(text, filter.getWidget()); 63 } 64 addControl(String text, Widget widget)65 public void addControl(String text, Widget widget) { 66 int row = numFilters; 67 numFilters++; 68 filterTable.setText(row, 0, text); 69 filterTable.setWidget(row, 1, widget); 70 } 71 addSelectionManager(boolean selectOnlyOne)72 public SelectionManager addSelectionManager(boolean selectOnlyOne) { 73 selectionManager = new DynamicTableSelectionManager(dataTable, selectOnlyOne); 74 dataTable.addListener(this); 75 return selectionManager; 76 } 77 addSelectionPanel(boolean wantSelectVisible)78 public void addSelectionPanel(boolean wantSelectVisible) { 79 assert selectionManager != null; 80 for(int i = 0; i < 2; i++) { 81 TableSelectionPanel selectionPanel = new TableSelectionPanel(wantSelectVisible); 82 selectionPanel.setListener(selectionManager); 83 if (i == 0) 84 setRow(LayoutRows.TOP_ACTIONS, selectionPanel); 85 else 86 setRow(LayoutRows.BOTTOM_ACTIONS, selectionPanel); 87 } 88 } 89 createTableActionsPanel(boolean wantSelectVisible)90 private TableActionsPanel createTableActionsPanel(boolean wantSelectVisible) { 91 assert selectionManager != null; 92 TableActionsPanel actionsPanel = new TableActionsPanel(wantSelectVisible); 93 actionsPanel.setSelectionListener(selectionManager); 94 return actionsPanel; 95 } 96 addTableActionsPanel(TableActionsListener listener, boolean wantSelectVisible)97 public void addTableActionsPanel(TableActionsListener listener, boolean wantSelectVisible) { 98 for(int i = 0; i < 2; i++) { 99 TableActionsPanel actionsPanel = createTableActionsPanel(wantSelectVisible); 100 actionsPanel.setActionsListener(listener); 101 if (i == 0) 102 setRow(LayoutRows.TOP_ACTIONS, actionsPanel); 103 else 104 setRow(LayoutRows.BOTTOM_ACTIONS, actionsPanel); 105 } 106 } 107 addTableActionsWithExportCsvListener(TableActionsWithExportCsvListener listener)108 public void addTableActionsWithExportCsvListener(TableActionsWithExportCsvListener listener) { 109 addTableActionsPanel(listener, true); 110 } 111 setActionsWidget(Widget widget)112 public void setActionsWidget(Widget widget) { 113 setRow(LayoutRows.TOP_ACTIONS, widget); 114 } 115 setRow(int row, Widget widget)116 private void setRow(int row, Widget widget) { 117 enclosingTable.setWidget(row, 0, widget); 118 } 119 120 @Override onRowClicked(int rowIndex, JSONObject row, boolean isRightClick)121 public void onRowClicked(int rowIndex, JSONObject row, boolean isRightClick) {} 122 onTableRefreshed()123 public void onTableRefreshed() { 124 assert selectionManager != null; 125 selectionManager.refreshSelection(); 126 } 127 } 128