1 | |
package org.kuali.student.common.ui.client.widgets; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.assembly.data.Data; |
7 | |
import org.kuali.student.common.assembly.data.Data.DataValue; |
8 | |
import org.kuali.student.common.assembly.data.Data.Value; |
9 | |
import org.kuali.student.common.ui.client.application.Application; |
10 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.ValidationMessagePanel; |
11 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
12 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
13 | |
import org.kuali.student.common.ui.client.util.Elements; |
14 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton; |
15 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.AbbrButton.AbbrButtonType; |
16 | |
import org.kuali.student.common.ui.client.widgets.field.layout.element.SpanPanel; |
17 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo; |
18 | |
import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel; |
19 | |
|
20 | |
import com.google.gwt.event.dom.client.ClickEvent; |
21 | |
import com.google.gwt.event.dom.client.ClickHandler; |
22 | |
import com.google.gwt.event.logical.shared.CloseEvent; |
23 | |
import com.google.gwt.event.logical.shared.CloseHandler; |
24 | |
import com.google.gwt.event.logical.shared.HasCloseHandlers; |
25 | |
import com.google.gwt.event.shared.HandlerRegistration; |
26 | |
import com.google.gwt.user.client.ui.Anchor; |
27 | |
import com.google.gwt.user.client.ui.Composite; |
28 | |
import com.google.gwt.user.client.ui.FlowPanel; |
29 | |
import com.google.gwt.user.client.ui.HTMLPanel; |
30 | |
import com.google.gwt.user.client.ui.Panel; |
31 | |
|
32 | 0 | public class KSItemLabel extends Composite implements HasCloseHandlers<KSItemLabel>, HasDataValue { |
33 | |
|
34 | 0 | private final String id = HTMLPanel.createUniqueId(); |
35 | 0 | private final String contentId = HTMLPanel.createUniqueId(); |
36 | |
|
37 | 0 | private final String removeLinkId = HTMLPanel.createUniqueId(); |
38 | 0 | private final String backgroundId = HTMLPanel.createUniqueId(); |
39 | 0 | private final String PANEL_CONTENT_OPEN = "<span id='" + contentId + "'></span>"; |
40 | 0 | private final String PANEL_CONTENT_REMOVE_LINK = "<span class='ks-selected-list-value-remove' id='" + removeLinkId + "'></span>"; |
41 | |
|
42 | 0 | private final String PANEL_CONTENT_BACKGROUND = "<div id='" + backgroundId + "' class='ks-selected-list-value-container'></div>"; |
43 | |
private Panel mainPanel; |
44 | |
private HTMLPanel panel; |
45 | 0 | private AbbrButton delete = new AbbrButton(AbbrButtonType.DELETE); |
46 | 0 | private Anchor viewDetails = new Anchor("View"); |
47 | |
private DataHelper dataHelper; |
48 | |
private Data data; |
49 | 0 | private List<Callback<Value>> valueChangeCallbacks = |
50 | |
new ArrayList<Callback<Value>>(); |
51 | |
private String deletedKey; |
52 | |
|
53 | 0 | private static int classInstanceId = -1; |
54 | |
public int instanceId; |
55 | |
|
56 | 0 | ValidationMessagePanel validationPanel = new ValidationMessagePanel(); |
57 | 0 | private ErrorLevel status = ErrorLevel.OK; |
58 | |
|
59 | 0 | public KSItemLabel(boolean canEdit, DataHelper dataParser) { |
60 | 0 | init(canEdit, false, dataParser); |
61 | 0 | } |
62 | |
|
63 | 0 | public KSItemLabel(boolean canEdit, boolean hasDetails, DataHelper dataParser) { |
64 | 0 | init(canEdit, hasDetails, dataParser); |
65 | 0 | } |
66 | |
|
67 | |
private void init(boolean canEdit, boolean hasDetails, DataHelper dataParser) { |
68 | 0 | classInstanceId++; |
69 | 0 | instanceId = classInstanceId; |
70 | 0 | mainPanel = new FlowPanel(); |
71 | 0 | mainPanel.setStyleName("ks-selected-list-value"); |
72 | 0 | panel = new HTMLPanel(PANEL_CONTENT_OPEN + PANEL_CONTENT_BACKGROUND); |
73 | 0 | panel.getElement().setId(id); |
74 | 0 | this.dataHelper = dataParser; |
75 | 0 | mainPanel.add(panel); |
76 | 0 | if (hasDetails) { |
77 | 0 | viewDetails.getElement().getStyle().setProperty("position", "absolute"); |
78 | 0 | viewDetails.getElement().getStyle().setProperty("right", "25px"); |
79 | 0 | viewDetails.getElement().getStyle().setProperty("top", "1px"); |
80 | 0 | mainPanel.add(viewDetails); |
81 | |
} |
82 | 0 | if(canEdit) { |
83 | 0 | delete.getElement().getStyle().setProperty("position", "absolute"); |
84 | 0 | delete.getElement().getStyle().setProperty("right", "3px"); |
85 | 0 | delete.getElement().getStyle().setProperty("top", "8px"); |
86 | 0 | mainPanel.add(delete); |
87 | 0 | initDeleteHandlers(); |
88 | 0 | mainPanel.add(validationPanel); |
89 | |
} |
90 | 0 | String labelText = ""; |
91 | 0 | panel.getElementById(contentId).setInnerText(labelText); |
92 | 0 | panel.setVisible(false); |
93 | 0 | mainPanel.setVisible(false); |
94 | 0 | super.initWidget(mainPanel); |
95 | 0 | } |
96 | |
|
97 | |
@Override |
98 | |
public HandlerRegistration addCloseHandler(CloseHandler<KSItemLabel> handler) { |
99 | 0 | return addHandler(handler, CloseEvent.getType()); |
100 | |
} |
101 | |
|
102 | |
@Override |
103 | |
public void addValueChangeCallback(Callback<Value> callback) { |
104 | 0 | valueChangeCallbacks.add(callback); |
105 | 0 | } |
106 | |
|
107 | |
private void callHandlers() { |
108 | 0 | if (valueChangeCallbacks != null) { |
109 | |
|
110 | 0 | for (Callback<Data.Value> handler : valueChangeCallbacks) { |
111 | 0 | handler.exec(getValue()); |
112 | |
} |
113 | |
} |
114 | 0 | } |
115 | |
|
116 | |
@Override |
117 | |
public Value getValue() { |
118 | 0 | DataValue result = new DataValue(data); |
119 | 0 | return result; |
120 | |
} |
121 | |
|
122 | |
@Override |
123 | |
public void setValue(Value value) { |
124 | 0 | deletedKey = null; |
125 | 0 | if (value == null) { |
126 | 0 | this.data = null; |
127 | |
} else { |
128 | 0 | this.data = value.get(); |
129 | |
} |
130 | 0 | callHandlers(); |
131 | 0 | redraw(); |
132 | 0 | } |
133 | |
|
134 | |
public String getKey() { |
135 | 0 | return dataHelper.getKey(data); |
136 | |
} |
137 | |
|
138 | |
public String getDisplayText() { |
139 | 0 | if (data == null) { |
140 | 0 | return null; |
141 | |
} |
142 | 0 | return dataHelper.parse(data); |
143 | |
} |
144 | |
|
145 | |
private String nvl(String inString) { |
146 | 0 | return (inString == null)? "" : inString; |
147 | |
} |
148 | |
|
149 | |
private void redraw() { |
150 | 0 | String labelText = null; |
151 | 0 | labelText = dataHelper.parse(data); |
152 | 0 | panel.getElementById(contentId).setInnerHTML(nvl(labelText)); |
153 | 0 | panel.setVisible(true); |
154 | 0 | mainPanel.setVisible(true); |
155 | 0 | } |
156 | |
|
157 | |
private void initDeleteHandlers() { |
158 | |
|
159 | 0 | delete.addClickHandler(new ClickHandler() { |
160 | |
@Override |
161 | |
public void onClick(ClickEvent event) { |
162 | 0 | doRemove(); |
163 | 0 | } |
164 | |
}); |
165 | 0 | } |
166 | |
|
167 | |
public HandlerRegistration addShowDetailsHandler(ClickHandler clickHandler) { |
168 | 0 | return viewDetails.addClickHandler(clickHandler); |
169 | |
} |
170 | |
|
171 | |
private void doRemove() { |
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | 0 | deletedKey = dataHelper.getKey(data); |
177 | 0 | data = null; |
178 | 0 | redraw(); |
179 | 0 | callHandlers(); |
180 | 0 | CloseEvent.fire(this, this); |
181 | 0 | } |
182 | |
|
183 | |
public String getDeletedKey() { |
184 | 0 | return deletedKey; |
185 | |
} |
186 | |
|
187 | |
public void setHighlighted(boolean highlighted) { |
188 | 0 | if (highlighted) { |
189 | 0 | Elements.fadeIn(panel.getElementById(backgroundId), 250, 100, new Elements.EmptyFadeCallback()); |
190 | |
|
191 | |
} else { |
192 | 0 | Elements.fadeOut(panel.getElementById(backgroundId), 1000, 0, new Elements.EmptyFadeCallback()); |
193 | |
|
194 | |
} |
195 | 0 | } |
196 | |
|
197 | |
public void removeHighlight(){ |
198 | 0 | Elements.setOpacity(panel.getElementById(backgroundId), 0); |
199 | 0 | } |
200 | |
|
201 | |
@Override |
202 | |
public boolean equals(Object obj) { |
203 | 0 | KSItemLabel item2 = null; |
204 | 0 | boolean result = false; |
205 | |
try { |
206 | 0 | item2 = (KSItemLabel) obj; |
207 | 0 | } catch (Exception e) { |
208 | 0 | return false; |
209 | 0 | } |
210 | 0 | if (item2 != null && nullSafeEquals(getKey(), item2.getKey()) && |
211 | |
nullSafeEquals(getDisplayText(), item2.getDisplayText())) { |
212 | 0 | result = true; |
213 | |
} |
214 | 0 | return result; |
215 | |
} |
216 | |
|
217 | |
|
218 | |
|
219 | |
private boolean nullSafeEquals(String str1, String str2) { |
220 | 0 | boolean result = false; |
221 | 0 | String tempStr1 = (str1 == null)? "" : str1; |
222 | 0 | String tempStr2 = (str2 == null)? "" : str2; |
223 | 0 | result = tempStr1.equals(tempStr2); |
224 | 0 | return result; |
225 | |
} |
226 | |
|
227 | |
@Override |
228 | |
public int hashCode() { |
229 | 0 | String key = getKey(); |
230 | 0 | String displayText = getDisplayText(); |
231 | 0 | key = (key == null)? "" : key; |
232 | 0 | displayText = (displayText == null)? "" : displayText; |
233 | 0 | return key.hashCode() + displayText.hashCode(); |
234 | |
} |
235 | |
|
236 | |
|
237 | |
|
238 | |
|
239 | |
|
240 | |
public void clearValidationErrors(){ |
241 | 0 | this.setErrorState(false); |
242 | 0 | if(validationPanel != null){ |
243 | 0 | this.validationPanel.clearErrors(); |
244 | |
} |
245 | 0 | } |
246 | |
|
247 | |
|
248 | |
|
249 | |
|
250 | |
public void clearValidationWarnings(){ |
251 | 0 | this.setWarnState(false); |
252 | 0 | if(validationPanel != null){ |
253 | 0 | this.validationPanel.clearWarnings(); |
254 | |
} |
255 | 0 | } |
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
public ErrorLevel processValidationResult(ValidationResultInfo vr, String fieldName) { |
263 | |
|
264 | 0 | status = ErrorLevel.OK; |
265 | |
|
266 | 0 | if(vr.getLevel() == ErrorLevel.ERROR){ |
267 | 0 | String message = Application.getApplicationContext().getUILabel("validation", vr.getMessage()); |
268 | 0 | this.addValidationErrorMessage(message,fieldName); |
269 | |
|
270 | 0 | if(status.getLevel() < ErrorLevel.ERROR.getLevel()){ |
271 | 0 | status = vr.getLevel(); |
272 | |
} |
273 | 0 | } |
274 | 0 | else if(vr.getLevel() == ErrorLevel.WARN){ |
275 | 0 | String message = Application.getApplicationContext().getUILabel("validation", vr.getMessage()); |
276 | 0 | this.addValidationWarningMessage(message,fieldName); |
277 | |
|
278 | 0 | if(status.getLevel() < ErrorLevel.WARN.getLevel()){ |
279 | 0 | status = vr.getLevel(); |
280 | |
} |
281 | |
} |
282 | |
else{ |
283 | |
|
284 | |
} |
285 | 0 | return status; |
286 | |
} |
287 | |
|
288 | |
|
289 | |
|
290 | |
|
291 | |
|
292 | |
public void addValidationErrorMessage(String text, String fieldName){ |
293 | 0 | if(validationPanel != null){ |
294 | |
KSLabel message; |
295 | 0 | if(fieldName != null && !fieldName.trim().equals("")){ |
296 | 0 | message = new KSLabel(fieldName + " - " + text); |
297 | |
} |
298 | |
else{ |
299 | 0 | message = new KSLabel(text); |
300 | |
} |
301 | 0 | this.setErrorState(true); |
302 | 0 | message.setStyleName("ks-form-error-label"); |
303 | 0 | this.validationPanel.addErrorMessage(message); |
304 | |
} |
305 | 0 | } |
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
|
311 | |
public void addValidationWarningMessage(String text, String fieldName){ |
312 | 0 | if(validationPanel != null){ |
313 | 0 | SpanPanel message = new SpanPanel(); |
314 | 0 | if(fieldName != null && !fieldName.trim().equals("")){ |
315 | 0 | message.setHTML("<b> Warning </b> " + fieldName + " - " + text); |
316 | |
} |
317 | |
else{ |
318 | 0 | message.setHTML("<b> Warning </b> " + text); |
319 | |
} |
320 | 0 | message.setStyleName("ks-form-warn-label"); |
321 | |
|
322 | 0 | this.setWarnState((status != ErrorLevel.ERROR)); |
323 | 0 | this.validationPanel.addWarnMessage(message); |
324 | |
} |
325 | 0 | } |
326 | |
|
327 | |
|
328 | |
|
329 | |
|
330 | |
|
331 | |
|
332 | |
public void setErrorState(boolean error){ |
333 | 0 | if(error){ |
334 | |
|
335 | 0 | if(mainPanel != null){ |
336 | 0 | mainPanel.addStyleName("error"); |
337 | |
} |
338 | |
|
339 | 0 | setWarnState(false); |
340 | |
} |
341 | |
else{ |
342 | |
|
343 | 0 | if(mainPanel != null){ |
344 | 0 | mainPanel.removeStyleName("error"); |
345 | |
} |
346 | |
|
347 | 0 | setWarnState(validationPanel.hasWarnings()); |
348 | |
} |
349 | 0 | } |
350 | |
|
351 | |
|
352 | |
|
353 | |
|
354 | |
|
355 | |
|
356 | |
public void setWarnState(boolean warn){ |
357 | 0 | if(warn){ |
358 | |
|
359 | 0 | if(mainPanel != null){ |
360 | 0 | mainPanel.addStyleName("warning"); |
361 | |
} |
362 | |
|
363 | |
|
364 | |
} |
365 | |
else{ |
366 | |
|
367 | 0 | if(mainPanel != null){ |
368 | 0 | mainPanel.removeStyleName("warning"); |
369 | |
} |
370 | |
|
371 | |
} |
372 | 0 | } |
373 | |
} |