From: Jens Jansson Date: Fri, 20 Jan 2012 09:05:43 +0000 (+0200) Subject: #8019 Table's column alignment constants to enum. X-Git-Tag: 7.0.0.alpha2~539 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=499ca3bbe89afdd4150bb58091a7b858ff79c5fd;p=vaadin-framework.git #8019 Table's column alignment constants to enum. --- diff --git a/src/com/vaadin/ui/Table.java b/src/com/vaadin/ui/Table.java index ccc68e06b0..bfd1bb4c5d 100644 --- a/src/com/vaadin/ui/Table.java +++ b/src/com/vaadin/ui/Table.java @@ -115,20 +115,66 @@ public class Table extends AbstractSelect implements Action.Container, protected static final int CELL_FIRSTCOL = 5; + public enum Align { + /** + * Left column alignment. This is the default behaviour. + */ + LEFT("b"), + + /** + * Center column alignment. + */ + CENTER("c"), + + /** + * Right column alignment. + */ + RIGHT("e"); + + private String alignment; + + private Align(String alignment) { + this.alignment = alignment; + } + + @Override + public String toString() { + return alignment; + } + + public Align convertStringToAlign(String string) { + if (string == null) { + return null; + } + if (string.equals("b")) { + return Align.LEFT; + } else if (string.equals("c")) { + return Align.CENTER; + } else if (string.equals("e")) { + return Align.RIGHT; + } else { + return null; + } + } + } + /** - * Left column alignment. This is the default behaviour. + * @deprecated from 7.0, use {@link Align#LEFT} instead */ - public static final String ALIGN_LEFT = "b"; + @Deprecated + public static final Align ALIGN_LEFT = Align.LEFT; /** - * Center column alignment. + * @deprecated from 7.0, use {@link Align#CENTER} instead */ - public static final String ALIGN_CENTER = "c"; + @Deprecated + public static final Align ALIGN_CENTER = Align.CENTER; /** - * Right column alignment. + * @deprecated from 7.0, use {@link Align#RIGHT} instead */ - public static final String ALIGN_RIGHT = "e"; + @Deprecated + public static final Align ALIGN_RIGHT = Align.RIGHT; public enum ColumnHeaderMode { /** @@ -278,7 +324,7 @@ public class Table extends AbstractSelect implements Action.Container, /** * Holds alignments for visible columns (by propertyId). */ - private HashMap columnAlignments = new HashMap(); + private HashMap columnAlignments = new HashMap(); /** * Holds column widths in pixels (Integer) or expand ratios (Float) for @@ -536,7 +582,7 @@ public class Table extends AbstractSelect implements Action.Container, final Object col = i.next(); if (!newVC.contains(col)) { setColumnHeader(col, null); - setColumnAlignment(col, null); + setColumnAlignment(col, (Align) null); setColumnIcon(col, null); } } @@ -680,21 +726,21 @@ public class Table extends AbstractSelect implements Action.Container, * {@link #getVisibleColumns()}. The possible values for the alignments * include: *
    - *
  • {@link #ALIGN_LEFT}: Left alignment
  • - *
  • {@link #ALIGN_CENTER}: Centered
  • - *
  • {@link #ALIGN_RIGHT}: Right alignment
  • + *
  • {@link Align#LEFT}: Left alignment
  • + *
  • {@link Align#CENTER}: Centered
  • + *
  • {@link Align#RIGHT}: Right alignment
  • *
- * The alignments default to {@link #ALIGN_LEFT}: any null values are + * The alignments default to {@link Align#LEFT}: any null values are * rendered as align lefts. *

* * @return the Column alignments array. */ - public String[] getColumnAlignments() { + public Align[] getColumnAlignments() { if (columnAlignments == null) { return null; } - final String[] alignments = new String[visibleColumns.size()]; + final Align[] alignments = new Align[visibleColumns.size()]; int i = 0; for (final Iterator it = visibleColumns.iterator(); it .hasNext(); i++) { @@ -708,39 +754,29 @@ public class Table extends AbstractSelect implements Action.Container, * Sets the column alignments. * *

- * The items in the array must match the properties identified by - * {@link #getVisibleColumns()}. The possible values for the alignments - * include: + * The amount of items in the array must match the amount of properties + * identified by {@link #getVisibleColumns()}. The possible values for the + * alignments include: *

    - *
  • {@link #ALIGN_LEFT}: Left alignment
  • - *
  • {@link #ALIGN_CENTER}: Centered
  • - *
  • {@link #ALIGN_RIGHT}: Right alignment
  • + *
  • {@link Align#LEFT}: Left alignment
  • + *
  • {@link Align#CENTER}: Centered
  • + *
  • {@link Align#RIGHT}: Right alignment
  • *
- * The alignments default to {@link #ALIGN_LEFT} + * The alignments default to {@link Align#LEFT} *

* * @param columnAlignments * the Column alignments array. */ - public void setColumnAlignments(String[] columnAlignments) { + public void setColumnAlignments(Align... columnAlignments) { if (columnAlignments.length != visibleColumns.size()) { throw new IllegalArgumentException( "The length of the alignments array must match the number of visible columns"); } - // Checks all alignments - for (int i = 0; i < columnAlignments.length; i++) { - final String a = columnAlignments[i]; - if (a != null && !a.equals(ALIGN_LEFT) && !a.equals(ALIGN_CENTER) - && !a.equals(ALIGN_RIGHT)) { - throw new IllegalArgumentException("Column " + i - + " aligment '" + a + "' is invalid"); - } - } - // Resets the alignments - final HashMap newCA = new HashMap(); + final HashMap newCA = new HashMap(); int i = 0; for (final Iterator it = visibleColumns.iterator(); it .hasNext() && i < columnAlignments.length; i++) { @@ -1097,9 +1133,9 @@ public class Table extends AbstractSelect implements Action.Container, * the propertyID identifying the column. * @return the specified column's alignment if it as one; null otherwise. */ - public String getColumnAlignment(Object propertyId) { - final String a = columnAlignments.get(propertyId); - return a == null ? ALIGN_LEFT : a; + public Align getColumnAlignment(Object propertyId) { + final Align a = columnAlignments.get(propertyId); + return a == null ? Align.LEFT : a; } /** @@ -1107,8 +1143,8 @@ public class Table extends AbstractSelect implements Action.Container, * *

* Throws IllegalArgumentException if the alignment is not one of the - * following: {@link #ALIGN_LEFT}, {@link #ALIGN_CENTER} or - * {@link #ALIGN_RIGHT} + * following: {@link Align#LEFT}, {@link Align#CENTER} or + * {@link Align#RIGHT} *

* * @param propertyId @@ -1116,17 +1152,8 @@ public class Table extends AbstractSelect implements Action.Container, * @param alignment * the desired alignment. */ - public void setColumnAlignment(Object propertyId, String alignment) { - - // Checks for valid alignments - if (alignment != null && !alignment.equals(ALIGN_LEFT) - && !alignment.equals(ALIGN_CENTER) - && !alignment.equals(ALIGN_RIGHT)) { - throw new IllegalArgumentException("Column alignment '" + alignment - + "' is not supported."); - } - - if (alignment == null || alignment.equals(ALIGN_LEFT)) { + public void setColumnAlignment(Object propertyId, Align alignment) { + if (alignment == null || alignment == Align.LEFT) { columnAlignments.remove(propertyId); } else { columnAlignments.put(propertyId, alignment); @@ -1432,8 +1459,9 @@ public class Table extends AbstractSelect implements Action.Container, * the New value of property columnHeaderMode. */ public void setColumnHeaderMode(ColumnHeaderMode columnHeaderMode) { - if(columnHeaderMode == null){ - throw new IllegalArgumentException("Column header mode can not be null"); + if (columnHeaderMode == null) { + throw new IllegalArgumentException( + "Column header mode can not be null"); } if (columnHeaderMode != this.columnHeaderMode) { this.columnHeaderMode = columnHeaderMode; @@ -2984,8 +3012,9 @@ public class Table extends AbstractSelect implements Action.Container, target.addAttribute("sortable", true); } } - if (!ALIGN_LEFT.equals(getColumnAlignment(colId))) { - target.addAttribute("align", getColumnAlignment(colId)); + if (!Align.LEFT.equals(getColumnAlignment(colId))) { + target.addAttribute("align", getColumnAlignment(colId) + .toString()); } paintColumnWidth(target, colId); target.endTag("column"); @@ -3746,7 +3775,7 @@ public class Table extends AbstractSelect implements Action.Container, */ public boolean addContainerProperty(Object propertyId, Class type, Object defaultValue, String columnHeader, Resource columnIcon, - String columnAlignment) throws UnsupportedOperationException { + Align columnAlignment) throws UnsupportedOperationException { if (!this.addContainerProperty(propertyId, type, defaultValue)) { return false; } diff --git a/tests/server-side/com/vaadin/tests/server/component/table/TableColumnAlignments.java b/tests/server-side/com/vaadin/tests/server/component/table/TableColumnAlignments.java index 04f436f5c4..299f9c79d4 100644 --- a/tests/server-side/com/vaadin/tests/server/component/table/TableColumnAlignments.java +++ b/tests/server-side/com/vaadin/tests/server/component/table/TableColumnAlignments.java @@ -5,6 +5,7 @@ import static org.junit.Assert.assertArrayEquals; import org.junit.Test; import com.vaadin.ui.Table; +import com.vaadin.ui.Table.Align; public class TableColumnAlignments { @@ -15,7 +16,7 @@ public class TableColumnAlignments { properties, 10); Object[] expected = new Object[properties]; for (int i = 0; i < properties; i++) { - expected[i] = Table.ALIGN_LEFT; + expected[i] = Align.LEFT; } org.junit.Assert.assertArrayEquals("getColumnAlignments", expected, t.getColumnAlignments()); @@ -27,9 +28,8 @@ public class TableColumnAlignments { int properties = 5; Table t = TableGenerator .createTableWithDefaultContainer(properties, 10); - String[] explicitAlignments = new String[] { Table.ALIGN_CENTER, - Table.ALIGN_LEFT, Table.ALIGN_RIGHT, Table.ALIGN_RIGHT, - Table.ALIGN_LEFT }; + Align[] explicitAlignments = new Align[] { Align.CENTER, Align.LEFT, + Align.RIGHT, Align.RIGHT, Align.LEFT }; t.setColumnAlignments(explicitAlignments); @@ -40,28 +40,10 @@ public class TableColumnAlignments { @Test public void invalidColumnAlignmentStrings() { Table t = TableGenerator.createTableWithDefaultContainer(3, 7); - String[] defaultAlignments = new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT }; + Align[] defaultAlignments = new Align[] { Align.LEFT, Align.LEFT, + Align.LEFT }; try { - t.setColumnAlignments(new String[] { "a", "b", "c" }); - junit.framework.Assert - .fail("No exception thrown for invalid array length"); - } catch (IllegalArgumentException e) { - // Ok, expected - } - - assertArrayEquals("Invalid change affected alignments", - defaultAlignments, t.getColumnAlignments()); - - } - - @Test - public void invalidColumnAlignmentString() { - Table t = TableGenerator.createTableWithDefaultContainer(3, 7); - String[] defaultAlignments = new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT }; - try { - t.setColumnAlignment("Property 1", "a"); + t.setColumnAlignments(new Align[] { Align.RIGHT, Align.RIGHT }); junit.framework.Assert .fail("No exception thrown for invalid array length"); } catch (IllegalArgumentException e) { @@ -76,10 +58,10 @@ public class TableColumnAlignments { @Test public void columnAlignmentForPropertyNotInContainer() { Table t = TableGenerator.createTableWithDefaultContainer(3, 7); - String[] defaultAlignments = new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT }; + Align[] defaultAlignments = new Align[] { Align.LEFT, Align.LEFT, + Align.LEFT }; try { - t.setColumnAlignment("Property 1200", Table.ALIGN_LEFT); + t.setColumnAlignment("Property 1200", Align.LEFT); // FIXME: Uncomment as there should be an exception (#6475) // junit.framework.Assert // .fail("No exception thrown for property not in container"); @@ -100,12 +82,11 @@ public class TableColumnAlignments { @Test public void invalidColumnAlignmentsLength() { Table t = TableGenerator.createTableWithDefaultContainer(7, 7); - String[] defaultAlignments = new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT, Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT, Table.ALIGN_LEFT }; + Align[] defaultAlignments = new Align[] { Align.LEFT, Align.LEFT, + Align.LEFT, Align.LEFT, Align.LEFT, Align.LEFT, Align.LEFT }; try { - t.setColumnAlignments(new String[] { Table.ALIGN_LEFT }); + t.setColumnAlignments(new Align[] { Align.LEFT }); junit.framework.Assert .fail("No exception thrown for invalid array length"); } catch (IllegalArgumentException e) { @@ -115,7 +96,7 @@ public class TableColumnAlignments { defaultAlignments, t.getColumnAlignments()); try { - t.setColumnAlignments(new String[] {}); + t.setColumnAlignments(new Align[] {}); junit.framework.Assert .fail("No exception thrown for invalid array length"); } catch (IllegalArgumentException e) { @@ -125,10 +106,9 @@ public class TableColumnAlignments { defaultAlignments, t.getColumnAlignments()); try { - t.setColumnAlignments(new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT, Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT, Table.ALIGN_LEFT, - Table.ALIGN_LEFT }); + t.setColumnAlignments(new Align[] { Align.LEFT, Align.LEFT, + Align.LEFT, Align.LEFT, Align.LEFT, Align.LEFT, Align.LEFT, + Align.LEFT }); junit.framework.Assert .fail("No exception thrown for invalid array length"); } catch (IllegalArgumentException e) { @@ -144,13 +124,11 @@ public class TableColumnAlignments { int properties = 5; Table t = TableGenerator .createTableWithDefaultContainer(properties, 10); - String[] explicitAlignments = new String[] { Table.ALIGN_CENTER, - Table.ALIGN_LEFT, Table.ALIGN_RIGHT, Table.ALIGN_RIGHT, - Table.ALIGN_LEFT }; + Align[] explicitAlignments = new Align[] { Align.CENTER, Align.LEFT, + Align.RIGHT, Align.RIGHT, Align.LEFT }; - String[] currentAlignments = new String[] { Table.ALIGN_LEFT, - Table.ALIGN_LEFT, Table.ALIGN_LEFT, Table.ALIGN_LEFT, - Table.ALIGN_LEFT }; + Align[] currentAlignments = new Align[] { Align.LEFT, Align.LEFT, + Align.LEFT, Align.LEFT, Align.LEFT }; for (int i = 0; i < properties; i++) { t.setColumnAlignment("Property " + i, explicitAlignments[i]); diff --git a/tests/testbench/com/vaadin/tests/components/table/Tables.java b/tests/testbench/com/vaadin/tests/components/table/Tables.java index 2675479e36..7fae10b24e 100644 --- a/tests/testbench/com/vaadin/tests/components/table/Tables.java +++ b/tests/testbench/com/vaadin/tests/components/table/Tables.java @@ -15,6 +15,7 @@ import com.vaadin.ui.Button; import com.vaadin.ui.Label; import com.vaadin.ui.Label.ContentMode; import com.vaadin.ui.Table; +import com.vaadin.ui.Table.Align; import com.vaadin.ui.Table.CellStyleGenerator; import com.vaadin.ui.Table.ColumnGenerator; import com.vaadin.ui.Table.ColumnHeaderMode; @@ -43,9 +44,9 @@ public class Tables extends AbstractSelectTestCase } /* COMMANDS */ - private Command columnAlignmentCommand = new Command() { + private Command columnAlignmentCommand = new Command() { - public void execute(T c, String alignment, Object propertyId) { + public void execute(T c, Align alignment, Object propertyId) { c.setColumnAlignment(propertyId, alignment); } @@ -136,7 +137,7 @@ public class Tables extends AbstractSelectTestCase protected Command alignColumnLeftCommand = new Command() { public void execute(T c, Object propertyId, Object data) { - c.setColumnAlignment(propertyId, (String) data); + c.setColumnAlignment(propertyId, (Align) data); } }; @@ -603,10 +604,10 @@ public class Tables extends AbstractSelectTestCase createBooleanAction("Collapsed", category, false, columnCollapsed, propertyId); t.log("Collapsed"); - LinkedHashMap options = new LinkedHashMap(); - options.put("Left", Table.ALIGN_LEFT); - options.put("Center", Table.ALIGN_CENTER); - options.put("Right", Table.ALIGN_RIGHT); + LinkedHashMap options = new LinkedHashMap(); + options.put("Left", Align.LEFT); + options.put("Center", Align.CENTER); + options.put("Right", Align.RIGHT); createSelectAction("Alignment", category, options, "Left", columnAlignmentCommand, propertyId);