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