1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.student.common.ui.client.configurable.mvc.views; |
17 | |
|
18 | |
import com.google.gwt.user.client.Window; |
19 | |
import com.google.gwt.user.client.ui.Widget; |
20 | |
|
21 | |
import org.kuali.student.common.assembly.data.Metadata; |
22 | |
import org.kuali.student.common.assembly.data.ModelDefinition; |
23 | |
import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor; |
24 | |
import org.kuali.student.common.ui.client.configurable.mvc.LayoutController; |
25 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.BaseSection; |
26 | |
import org.kuali.student.common.ui.client.configurable.mvc.sections.Section; |
27 | |
import org.kuali.student.common.ui.client.mvc.*; |
28 | |
|
29 | |
import java.util.ArrayList; |
30 | |
import java.util.List; |
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
public abstract class SectionView extends BaseSection implements View { |
40 | |
|
41 | |
protected String modelId; |
42 | |
protected DataModel model; |
43 | |
|
44 | |
private Enum<?> viewEnum; |
45 | |
private String viewName; |
46 | |
|
47 | 0 | private List<View> views = new ArrayList<View>(); |
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | 0 | public SectionView(Enum<?> viewEnum, String viewName) { |
54 | 0 | this.viewEnum = viewEnum; |
55 | 0 | this.viewName = viewName; |
56 | 0 | } |
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
@Override |
64 | |
public Enum<?> getViewEnum() { |
65 | 0 | return viewEnum; |
66 | |
} |
67 | |
|
68 | |
public void setViewEnum(Enum<?> viewEnum) { |
69 | 0 | this.viewEnum = viewEnum; |
70 | 0 | } |
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | |
|
79 | |
|
80 | |
@Override |
81 | |
public void beforeShow(final Callback<Boolean> onReadyCallback) { |
82 | |
|
83 | 0 | super.clearValidation(); |
84 | 0 | if (getController() != null) { |
85 | 0 | getController().requestModel(modelId, new ModelRequestCallback<DataModel>() { |
86 | |
|
87 | |
@Override |
88 | |
public void onRequestFail(Throwable cause) { |
89 | 0 | Window.alert("Failed to get model: " + getName()); |
90 | 0 | onReadyCallback.exec(false); |
91 | 0 | } |
92 | |
|
93 | |
@Override |
94 | |
public void onModelReady(DataModel m) { |
95 | 0 | model = m; |
96 | 0 | updateWidgetData(m); |
97 | 0 | resetFieldInteractionFlags(); |
98 | 0 | onReadyCallback.exec(true); |
99 | 0 | } |
100 | |
|
101 | |
}); |
102 | |
} |
103 | |
|
104 | 0 | for (Section section : sections) { |
105 | 0 | if (section instanceof SectionView) { |
106 | 0 | ((SectionView) section).beforeShow(new Callback<Boolean>() { |
107 | |
@Override |
108 | |
public void exec(Boolean result) { |
109 | 0 | } |
110 | |
}); |
111 | |
} |
112 | |
} |
113 | 0 | for (View view : views) { |
114 | 0 | view.beforeShow(Controller.NO_OP_CALLBACK); |
115 | |
} |
116 | |
|
117 | 0 | } |
118 | |
|
119 | |
public String getModelId() { |
120 | 0 | return modelId; |
121 | |
} |
122 | |
|
123 | |
public void setModelId(String modelId) { |
124 | 0 | this.modelId = modelId; |
125 | 0 | } |
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
|
133 | |
@Override |
134 | |
public boolean beforeHide() { |
135 | 0 | return true; |
136 | |
} |
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
@Override |
144 | |
public Controller getController() { |
145 | 0 | return super.getLayoutController(); |
146 | |
} |
147 | |
|
148 | |
|
149 | |
|
150 | |
|
151 | |
|
152 | |
|
153 | |
@Override |
154 | |
public String getName() { |
155 | 0 | return viewName; |
156 | |
} |
157 | |
|
158 | |
public void setName(String name) { |
159 | 0 | this.viewName = name; |
160 | 0 | } |
161 | |
|
162 | |
public void setController(Controller controller) { |
163 | 0 | if (controller instanceof LayoutController) { |
164 | 0 | super.setLayoutController((LayoutController) controller); |
165 | |
} else { |
166 | 0 | throw new IllegalArgumentException("Configurable UI sections require a LayoutController, not a base MVC controller"); |
167 | |
} |
168 | 0 | } |
169 | |
|
170 | |
|
171 | |
|
172 | |
|
173 | |
public void updateView() { |
174 | 0 | getController().requestModel(modelId, new ModelRequestCallback<DataModel>() { |
175 | |
@Override |
176 | |
public void onModelReady(DataModel m) { |
177 | |
|
178 | 0 | SectionView.this.model = m; |
179 | 0 | updateWidgetData(m); |
180 | 0 | } |
181 | |
|
182 | |
|
183 | |
@Override |
184 | |
public void onRequestFail(Throwable cause) { |
185 | 0 | Window.alert("Failed to get model"); |
186 | 0 | } |
187 | |
}); |
188 | |
|
189 | 0 | } |
190 | |
|
191 | |
|
192 | |
|
193 | |
|
194 | |
|
195 | |
public void updateView(DataModel m) { |
196 | 0 | this.model = m; |
197 | 0 | updateWidgetData(m); |
198 | 0 | } |
199 | |
|
200 | |
|
201 | |
|
202 | |
|
203 | |
public Widget asWidget() { |
204 | 0 | return this.getLayout(); |
205 | |
} |
206 | |
|
207 | |
|
208 | |
|
209 | |
|
210 | |
@Override |
211 | |
public String collectHistory(String historyStack) { |
212 | 0 | return null; |
213 | |
} |
214 | |
|
215 | |
|
216 | |
|
217 | |
|
218 | |
@Override |
219 | |
public void onHistoryEvent(String historyStack) { |
220 | |
|
221 | 0 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
@Override |
227 | |
public void collectBreadcrumbNames(List<String> names) { |
228 | 0 | names.add(this.getName()); |
229 | 0 | } |
230 | |
|
231 | |
|
232 | |
|
233 | |
|
234 | |
|
235 | |
public void addView(View view) { |
236 | 0 | views.add(view); |
237 | 0 | addWidget(view.asWidget()); |
238 | 0 | } |
239 | |
|
240 | |
public DataModel getModel() { |
241 | 0 | return model; |
242 | |
} |
243 | |
|
244 | |
public void updateMetadata(ModelDefinition modelDefinition) { |
245 | 0 | updateMetadata(modelDefinition, this); |
246 | 0 | } |
247 | |
|
248 | |
private void updateMetadata(ModelDefinition modelDefinition, Section topSection) { |
249 | 0 | for (Section section : topSection.getSections()) { |
250 | 0 | updateMetadata(modelDefinition, section); |
251 | |
} |
252 | 0 | for (FieldDescriptor field : topSection.getFields()) { |
253 | 0 | Metadata newMetadata = modelDefinition.getMetadata(field.getFieldKey()); |
254 | 0 | if (newMetadata != null) { |
255 | 0 | field.setMetadata(newMetadata); |
256 | |
} |
257 | 0 | } |
258 | 0 | } |
259 | |
|
260 | |
@Override |
261 | |
public String toString() { |
262 | 0 | return viewName; |
263 | |
} |
264 | |
} |