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  @Deprecated
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 = new MultiplicityGroup(config, swappableFieldsDefinition, deletionParentKeys);
122                 } else {
123                     widget = config.getCustomMultiplicityGroup();
124                 }
125                 config.getParentFd().setFieldWidget(widget);
126                 config.getParentFd().setWidgetBinding(new MultiplicityGroupBinding());
127                 this.addField(config.getParentFd());
128 
129                 break;
130             case TABLE:
131                 if (config.getFields().size() > 1) {
132                     KSErrorDialog.show (new Throwable ("MultiplicityTable can have only one row defined"));
133                     return;
134                 }
135                 if (config.getTitle() == null) {
136                     layout = new TableFieldLayout();
137                 }
138                 else {
139                     layout = new TableFieldLayout(config.getTitle(), false);
140                 }
141                 widget = new MultiplicityTable(config);
142                 config.getParentFd().setFieldWidget(widget);
143                 config.getParentFd().setWidgetBinding(new MultiplicityTableBinding());
144                 this.addField(config.getParentFd());
145 
146                 break;
147             default:
148                 layout = null;
149             }
150         }
151 
152     public void setParentPath(String parentPath) {
153     	if (widget instanceof MultiplicityGroup) {
154     		((MultiplicityGroup)widget).setParentPath(parentPath);
155     	}
156     	else {
157             ((MultiplicityTable)widget).setParentPath(parentPath);
158     	}
159     	
160     }
161 
162     @Override
163     public void resetFieldInteractionFlags() {
164     	super.resetFieldInteractionFlags();
165 		if (widget instanceof MultiplicityGroup){
166 			((MultiplicityGroup)widget).resetDirtyFlags();
167 		}		
168     }
169 
170     @Override
171 	public void resetDirtyFlags() {
172 		super.resetDirtyFlags();
173 		if (widget instanceof MultiplicityGroup){
174 			((MultiplicityGroup)widget).resetDirtyFlags();
175 		}		
176 	}
177 
178 	@Override
179 	public boolean isDirty() {
180 		boolean isDirty = super.isDirty();
181 		if (!isDirty && widget instanceof MultiplicityGroup){
182 			isDirty = ((MultiplicityGroup)widget).isDirty();
183 		}
184 		
185 		return isDirty;
186 	}
187 	
188 	public MultiplicityConfiguration getConfig(){
189 		return config;
190 	}
191 }