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   * User: hjohnson
15   * Date: 2-Jun-2010
16   * Time: 3:26:53 PM
17   *
18   */
19  
20  package org.kuali.student.common.ui.client.configurable.mvc.sections;
21  
22  import java.util.List;
23  import java.util.Map;
24  
25  import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
26  import org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityGroupBinding;
27  import org.kuali.student.common.ui.client.configurable.mvc.binding.MultiplicityTableBinding;
28  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityConfiguration;
29  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityGroup;
30  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.MultiplicityTable;
31  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.SwapCompositeCondition;
32  import org.kuali.student.common.ui.client.configurable.mvc.multiplicity.SwapCompositeConditionFieldConfig;
33  import org.kuali.student.common.ui.client.widgets.KSErrorDialog;
34  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.TableFieldLayout;
35  import org.kuali.student.common.ui.client.widgets.field.layout.layouts.VerticalFieldLayout;
36  
37  import com.google.gwt.core.client.GWT;
38  import com.google.gwt.user.client.ui.Widget;
39  
40  /**
41   *
42   * This class creates a section containing a multiplicity widget based on the supplied configuration
43   *
44   * Sample code to use this class :-
45   * 
46   *
47   * {@code
48      private void addVersionCodeFields(Section section) {
49          QueryPath parentPath = QueryPath.concat(COURSE, QueryPath.getPathSeparator(), VERSIONS);
50  
51          MultiplicityConfiguration config = new MultiplicityConfiguration(MultiplicityConfiguration.MultiplicityType.GROUP,
52                  MultiplicityConfiguration.StyleType.TOP_LEVEL_GROUP, getMetaData(parentPath.toString()));
53          config.setAddItemLabel(getLabel(LUConstants.ADD_VERSION_CODE_LABEL_KEY));
54          config.setItemLabel(getLabel(LUConstants.VERSION_CODE_LABEL_KEY));
55          config.setUpdateable(true);
56  
57          FieldDescriptor parentFd = buildFieldDescriptor(COURSE + QueryPath.getPathSeparator() + VERSIONS, getLabel(LUConstants.VERSION_CODES_LABEL_KEY), null);
58          config.setParentFd(parentFd);
59  
60          FieldDescriptor versionCode = buildFieldDescriptor(CreditCourseVersionsConstants.VERSION_CODE, LUConstants.VERSION_CODE_LABEL_KEY, parentPath.toString());
61          FieldDescriptor versionTitle = buildFieldDescriptor(CreditCourseVersionsConstants.VERSION_TITLE, LUConstants.TITLE_LABEL_KEY, parentPath.toString());
62          config.addField(versionCode);
63          config.addField(versionTitle);
64  
65          MultiplicitySection ms = new MultiplicitySection(config);
66          section.addSection(ms);
67          }
68   * }
69   * 
70   * TODO:
71   *   - Create factory methods for each 'flavour' of multiplicity
72   *   - Styling options for table, e.g. no grid lines
73   *   - For read-only multiplicities, set contained widgets to be read only too
74   *   
75   */
76      public class MultiplicitySection extends BaseSection {
77  
78      private MultiplicityConfiguration config;
79      private Widget widget;
80      private Map<SwapCompositeCondition, List<SwapCompositeConditionFieldConfig>> swappableFieldsDefinition;
81      private List<String> deletionParentKeys;
82      public MultiplicitySection(MultiplicityConfiguration config) {
83          this.config = config;
84          initialize();
85      }
86  
87      public MultiplicitySection( 
88              MultiplicityConfiguration config,
89              Map<SwapCompositeCondition, List<SwapCompositeConditionFieldConfig>> swappableFieldsDefinition,
90              List<String> deletionParentKeys) {
91          this.config = config;
92          this.swappableFieldsDefinition = swappableFieldsDefinition;
93          this.deletionParentKeys = deletionParentKeys;
94          initialize();
95      }
96      
97  
98      private void initialize() {
99          buildLayout();
100         this.add(layout);
101     }
102 
103     /**
104      * Builds a suitable layout and adds the required multiplicity widget
105      *
106      * Builds a MultiplicityGroup for a grid layout or a MultiplicityTable
107      * for a FlexTable layout . Sets the appropriate binding for the selected
108      * widget
109      *
110      */
111     private void buildLayout() {
112 
113         if (config.getParentFd() == null) {
114             KSErrorDialog.show (new Throwable ("Multiplicity Parent FD cannot be null"));
115             return;
116         }
117          switch (config.getLayoutType()) {
118             case GROUP:
119                 layout = new VerticalFieldLayout(config.getTitle());
120                 if (config.getCustomMultiplicityGroup() == null) {
121                     widget = GWT.create(MultiplicityGroup.class);
122                     ((MultiplicityGroup) widget).init(config, swappableFieldsDefinition, deletionParentKeys);
123                 } else {
124                     widget = config.getCustomMultiplicityGroup();
125                 }
126                 config.getParentFd().setFieldWidget(widget);
127                 config.getParentFd().setWidgetBinding(new MultiplicityGroupBinding());
128                 this.addField(config.getParentFd());
129 
130                 break;
131             case TABLE:
132                 if (config.getFields().size() > 1) {
133                     KSErrorDialog.show (new Throwable ("MultiplicityTable can have only one row defined"));
134                     return;
135                 }
136                 if (config.getTitle() == null) {
137                     layout = new TableFieldLayout();
138                 }
139                 else {
140                     layout = new TableFieldLayout(config.getTitle(), false);
141                 }
142                 widget = new MultiplicityTable(config);
143                 config.getParentFd().setFieldWidget(widget);
144                 config.getParentFd().setWidgetBinding(new MultiplicityTableBinding());
145                 this.addField(config.getParentFd());
146 
147                 break;
148             default:
149                 layout = null;
150             }
151         }
152 
153     public void setParentPath(String parentPath) {
154     	if (widget instanceof MultiplicityGroup) {
155     		((MultiplicityGroup)widget).setParentPath(parentPath);
156     	}
157     	else {
158             ((MultiplicityTable)widget).setParentPath(parentPath);
159     	}
160     	
161     }
162 
163     @Override
164     public void resetFieldInteractionFlags() {
165     	super.resetFieldInteractionFlags();
166 		if (widget instanceof MultiplicityGroup){
167 			((MultiplicityGroup)widget).resetDirtyFlags();
168 		}		
169     }
170 
171     @Override
172 	public void resetDirtyFlags() {
173 		super.resetDirtyFlags();
174 		if (widget instanceof MultiplicityGroup){
175 			((MultiplicityGroup)widget).resetDirtyFlags();
176 		}		
177 	}
178 
179 	@Override
180 	public boolean isDirty() {
181 		boolean isDirty = super.isDirty();
182 		if (!isDirty && widget instanceof MultiplicityGroup){
183 			isDirty = ((MultiplicityGroup)widget).isDirty();
184 		}
185 		
186 		return isDirty;
187 	}
188 	
189 	public MultiplicityConfiguration getConfig(){
190 		return config;
191 	}
192 }