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  package org.kuali.student.lum.common.client.widgets;
16  
17  import java.util.List;
18  
19  import org.kuali.student.r1.common.assembly.data.Data;
20  import org.kuali.student.r1.common.assembly.data.Metadata;
21  import org.kuali.student.common.ui.client.configurable.mvc.DefaultWidgetFactory;
22  import org.kuali.student.common.ui.client.configurable.mvc.sections.VerticalSection;
23  import org.kuali.student.common.ui.client.mvc.Callback;
24  import org.kuali.student.common.ui.client.mvc.HasDataValue;
25  import org.kuali.student.common.ui.client.widgets.KSDropDown;
26  import org.kuali.student.common.ui.client.widgets.KSLabel;
27  import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
28  import org.kuali.student.common.ui.client.widgets.impl.KSDropDownImpl;
29  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeEvent;
30  import org.kuali.student.common.ui.client.widgets.list.SelectionChangeHandler;
31  import org.kuali.student.common.ui.client.widgets.list.impl.SimpleListItems;
32  import org.kuali.student.common.ui.client.widgets.progress.BlockingTask;
33  import org.kuali.student.common.ui.client.widgets.progress.KSBlockingProgressIndicator;
34  import org.kuali.student.common.ui.client.widgets.search.KSPicker;
35  import org.kuali.student.core.statement.ui.client.widgets.rules.AccessWidgetValue;
36  
37  import com.google.gwt.user.client.ui.Composite;
38  
39  public class CourseWidget extends Composite implements AccessWidgetValue, HasDataValue {
40  
41      private CluSetRetriever courseMetadataRetriever  = new CluSetRetrieverImpl();
42      private Callback getCluNameCallback;
43  
44      //widgets
45      protected VerticalSection layout = new VerticalSection();
46      protected KSDropDown courseTypeWidget;
47      protected KSPicker courseWidget = null;
48      private KSLabel previousCourseCode;
49      private String previousCourseId;
50  
51      //data
52      private Metadata searchCourseMetadata = null;
53      private BlockingTask initializeTask = new BlockingTask("Initializing");
54  
55      public CourseWidget() {
56          this.initWidget(layout);
57      }
58  
59      @Override
60      public void initWidget(List<Metadata> fieldsMetadata) {
61  
62          previousCourseCode = null;
63          previousCourseId = null;
64          
65          layout.clear();
66  
67          final VerticalSection choosingSection = new VerticalSection();
68          choosingSection.addWidget(new KSLabel("<b>Add a course</b>"));
69  
70          //first metadata is a drop down to select type of course
71          createAndAddCourseTypesDropdown();
72  
73          retrieveMetadata();
74      }
75  
76      protected void createAndAddCourseTypesDropdown() {
77          courseTypeWidget = new KSDropDown();
78          SimpleListItems courseTypes = new SimpleListItems();
79          courseTypes.addItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD, "Approved Courses");
80          courseTypes.addItem(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD, "Proposed Courses");
81          courseTypeWidget.setListItems(courseTypes);
82          courseTypeWidget.addSelectionChangeHandler(new SelectionChangeHandler() {
83  
84              @Override
85              public void onSelectionChange(SelectionChangeEvent event) {
86                  String courseTypeSelected = ((KSDropDownImpl)event.getWidget()).getSelectedItem();
87                  if (courseTypeSelected == null) {
88                      if (courseWidget != null) {
89                          layout.remove(courseWidget);
90                      }                    
91                      return;
92                  }
93                  addCourseListWidget(true, courseTypeSelected);
94              }
95          });        
96          layout.add(courseTypeWidget);
97      }
98  
99      private void retrieveMetadata() {
100         if (searchCourseMetadata == null) {
101             KSBlockingProgressIndicator.addTask(initializeTask);
102             courseMetadataRetriever.getMetadata("courseSet", new Callback<Metadata>(){
103                 @Override
104                 public void exec(Metadata result) {
105                     searchCourseMetadata = result;
106                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD).getConstraints().get(0).setMaxOccurs(1);
107                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD).getConstraints().get(0).setMaxOccurs(1);
108                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD).getConstraints().get(0).setId("single");
109                     searchCourseMetadata.getProperties().get(CommonWidgetConstants.CLU_SET_PROPOSED_CLUS_FIELD).getConstraints().get(0).setId("single");
110                     KSBlockingProgressIndicator.removeTask(initializeTask);
111 
112                     if (previousCourseCode == null) {
113                         courseTypeWidget.selectItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD);
114                         addCourseListWidget(true, courseTypeWidget.getSelectedItem());
115                     }
116                 }
117             });
118         } else {
119             if (previousCourseCode == null) {
120                 courseTypeWidget.selectItem(CommonWidgetConstants.CLU_SET_APPROVED_CLUS_FIELD);
121                 addCourseListWidget(true, courseTypeWidget.getSelectedItem());
122             }
123         }
124     }
125 
126     protected void addCourseListWidget(boolean enabled, String courseType) {
127         if (courseWidget != null) {
128             layout.remove(courseWidget);
129         }
130         if (previousCourseCode != null) {
131             layout.remove(previousCourseCode);
132             previousCourseCode = null;
133             previousCourseId = null;
134         }
135         courseWidget = (KSPicker) DefaultWidgetFactory.getInstance().getWidget(searchCourseMetadata.getProperties().get(courseType));
136         courseWidget.getSearchPanel().setMutipleSelect(false);
137     //    ((KSSuggestBox) courseWidget.getInputWidget()).setEnabled(enabled);
138         layout.add(courseWidget);
139     }
140 
141     protected MessageKeyInfo generateMessageInfo(String labelKey) {
142         return new MessageKeyInfo("clusetmanagement", "clusetmanagement", "draft", labelKey);
143     }
144 
145 	@Override
146 	public void addValueChangeCallback(Callback<Data.Value> callback) {
147 	}
148 
149 	@Override
150 	public void setValue(Data.Value value) {    
151 	}
152 
153     @Override
154     public void getValue(Callback<String> doneSaveCallback) { 
155     }
156 
157     @Override
158     public void setValue(final String id) {
159         if (id != null) {
160             getCluNameCallback.exec(id);
161         }        
162     }
163 
164     public void setLabelContent(String id, final String code) {
165         layout.clear();
166         previousCourseId = id;
167         previousCourseCode = new KSLabel(code);
168         layout.add(previousCourseCode);
169         createAndAddCourseTypesDropdown();
170     }
171 
172     public void addGetCluNameCallback(Callback callback) {
173         this.getCluNameCallback = callback;    
174     }
175 
176     @Override
177     public Data.Value getValue() {
178         Data.Value pickerValue = courseWidget.getValue();
179         if ((pickerValue.toString().isEmpty()) && (previousCourseCode != null) && (!previousCourseCode.getText().isEmpty())) {
180             return new Data.StringValue(previousCourseId);
181         }
182 
183         return pickerValue;
184     }
185 }