You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@click.apache.org by sa...@apache.org on 2010/11/02 13:40:44 UTC

svn commit: r1030021 - in /click/trunk/click/extras: src/org/apache/click/extras/panel/TabbedPanel.java test/org/apache/click/extras/panel/TabbedPanelTest.java

Author: sabob
Date: Tue Nov  2 12:40:43 2010
New Revision: 1030021

URL: http://svn.apache.org/viewvc?rev=1030021&view=rev
Log:
added stateful TabbedPanel. CLK-715

Modified:
    click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java
    click/trunk/click/extras/test/org/apache/click/extras/panel/TabbedPanelTest.java

Modified: click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java?rev=1030021&r1=1030020&r2=1030021&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/panel/TabbedPanel.java Tue Nov  2 12:40:43 2010
@@ -20,9 +20,10 @@ package org.apache.click.extras.panel;
 
 import java.util.List;
 import org.apache.click.ActionListener;
-import org.apache.click.ControlRegistry;
 import org.apache.click.Context;
 import org.apache.click.Control;
+import org.apache.click.Page;
+import org.apache.click.Stateful;
 import org.apache.click.control.ActionLink;
 import org.apache.click.control.Panel;
 import org.apache.click.element.CssImport;
@@ -137,7 +138,7 @@ import org.apache.commons.lang.math.Numb
  * </body>
  * &lt;/html&gt; </pre>
  */
-public class TabbedPanel extends Panel {
+public class TabbedPanel extends Panel implements Stateful {
 
     private static final long serialVersionUID = 1L;
 
@@ -339,6 +340,68 @@ public class TabbedPanel extends Panel {
     }
 
     /**
+     * Return the TabbedPanel state. The following state is returned:
+     * <ul>
+     * <li>The {@link #getActivePanel() activePanel's} name</li>
+     * <li>The {@link #getTabLink() tabLink} parameters</li>
+     * </ul>
+     *
+     * @return the TabbedPanel state
+     */
+    public Object getState() {
+        Object[] panelState = new Object[2];
+        boolean hasState = false;
+
+        Panel localActivePanel = getActivePanel();
+        if (localActivePanel != null) {
+            String activePanelName = localActivePanel.getName();
+            hasState = true;
+            panelState[0] = activePanelName;
+        }
+
+        Object tabLinkState = getTabLink().getState();
+
+        if (tabLinkState != null) {
+            hasState = true;
+            panelState[1] = tabLinkState;
+        }
+
+        if (hasState) {
+            return panelState;
+        } else {
+            return null;
+        }
+    }
+
+    /**
+     * Set the TabbedPanel state.
+     *
+     * @param state the tabbedPanel state to set
+     */
+    public void setState(Object state) {
+        if (state == null) {
+            return;
+        }
+
+        Object[] panelState = (Object[]) state;
+
+        if (panelState[0] != null) {
+
+            String activePanelName = (String) panelState[0];
+               Control control = getControlMap().get(activePanelName);
+                if (control instanceof Panel) {
+                    Panel localActivePanel = (Panel) control;
+                    setActivePanel(localActivePanel);
+                }
+            }
+
+        if (panelState[1] != null) {
+            Object tabLinkState = panelState[1];
+            getTabLink().setState(tabLinkState);
+        }
+    }
+
+    /**
      * Set the tab switch listener.  If the listener <b>and</b> method are
      * non-null, then the listener will be called whenever a request to switch
      * tabs is placed by clicking the link associated with that tab.
@@ -408,10 +471,6 @@ public class TabbedPanel extends Panel {
      */
     @Override
     public void onInit() {
-        if (hasBehaviors()) {
-            ControlRegistry.registerAjaxTarget(this);
-        }
-
         initActivePanel();
 
         for (int i = 0, size = getControls().size(); i < size; i++) {
@@ -480,6 +539,48 @@ public class TabbedPanel extends Panel {
         }
     }
 
+    /**
+     * Remove the TabbedPanel 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 TabbedPanel state from the session for the given request context.
+     * <p/>
+     * This method delegates to {@link #setState(java.lang.Object)} to set the
+     * panel's 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 TabbedPanel state to the session for the given request context.
+     * <p/>
+     * * This method delegates to {@link #getState()} to retrieve the panel's
+     * 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/extras/test/org/apache/click/extras/panel/TabbedPanelTest.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/test/org/apache/click/extras/panel/TabbedPanelTest.java?rev=1030021&r1=1030020&r2=1030021&view=diff
==============================================================================
--- click/trunk/click/extras/test/org/apache/click/extras/panel/TabbedPanelTest.java (original)
+++ click/trunk/click/extras/test/org/apache/click/extras/panel/TabbedPanelTest.java Tue Nov  2 12:40:43 2010
@@ -18,6 +18,8 @@
  */
 package org.apache.click.extras.panel;
 
+import java.util.HashMap;
+import java.util.Map;
 import junit.framework.TestCase;
 import org.apache.click.ActionListener;
 import org.apache.click.Control;
@@ -160,4 +162,73 @@ public class TabbedPanelTest extends Tes
         assertTrue(child1.isActive());
         assertFalse(child2.isActive());
     }
+
+
+    /**
+     * Test that TabbedPanel.getState contains the active panel.
+     *
+     * CLK-715
+     */
+    public void testGetState() {
+        // Setup Panel
+        TabbedPanel panel = new TabbedPanel("panel");
+
+        // Add two panels named child1 and child2
+        Panel child1 = new Panel("child1");
+        Panel child2 = new Panel("child2");
+        panel.add(child1);
+        panel.add(child2);
+
+        Map expectedTabLinkState = new HashMap();
+        expectedTabLinkState.put("id", "1");
+        panel.getTabLink().setParameters(expectedTabLinkState);
+
+        panel.setActivePanel(child2);
+
+        String expectedActivePanel = "child2";
+
+        // Get state
+        Object[] state = (Object[]) panel.getState();
+
+        // Perform tests
+        assertEquals(expectedActivePanel, state[0]);
+        assertEquals(expectedTabLinkState, state[1]);
+    }
+
+    /**
+     * Test that TabbedPanel.setState set the active panel.
+     *
+     * CLK-715
+     */
+    public void testSetState() {
+        // Setup Panel
+        TabbedPanel panel = new TabbedPanel("panel");
+
+        // Add two panels named child1 and child2
+        Panel child1 = new Panel("child1");
+        Panel child2 = new Panel("child2");
+        panel.add(child1);
+        panel.add(child2);
+
+        Map expectedTabLinkState = new HashMap();
+        expectedTabLinkState.put("id", "1");
+
+        String expectedActivePanelName = "child2";
+
+        Object[] state = new Object[2];
+        state[0] = expectedActivePanelName;
+        state[1] = expectedTabLinkState;
+
+        // Initially child1 should be active
+        assertEquals(child1, panel.getActivePanel());
+        // TabLink shouldn't have any parameters
+        assertEquals(0, panel.getTabLink().getParameters().size());
+
+        // Set state
+        panel.setState(state);
+
+        // Perform tests
+        assertEquals(panel.getActivePanel().getName(), expectedActivePanelName);
+        assertEquals(expectedTabLinkState, panel.getTabLink().getParameters());
+    }
 }