You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by iv...@apache.org on 2008/04/15 20:39:48 UTC

svn commit: r648369 - in /wicket/trunk/wicket/src: main/java/org/apache/wicket/markup/html/internal/ test/java/org/apache/wicket/markup/html/internal/

Author: ivaynberg
Date: Tue Apr 15 11:39:31 2008
New Revision: 648369

URL: http://svn.apache.org/viewvc?rev=648369&view=rev
Log:
WICKET-1536

Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html   (with props)
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java   (with props)
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/Enclosure.java
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosureTest.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/Enclosure.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/Enclosure.java?rev=648369&r1=648368&r2=648369&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/Enclosure.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/Enclosure.java Tue Apr 15 11:39:31 2008
@@ -16,6 +16,9 @@
  */
 package org.apache.wicket.markup.html.internal;
 
+import java.util.HashMap;
+import java.util.Map;
+
 import org.apache.wicket.Component;
 import org.apache.wicket.MarkupContainer;
 import org.apache.wicket.WicketRuntimeException;
@@ -74,18 +77,20 @@
  * @author Juergen Donnerstag
  * @since 1.3
  */
-public class Enclosure extends WebMarkupContainer
+public class Enclosure extends WebMarkupContainer<Object>
 {
 	private static final long serialVersionUID = 1L;
 
 	private static final Logger log = LoggerFactory.getLogger(Enclosure.class);
 
 	/** The child component to delegate the isVisible() call to */
-	private Component childComponent;
+	private Component< ? > childComponent;
 
 	/** Id of the child component that will control visibility of the enclosure */
 	private final CharSequence childId;
 
+	private transient Map<Component< ? >, Boolean> originalVisibilityStatus;
+
 	/**
 	 * Construct.
 	 * 
@@ -102,6 +107,7 @@
 	 * 
 	 * @see org.apache.wicket.MarkupContainer#isTransparentResolver()
 	 */
+	@Override
 	public boolean isTransparentResolver()
 	{
 		return true;
@@ -112,11 +118,11 @@
 	 * @param childId
 	 * @return Child Component
 	 */
-	public Component getChildComponent()
+	public Component< ? > getChildComponent()
 	{
 		if (childComponent == null)
 		{
-			MarkupContainer parent = getEnclosureParent();
+			MarkupContainer< ? > parent = getEnclosureParent();
 
 			if (childId == null)
 			{
@@ -124,7 +130,7 @@
 					"You most likely forgot to register the EnclosureHandler with the MarkupParserFactory");
 			}
 
-			final Component child = parent.get(childId.toString());
+			final Component< ? > child = parent.get(childId.toString());
 			if (child == null)
 			{
 				throw new MarkupException(
@@ -139,11 +145,11 @@
 	/**
 	 * Get the real parent container
 	 * 
-	 * @return
+	 * @return enclosure's parent markup container
 	 */
-	private MarkupContainer getEnclosureParent()
+	private MarkupContainer< ? > getEnclosureParent()
 	{
-		MarkupContainer parent = getParent();
+		MarkupContainer< ? > parent = getParent();
 		while (parent != null)
 		{
 			if (parent.isTransparentResolver())
@@ -152,7 +158,7 @@
 			}
 			else if (parent instanceof BorderBodyContainer)
 			{
-				parent = ((BorderBodyContainer)parent).findParent(Border.class);
+				parent = ((Border< ? >.BorderBodyContainer)parent).findParent(Border.class);
 			}
 			else
 			{
@@ -173,9 +179,10 @@
 	 * @see org.apache.wicket.MarkupContainer#onComponentTagBody(org.apache.wicket.markup.MarkupStream,
 	 *      org.apache.wicket.markup.ComponentTag)
 	 */
+	@Override
 	protected void onComponentTagBody(MarkupStream markupStream, ComponentTag openTag)
 	{
-		final Component controller = getChildComponent();
+		final Component< ? > controller = getChildComponent();
 		if (controller == this)
 		{
 			throw new WicketRuntimeException(
@@ -185,15 +192,19 @@
 		setVisible(controller.determineVisibility());
 
 		// transfer visibility to direct children
+		originalVisibilityStatus = new HashMap<Component< ? >, Boolean>();
 		DirectChildTagIterator it = new DirectChildTagIterator(markupStream, openTag);
-		MarkupContainer controllerParent = getEnclosureParent();
+		MarkupContainer< ? > controllerParent = getEnclosureParent();
 		while (it.hasNext())
 		{
-			ComponentTag t = (ComponentTag)it.next();
-			Component child = controllerParent.get(t.getId());
+			ComponentTag t = it.next();
+			Component< ? > child = controllerParent.get(t.getId());
 			if (child != null)
 			{
+				// record original visiblity allowed value, will restore later
+				originalVisibilityStatus.put(child, child.isVisibilityAllowed());
 				child.setVisibilityAllowed(isVisible());
+
 			}
 		}
 		it.rewind();
@@ -208,11 +219,26 @@
 		}
 	}
 
+	@Override
+	protected void onDetach()
+	{
+		if (originalVisibilityStatus != null)
+		{
+			// restore original visibility statuses
+			for (Map.Entry<Component< ? >, Boolean> entry : originalVisibilityStatus.entrySet())
+			{
+				entry.getKey().setVisibilityAllowed(entry.getValue());
+			}
+			originalVisibilityStatus = null;
+		}
+		super.onDetach();
+	}
+
 	/**
 	 * Iterator that iterates over direct child component tags of the given component tag
 	 * 
 	 */
-	private static class DirectChildTagIterator extends ReadOnlyIterator
+	private static class DirectChildTagIterator extends ReadOnlyIterator<ComponentTag>
 	{
 		private final MarkupStream markupStream;
 		private final ComponentTag parent;
@@ -253,7 +279,7 @@
 		/**
 		 * @see java.util.Iterator#next()
 		 */
-		public Object next()
+		public ComponentTag next()
 		{
 			ComponentTag ret = next;
 			findNext();

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html?rev=648369&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html Tue Apr 15 11:39:31 2008
@@ -0,0 +1,9 @@
+<html>
+<body>
+<link wicket:id="link">toggle</link>
+<wicket:enclosure child="label1">
+<span wicket:id="label1"></span>
+<span wicket:id="label2"></span>
+</wicket:enclosure>
+</body>
+</html>
\ No newline at end of file

Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.html
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java?rev=648369&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java Tue Apr 15 11:39:31 2008
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.wicket.markup.html.internal;
+
+import org.apache.wicket.markup.html.WebPage;
+import org.apache.wicket.markup.html.basic.Label;
+import org.apache.wicket.markup.html.link.Link;
+
+/**
+ * @author ivaynberg
+ */
+public class EnclosurePage_6 extends WebPage
+{
+	private int counter;
+
+	/**
+	 * Constructor
+	 */
+	public EnclosurePage_6()
+	{
+		add(new Link<Object>("link")
+		{
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			public void onClick()
+			{
+				counter++;
+			}
+
+		});
+
+		add(new Label<String>("label1", "content1")
+		{
+			private static final long serialVersionUID = 1L;
+
+			@Override
+			public boolean isVisible()
+			{
+				return counter % 2 == 0;
+			}
+		});
+
+		add(new Label<String>("label2", "content2"));
+	}
+}

Propchange: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosurePage_6.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosureTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosureTest.java?rev=648369&r1=648368&r2=648369&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosureTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/internal/EnclosureTest.java Tue Apr 15 11:39:31 2008
@@ -44,6 +44,7 @@
 	 * @see org.apache.wicket.WicketTestCase#setUp()
 	 */
 
+	@Override
 	protected void setUp() throws Exception
 	{
 		WebApplication app = new DummyApplication();
@@ -108,5 +109,31 @@
 	{
 		executeTest(EnclosurePage_5.class, new PageParameters("visible=true"),
 			"EnclosurePageExpectedResult_5-1.html");
+	}
+
+	/**
+	 * Tests visibility of children after enclosure has been made hidden and visible again
+	 * 
+	 * @throws Exception
+	 */
+	public void testVisibilityOfChildren() throws Exception
+	{
+		// render with enclosure initally visible
+		tester.startPage(EnclosurePage_6.class);
+		String doc = tester.getServletResponse().getDocument();
+		assertTrue(doc.contains("content1"));
+		assertTrue(doc.contains("content2"));
+
+		// render with enclosure hidden
+		tester.clickLink("link");
+		doc = tester.getServletResponse().getDocument();
+		assertFalse(doc.contains("content1"));
+		assertFalse(doc.contains("content2"));
+
+		// render with enclosure visible again
+		tester.clickLink("link");
+		doc = tester.getServletResponse().getDocument();
+		assertTrue(doc.contains("content1"));
+		assertTrue(doc.contains("content2"));
 	}
 }