View Javadoc

1   
2   /**
3    * Copyright 2010 The Kuali Foundation Licensed under the
4    * Educational Community License, Version 2.0 (the "License"); you may
5    * not use this file except in compliance with the License. You may
6    * obtain a copy of the License at
7    * <p/>
8    * http://www.osedu.org/licenses/ECL-2.0
9    * <p/>
10   * Unless required by applicable law or agreed to in writing,
11   * software distributed under the License is distributed on an "AS IS"
12   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13   * or implied. See the License for the specific language governing
14   * permissions and limitations under the License.
15   * <p/>
16   *
17   */
18  package org.kuali.student.common.ui.client.configurable.mvc.multiplicity;
19  
20  import java.util.List;
21  
22  import org.kuali.student.common.ui.client.application.Application;
23  import org.kuali.student.common.ui.client.widgets.field.layout.element.MessageKeyInfo;
24  
25  import com.google.gwt.user.client.ui.FlexTable;
26  
27  /**
28   * A MultiplicityTable displays data in a GWT FlexTable, one cell per field defined, one row per iteration in the supplied data.
29   * Data is displayed as defined in the MultiplicityConfiguration passed in the ctor
30   * If concatenated fields have been defined the values will be concatenated (comma delimited)  and displayed in a single table cell
31   *
32   * Fields are included in the table based on the FieldDescriptors added to the configuration
33   */
34  public class MultiplicityTable extends FlexTable {
35  
36      private MultiplicityConfiguration config;
37  
38      private static final String STYLE_TABLE = "KS-MultiplicityTable";
39      private static final String STYLE_TABLE_BORDER = "KS-MultiplicityTableBorder";
40      private static final String STYLE_CELL = "KS-MultiplicityTableCell";
41      private static final String STYLE_CELL_BORDER = "KS-MultiplicityTableCellBorder";
42      private static final String STYLE_HEADER_CELL = "KS-MultiplicityTableHeaderCell";
43      private static final String BLANK_STRING = " ";
44      private int col = 0;
45      protected int row = 0;
46  
47      private String parentPath;
48  
49  
50      /**
51       *
52       * Creates an instance of a MultiplicityTable based on the options in the MultiplicityConfiguration
53       *
54       * @param config
55       */
56      public MultiplicityTable(MultiplicityConfiguration config) {
57          this.config = config;
58          setStyleName(STYLE_TABLE);
59          if (config.getStyleType() == MultiplicityConfiguration.StyleType.BORDERED_TABLE){
60               addStyleName(STYLE_TABLE_BORDER);
61          }
62      }
63  
64      public void initTable() {
65          clear();
66          while (getRowCount() > 0) {
67              removeRow(0);
68          }
69          row = 0;
70          col = 0;
71      }
72  
73      public void buildHeaders() {
74          if (config.isShowHeaders()) {
75              //TODO should just be 1 row def for a table - throw exception if > 1?
76              for (Integer row  : config.getFields().keySet()) {
77                  List<MultiplicityFieldConfiguration> fieldConfigs = config.getFields().get(row);
78                  for (MultiplicityFieldConfiguration fieldConfig : fieldConfigs) {
79                      MessageKeyInfo info = fieldConfig.getMessageKeyInfo();
80                      String title = Application.getApplicationContext().getUILabel(info.getGroup(), info.getType(), info.getState(), info.getId());
81                      addHeaderCell(title);
82                  }
83              }
84              nextRow();
85          }
86      }
87  
88      public void addHeaderCell(String fieldValue) {
89          if (config.isShowHeaders()) {
90              setCellText(row, col, fieldValue);
91              getCellFormatter().addStyleName(row, col++, STYLE_HEADER_CELL);
92          }
93      }
94  
95      public void addNextCell(String fieldValue) {
96          setCellText(row, col++, fieldValue);
97      }
98  
99      private void setCellText(int row, int cell, String fieldValue) {
100         setText(row, cell, fieldValue);
101         getCellFormatter().addStyleName(row, cell, STYLE_CELL);
102         if (config.getStyleType() == MultiplicityConfiguration.StyleType.BORDERED_TABLE) {
103             getCellFormatter().addStyleName(row, cell, STYLE_CELL_BORDER);
104         }
105     }
106 
107     public void addEmptyCell() {
108         addNextCell(BLANK_STRING);
109     }
110 
111     public void nextRow() {
112         row++;
113         col = 0;
114     }
115 
116     public MultiplicityConfiguration getConfig() {
117         return config;
118     }
119 
120     public void setConfig(MultiplicityConfiguration config) {
121         this.config = config;
122     }
123 
124     public String getParentPath() {
125         return parentPath;
126     }
127 
128     public void setParentPath(String parentPath) {
129         this.parentPath = parentPath;
130     }
131 }