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