You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by jd...@apache.org on 2008/12/30 00:05:33 UTC

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

Author: jdonnerstag
Date: Mon Dec 29 15:05:33 2008
New Revision: 730011

URL: http://svn.apache.org/viewvc?rev=730011&view=rev
Log:
fixed Wicket-1917: Automatically Load CSS files based on name matching
Added a test case

Added:
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.css
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.html
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2_ExpectedResult.html
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainer.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainerWithAssociatedMarkup.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java
    wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/HeaderScopingTest.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/Component.java Mon Dec 29 15:05:33 2008
@@ -29,6 +29,7 @@
 import org.apache.wicket.authorization.AuthorizationException;
 import org.apache.wicket.authorization.IAuthorizationStrategy;
 import org.apache.wicket.authorization.UnauthorizedActionException;
+import org.apache.wicket.behavior.HeaderContributor;
 import org.apache.wicket.behavior.IBehavior;
 import org.apache.wicket.feedback.FeedbackMessage;
 import org.apache.wicket.feedback.IFeedback;
@@ -37,6 +38,7 @@
 import org.apache.wicket.markup.MarkupStream;
 import org.apache.wicket.markup.WicketTag;
 import org.apache.wicket.markup.html.IHeaderContributor;
+import org.apache.wicket.markup.html.PackageResource;
 import org.apache.wicket.markup.html.internal.HtmlHeaderContainer;
 import org.apache.wicket.model.IComponentAssignedModel;
 import org.apache.wicket.model.IComponentInheritedModel;
@@ -890,15 +892,7 @@
 	 */
 	public Component(final String id)
 	{
-		setId(id);
-		getApplication().notifyComponentInstantiationListeners(this);
-
-		final IDebugSettings debugSettings = Application.get().getDebugSettings();
-		if (debugSettings.isLinePreciseReportingOnNewComponentEnabled())
-		{
-			setMetaData(CONSTRUCTED_AT_KEY, Strings.toString(this, new MarkupException(
-				"constructed")));
-		}
+		this(id, null);
 	}
 
 	/**
@@ -915,8 +909,38 @@
 	 */
 	public Component(final String id, final IModel<?> model)
 	{
-		this(id);
-		setModelImpl(wrap(model));
+		setId(id);
+		getApplication().notifyComponentInstantiationListeners(this);
+
+		final IDebugSettings debugSettings = Application.get().getDebugSettings();
+		if (debugSettings.isLinePreciseReportingOnNewComponentEnabled())
+		{
+			setMetaData(CONSTRUCTED_AT_KEY, Strings.toString(this, new MarkupException(
+				"constructed")));
+		}
+
+		if (model != null)
+		{
+			setModelImpl(wrap(model));
+		}
+
+		// Any component is allowed to have associated *.js or *.css file which are automatically
+		// added to the header section of the page.
+		// ignore anonymous classes and internal components
+		String name = this.getClass().getSimpleName();
+		if ((name != null) &&
+			((this instanceof Page) || ((id != null) && (id.length() > 0) && (id.charAt(0) != '_'))))
+		{
+			if (PackageResource.exists(this.getClass(), name + ".css", getLocale(), getStyle()))
+			{
+				add(HeaderContributor.forCss(this.getClass(), name + ".css"));
+			}
+
+			if (PackageResource.exists(this.getClass(), name + ".js", getLocale(), getStyle()))
+			{
+				add(HeaderContributor.forJavaScript(this.getClass(), name + ".js"));
+			}
+		}
 	}
 
 	/**
@@ -3634,9 +3658,9 @@
 	/**
 	 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL OR OVERRIDE.
 	 * 
-     * <p>
+	 * <p>
 	 * Called when a request begins.
-     * </p>
+	 * </p>
 	 * 
 	 * @Deprecated use {@link #onBeforeRender()} instead
 	 */
