From 3bda753a4aff633e64dffa0c118a91f5cf86c1af Mon Sep 17 00:00:00 2001 From: Jens Jansson Date: Fri, 20 Jan 2012 14:47:56 +0200 Subject: [PATCH] #8019 Added ItemCaptionMode enum for AbstractSelect and RowHeaderMode enum for Table --- src/com/vaadin/ui/AbstractSelect.java | 112 ++++++++++----- src/com/vaadin/ui/Table.java | 135 ++++++++++++------ .../vaadin/tests/components/table/Tables.java | 26 ++-- 3 files changed, 185 insertions(+), 88 deletions(-) diff --git a/src/com/vaadin/ui/AbstractSelect.java b/src/com/vaadin/ui/AbstractSelect.java index b65cb61450..5e086f0b8d 100644 --- a/src/com/vaadin/ui/AbstractSelect.java +++ b/src/com/vaadin/ui/AbstractSelect.java @@ -61,41 +61,85 @@ public abstract class AbstractSelect extends AbstractField implements Container.PropertySetChangeNotifier, Container.ItemSetChangeNotifier, Container.ItemSetChangeListener { + public enum ItemCaptionMode { + /** + * Item caption mode: Item's ID's String representation is + * used as caption. + */ + ID, + /** + * Item caption mode: Item's String representation is used + * as caption. + */ + ITEM, + /** + * Item caption mode: Index of the item is used as caption. The index + * mode can only be used with the containers implementing the + * {@link com.vaadin.data.Container.Indexed} interface. + */ + INDEX, + /** + * Item caption mode: If an Item has a caption it's used, if not, Item's + * ID's String representation is used as caption. This + * is the default. + */ + EXPLICIT_DEFAULTS_ID, + /** + * Item caption mode: Captions must be explicitly specified. + */ + EXPLICIT, + /** + * Item caption mode: Only icons are shown, captions are hidden. + */ + ICON_ONLY, + /** + * Item caption mode: Item captions are read from property specified + * with setItemCaptionPropertyId. + */ + PROPERTY; + } + /** - * Item caption mode: Item's ID's String representation is used - * as caption. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_ID = 0; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_ID = ItemCaptionMode.ID; + /** - * Item caption mode: Item's String representation is used as - * caption. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_ITEM = 1; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_ITEM = ItemCaptionMode.ITEM; + /** - * Item caption mode: Index of the item is used as caption. The index mode - * can only be used with the containers implementing the - * {@link com.vaadin.data.Container.Indexed} interface. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_INDEX = 2; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_INDEX = ItemCaptionMode.INDEX; + /** - * Item caption mode: If an Item has a caption it's used, if not, Item's - * ID's String representation is used as caption. This is - * the default. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID = 3; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID = ItemCaptionMode.EXPLICIT_DEFAULTS_ID; + /** - * Item caption mode: Captions must be explicitly specified. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_EXPLICIT = 4; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_EXPLICIT = ItemCaptionMode.EXPLICIT; + /** - * Item caption mode: Only icons are shown, captions are hidden. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_ICON_ONLY = 5; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_ICON_ONLY = ItemCaptionMode.ICON_ONLY; + /** - * Item caption mode: Item captions are read from property specified with - * setItemCaptionPropertyId. + * @deprecated from 7.0, use {@link ItemCaptionMode.ID} instead */ - public static final int ITEM_CAPTION_MODE_PROPERTY = 6; + @Deprecated + public static final ItemCaptionMode ITEM_CAPTION_MODE_PROPERTY = ItemCaptionMode.PROPERTY; /** * Interface for option filtering, used to filter options based on user @@ -175,7 +219,7 @@ public abstract class AbstractSelect extends AbstractField implements /** * Item caption mode. */ - private int itemCaptionMode = ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID; + private ItemCaptionMode itemCaptionMode = ItemCaptionMode.EXPLICIT_DEFAULTS_ID; /** * Item caption source property id. @@ -1038,11 +1082,11 @@ public abstract class AbstractSelect extends AbstractField implements switch (getItemCaptionMode()) { - case ITEM_CAPTION_MODE_ID: + case ID: caption = itemId.toString(); break; - case ITEM_CAPTION_MODE_INDEX: + case INDEX: if (items instanceof Container.Indexed) { caption = String.valueOf(((Container.Indexed) items) .indexOfId(itemId)); @@ -1051,25 +1095,25 @@ public abstract class AbstractSelect extends AbstractField implements } break; - case ITEM_CAPTION_MODE_ITEM: + case ITEM: final Item i = getItem(itemId); if (i != null) { caption = i.toString(); } break; - case ITEM_CAPTION_MODE_EXPLICIT: + case EXPLICIT: caption = itemCaptions.get(itemId); break; - case ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID: + case EXPLICIT_DEFAULTS_ID: caption = itemCaptions.get(itemId); if (caption == null) { caption = itemId.toString(); } break; - case ITEM_CAPTION_MODE_PROPERTY: + case PROPERTY: final Property p = getContainerProperty(itemId, getItemCaptionPropertyId()); if (p != null) { @@ -1086,7 +1130,7 @@ public abstract class AbstractSelect extends AbstractField implements } /** - * Sets the icon for an item. + * Sets tqhe icon for an item. * * @param itemId * the id of the item to be assigned an icon. @@ -1163,8 +1207,8 @@ public abstract class AbstractSelect extends AbstractField implements * @param mode * the One of the modes listed above. */ - public void setItemCaptionMode(int mode) { - if (ITEM_CAPTION_MODE_ID <= mode && mode <= ITEM_CAPTION_MODE_PROPERTY) { + public void setItemCaptionMode(ItemCaptionMode mode) { + if (mode != null) { itemCaptionMode = mode; requestRepaint(); } @@ -1198,7 +1242,7 @@ public abstract class AbstractSelect extends AbstractField implements * * @return the One of the modes listed above. */ - public int getItemCaptionMode() { + public ItemCaptionMode getItemCaptionMode() { return itemCaptionMode; } @@ -1689,7 +1733,7 @@ public abstract class AbstractSelect extends AbstractField implements public void addNotifierForItem(Object itemId) { switch (getItemCaptionMode()) { - case ITEM_CAPTION_MODE_ITEM: + case ITEM: final Item i = getItem(itemId); if (i == null) { return; @@ -1713,7 +1757,7 @@ public abstract class AbstractSelect extends AbstractField implements } break; - case ITEM_CAPTION_MODE_PROPERTY: + case PROPERTY: final Property p = getContainerProperty(itemId, getItemCaptionPropertyId()); if (p != null && p instanceof Property.ValueChangeNotifier) { diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index bfd1bb4c5d..cdcf79cd2b 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -225,51 +225,105 @@ public class Table extends AbstractSelect implements Action.Container, @Deprecated public static final ColumnHeaderMode COLUMN_HEADER_MODE_EXPLICIT_DEFAULTS_ID = ColumnHeaderMode.EXPLICIT_DEFAULTS_ID; + public enum RowHeaderMode { + /** + * Row caption mode: The row headers are hidden. This is the default + * mode. + */ + HIDDEN(null), + /** + * Row caption mode: Items Id-objects toString is used as row caption. + */ + ID(ItemCaptionMode.ID), + /** + * Row caption mode: Item-objects toString is used as row caption. + */ + ITEM(ItemCaptionMode.ITEM), + /** + * Row caption mode: Index of the item is used as item caption. The + * index mode can only be used with the containers implementing the + * {@link com.vaadin.data.Container.Indexed} interface. + */ + INDEX(ItemCaptionMode.INDEX), + /** + * Row caption mode: Item captions are explicitly specified, but if the + * caption is missing, the item id objects toString() is + * used instead. + */ + EXPLICIT_DEFAULTS_ID(ItemCaptionMode.EXPLICIT_DEFAULTS_ID), + /** + * Row caption mode: Item captions are explicitly specified. + */ + EXPLICIT(ItemCaptionMode.EXPLICIT), + /** + * Row caption mode: Only icons are shown, the captions are hidden. + */ + ICON_ONLY(ItemCaptionMode.ICON_ONLY), + /** + * Row caption mode: Item captions are read from property specified with + * {@link #setItemCaptionPropertyId(Object)}. + */ + PROPERTY(ItemCaptionMode.PROPERTY); + + ItemCaptionMode mode; + + private RowHeaderMode(ItemCaptionMode mode) { + this.mode = mode; + } + + public ItemCaptionMode getItemCaptionMode() { + return mode; + } + } + /** - * Row caption mode: The row headers are hidden. This is the default - * mode. + * @deprecated from 7.0, use {@link RowHeaderMode#HIDDEN} instead */ - public static final int ROW_HEADER_MODE_HIDDEN = -1; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_HIDDEN = RowHeaderMode.HIDDEN; /** - * Row caption mode: Items Id-objects toString is used as row caption. + * @deprecated from 7.0, use {@link RowHeaderMode#ID} instead */ - public static final int ROW_HEADER_MODE_ID = AbstractSelect.ITEM_CAPTION_MODE_ID; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_ID = RowHeaderMode.ID; /** - * Row caption mode: Item-objects toString is used as row caption. + * @deprecated from 7.0, use {@link RowHeaderMode#ITEM} instead */ - public static final int ROW_HEADER_MODE_ITEM = AbstractSelect.ITEM_CAPTION_MODE_ITEM; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_ITEM = RowHeaderMode.ITEM; /** - * Row caption mode: Index of the item is used as item caption. The index - * mode can only be used with the containers implementing Container.Indexed - * interface. + * @deprecated from 7.0, use {@link RowHeaderMode#INDEX} instead */ - public static final int ROW_HEADER_MODE_INDEX = AbstractSelect.ITEM_CAPTION_MODE_INDEX; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_INDEX = RowHeaderMode.INDEX; /** - * Row caption mode: Item captions are explicitly specified. + * @deprecated from 7.0, use {@link RowHeaderMode#EXPLICIT_DEFAULTS_ID} + * instead */ - public static final int ROW_HEADER_MODE_EXPLICIT = AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID = RowHeaderMode.EXPLICIT_DEFAULTS_ID; /** - * Row caption mode: Item captions are read from property specified with - * {@link #setItemCaptionPropertyId(Object)}. + * @deprecated from 7.0, use {@link RowHeaderMode#EXPLICIT} instead */ - public static final int ROW_HEADER_MODE_PROPERTY = AbstractSelect.ITEM_CAPTION_MODE_PROPERTY; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_EXPLICIT = RowHeaderMode.EXPLICIT; /** - * Row caption mode: Only icons are shown, the captions are hidden. + * @deprecated from 7.0, use {@link RowHeaderMode#ICON_ONLY} instead */ - public static final int ROW_HEADER_MODE_ICON_ONLY = AbstractSelect.ITEM_CAPTION_MODE_ICON_ONLY; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_ICON_ONLY = RowHeaderMode.ICON_ONLY; /** - * Row caption mode: Item captions are explicitly specified, but if the - * caption is missing, the item id objects toString() is used - * instead. + * @deprecated from 7.0, use {@link RowHeaderMode#PROPERTY} instead */ - public static final int ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID = AbstractSelect.ITEM_CAPTION_MODE_EXPLICIT_DEFAULTS_ID; + @Deprecated + public static final RowHeaderMode ROW_HEADER_MODE_PROPERTY = RowHeaderMode.PROPERTY; /** * The default rate that table caches rows for smooth scrolling. @@ -363,14 +417,14 @@ public class Table extends AbstractSelect implements Action.Container, private ColumnHeaderMode columnHeaderMode = ColumnHeaderMode.EXPLICIT_DEFAULTS_ID; /** - * Should the Table footer be visible? + * Holds value of property rowHeaderMode. */ - private boolean columnFootersVisible = false; + private RowHeaderMode rowHeaderMode = RowHeaderMode.EXPLICIT_DEFAULTS_ID; /** - * True iff the row captions are hidden. + * Should the Table footer be visible? */ - private boolean rowCaptionsAreHidden = true; + private boolean columnFootersVisible = false; /** * Page contents buffer used in buffered mode. @@ -1803,7 +1857,7 @@ public class Table extends AbstractSelect implements Action.Container, } } - final int headmode = getRowHeaderMode(); + final RowHeaderMode headmode = getRowHeaderMode(); final boolean[] iscomponent = new boolean[cols]; for (int i = 0; i < cols; i++) { iscomponent[i] = columnGenerators.containsKey(colids[i]) @@ -1824,7 +1878,7 @@ public class Table extends AbstractSelect implements Action.Container, cells[CELL_KEY][i] = itemIdMapper.key(id); if (headmode != ROW_HEADER_MODE_HIDDEN) { switch (headmode) { - case ROW_HEADER_MODE_INDEX: + case INDEX: cells[CELL_HEADER][i] = String.valueOf(i + firstIndex + 1); break; default: @@ -2123,17 +2177,17 @@ public class Table extends AbstractSelect implements Action.Container, * @param mode * the One of the modes listed above. */ - public void setRowHeaderMode(int mode) { - if (ROW_HEADER_MODE_HIDDEN == mode) { - rowCaptionsAreHidden = true; - } else { - rowCaptionsAreHidden = false; - setItemCaptionMode(mode); + public void setRowHeaderMode(RowHeaderMode mode) { + if (mode != null) { + rowHeaderMode = mode; + if (mode != RowHeaderMode.HIDDEN) { + setItemCaptionMode(mode.getItemCaptionMode()); + } + // Assures the visual refresh. No need to reset the page buffer + // before + // as the content has not changed, only the alignments. + refreshRenderedCells(); } - - // Assures the visual refresh. No need to reset the page buffer before - // as the content has not changed, only the alignments. - refreshRenderedCells(); } /** @@ -2142,9 +2196,8 @@ public class Table extends AbstractSelect implements Action.Container, * @return the Row header mode. * @see #setRowHeaderMode(int) */ - public int getRowHeaderMode() { - return rowCaptionsAreHidden ? ROW_HEADER_MODE_HIDDEN - : getItemCaptionMode(); + public RowHeaderMode getRowHeaderMode() { + return rowHeaderMode; } /** diff --git a/tests/testbench/com/vaadin/tests/components/table/Tables.java b/tests/testbench/com/vaadin/tests/components/table/Tables.java index 7fae10b24e..3b13c9136b 100644 --- a/tests/testbench/com/vaadin/tests/components/table/Tables.java +++ b/tests/testbench/com/vaadin/tests/components/table/Tables.java @@ -27,6 +27,7 @@ import com.vaadin.ui.Table.GeneratedRow; import com.vaadin.ui.Table.HeaderClickEvent; import com.vaadin.ui.Table.HeaderClickListener; import com.vaadin.ui.Table.RowGenerator; +import com.vaadin.ui.Table.RowHeaderMode; public class Tables extends AbstractSelectTestCase implements ItemClickListener, HeaderClickListener, FooterClickListener, @@ -110,10 +111,10 @@ public class Tables extends AbstractSelectTestCase } }; - protected Command rowHeaderModeCommand = new Command() { + protected Command rowHeaderModeCommand = new Command() { - public void execute(Table c, Integer value, Object data) { - if (value == Table.ROW_HEADER_MODE_PROPERTY) { + public void execute(Table c, RowHeaderMode value, Object data) { + if (value == RowHeaderMode.PROPERTY) { c.setItemCaptionPropertyId("Property 3"); } c.setRowHeaderMode(value); @@ -653,16 +654,15 @@ public class Tables extends AbstractSelectTestCase } private void createRowHeaderModeSelect(String category) { - LinkedHashMap options = new LinkedHashMap(); - options.put("Explicit", Table.ROW_HEADER_MODE_EXPLICIT); - options.put("Explicit defaults id", - Table.ROW_HEADER_MODE_EXPLICIT_DEFAULTS_ID); - options.put("Hidden", Table.ROW_HEADER_MODE_HIDDEN); - options.put("Icon only", Table.ROW_HEADER_MODE_ICON_ONLY); - options.put("Id", Table.ROW_HEADER_MODE_ID); - options.put("Index", Table.ROW_HEADER_MODE_INDEX); - options.put("Item", Table.ROW_HEADER_MODE_ITEM); - options.put("'Property 3' property", Table.ROW_HEADER_MODE_PROPERTY); + LinkedHashMap options = new LinkedHashMap(); + options.put("Explicit", RowHeaderMode.EXPLICIT); + options.put("Explicit defaults id", RowHeaderMode.EXPLICIT_DEFAULTS_ID); + options.put("Hidden", RowHeaderMode.HIDDEN); + options.put("Icon only", RowHeaderMode.ICON_ONLY); + options.put("Id", RowHeaderMode.ID); + options.put("Index", RowHeaderMode.INDEX); + options.put("Item", RowHeaderMode.ITEM); + options.put("'Property 3' property", RowHeaderMode.PROPERTY); createSelectAction("Row header mode", category, options, "Hidden", rowHeaderModeCommand); -- 2.39.5