/** * Checks the value of a String to determine the corresponding enum. * * @param direction the string to check * @return the corresponding enum (default is OrderByDirection.asc) */ public static SortDirection checkValue(String direction) { try { return SortDirection.valueOf(Val.chkStr(direction)); } catch (IllegalArgumentException ex) { return SortDirection.defaultValue(); } }
public List<UserModel> allUsersForDisplay(SortableColumn column, SortDirection direction) { List<UserModel> userModels = allUsersForDisplay(); Comparator<UserModel> userModelComparator = direction.forColumn(column); Collections.sort(userModels, userModelComparator); return userModels; }
/** * Sets the column key for the sort with toggling options. * * @param key the column key * @param toggleDirection if true toggle the direction if the new column key matches the current * column key * @param defaultToggleDirection the direction to use when toggling if the new column key does not * match the current column key */ public void setColumnKey(String key, boolean toggleDirection, String defaultToggleDirection) { key = Val.chkStr(key); if (!toggleDirection) { _columnKey = key; } else { if (key.equalsIgnoreCase(_columnKey)) { if (getDirection().equals(SortDirection.asc)) { setDirection(SortDirection.desc); } else { setDirection(SortDirection.asc); } } else { setDirection(SortDirection.checkValue(defaultToggleDirection)); } _columnKey = key; } }
protected String buildSortOrderClause(SortDirection direction, String prefix, String field) { return " ORDER BY " + prefix + "." + field + " " + direction.name(); }
/** Defines the option for sorting results. */ public class SortOption implements Serializable { // class variables ============================================================= // instance variables ========================================================== private String _columnKey = ""; private SortDirection _direction = SortDirection.defaultValue(); private SortOptionStyleMap _styleMap; // constructors ================================================================ /** Default constructor. */ public SortOption() { _styleMap = new SortOptionStyleMap(this); } // properties ================================================================== /** * Gets the column key for the sort. * * @return the column key */ public String getColumnKey() { return _columnKey; } /** * Sets the column key for the sort. * * @param key the column key */ public void setColumnKey(String key) { _columnKey = Val.chkStr(key); } /** * Sets the column key for the sort with toggling options. * * @param key the column key * @param toggleDirection if true toggle the direction if the new column key matches the current * column key * @param defaultToggleDirection the direction to use when toggling if the new column key does not * match the current column key */ public void setColumnKey(String key, boolean toggleDirection, String defaultToggleDirection) { key = Val.chkStr(key); if (!toggleDirection) { _columnKey = key; } else { if (key.equalsIgnoreCase(_columnKey)) { if (getDirection().equals(SortDirection.asc)) { setDirection(SortDirection.desc); } else { setDirection(SortDirection.asc); } } else { setDirection(SortDirection.checkValue(defaultToggleDirection)); } _columnKey = key; } } /** * Gets the sort direction. * * @return the sort direction. */ public SortDirection getDirection() { return _direction; } /** * Sets the sort direction. * * @param direction the sort direction */ public void setDirection(SortDirection direction) { _direction = direction; } /** * Sets the sort direction. * * @param direction the sort direction */ public void setDirection(String direction) { _direction = SortDirection.checkValue(direction); } /** * Gets the style map. * * @return the style map */ public SortOptionStyleMap getStyleMap() { return _styleMap; } // methods ===================================================================== /** * Gets the style class for a column key. <br> * "ascending" is returned if the sort direction is ascending and the supplied column key matches * the active column key <br> * "descending" is returned if the sort direction is descending and the supplied column key * matches the active column key <br> * "" is returned if the the supplied column key does not match the active column key * * @param columnKey the subject column key * @return the style class for the column key */ public String getStyleClass(String columnKey) { if ((getColumnKey().length() > 0) && getColumnKey().equalsIgnoreCase(columnKey)) { if (getDirection().equals(SortDirection.asc)) { return "ascending"; } else { return "descending"; } } return ""; } // enums ======================================================================= /** An enumeration describing a the sort direction. */ public enum SortDirection { /** Ascending order (default value). */ asc, /** Descending order. */ desc; /** * Checks the value of a String to determine the corresponding enum. * * @param direction the string to check * @return the corresponding enum (default is OrderByDirection.asc) */ public static SortDirection checkValue(String direction) { try { return SortDirection.valueOf(Val.chkStr(direction)); } catch (IllegalArgumentException ex) { return SortDirection.defaultValue(); } } /** * Returns the default value for the enum. * * @return OrderByDirection.asc */ public static SortDirection defaultValue() { return SortDirection.asc; } } }
/** * Sets the sort direction. * * @param direction the sort direction */ public void setDirection(String direction) { _direction = SortDirection.checkValue(direction); }