1 | |
package org.kuali.student.lum.lu.ui.tools.client.widgets; |
2 | |
|
3 | |
import java.util.ArrayList; |
4 | |
import java.util.List; |
5 | |
|
6 | |
import org.kuali.student.common.ui.client.mvc.Callback; |
7 | |
import org.kuali.student.common.ui.client.mvc.HasDataValue; |
8 | |
import org.kuali.student.common.ui.client.widgets.KSLabel; |
9 | |
import org.kuali.student.core.assembly.data.Data; |
10 | |
import org.kuali.student.core.assembly.data.LookupMetadata; |
11 | |
import org.kuali.student.core.assembly.data.LookupParamMetadata; |
12 | |
import org.kuali.student.core.assembly.data.Data.DataValue; |
13 | |
import org.kuali.student.core.assembly.data.Data.Value; |
14 | |
import org.kuali.student.core.search.dto.SearchParam; |
15 | |
import org.kuali.student.lum.common.client.widgets.CluSetRangeModelUtil; |
16 | |
import org.kuali.student.lum.lu.dto.MembershipQueryInfo; |
17 | |
|
18 | |
import com.google.gwt.event.dom.client.ClickEvent; |
19 | |
import com.google.gwt.event.dom.client.ClickHandler; |
20 | |
import com.google.gwt.user.client.ui.Composite; |
21 | |
import com.google.gwt.user.client.ui.HorizontalPanel; |
22 | |
import com.google.gwt.user.client.ui.SimplePanel; |
23 | |
|
24 | 0 | public class CluSetRangeLabel extends Composite implements HasDataValue { |
25 | |
|
26 | |
private MembershipQueryInfo membershipQueryInfo; |
27 | 0 | protected SimplePanel mainPanel = new SimplePanel(); |
28 | 0 | private KSLabel label = new KSLabel(); |
29 | 0 | private KSLabel delete = new KSLabel(" X "); |
30 | |
private LookupMetadata lookupMetadata; |
31 | 0 | private List<Callback<Value>> valueChangeCallbacks = |
32 | |
new ArrayList<Callback<Value>>(); |
33 | |
private boolean initialized; |
34 | |
|
35 | 0 | public CluSetRangeLabel() { |
36 | 0 | super.initWidget(mainPanel); |
37 | 0 | } |
38 | |
|
39 | |
@Override |
40 | |
public Value getValue() { |
41 | 0 | DataValue value = new DataValue( |
42 | |
CluSetRangeModelUtil.INSTANCE.toData(membershipQueryInfo)); |
43 | 0 | return value; |
44 | |
} |
45 | |
|
46 | |
@Override |
47 | |
public void setValue(Value value) { |
48 | 0 | membershipQueryInfo = null; |
49 | 0 | if (value != null) { |
50 | 0 | Data d = value.get(); |
51 | 0 | membershipQueryInfo = CluSetRangeModelUtil.INSTANCE.toMembershipQueryInfo(d); |
52 | |
} |
53 | 0 | callHandlers(); |
54 | 0 | redraw(); |
55 | 0 | } |
56 | |
|
57 | |
@Override |
58 | |
public void addValueChangeCallback(Callback<Value> callback) { |
59 | 0 | valueChangeCallbacks.add(callback); |
60 | 0 | } |
61 | |
|
62 | |
private void callHandlers() { |
63 | 0 | if (valueChangeCallbacks != null) { |
64 | |
|
65 | 0 | for (Callback<Data.Value> handler : valueChangeCallbacks) { |
66 | 0 | handler.exec(getValue()); |
67 | |
} |
68 | |
} |
69 | 0 | } |
70 | |
|
71 | |
private void redraw() { |
72 | 0 | StringBuilder labelText = new StringBuilder(); |
73 | 0 | int paramCounter = 0; |
74 | 0 | HorizontalPanel labelPanel = new HorizontalPanel(); |
75 | 0 | labelPanel.add(label); |
76 | 0 | mainPanel.clear(); |
77 | 0 | mainPanel.setWidget(labelPanel); |
78 | 0 | label.setText(""); |
79 | 0 | if (!initialized) { |
80 | 0 | delete.addClickHandler(new ClickHandler() { |
81 | |
@Override |
82 | |
public void onClick(ClickEvent event) { |
83 | 0 | membershipQueryInfo = null; |
84 | 0 | label.setText(""); |
85 | 0 | callHandlers(); |
86 | 0 | } |
87 | |
}); |
88 | 0 | initialized = true; |
89 | |
} |
90 | 0 | if (membershipQueryInfo != null && membershipQueryInfo.getQueryParamValueList() != null && |
91 | |
!membershipQueryInfo.getQueryParamValueList().isEmpty()) { |
92 | 0 | labelText.append(getLookupDisplayName()).append(": "); |
93 | 0 | for (SearchParam searchParam : membershipQueryInfo.getQueryParamValueList()) { |
94 | 0 | if (paramCounter > 0) { |
95 | 0 | labelText.append(" "); |
96 | |
} |
97 | 0 | labelText.append(getParameterDisplayName(searchParam.getKey())).append(" "); |
98 | 0 | labelText.append(searchParam.getValue()); |
99 | 0 | paramCounter++; |
100 | |
} |
101 | 0 | label.setText(labelText.toString()); |
102 | 0 | labelPanel.add(delete); |
103 | |
} |
104 | 0 | } |
105 | |
|
106 | |
private String getLookupDisplayName() { |
107 | 0 | String lookupDisplayName = null; |
108 | 0 | if (lookupMetadata == null) { |
109 | 0 | lookupDisplayName = membershipQueryInfo.getSearchTypeKey(); |
110 | |
} else { |
111 | 0 | lookupDisplayName = lookupMetadata.getName(); |
112 | |
} |
113 | 0 | return lookupDisplayName; |
114 | |
} |
115 | |
|
116 | |
private String getParameterDisplayName(String parameterKey) { |
117 | 0 | String parameterDisplayName = null; |
118 | 0 | if (lookupMetadata == null) { |
119 | 0 | parameterDisplayName = parameterKey; |
120 | |
} else { |
121 | 0 | List<LookupParamMetadata> searchParams = lookupMetadata.getParams(); |
122 | 0 | if (searchParams != null) { |
123 | 0 | for (LookupParamMetadata searchParam : searchParams) { |
124 | 0 | if (nullSafeEquals(searchParam.getKey(), parameterKey)) { |
125 | 0 | parameterDisplayName = searchParam.getName(); |
126 | |
} |
127 | |
} |
128 | |
} |
129 | 0 | if (parameterDisplayName == null) { |
130 | 0 | parameterDisplayName = parameterKey; |
131 | |
} |
132 | |
} |
133 | 0 | return parameterDisplayName; |
134 | |
} |
135 | |
|
136 | |
private boolean nullSafeEquals(Object str1, Object str2) { |
137 | 0 | return (str1 == null && str2 == null || |
138 | |
str1 != null && str2 != null && str1.equals(str2)); |
139 | |
} |
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
public LookupMetadata getLookupMetadata() { |
148 | 0 | return lookupMetadata; |
149 | |
} |
150 | |
|
151 | |
public void setLookupMetadata(LookupMetadata lookupMetadata) { |
152 | 0 | this.lookupMetadata = lookupMetadata; |
153 | 0 | } |
154 | |
|
155 | |
} |