click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1026307 - in /click/trunk/click/framework: src/org/apache/click/control/AbstractLink.java src/org/apache/click/control/Table.java test/org/apache/click/control/AbstractLinkTest.java test/org/apache/click/control/TableTest.java
Date Fri, 22 Oct 2010 11:53:53 GMT
Author: sabob
Date: Fri Oct 22 11:53:53 2010
New Revision: 1026307

URL: http://svn.apache.org/viewvc?rev=1026307&view=rev
Log:
added stateful support for AbstractLink and Table, including tests. CLK-715

Modified:
    click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
    click/trunk/click/framework/src/org/apache/click/control/Table.java
    click/trunk/click/framework/test/org/apache/click/control/AbstractLinkTest.java
    click/trunk/click/framework/test/org/apache/click/control/TableTest.java

Modified: click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java?rev=1026307&r1=1026306&r2=1026307&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/AbstractLink.java Fri Oct 22
11:53:53 2010
@@ -25,6 +25,7 @@ import java.util.Set;
 
 import javax.servlet.http.HttpServletRequest;
 import org.apache.click.Context;
+import org.apache.click.Stateful;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
@@ -40,7 +41,7 @@ import org.apache.commons.lang.StringUti
  * @see ActionLink
  * @see Submit
  */
-public abstract class AbstractLink extends AbstractControl {
+public abstract class AbstractLink extends AbstractControl implements Stateful {
 
     private static final long serialVersionUID = 1L;
 
@@ -549,6 +550,35 @@ public abstract class AbstractLink exten
     }
 
     /**
+     * Return the link state. The following state is returned:
+     * <ul>
+     * <li>{@link #getParameters() link parameters}</li>
+     * </ul>
+     *
+     * @return the link state
+     */
+    public Object getState() {
+        if (hasParameters()) {
+            return getParameters();
+        }
+        return null;
+    }
+
+    /**
+     * Set the link state.
+     *
+     * @param state the link state to set
+     */
+    public void setState(Object state) {
+        if (state == null) {
+            return;
+        }
+
+        Map linkState = (Map) state;
+        setParameters(linkState);
+    }
+
+    /**
      * Render the HTML representation of the anchor link. This method
      * will render the entire anchor link including the tags, the label and
      * any attributes, see {@link #setAttribute(String, String)} for an
@@ -623,6 +653,48 @@ public abstract class AbstractLink exten
         }
     }
 
+    /**
+     * Remove the link state from the session for the given request context.
+     *
+     * @see #saveState(org.apache.click.Context)
+     * @see #restoreState(org.apache.click.Context)
+     *
+     * @param context the request context
+     */
+    public void removeState(Context context) {
+        ClickUtils.removeState(this, getName(), context);
+    }
+
+    /**
+     * Restore the link state from the session for the given request context.
+     * <p/>
+     * This method delegates to {@link #setState(java.lang.Object)} to set the
+     * link restored state.
+     *
+     * @see #saveState(org.apache.click.Context)
+     * @see #removeState(org.apache.click.Context)
+     *
+     * @param context the request context
+     */
+    public void restoreState(Context context) {
+        ClickUtils.restoreState(this, getName(), context);
+    }
+
+    /**
+     * Save the link state to the session for the given request context.
+     * <p/>
+     * * This method delegates to {@link #getState()} to retrieve the link state
+     * to save.
+     *
+     * @see #restoreState(org.apache.click.Context)
+     * @see #removeState(org.apache.click.Context)
+     *
+     * @param context the request context
+     */
+    public void saveState(Context context) {
+        ClickUtils.saveState(this, getName(), context);
+    }
+
     // Protected Methods ------------------------------------------------------
 
     /**

Modified: click/trunk/click/framework/src/org/apache/click/control/Table.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/src/org/apache/click/control/Table.java?rev=1026307&r1=1026306&r2=1026307&view=diff
==============================================================================
--- click/trunk/click/framework/src/org/apache/click/control/Table.java (original)
+++ click/trunk/click/framework/src/org/apache/click/control/Table.java Fri Oct 22 11:53:53
2010
@@ -29,6 +29,7 @@ import java.util.StringTokenizer;
 import org.apache.click.Context;
 
 import org.apache.click.Control;
+import org.apache.click.Stateful;
 import org.apache.click.element.CssImport;
 import org.apache.click.element.CssStyle;
 import org.apache.click.element.Element;
@@ -278,7 +279,7 @@ import org.apache.commons.lang.math.Numb
  * } </pre>
  *
  * For a live demonstration see the
- * <a href="http://www.avoka.com/click-examples/table/large-dataset-demo.htm">Large
Dataset Demo</a>.
+ * <a href="http://click.avoka.com/click-examples/table/large-dataset-demo.htm">Large
Dataset Demo</a>.
  * <p/>
  *
  * See the W3C HTML reference
@@ -291,7 +292,7 @@ import org.apache.commons.lang.math.Numb
  * @see Column
  * @see Decorator
  */
-public class Table extends AbstractControl {
+public class Table extends AbstractControl implements Stateful {
 
     // Constants --------------------------------------------------------------
 
@@ -1282,6 +1283,85 @@ public class Table extends AbstractContr
     }
 
     /**
+     * Return the Table state. The following state is returned:
+     * <ul>
+     * <li>{@link #getPageNumber()}</li>
+     * <li>{@link #isSortedAscending()}</li>
+     * <li>{@link #getSortedColumn()}</li>
+     * <li>{@link #getControlLink() controlLink parameters}</li>
+     * </ul>
+     *
+     * @return the Table state
+     */
+    public Object getState() {
+        Object[] tableState = new Object[4];
+        boolean hasState = false;
+
+        int currentPageNumber = getPageNumber();
+        if (currentPageNumber != 0) {
+            hasState = true;
+            tableState[0] = Integer.valueOf(currentPageNumber);
+        }
+
+        String currentSortedColumn = getSortedColumn();
+        if (currentSortedColumn != null) {
+            hasState = true;
+            tableState[1] = currentSortedColumn;
+        }
+
+        boolean currentSortedAscending = isSortedAscending();
+        if (!currentSortedAscending) {
+            hasState = true;
+            tableState[2] = Boolean.valueOf(currentSortedAscending);
+        }
+
+        Object controlLinkState = getControlLink().getState();
+        if (controlLinkState != null) {
+            hasState = true;
+            tableState[3] = controlLinkState;
+        }
+
+        if (hasState) {
+            return tableState;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Set the Table state.
+     *
+     * @param state the Table state to set
+     */
+    public void setState(Object state) {
+        if (state == null) {
+            return;
+        }
+
+        Object[] tableState = (Object[]) state;
+
+        if (tableState[0] != null) {
+            int storedPageNumber = ((Integer) tableState[0]).intValue();
+            setPageNumber(storedPageNumber);
+        }
+
+        if (tableState[1] != null) {
+            String storedSortedColumn = (String) tableState[1];
+            setSortedColumn(storedSortedColumn);
+        }
+
+        if (tableState[2] != null) {
+            boolean storedAscending = ((Boolean) tableState[2]).booleanValue();
+            setSortedAscending(storedAscending);
+        }
+
+        if (tableState[3] != null) {
+            Object controlLinkState = tableState[3];
+            getControlLink().setState(controlLinkState);
+        }
+    }
+
+    /**
      * Return the table HTML &lt;td&gt; width attribute.
      *
      * @return the table HTML &lt;td&gt; width attribute
@@ -1565,6 +1645,48 @@ public class Table extends AbstractContr
         }
     }
 
+    /**
+     * Remove the Table state from the session for the given request context.
+     *
+     * @param context the request context
+     *
+     * @see #saveState(org.apache.click.Context)
+     * @see #restoreState(org.apache.click.Context)
+     */
+    public void removeState(Context context) {
+        ClickUtils.removeState(this, getName(), context);
+    }
+
+    /**
+     * Restore the Table state from the session for the given request context.
+     * <p/>
+     * This method delegates to {@link #setState(java.lang.Object)} to set the
+     * table restored state.
+     *
+     * @param context the request context
+     *
+     * @see #saveState(org.apache.click.Context)
+     * @see #removeState(org.apache.click.Context)
+     */
+    public void restoreState(Context context) {
+        ClickUtils.restoreState(this, getName(), context);
+    }
+
+    /**
+     * Save the Table state to the session for the given request context.
+     * <p/>
+     * * This method delegates to {@link #getState()} to retrieve the table state
+     * to save.
+     *
+     * @see #restoreState(org.apache.click.Context)
+     * @see #removeState(org.apache.click.Context)
+     *
+     * @param context the request context
+     */
+    public void saveState(Context context) {
+        ClickUtils.saveState(this, getName(), context);
+    }
+
     // Protected Methods ------------------------------------------------------
 
     /**

Modified: click/trunk/click/framework/test/org/apache/click/control/AbstractLinkTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/control/AbstractLinkTest.java?rev=1026307&r1=1026306&r2=1026307&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/control/AbstractLinkTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/control/AbstractLinkTest.java Fri Oct
22 11:53:53 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.click.control;
 
+import java.util.HashMap;
+import java.util.Map;
 import junit.framework.TestCase;
 import org.apache.click.MockContext;
 
@@ -58,4 +60,45 @@ public class AbstractLinkTest extends Te
 
         assertTrue(link.toString().indexOf("&amp;") > 0);
     }
+
+    /**
+     * Test that AbstractLink.getState contains the link parameters.
+     * CLK-715
+     */
+    public void testGetState() {
+        // Setup link
+
+        ActionLink link  = new ActionLink("name");
+
+        link.setParameter("name", "Steve");
+        link.setParameter("age", "10");
+        link.setValue("myval");
+
+        Map state = (Map) link.getState();
+
+        assertEquals(state, link.getParameters());
+        assertEquals(state.get("value"), link.getValue());
+    }
+
+    /**
+     * Test that AbstractLink.setState set the link parameters.
+     *
+     * CLK-715
+     */
+    public void testSetState() {
+        // Setup link
+        ActionLink link  = new ActionLink("name");
+
+        // Setup state
+        Map state = new HashMap();
+        state.put("name", "Steve");
+        state.put("age", "10");
+        state.put("value", "myval");
+
+        link.setState(state);
+
+        // Check that link parameters is restored
+        assertEquals("Steve", link.getParameter("name"));
+        assertEquals("myval", link.getValue());
+    }
 }

Modified: click/trunk/click/framework/test/org/apache/click/control/TableTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/framework/test/org/apache/click/control/TableTest.java?rev=1026307&r1=1026306&r2=1026307&view=diff
==============================================================================
--- click/trunk/click/framework/test/org/apache/click/control/TableTest.java (original)
+++ click/trunk/click/framework/test/org/apache/click/control/TableTest.java Fri Oct 22 11:53:53
2010
@@ -19,6 +19,7 @@
 package org.apache.click.control;
 
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.List;
 
 import java.util.Map;
@@ -165,4 +166,71 @@ public class TableTest extends TestCase 
 
         assertTrue(table.toString().contains("<caption>caption<tt>tt</tt></caption>"));
     }
+
+    /**
+     * Test that Table.getState contains the table internal state.
+     * CLK-715
+     */
+    public void testGetState() {
+        // Setup table
+
+        Table table  = new Table("table");
+        // Set table state
+        int pageNumber = 5;
+        boolean ascending = false;
+        String sortedColumn = "Dummy";
+        String linkValue = "myval";
+
+        table.setPageNumber(pageNumber);
+        table.setSortedAscending(ascending);
+        table.setSortedColumn(sortedColumn);
+        table.getControlLink().setValue(linkValue);
+
+        // Retrieve table state
+        Object[] state = (Object[]) table.getState();
+
+        // Perform tests
+        assertEquals(state[0], pageNumber);
+        assertEquals(state[1], sortedColumn);
+        assertEquals(state[2], ascending);
+
+        Map controlLinkParams = (Map) state[3];
+
+        assertEquals(controlLinkParams, table.getControlLink().getParameters());
+        assertEquals(controlLinkParams.get("value"), linkValue);
+    }
+
+    /**
+     * Test that Table.setState set the table internal state.
+     *
+     * CLK-715
+     */
+    public void testSetState() {
+                // Setup table
+
+        Table table  = new Table("table");
+        // Set table state
+        int pageNumber = 5;
+        boolean ascending = false;
+        String sortedColumn = "Dummy";
+        String linkValue = "myval";
+
+        Object[] state = new Object[4];
+        state[0] = Integer.valueOf(pageNumber);
+        state[1] = sortedColumn;
+        state[2] = Boolean.valueOf(ascending);
+        Map controlLinkParams = new HashMap();
+        controlLinkParams.put("value", linkValue);
+        state[3] = controlLinkParams;
+
+        // Set table state
+        table.setState(state);
+
+        // Perform tests
+        assertEquals(pageNumber, table.getPageNumber());
+        assertEquals(sortedColumn, table.getSortedColumn());
+        assertEquals(ascending, table.isSortedAscending());
+        assertEquals(controlLinkParams, table.getControlLink().getParameters());
+        assertEquals(linkValue, table.getControlLink().getValue());
+    }
 }



Mime
View raw message