@@ -3647,9 +3671,9 @@
 	/**
 	 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL OR OVERRIDE.
 	 * 
-     * <p>
+	 * <p>
 	 * Called when a request ends.
-     * </p>
+	 * </p>
 	 * 
 	 * @Deprecated use {@link #onBeforeRender()} instead
 	 * 
@@ -3661,9 +3685,9 @@
 	/**
 	 * THIS METHOD IS NOT PART OF THE WICKET PUBLIC API. DO NOT CALL OR OVERRIDE.
 	 * 
-     * <p>
+	 * <p>
 	 * Called anytime a model is changed via setModel or setModelObject.
-     * </p>
+	 * </p>
 	 */
 	protected void internalOnModelChanged()
 	{
@@ -4222,7 +4246,7 @@
 	 */
 	final void setId(final String id)
 	{
-		if (id == null && !(this instanceof Page))
+		if ((id == null) && !(this instanceof Page))
 		{
 			throw new WicketRuntimeException("Null component id is not allowed.");
 		}

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/MarkupContainer.java Mon Dec 29 15:05:33 2008
@@ -102,7 +102,7 @@
 	 */
 	public MarkupContainer(final String id)
 	{
-		super(id);
+		this(id, null);
 	}
 
 	/**

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainer.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainer.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainer.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainer.java Mon Dec 29 15:05:33 2008
@@ -37,7 +37,7 @@
 	 */
 	public WebMarkupContainer(final String id)
 	{
-		super(id);
+		this(id, null);
 	}
 
 	/**

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainerWithAssociatedMarkup.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainerWithAssociatedMarkup.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainerWithAssociatedMarkup.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/WebMarkupContainerWithAssociatedMarkup.java Mon Dec 29 15:05:33 2008
@@ -40,7 +40,7 @@
 	 */
 	public WebMarkupContainerWithAssociatedMarkup(final String id)
 	{
-		super(id);
+		this(id, null);
 	}
 
 	/**

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/internal/HtmlHeaderContainer.java Mon Dec 29 15:05:33 2008
@@ -62,7 +62,6 @@
  * </ul>
  * 
  * @author Juergen Donnerstag
- * 
  */
 public class HtmlHeaderContainer extends WebMarkupContainer
 {
@@ -171,12 +170,16 @@
 			if (output.length() > 0)
 			{
 				if (renderOpenAndCloseTags())
+				{
 					webResponse.write("<head>");
+				}
 
 				webResponse.write(output);
 
 				if (renderOpenAndCloseTags())
+				{
 					webResponse.write("</head>");
+				}
 			}
 		}
 		finally
@@ -186,6 +189,10 @@
 		}
 	}
 
+	/**
+	 * 
+	 * @return True if open and close tag are to be rendered.
+	 */
 	protected boolean renderOpenAndCloseTags()
 	{
 		return true;
@@ -209,6 +216,7 @@
 		final HtmlHeaderContainer container)
 	{
 		page.renderHead(container);
+
 		// Make sure all Components interested in contributing to the header
 		// and there attached behaviors are asked.
 		page.visitChildren(new IVisitor<Component>()
@@ -223,10 +231,7 @@
 					component.renderHead(container);
 					return IVisitor.CONTINUE_TRAVERSAL;
 				}
-				else
-				{
-					return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
-				}
+				return IVisitor.CONTINUE_TRAVERSAL_BUT_DONT_GO_DEEPER;
 			}
 		});
 	}
@@ -320,5 +325,4 @@
 		}
 		return headerResponse;
 	}
-
 }

Modified: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/HeaderScopingTest.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/HeaderScopingTest.java?rev=730011&r1=730010&r2=730011&view=diff
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/HeaderScopingTest.java (original)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/HeaderScopingTest.java Mon Dec 29 15:05:33 2008
@@ -20,7 +20,6 @@
 
 /**
  * 
- * @author Matheus
  */
 public class HeaderScopingTest extends WicketTestCase
 {
@@ -42,4 +41,13 @@
 	{
 		executeTest(MyPage.class, "MyPageExpectedResult.html");
 	}
+
+	/**
+	 * 
+	 * @throws Exception
+	 */
+	public void test_2() throws Exception
+	{
+		executeTest(MyPage2.class, "MyPage2_ExpectedResult.html");
+	}
 }

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.css
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.css?rev=730011&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.css (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.css Mon Dec 29 15:05:33 2008
@@ -0,0 +1 @@
+CSS File
\ No newline at end of file

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.html?rev=730011&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.html (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.html Mon Dec 29 15:05:33 2008
@@ -0,0 +1,8 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+  <head>
+    <title>My Page</title>
+  </head>
+  <body>
+  </body>
+</html>

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java?rev=730011&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2.java Mon Dec 29 15:05:33 2008
@@ -0,0 +1,37 @@
+/*
+ * 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.header;
+
+import org.apache.wicket.behavior.HeaderContributor;
+import org.apache.wicket.markup.html.WebPage;
+
+/**
+ * 
+ */
+public class MyPage2 extends WebPage
+{
+	private static final long serialVersionUID = 1L;
+
+	/**
+	 * Creates a new instance of MyPage
+	 */
+	public MyPage2()
+	{
+		// And it is not problem if historically it has been added. Wicket will render it just once
+		add(HeaderContributor.forCss(this.getClass(), this.getClass().getSimpleName() + ".css"));
+	}
+}

Added: wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2_ExpectedResult.html
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2_ExpectedResult.html?rev=730011&view=auto
==============================================================================
--- wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2_ExpectedResult.html (added)
+++ wicket/trunk/wicket/src/test/java/org/apache/wicket/markup/html/header/MyPage2_ExpectedResult.html Mon Dec 29 15:05:33 2008
@@ -0,0 +1,9 @@
+<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
+<html>
+  <head>
+    <title>My Page</title>
+  <link rel="stylesheet" type="text/css" href="resources/org.apache.wicket.markup.html.header.MyPage2/MyPage2.css" />
+</head>
+  <body>
+  </body>
+</html>