All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.javascript.ext.ExtGridCol Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.javascript.ext;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.hfg.exception.ProgrammingException;
import com.hfg.html.attribute.Align;
import com.hfg.javascript.JsObjMap;
import com.hfg.util.AttributeMgr;
import com.hfg.util.BooleanUtil;
import com.hfg.util.CompareUtil;
import com.hfg.util.StringUtil;
import com.hfg.util.collection.CollectionUtil;
import com.hfg.xml.XMLAttribute;
import com.hfg.xml.XMLTag;

//------------------------------------------------------------------------------
/**
 Definition for a column from an ExtJs grid.
 
@author J. Alex Taylor, hairyfatguy.com
*/ //------------------------------------------------------------------------------ // com.hfg Library // // This library is free software; you can redistribute it and/or // modify it under the terms of the GNU Lesser General Public // License as published by the Free Software Foundation; either // version 2.1 of the License, or (at your option) any later version. // // This library is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // Lesser General Public License for more details. // // You should have received a copy of the GNU Lesser General Public // License along with this library; if not, write to the Free Software // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA // // J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com // [email protected] //------------------------------------------------------------------------------ public class ExtGridCol implements Cloneable, Comparable { private String mText; private String mTooltip; private String mDataIndex; private Integer mWidth; private Integer mMinWidth; private Integer mMaxWidth; private Align mAlign; private String mRenderer; private String mEditor; private String mItemId; private Boolean mSortable; private Boolean mHideable; private Boolean mHidden; private Boolean mLockable; private Boolean mLocked; private Boolean mDraggable; private Boolean mGroupable; private Boolean mResizable; private Boolean mVariableRowHeight; private String mFilter; private Integer mFlex; private String mXType; private String mSortType; private String mEmptyCellText; private ExtFieldType mFieldType; private List mGroupCols; private ExtGridCol mParentCol; private Map mCustomFields; private AttributeMgr mAttributeMgr; private enum Field { align, columns, dataIndex, draggable, editor, emptyCellText, flex, filter, groupable, hidden, hideable, itemId, lockable, locked, maxWidth, minWidth, renderer, resizable, sortable, sortType, text, tooltip, variaibleRowHeight, width, xtype } private static final String XML_TAG_NAME = "ExtGridCol"; //########################################################################## // CONSTRUCTORS //########################################################################## //-------------------------------------------------------------------------- public ExtGridCol() { } //-------------------------------------------------------------------------- public ExtGridCol(JsObjMap inJson) { for (String key : inJson.keySet()) { try { Field field = Field.valueOf(key); Object value = inJson.get(field.name()); switch (field) { case align: setAlign(Align.valueOf((String) value)); break; case columns: for (JsObjMap subCol : (List) value) { addGroupColumn(new ExtGridCol(subCol)); } break; case dataIndex: setDataIndex((String) value); break; case draggable: setDraggable((Boolean) value); break; case editor: setEditor((String) value); break; case emptyCellText: setEmptyCellText((String) value); break; case filter: setFilter((String) value); break; case flex: setFlex((Integer) value); break; case groupable: setGroupable((Boolean) value); break; case hidden: setHidden((Boolean) value); break; case hideable: setHideable((Boolean) value); break; case itemId: setItemId((String) value); break; case lockable: setLockable((Boolean) value); break; case locked: setLocked((Boolean) value); break; case minWidth: setMinWidth((Integer) value); break; case maxWidth: setMaxWidth((Integer) value); break; case renderer: setRenderer((String) value); break; case resizable: setResizable((Boolean) value); break; case sortable: setSortable((Boolean) value); break; case sortType: setSortType((String) value); break; case text: setText((String) value); break; case tooltip: setTooltip((String) value); break; case variaibleRowHeight: setVariableRowHeight((Boolean) value); break; case width: setWidth(value != null ? ((Number) value).intValue() : null); break; case xtype: setXType((String) value); break; } } catch (Exception e) { // Ignore } } } //-------------------------------------------------------------------------- public ExtGridCol(XMLTag inXMLTag) { this(); inXMLTag.verifyTagName(XML_TAG_NAME); for (XMLAttribute attr : inXMLTag.getAttributes()) { Field field = Field.valueOf(attr.getName()); if (field != null) { String value = attr.getValue(); switch (field) { case align: setAlign(Align.valueOf(value)); break; case dataIndex: setDataIndex(value); break; case draggable: setDraggable(BooleanUtil.valueOf(value)); break; case editor: setEditor(value); break; case emptyCellText: setEmptyCellText(value); break; case filter: setFilter(value); break; case flex: setFlex(Integer.parseInt(value)); break; case groupable: setGroupable(BooleanUtil.valueOf(value)); break; case hidden: setHidden(BooleanUtil.valueOf(value)); break; case hideable: setHideable(BooleanUtil.valueOf(value)); break; case itemId: setItemId(value); break; case lockable: setLockable(BooleanUtil.valueOf(value)); break; case locked: setLocked(BooleanUtil.valueOf(value)); break; case minWidth: setMinWidth(Integer.parseInt(value)); break; case maxWidth: setMaxWidth(Integer.parseInt(value)); break; case renderer: setRenderer(value); break; case resizable: setResizable(BooleanUtil.valueOf(value)); break; case sortable: setSortable(BooleanUtil.valueOf(value)); break; case sortType: setSortType(value); break; case text: setText(value); break; case tooltip: setTooltip(value); break; case variaibleRowHeight: setVariableRowHeight(BooleanUtil.valueOf(value)); break; case width: setWidth(Integer.parseInt(value)); break; case xtype: setXType(value); break; } } } XMLTag columnsTag = inXMLTag.getOptionalSubtagByName(Field.columns.name()); if (columnsTag != null) { for (XMLTag subtag : (List) (Object) columnsTag.getSubtags()) { addGroupColumn(new ExtGridCol(subtag)); } } } //########################################################################## // PUBLIC METHODS //########################################################################## //-------------------------------------------------------------------------- @Override public String toString() { return toJsObjMap().toJavascript(); } //-------------------------------------------------------------------------- @Override public boolean equals(Object inObj2) { return (0 == compareTo(inObj2)); } //-------------------------------------------------------------------------- @Override public int hashCode() { int hashCode = 1; if (StringUtil.isSet(getDataIndex())) { hashCode += 31 * getDataIndex().hashCode(); } else if (StringUtil.isSet(getText())) { hashCode += getText().hashCode(); } return hashCode; } //-------------------------------------------------------------------------- /** * Currently compares just dataIndex values or text if the dataIndex isn't set. * @param inObj2 the Object to compare to this ExtGridCol object. * @return whether the specified object is less than, equal to, or greater than this object. */ public int compareTo(Object inObj2) { int result = -1; if (inObj2 != null && inObj2 instanceof ExtGridCol) { ExtGridCol col2 = (ExtGridCol) inObj2; result = CompareUtil.compare(getDataIndex(), col2.getDataIndex()); if (0 == result && null == getDataIndex() && getText() != null) { result = CompareUtil.compare(getText(), col2.getText()); } } return result; } //-------------------------------------------------------------------------- public ExtModelField toExtModelField() { ExtModelField modelField = null; if (getDataIndex() != null) { modelField = new ExtModelField(getDataIndex()); if (getSortType() != null) { modelField.setSortType(getSortType()); } if (getFieldType() != null) { modelField.setType(getFieldType()); } else if (getFilter() != null) { switch (getFilter()) { case "string": modelField.setType(ExtFieldType.STRING); break; case "number": modelField.setType(ExtFieldType.FLOAT); break; case "boolean": modelField.setType(ExtFieldType.BOOLEAN); break; case "date": modelField.setType(ExtFieldType.DATE); modelField.setDateFormat("time"); break; } } else if (getXType() != null && getXType().equalsIgnoreCase("booleancolumn")) { modelField.setType(ExtFieldType.BOOLEAN); } } return modelField; } //-------------------------------------------------------------------------- public JsObjMap toJsObjMap() { JsObjMap js = new JsObjMap(); if (getText() != null) { js.put(Field.text.name(), getText()); } if (getDataIndex() != null) { js.put(Field.dataIndex.name(), getDataIndex()); } if (getTooltip() != null) { js.put(Field.tooltip.name(), getTooltip()); } if (getWidth() != null) { js.put(Field.width.name(), getWidth()); } if (getMinWidth() != null) { js.put(Field.minWidth.name(), getMinWidth()); } if (getMaxWidth() != null) { js.put(Field.maxWidth.name(), getMaxWidth()); } if (getAlign() != null) { js.put(Field.align.name(), getAlign()); } if (getSortable() != null) { js.put(Field.sortable.name(), getSortable()); } /* if (getSortType() != null) { js.put(Field.sortType.name(), getSortType()); } */ if (getHidden() != null) { js.put(Field.hidden.name(), getHidden()); } if (getHideable() != null) { js.put(Field.hideable.name(), getHideable()); } if (getLocked() != null) { js.put(Field.locked.name(), getLocked()); } if (getLockable() != null) { js.put(Field.lockable.name(), getLockable()); } if (getDraggable() != null) { js.put(Field.draggable.name(), getDraggable()); } if (getGroupable() != null) { js.put(Field.groupable.name(), getGroupable()); } if (getResizable() != null) { js.put(Field.resizable.name(), getResizable()); } if (getVariableRowHeight() != null) { js.put(Field.variaibleRowHeight.name(), getVariableRowHeight()); } if (getRenderer() != null) { js.putUnquoted(Field.renderer.name(), getRenderer()); } if (getEditor() != null) { js.putUnquoted(Field.editor.name(), getEditor()); } if (getItemId() != null) { js.put(Field.itemId.name(), getItemId()); } if (getFilter() != null) { js.put(Field.filter.name(), getFilter()); } if (getFlex() != null) { js.put(Field.flex.name(), getFlex()); } if (getXType() != null) { js.put(Field.xtype.name(), getXType()); } if (getEmptyCellText() != null) { js.put(Field.emptyCellText.name(), getEmptyCellText()); } if (CollectionUtil.hasValues(mCustomFields)) { for (String key : mCustomFields.keySet()) { js.put(key, mCustomFields.get(key)); } } if (CollectionUtil.hasValues(getGroupColumns())) { js.putUnquoted(Field.columns.name(), "[" + StringUtil.join(getGroupColumns(), ", ") + "]"); } return js; } //-------------------------------------------------------------------------- public XMLTag toXMLTag() { XMLTag tag = new XMLTag(XML_TAG_NAME); if (getText() != null) { tag.setAttribute(Field.text.name(), getText()); } if (getDataIndex() != null) { tag.setAttribute(Field.dataIndex.name(), getDataIndex()); } if (getTooltip() != null) { tag.setAttribute(Field.tooltip.name(), getTooltip()); } if (getWidth() != null) { tag.setAttribute(Field.width.name(), getWidth()); } if (getMinWidth() != null) { tag.setAttribute(Field.minWidth.name(), getMinWidth()); } if (getMaxWidth() != null) { tag.setAttribute(Field.maxWidth.name(), getMaxWidth()); } if (getAlign() != null) { tag.setAttribute(Field.align.name(), getAlign()); } if (getSortable() != null) { tag.setAttribute(Field.sortable.name(), getSortable()); } if (getSortType() != null) { tag.setAttribute(Field.sortType.name(), getSortType()); } if (getHidden() != null) { tag.setAttribute(Field.hidden.name(), getHidden()); } if (getHideable() != null) { tag.setAttribute(Field.hideable.name(), getHideable()); } if (getLocked() != null) { tag.setAttribute(Field.locked.name(), getLocked()); } if (getLockable() != null) { tag.setAttribute(Field.lockable.name(), getLockable()); } if (getDraggable() != null) { tag.setAttribute(Field.draggable.name(), getDraggable()); } if (getGroupable() != null) { tag.setAttribute(Field.groupable.name(), getGroupable()); } if (getResizable() != null) { tag.setAttribute(Field.resizable.name(), getResizable()); } if (getVariableRowHeight() != null) { tag.setAttribute(Field.variaibleRowHeight.name(), getVariableRowHeight()); } if (getRenderer() != null) { tag.setAttribute(Field.renderer.name(), getRenderer()); } if (getEditor() != null) { tag.setAttribute(Field.editor.name(), getEditor()); } if (getItemId() != null) { tag.setAttribute(Field.itemId.name(), getItemId()); } if (getFilter() != null) { tag.setAttribute(Field.filter.name(), getFilter()); } if (getFlex() != null) { tag.setAttribute(Field.flex.name(), getFlex()); } if (getXType() != null) { tag.setAttribute(Field.xtype.name(), getXType()); } if (getEmptyCellText() != null) { tag.setAttribute(Field.emptyCellText.name(), getEmptyCellText()); } if (CollectionUtil.hasValues(getGroupColumns())) { XMLTag columnsTag = new XMLTag(Field.columns.name()); tag.addSubtag(columnsTag); for (ExtGridCol groupCol : getGroupColumns()) { columnsTag.addSubtag(groupCol.toXMLTag()); } } return tag; } //--------------------------------------------------------------------------- @Override public ExtGridCol clone() { ExtGridCol cloneObj; try { cloneObj = (ExtGridCol) super.clone(); } catch (CloneNotSupportedException e) { throw new ProgrammingException(e); } if (mGroupCols != null) { cloneObj.mGroupCols = new ArrayList<>(mGroupCols.size()); for (ExtGridCol groupCol : mGroupCols) { cloneObj.addGroupColumn(groupCol.clone()); } } return cloneObj; } //-------------------------------------------------------------------------- public List getGroupColumns() { return mGroupCols; } //-------------------------------------------------------------------------- public void addGroupColumn(ExtGridCol inValue) { if (null == mGroupCols) { mGroupCols = new ArrayList<>(20); } mGroupCols.add(inValue); inValue.mParentCol = this; } //-------------------------------------------------------------------------- public void setGroupColumn(int inIndex, ExtGridCol inValue) { if (null == mGroupCols) { mGroupCols = new ArrayList<>(20); } mGroupCols.set(inIndex, inValue); inValue.mParentCol = this; } //-------------------------------------------------------------------------- public void removeGroupColumn(ExtGridCol inValue) { if (mGroupCols != null) { mGroupCols.remove(inValue); inValue.mParentCol = null; } } //-------------------------------------------------------------------------- public void clearGroupColumns() { if (mGroupCols != null) { for (ExtGridCol child : mGroupCols) { child.mParentCol = null; } mGroupCols.clear(); } } //-------------------------------------------------------------------------- public ExtGridCol getParentCol() { return mParentCol; } //-------------------------------------------------------------------------- public ExtGridCol setCustomField(String inName, String inValue) { if (null == mCustomFields) { mCustomFields = new HashMap<>(5); } mCustomFields.put(inName, inValue); return this; } //-------------------------------------------------------------------------- public String getCustomField(String inName) { return (mCustomFields != null ? mCustomFields.get(inName) : null); } //-------------------------------------------------------------------------- public ExtGridCol setText(String inValue) { mText = inValue; return this; } //-------------------------------------------------------------------------- public String getText() { return mText; } //-------------------------------------------------------------------------- public ExtGridCol setEmptyCellText(String inValue) { mEmptyCellText = inValue; return this; } //-------------------------------------------------------------------------- public String getEmptyCellText() { return mEmptyCellText; } //-------------------------------------------------------------------------- public ExtGridCol setTooltip(String inValue) { mTooltip = inValue; return this; } //-------------------------------------------------------------------------- public String getTooltip() { return mTooltip; } //-------------------------------------------------------------------------- public ExtGridCol setDataIndex(ExtModelField inValue) { mDataIndex = inValue.name(); return this; } //-------------------------------------------------------------------------- public ExtGridCol setDataIndex(String inValue) { mDataIndex = inValue; return this; } //-------------------------------------------------------------------------- public String getDataIndex() { return mDataIndex; } //-------------------------------------------------------------------------- public ExtGridCol setWidth(Integer inValue) { mWidth = inValue; return this; } //-------------------------------------------------------------------------- public Integer getWidth() { return mWidth; } //-------------------------------------------------------------------------- public ExtGridCol setMinWidth(Integer inValue) { mMinWidth = inValue; return this; } //-------------------------------------------------------------------------- public Integer getMinWidth() { return mMinWidth; } //-------------------------------------------------------------------------- public ExtGridCol setMaxWidth(Integer inValue) { mMaxWidth = inValue; return this; } //-------------------------------------------------------------------------- public Integer getMaxWidth() { return mMaxWidth; } //-------------------------------------------------------------------------- public ExtGridCol setAlign(Align inValue) { mAlign = inValue; return this; } //-------------------------------------------------------------------------- public Align getAlign() { return mAlign; } //-------------------------------------------------------------------------- /** Specifies a method name or method definition for composing the display ov column fields. @param inValue either a javascript method name or a definition of a javascript method @return this ExtGridCol object - for method chaining */ public ExtGridCol setRenderer(String inValue) { mRenderer = inValue; return this; } //-------------------------------------------------------------------------- public String getRenderer() { return mRenderer; } //-------------------------------------------------------------------------- public ExtGridCol setEditor(String inValue) { mEditor = inValue; return this; } //-------------------------------------------------------------------------- public String getEditor() { return mEditor; } //-------------------------------------------------------------------------- public ExtGridCol setSortable(Boolean inValue) { mSortable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getSortable() { return mSortable; } //-------------------------------------------------------------------------- /** sortType isn't a true column property but is included here so that the information can be transferred to the corresponding model field if desired. * @param inValue name of the javascript SortType method. * @return this grid column object to facilitate method chaining */ public ExtGridCol setSortType(String inValue) { mSortType = inValue; return this; } //-------------------------------------------------------------------------- public String getSortType() { return mSortType; } //-------------------------------------------------------------------------- public ExtGridCol setHideable(Boolean inValue) { mHideable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getHideable() { return mHideable; } //-------------------------------------------------------------------------- public ExtGridCol setHidden(Boolean inValue) { mHidden = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getHidden() { return mHidden; } //-------------------------------------------------------------------------- public ExtGridCol setLockable(Boolean inValue) { mLockable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getLockable() { return mLockable; } //-------------------------------------------------------------------------- public ExtGridCol setLocked(Boolean inValue) { mLocked = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getLocked() { return mLocked; } //-------------------------------------------------------------------------- /** Specifies whether the column header can be reordered by dragging. @param inValue whether or not the column can be reordered @return this ExtGridCol object - for method chaining */ public ExtGridCol setDraggable(Boolean inValue) { mDraggable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getDraggable() { return mDraggable; } //-------------------------------------------------------------------------- /** Specifies whether the grid can be grouped by the column dataIndex. @param inValue whether or not the column can be grouped @return this ExtGridCol object - for method chaining */ public ExtGridCol setGroupable(Boolean inValue) { mGroupable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getGroupable() { return mGroupable; } //-------------------------------------------------------------------------- public ExtGridCol setResizable(Boolean inValue) { mResizable = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getResizable() { return mResizable; } //-------------------------------------------------------------------------- public ExtGridCol setVariableRowHeight(Boolean inValue) { mVariableRowHeight = inValue; return this; } //-------------------------------------------------------------------------- public Boolean getVariableRowHeight() { return mVariableRowHeight; } //-------------------------------------------------------------------------- public ExtGridCol setItemId(String inValue) { mItemId = inValue; return this; } //-------------------------------------------------------------------------- public String getItemId() { return mItemId; } //-------------------------------------------------------------------------- public ExtGridCol setFilter(String inValue) { mFilter = inValue; return this; } //-------------------------------------------------------------------------- public String getFilter() { return mFilter; } //-------------------------------------------------------------------------- public ExtGridCol setFlex(Integer inValue) { mFlex = inValue; return this; } //-------------------------------------------------------------------------- public Integer getFlex() { return mFlex; } //-------------------------------------------------------------------------- public ExtGridCol setXType(String inValue) { mXType = inValue; return this; } //-------------------------------------------------------------------------- public String getXType() { return mXType; } //-------------------------------------------------------------------------- /** Specifies the field type to use for the column's model. * @param inValue the field type * @return this ExtGridCol object to enable method chaining */ public ExtGridCol setFieldType(ExtFieldType inValue) { mFieldType = inValue; return this; } //-------------------------------------------------------------------------- public ExtFieldType getFieldType() { return mFieldType; } //-------------------------------------------------------------------------- public AttributeMgr getAttributeMgr() { if (null == mAttributeMgr) { mAttributeMgr = new AttributeMgr(); } return mAttributeMgr; } //-------------------------------------------------------------------------- public int getHeaderRowDepth() { return getHeaderRowDepth(1); } //-------------------------------------------------------------------------- public int getTerminalColumnCount() { int colCount = 0; if (CollectionUtil.hasValues(getGroupColumns())) { for (ExtGridCol col : getGroupColumns()) { colCount += col.getTerminalColumnCount(); } } else { colCount++; } return colCount; } //-------------------------------------------------------------------------- private int getHeaderRowDepth(int inInitialDepth) { int maxDepth = inInitialDepth; if (CollectionUtil.hasValues(getGroupColumns())) { for (ExtGridCol col : getGroupColumns()) { int depth = col.getHeaderRowDepth(inInitialDepth + 1); if (depth > maxDepth) { maxDepth = depth; } } } return maxDepth; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy