| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.krad.uif.widget; |
| 17 | |
|
| 18 | |
import org.apache.commons.lang.ClassUtils; |
| 19 | |
import org.apache.commons.lang.StringUtils; |
| 20 | |
import org.kuali.rice.core.api.util.type.KualiDecimal; |
| 21 | |
import org.kuali.rice.core.api.util.type.KualiInteger; |
| 22 | |
import org.kuali.rice.core.api.util.type.KualiPercent; |
| 23 | |
import org.kuali.rice.krad.uif.UifConstants; |
| 24 | |
import org.kuali.rice.krad.uif.container.CollectionGroup; |
| 25 | |
import org.kuali.rice.krad.uif.view.View; |
| 26 | |
import org.kuali.rice.krad.uif.control.CheckboxControl; |
| 27 | |
import org.kuali.rice.krad.uif.control.CheckboxGroupControl; |
| 28 | |
import org.kuali.rice.krad.uif.control.RadioGroupControl; |
| 29 | |
import org.kuali.rice.krad.uif.control.SelectControl; |
| 30 | |
import org.kuali.rice.krad.uif.control.TextControl; |
| 31 | |
import org.kuali.rice.krad.uif.component.Component; |
| 32 | |
import org.kuali.rice.krad.uif.field.AttributeField; |
| 33 | |
import org.kuali.rice.krad.uif.field.FieldGroup; |
| 34 | |
import org.kuali.rice.krad.uif.layout.LayoutManager; |
| 35 | |
import org.kuali.rice.krad.uif.layout.TableLayoutManager; |
| 36 | |
import org.kuali.rice.krad.uif.util.ObjectPropertyUtils; |
| 37 | |
import org.kuali.rice.krad.web.form.UifFormBase; |
| 38 | |
|
| 39 | |
import java.sql.Timestamp; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public class TableTools extends WidgetBase { |
| 52 | |
private static final long serialVersionUID = 4671589690877390070L; |
| 53 | |
|
| 54 | |
private String emptyTableMessage; |
| 55 | |
private boolean disableTableSort; |
| 56 | |
|
| 57 | 0 | private boolean showSearchAndExportOptions = true; |
| 58 | |
|
| 59 | |
public TableTools() { |
| 60 | 0 | super(); |
| 61 | 0 | } |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
@Override |
| 71 | |
public void performFinalize(View view, Object model, Component component) { |
| 72 | 0 | super.performFinalize(view, model, component); |
| 73 | |
|
| 74 | 0 | UifFormBase formBase = (UifFormBase) model; |
| 75 | |
|
| 76 | 0 | if (isRender()) { |
| 77 | 0 | if (StringUtils.isNotBlank(getEmptyTableMessage())) { |
| 78 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.LANGUAGE, |
| 79 | |
"{\"" + UifConstants.TableToolsKeys.EMPTY_TABLE + "\" : \"" + getEmptyTableMessage() + "\"}"); |
| 80 | |
} |
| 81 | |
|
| 82 | 0 | if (!isShowSearchAndExportOptions()) { |
| 83 | 0 | Object domOption = getComponentOptions().get(UifConstants.TableToolsKeys.SDOM); |
| 84 | 0 | if (domOption instanceof String) { |
| 85 | 0 | String sDomOption = (String) domOption; |
| 86 | 0 | if (StringUtils.isNotBlank(sDomOption)) { |
| 87 | 0 | sDomOption = StringUtils.remove(sDomOption, "T"); |
| 88 | 0 | sDomOption = StringUtils.remove(sDomOption, "f"); |
| 89 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.SDOM, sDomOption); |
| 90 | |
} |
| 91 | |
} |
| 92 | |
|
| 93 | |
} |
| 94 | |
|
| 95 | |
|
| 96 | 0 | if (UifConstants.ActionEvents.ADD_LINE.equals(formBase.getActionEvent())) { |
| 97 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.AASORTING, "[]"); |
| 98 | |
} |
| 99 | |
|
| 100 | 0 | if (component instanceof CollectionGroup) { |
| 101 | 0 | buildTableSortOptions((CollectionGroup) component); |
| 102 | |
} |
| 103 | |
|
| 104 | 0 | if (isDisableTableSort()) { |
| 105 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.TABLE_SORT, "false"); |
| 106 | |
} |
| 107 | |
} |
| 108 | 0 | } |
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 114 | |
|
| 115 | |
protected void buildTableSortOptions(CollectionGroup collectionGroup) { |
| 116 | 0 | LayoutManager layoutManager = collectionGroup.getLayoutManager(); |
| 117 | |
|
| 118 | |
|
| 119 | 0 | if (!collectionGroup.getSubCollections().isEmpty()) { |
| 120 | 0 | setDisableTableSort(true); |
| 121 | |
} |
| 122 | |
|
| 123 | |
|
| 124 | |
|
| 125 | 0 | if (collectionGroup.isRenderAddLine() |
| 126 | |
&& !collectionGroup.isReadOnly() |
| 127 | |
&& !((layoutManager instanceof TableLayoutManager) && ((TableLayoutManager) layoutManager) |
| 128 | |
.isSeparateAddLine())) { |
| 129 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.SORT_SKIP_ROWS, |
| 130 | |
"[" + UifConstants.TableToolsValues.ADD_ROW_DEFAULT_INDEX + "]"); |
| 131 | |
} |
| 132 | |
|
| 133 | 0 | StringBuffer tableToolsColumnOptions = new StringBuffer("["); |
| 134 | |
|
| 135 | 0 | if (layoutManager instanceof TableLayoutManager && ((TableLayoutManager) layoutManager) |
| 136 | |
.isRenderSequenceField()) { |
| 137 | 0 | tableToolsColumnOptions.append(" null ,"); |
| 138 | |
} |
| 139 | |
|
| 140 | |
|
| 141 | 0 | if (collectionGroup.isRenderSelectField()) { |
| 142 | 0 | String colOptions = constructTableColumnOptions(false, null, null); |
| 143 | 0 | tableToolsColumnOptions.append(colOptions + " , "); |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | 0 | for (Component component : collectionGroup.getItems()) { |
| 148 | |
|
| 149 | 0 | if (component instanceof FieldGroup) { |
| 150 | 0 | component = ((FieldGroup) component).getItems().get(0); |
| 151 | |
} |
| 152 | |
|
| 153 | 0 | if (component instanceof AttributeField) { |
| 154 | 0 | AttributeField field = (AttributeField) component; |
| 155 | |
|
| 156 | 0 | String sortType = null; |
| 157 | 0 | if (collectionGroup.isReadOnly() || (field.getControl() == null)) { |
| 158 | 0 | sortType = UifConstants.TableToolsValues.DOM_TEXT; |
| 159 | 0 | } else if (field.getControl() instanceof TextControl) { |
| 160 | 0 | sortType = UifConstants.TableToolsValues.DOM_TEXT; |
| 161 | 0 | } else if (field.getControl() instanceof SelectControl) { |
| 162 | 0 | sortType = UifConstants.TableToolsValues.DOM_SELECT; |
| 163 | 0 | } else if (field.getControl() instanceof CheckboxControl || field |
| 164 | |
.getControl() instanceof CheckboxGroupControl) { |
| 165 | 0 | sortType = UifConstants.TableToolsValues.DOM_CHECK; |
| 166 | 0 | } else if (field.getControl() instanceof RadioGroupControl) { |
| 167 | 0 | sortType = UifConstants.TableToolsValues.DOM_RADIO; |
| 168 | |
} |
| 169 | |
|
| 170 | 0 | Class dataTypeClass = ObjectPropertyUtils.getPropertyType( |
| 171 | |
collectionGroup.getCollectionObjectClass(), ((AttributeField) component).getPropertyName()); |
| 172 | 0 | String colOptions = constructTableColumnOptions(true, dataTypeClass, sortType); |
| 173 | 0 | tableToolsColumnOptions.append(colOptions + " , "); |
| 174 | 0 | } else { |
| 175 | 0 | String colOptions = constructTableColumnOptions(false, null, null); |
| 176 | 0 | tableToolsColumnOptions.append(colOptions + " , "); |
| 177 | 0 | } |
| 178 | |
} |
| 179 | |
|
| 180 | 0 | if (collectionGroup.isRenderLineActions() && !collectionGroup.isReadOnly()) { |
| 181 | 0 | String colOptions = constructTableColumnOptions(false, null, null); |
| 182 | 0 | tableToolsColumnOptions.append(colOptions); |
| 183 | 0 | } else { |
| 184 | 0 | tableToolsColumnOptions = new StringBuffer(StringUtils.removeEnd(tableToolsColumnOptions.toString(), |
| 185 | |
", ")); |
| 186 | |
} |
| 187 | |
|
| 188 | 0 | tableToolsColumnOptions.append("]"); |
| 189 | |
|
| 190 | 0 | getComponentOptions().put(UifConstants.TableToolsKeys.AO_COLUMNS, tableToolsColumnOptions.toString()); |
| 191 | |
|
| 192 | 0 | } |
| 193 | |
|
| 194 | |
|
| 195 | |
|
| 196 | |
|
| 197 | |
protected String constructTableColumnOptions(boolean isSortable, Class dataTypeClass, String sortDataType) { |
| 198 | 0 | String colOptions = "null"; |
| 199 | |
|
| 200 | 0 | String sortType = ""; |
| 201 | 0 | if (!isSortable || dataTypeClass == null || sortType == null) { |
| 202 | 0 | colOptions = "\"" + UifConstants.TableToolsKeys.SORTABLE + "\" : false, \"sType\" : \"string\""; |
| 203 | |
} else { |
| 204 | 0 | if (ClassUtils.isAssignable(dataTypeClass, KualiPercent.class)) { |
| 205 | 0 | sortType = UifConstants.TableToolsValues.PERCENT; |
| 206 | 0 | } else if (ClassUtils.isAssignable(dataTypeClass, KualiInteger.class) || ClassUtils.isAssignable( |
| 207 | |
dataTypeClass, KualiDecimal.class)) { |
| 208 | 0 | sortType = UifConstants.TableToolsValues.CURRENCY; |
| 209 | 0 | } else if (ClassUtils.isAssignable(dataTypeClass, Timestamp.class)) { |
| 210 | 0 | sortType = "date"; |
| 211 | 0 | } else if (ClassUtils.isAssignable(dataTypeClass, java.sql.Date.class) || ClassUtils.isAssignable( |
| 212 | |
dataTypeClass, java.util.Date.class)) { |
| 213 | 0 | sortType = UifConstants.TableToolsValues.DATE; |
| 214 | 0 | } else if (ClassUtils.isAssignable(dataTypeClass, Number.class)) { |
| 215 | 0 | sortType = UifConstants.TableToolsValues.NUMERIC; |
| 216 | |
} |
| 217 | |
else { |
| 218 | 0 | sortType = UifConstants.TableToolsValues.STRING; |
| 219 | |
} |
| 220 | |
|
| 221 | 0 | colOptions = "\"" + UifConstants.TableToolsKeys.SORT_DATA_TYPE + "\" : \"" + sortDataType + "\""; |
| 222 | 0 | colOptions += " , \"" + UifConstants.TableToolsKeys.SORT_TYPE + "\" : \"" + sortType + "\""; |
| 223 | |
} |
| 224 | |
|
| 225 | 0 | colOptions = "{" + colOptions + "}"; |
| 226 | |
|
| 227 | 0 | return colOptions; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
public String getEmptyTableMessage() { |
| 236 | 0 | return emptyTableMessage; |
| 237 | |
} |
| 238 | |
|
| 239 | |
|
| 240 | |
|
| 241 | |
|
| 242 | |
|
| 243 | |
|
| 244 | |
public void setEmptyTableMessage(String emptyTableMessage) { |
| 245 | 0 | this.emptyTableMessage = emptyTableMessage; |
| 246 | 0 | } |
| 247 | |
|
| 248 | |
|
| 249 | |
|
| 250 | |
|
| 251 | |
|
| 252 | |
|
| 253 | |
public boolean isDisableTableSort() { |
| 254 | 0 | return this.disableTableSort; |
| 255 | |
} |
| 256 | |
|
| 257 | |
|
| 258 | |
|
| 259 | |
|
| 260 | |
|
| 261 | |
|
| 262 | |
public void setDisableTableSort(boolean disableTableSort) { |
| 263 | 0 | this.disableTableSort = disableTableSort; |
| 264 | 0 | } |
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 269 | |
|
| 270 | |
|
| 271 | |
public boolean isShowSearchAndExportOptions() { |
| 272 | 0 | return this.showSearchAndExportOptions; |
| 273 | |
} |
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
public void setShowSearchAndExportOptions(boolean showSearchAndExportOptions) { |
| 281 | 0 | this.showSearchAndExportOptions = showSearchAndExportOptions; |
| 282 | 0 | } |
| 283 | |
} |