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.event.dom.client.ScrollEvent; |
6 | |
import com.google.gwt.event.dom.client.ScrollHandler; |
7 | |
import com.google.gwt.user.client.ui.Button; |
8 | |
import com.google.gwt.user.client.ui.Composite; |
9 | |
import com.google.gwt.user.client.ui.VerticalPanel; |
10 | |
import com.google.gwt.user.client.ui.Widget; |
11 | |
|
12 | |
public class TableDemoPanel extends Composite{ |
13 | 0 | DefaultTableModel tableModel = new DefaultTableModel(); |
14 | 0 | public TableDemoPanel(){ |
15 | 0 | super.initWidget(createDemoPage()); |
16 | 0 | } |
17 | |
private Widget createDemoPage(){ |
18 | 0 | VerticalPanel p = new VerticalPanel(); |
19 | 0 | tableModel.setMultipleSelectable(true); |
20 | 0 | final Table table = createTable(); |
21 | 0 | p.add(table); |
22 | 0 | table.getScrollPanel().addScrollHandler(new ScrollHandler(){ |
23 | |
@Override |
24 | |
public void onScroll(ScrollEvent event) { |
25 | 0 | System.out.println(table.getScrollPanel().getOffsetHeight()); |
26 | 0 | System.out.println(table.getScrollPanel().getScrollPosition()); |
27 | 0 | } |
28 | |
}); |
29 | 0 | table.getScrollPanel().setHeight("700px"); |
30 | 0 | Button appendDataButton = new Button("Append Data"); |
31 | 0 | appendDataButton.addClickHandler(new ClickHandler(){ |
32 | |
@Override |
33 | |
public void onClick(ClickEvent event) { |
34 | 0 | Course c = new Course(); |
35 | 0 | c.setId(1); |
36 | 0 | c.setName("1aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"); |
37 | 0 | tableModel.addRow(new CourseRow(c)); |
38 | |
|
39 | 0 | c = new Course(); |
40 | 0 | c.setId(2); |
41 | 0 | c.setName("b"); |
42 | 0 | tableModel.addRow(new CourseRow(c)); |
43 | |
|
44 | 0 | c = new Course(); |
45 | 0 | c.setId(3); |
46 | 0 | c.setName("c"); |
47 | 0 | tableModel.addRow(new CourseRow(c)); |
48 | |
|
49 | 0 | c = new Course(); |
50 | 0 | c.setId(4); |
51 | 0 | c.setName("d"); |
52 | 0 | tableModel.addRow(new CourseRow(c)); |
53 | |
|
54 | 0 | c = new Course(); |
55 | 0 | c.setId(4); |
56 | 0 | c.setName("d"); |
57 | 0 | tableModel.addRow(new CourseRow(c)); |
58 | |
|
59 | 0 | c = new Course(); |
60 | 0 | c.setId(4); |
61 | 0 | c.setName("d"); |
62 | 0 | tableModel.addRow(new CourseRow(c)); |
63 | |
|
64 | 0 | tableModel.fireTableDataChanged(); |
65 | 0 | } |
66 | |
|
67 | |
}); |
68 | 0 | p.add(appendDataButton); |
69 | 0 | return p; |
70 | |
|
71 | |
} |
72 | |
private Table createTable(){ |
73 | 0 | Column col1 = new Column(); |
74 | 0 | col1.setId("id"); |
75 | 0 | col1.setName("ID"); |
76 | 0 | col1.setWidth("100px"); |
77 | |
|
78 | 0 | col1.setAscendingRowComparator(new CourseIdAscendingRowComparator()); |
79 | 0 | col1.setDescendingRowComparator(new CourseIdDescendingRowComparator()); |
80 | |
|
81 | 0 | Column col2 = new Column(); |
82 | 0 | col2.setId("name"); |
83 | 0 | col2.setName("Name"); |
84 | 0 | col2.setWidth("300px"); |
85 | |
|
86 | 0 | Column col3 = new Column(); |
87 | 0 | col3.setId("isFull"); |
88 | 0 | col3.setName("Full"); |
89 | 0 | col3.setWidth("500px"); |
90 | |
|
91 | 0 | tableModel.installCheckBoxRowHeaderColumn(); |
92 | |
|
93 | 0 | tableModel.addColumn(col1); |
94 | 0 | tableModel.addColumn(col2); |
95 | 0 | tableModel.addColumn(col3); |
96 | |
|
97 | 0 | Table table = new Table(); |
98 | 0 | table.setTableModel(tableModel); |
99 | 0 | return table; |
100 | |
|
101 | |
} |
102 | |
} |
103 | 0 | class CourseIdAscendingRowComparator extends RowComparator{ |
104 | |
@Override |
105 | |
public int compare(Row row0, Row row1) { |
106 | 0 | Integer id0 = (Integer)row0.getCellData("id"); |
107 | 0 | Integer id1 = (Integer)row1.getCellData("id"); |
108 | 0 | return id0.compareTo(id1); |
109 | |
} |
110 | |
|
111 | |
} |
112 | 0 | class CourseIdDescendingRowComparator extends RowComparator{ |
113 | |
@Override |
114 | |
public int compare(Row row0, Row row1) { |
115 | 0 | Integer id0 = (Integer)row0.getCellData("id"); |
116 | 0 | Integer id1 = (Integer)row1.getCellData("id"); |
117 | 0 | return id1.compareTo(id0); |
118 | |
} |
119 | |
|
120 | |
} |
121 | |
class CourseRow extends Row{ |
122 | |
private Course course; |
123 | 0 | public CourseRow(Course c){ |
124 | 0 | course = c; |
125 | 0 | } |
126 | |
|
127 | |
@Override |
128 | |
public Object getCellData(String columnId) { |
129 | 0 | if ("id".equals(columnId)) { |
130 | 0 | return course.getId(); |
131 | 0 | } else if ("name".equals(columnId)) { |
132 | 0 | return course.getName(); |
133 | 0 | } else if ("isFull".equals(columnId)) { |
134 | 0 | return course.isFull(); |
135 | |
} |
136 | 0 | return null; |
137 | |
} |
138 | |
@Override |
139 | |
public void setCellData(String columnId, Object newValue) { |
140 | 0 | if(newValue == null){ |
141 | 0 | newValue = ""; |
142 | |
} |
143 | 0 | if ("id".equals(columnId)) { |
144 | 0 | course.setId(Integer.parseInt(newValue.toString())); |
145 | 0 | } else if ("name".equals(columnId)) { |
146 | 0 | course.setName(newValue.toString()); |
147 | 0 | } else if ("isFull".equals(columnId)) { |
148 | 0 | course.setFull((Boolean)newValue); |
149 | |
} |
150 | 0 | } |
151 | |
@Override |
152 | |
public String toString(){ |
153 | 0 | return course.getId()+""; |
154 | |
} |
155 | |
} |
156 | |
|
157 | 0 | class Course { |
158 | |
private int id; |
159 | |
private String name; |
160 | |
private boolean isFull; |
161 | |
|
162 | |
public boolean isFull() { |
163 | 0 | return isFull; |
164 | |
} |
165 | |
|
166 | |
public void setFull(boolean isFull) { |
167 | 0 | this.isFull = isFull; |
168 | 0 | } |
169 | |
|
170 | |
public int getId() { |
171 | 0 | return id; |
172 | |
} |
173 | |
|
174 | |
public void setId(int id) { |
175 | 0 | this.id = id; |
176 | 0 | } |
177 | |
|
178 | |
public String getName() { |
179 | 0 | return name; |
180 | |
} |
181 | |
|
182 | |
public void setName(String name) { |
183 | 0 | this.name = name; |
184 | 0 | } |
185 | |
public String toString(){ |
186 | 0 | return ""+id; |
187 | |
} |
188 | |
} |