View Javadoc

1   /**
2    * Copyright 2010 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
14   */
15  
16  package org.kuali.student.common.ui.client.configurable.mvc.views;
17  
18  import java.util.ArrayList;
19  import java.util.List;
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.Callback;
28  import org.kuali.student.common.ui.client.mvc.Controller;
29  import org.kuali.student.common.ui.client.mvc.DataModel;
30  import org.kuali.student.common.ui.client.mvc.ModelRequestCallback;
31  import org.kuali.student.common.ui.client.mvc.View;
32  
33  import com.google.gwt.user.client.Window;
34  import com.google.gwt.user.client.ui.Widget;
35  
36  
37  /**
38   * A view implementation of a section.  A section view is used to add sections as views to a controller.
39   * 
40   * @author Kuali Student
41   *
42   */
43  public abstract class SectionView extends BaseSection implements View {
44  
45      protected String modelId;
46      protected DataModel model;
47  
48      private Enum<?> viewEnum;
49      private String viewName;
50  
51      private List<View> views = new ArrayList<View>();
52  
53      public SectionView() {}
54  
55      /**
56       * @param viewEnum Enumeration of this view - id used for navigation, history, and showing a view
57       * @param viewName Name of this view - what this view is called in the breadcrumb
58       */
59      public SectionView(Enum<?> viewEnum, String viewName) {
60          this.viewEnum = viewEnum;
61          this.viewName = viewName;
62      }
63      
64      public void init(Enum<?> viewEnum, String viewName) {
65          this.viewEnum = viewEnum;
66          this.viewName = viewName;
67      }
68  
69      /**
70       * This method gets view name enumeration
71       *
72       * @return
73       */
74      @Override
75      public Enum<?> getViewEnum() {
76          return viewEnum;
77      }
78  
79      public void setViewEnum(Enum<?> viewEnum) {
80          this.viewEnum = viewEnum;
81      }
82  
83  
84      /**
85       * Called by controller before the view is displayed to allow lazy initialization or any other preparatory work to be
86       * done.
87       * In SectionView, the section is cleared of all validation errors, the model is requested from its parent
88       * controller, the widgets are updated with the latest data, and beforeShow is called on all of its potential child
89       * views.
90       */
91      @Override
92      public void beforeShow(final Callback<Boolean> onReadyCallback) {
93  
94          super.clearValidationErrors();
95          
96          if (getController() != null) {
97              getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
98  
99                  @Override
100                 public void onRequestFail(Throwable cause) {	//Don't place a breakpoint here:  It will stall debugging for some unknown reason!
101                     Window.alert("Failed to get model: "  + modelId + " for SectionView " + getName());
102                     onReadyCallback.exec(false);
103                 }
104 
105                 @Override	//Don't place a breakpoint here:  It will stall debugging for some unknown reason!
106                 public void onModelReady(DataModel m) {
107                     model = m;
108                     updateWidgetData(m);
109                     resetFieldInteractionFlags();
110                     onReadyCallback.exec(true);
111                 }
112 
113             });
114         }
115 
116         for (Section section : sections) {
117             if (section instanceof SectionView) {
118                 ((SectionView) section).beforeShow(new Callback<Boolean>() {
119                     @Override	//Don't place a breakpoint here:  It will stall debugging for some unknown reason!
120                     public void exec(Boolean result) {
121                     }
122                 });
123             }
124         }
125         for (View view : views) {
126             view.beforeShow(Controller.NO_OP_CALLBACK);
127         }
128 
129     }
130 
131     public String getModelId() {
132         return modelId;
133     }
134 
135     public void setModelId(String modelId) {
136         this.modelId = modelId;
137     }
138 
139     /**
140      * Called by the controller before the view is hidden to allow the view to perform cleanup or request confirmation from
141      * the user, etc. Can cancel the action by returning false.
142      *
143      * @return true if the view can be hidden, or false to cancel the action.
144      */
145     @Override
146     public boolean beforeHide() {
147         return true;
148     }
149 
150     /**
151      * Returns the controller associated with the view
152      *
153      * @see org.kuali.student.common.ui.client.mvc.View#getController()
154      */
155     @Override
156     public Controller getController() {
157         return super.getLayoutController();
158     }
159 
160     /**
161      * Returns the view's name
162      *
163      * @see org.kuali.student.common.ui.client.mvc.View#getName()
164      */
165     @Override
166     public String getName() {
167         return viewName;
168     }
169 
170     public void setName(String name) {
171         this.viewName = name;
172     }
173 
174     public void setController(Controller controller) {
175         if (controller instanceof LayoutController) {
176             super.setLayoutController((LayoutController) controller);
177         } else {
178             throw new IllegalArgumentException("Configurable UI sections require a LayoutController, not a base MVC controller");
179         }
180     }
181 
182     /**
183      * Update the fields on the screen with the model received back from requestModel on the parent controller
184      */
185     public void updateView() {
186         getController().requestModel(modelId, new ModelRequestCallback<DataModel>() {
187             @Override
188             public void onModelReady(DataModel m) {
189                 // TODO review this, shouldn't it assign this.model = m?
190                 SectionView.this.model = m;
191                 updateWidgetData(m);
192             }
193 
194 
195             @Override
196             public void onRequestFail(Throwable cause) {
197                 Window.alert("Failed to get model");
198             }
199         });
200 
201     }
202 
203     /**
204      * Force an update of fields on this section with the model passed in
205      * @param m
206      */
207     public void updateView(DataModel m) {
208         this.model = m;
209         updateWidgetData(m);
210     }
211 
212     /**
213      * @see org.kuali.student.common.ui.client.mvc.View#asWidget()
214      */
215     public Widget asWidget() {
216         return this.getLayout();
217     }
218 
219     /**
220      * @see org.kuali.student.common.ui.client.mvc.history.HistorySupport#collectHistory(java.lang.String)
221      */
222     @Override
223     public String collectHistory(String historyStack) {
224         return null;
225     }
226 
227     /**
228      * @see org.kuali.student.common.ui.client.mvc.history.HistorySupport#onHistoryEvent(java.lang.String)
229      */
230     @Override
231     public void onHistoryEvent(String historyStack) {
232 
233     }
234 
235     /**
236      * @see org.kuali.student.common.ui.client.mvc.breadcrumb.BreadcrumbSupport#collectBreadcrumbNames(java.util.List)
237      */
238     @Override
239     public void collectBreadcrumbNames(List<String> names) {
240         names.add(this.getName());
241     }
242 
243     /**
244      * Add a view as a widget to this section
245      * @param view
246      */
247     public void addView(View view) {
248         views.add(view);
249         addWidget(view.asWidget());
250     }
251 
252     public DataModel getModel() {
253         return model;
254     }
255 
256     public void updateMetadata(ModelDefinition modelDefinition) {
257         updateMetadata(modelDefinition, this);
258     }
259 
260     private void updateMetadata(ModelDefinition modelDefinition, Section topSection) {
261         for (Section section : topSection.getSections()) {
262             updateMetadata(modelDefinition, section);
263         }
264         for (FieldDescriptor field : topSection.getFields()) {
265             Metadata newMetadata = modelDefinition.getMetadata(field.getFieldKey());
266             if (newMetadata != null) {
267                 field.setMetadata(newMetadata);
268             }
269         }
270     }
271 
272     @Override
273     public String toString() {
274         return viewName;
275     }
276     
277     public boolean isExportButtonActive() {
278         return false;
279     }
280 
281 	@Override
282 	public void showExport(boolean show) {
283 		// TODO Auto-generated method stub
284 		
285 	}
286 }