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