1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.widgets.list.impl; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.List; |
20 | |
|
21 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
22 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
23 | |
import org.kuali.student.common.ui.client.widgets.list.KSSelectItemWidgetAbstract; |
24 | |
import org.kuali.student.common.ui.client.widgets.list.ListItems; |
25 | |
import org.kuali.student.common.ui.client.widgets.list.ModelListItems; |
26 | |
import org.kuali.student.core.dto.Idable; |
27 | |
|
28 | |
import com.google.gwt.event.dom.client.BlurHandler; |
29 | |
import com.google.gwt.event.dom.client.FocusHandler; |
30 | |
import com.google.gwt.event.shared.HandlerRegistration; |
31 | |
import com.google.gwt.user.client.ui.FlexTable; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
public class KSLabelListImpl extends KSSelectItemWidgetAbstract { |
41 | |
|
42 | 0 | private static final HandlerRegistration NO_OP_REGISTRATION = new HandlerRegistration() { |
43 | |
@Override |
44 | |
public void removeHandler() { |
45 | |
|
46 | 0 | } |
47 | |
}; |
48 | |
|
49 | |
|
50 | 0 | private FlexTable labels = new FlexTable(); |
51 | 0 | private List<String> selectedItems = new ArrayList<String>(); |
52 | |
|
53 | 0 | private int maxCols = 1; |
54 | 0 | private boolean enabled = true; |
55 | |
|
56 | 0 | public KSLabelListImpl() { |
57 | 0 | initWidget(labels); |
58 | 0 | } |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
public void deSelectItem(String id) { |
64 | 0 | this.selectedItems.remove(id); |
65 | 0 | } |
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
public List<String> getSelectedItems() { |
71 | 0 | return selectedItems; |
72 | |
} |
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
public void selectItem(String id) { |
80 | 0 | if (!selectedItems.contains(id)){ |
81 | 0 | this.selectedItems.add(id); |
82 | |
} |
83 | |
|
84 | |
|
85 | 0 | redraw(); |
86 | 0 | } |
87 | |
|
88 | |
public void redraw(){ |
89 | 0 | labels.clear(); |
90 | 0 | int currCount = 0; |
91 | 0 | int row = 0; |
92 | 0 | int col = 0; |
93 | |
|
94 | 0 | int itemCount = 0; |
95 | 0 | if (super.getListItems() != null){ |
96 | 0 | itemCount = super.getListItems().getItemCount(); |
97 | |
} else { |
98 | 0 | itemCount = selectedItems.size(); |
99 | |
} |
100 | |
|
101 | 0 | if (maxCols <= 2){ |
102 | |
|
103 | 0 | int maxRows = (itemCount / maxCols) + (itemCount % 2); |
104 | 0 | for (String id:selectedItems){ |
105 | 0 | currCount++; |
106 | 0 | row = (currCount % maxRows); |
107 | 0 | row = ((row == 0) ? maxRows:row) - 1; |
108 | |
|
109 | 0 | labels.setWidget(row, col, createLabel(id)); |
110 | |
|
111 | 0 | col += ((row + 1)/ maxRows) * 1; |
112 | |
} |
113 | 0 | } else { |
114 | |
|
115 | 0 | for (String id:selectedItems){ |
116 | 0 | currCount++; |
117 | 0 | col = currCount % maxCols; |
118 | 0 | col = ((col == 0) ? maxCols:col) - 1; |
119 | |
|
120 | 0 | labels.setWidget(row, col, createLabel(id)); |
121 | |
|
122 | 0 | row += ((col + 1 )/ maxCols) * 1; |
123 | |
} |
124 | |
} |
125 | |
|
126 | 0 | super.setInitialized(true); |
127 | 0 | } |
128 | |
|
129 | |
@Override |
130 | |
public <T extends Idable> void setListItems(ListItems listItems) { |
131 | 0 | if(listItems instanceof ModelListItems){ |
132 | 0 | Callback<T> redrawCallback = new Callback<T>(){ |
133 | |
|
134 | |
@Override |
135 | |
public void exec(T result){ |
136 | 0 | KSLabelListImpl.this.redraw(); |
137 | 0 | } |
138 | |
}; |
139 | 0 | ((ModelListItems<T>)listItems).addOnAddCallback(redrawCallback); |
140 | 0 | ((ModelListItems<T>)listItems).addOnRemoveCallback(redrawCallback); |
141 | 0 | ((ModelListItems<T>)listItems).addOnUpdateCallback(redrawCallback); |
142 | 0 | ((ModelListItems<T>)listItems).addOnBulkUpdateCallback(redrawCallback); |
143 | |
} |
144 | |
|
145 | 0 | super.setListItems(listItems); |
146 | |
|
147 | 0 | redraw(); |
148 | 0 | } |
149 | |
|
150 | |
private KSLabel createLabel(String id){ |
151 | 0 | if (super.getListItems() == null || super.getListItems().getItemCount() == 0){ |
152 | 0 | return new KSLabel(id); |
153 | |
} else { |
154 | 0 | return new KSLabel(super.getListItems().getItemText(id)); |
155 | |
} |
156 | |
} |
157 | |
|
158 | 0 | public void onLoad() {} |
159 | |
|
160 | |
@Override |
161 | |
public void setColumnSize(int col) { |
162 | 0 | maxCols = col; |
163 | 0 | } |
164 | |
|
165 | |
@Override |
166 | |
public void setEnabled(boolean b) { |
167 | |
|
168 | |
|
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | 0 | } |
174 | |
|
175 | |
@Override |
176 | |
public boolean isEnabled() { |
177 | 0 | return enabled; |
178 | |
} |
179 | |
|
180 | |
@Override |
181 | |
public boolean isMultipleSelect(){ |
182 | 0 | return true; |
183 | |
} |
184 | |
@Override |
185 | |
public void clear(){ |
186 | 0 | selectedItems.clear(); |
187 | 0 | redraw(); |
188 | 0 | } |
189 | |
|
190 | |
@Override |
191 | |
public HandlerRegistration addFocusHandler(FocusHandler handler) { |
192 | 0 | return NO_OP_REGISTRATION; |
193 | |
} |
194 | |
|
195 | |
@Override |
196 | |
public HandlerRegistration addBlurHandler(BlurHandler handler) { |
197 | 0 | return NO_OP_REGISTRATION; |
198 | |
} |
199 | |
|
200 | |
} |