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