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/06/20 19:23:02 UTC

svn commit: r669996 - in /wicket/trunk/wicket/src/main/java/org/apache/wicket: markup/html/image/ContextImage.java markup/html/image/ContextPathGenerator.java util/string/Strings.java

Author: ivaynberg
Date: Fri Jun 20 10:23:02 2008
New Revision: 669996

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

Added:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java   (with props)
Modified:
    wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextImage.java
    wicket/trunk/wicket/src/main/java/org/apache/wicket/util/string/Strings.java

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextImage.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextImage.java?rev=669996&r1=669995&r2=669996&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextImage.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextImage.java Fri Jun 20 10:23:02 2008
@@ -16,7 +16,6 @@
  */
 package org.apache.wicket.markup.html.image;
 
-import org.apache.wicket.Component;
 import org.apache.wicket.markup.ComponentTag;
 import org.apache.wicket.markup.html.WebComponent;
 import org.apache.wicket.model.IModel;
@@ -28,27 +27,39 @@
  * to the context root, no matter what URL the page the ContextImage is on is rendered at.
  * 
  * @author Alastair Maw
+ * @author Igor Vaynberg (ivaynberg)
  */
 public class ContextImage extends WebComponent<String>
 {
 	private static final long serialVersionUID = 1L;
 
 	/**
-	 * @see Component#Component(String)
+	 * Constructor
+	 * 
+	 * @param id
+	 * @param contextRelativePath
+	 *            context-relative path eg <code>images/border.jpg</code>
 	 */
-	public ContextImage(String id)
+	public ContextImage(String id, IModel<String> contextRelativePath)
 	{
 		super(id);
+		add(new ContextPathGenerator(contextRelativePath));
 	}
 
 	/**
-	 * @see Component#Component(String, IModel)
+	 * Constructor
+	 * 
+	 * @param id
+	 * @param contextRelativePath
+	 *            context-relative path eg <code>images/border.jpg</code>
 	 */
-	public ContextImage(String id, IModel<String> model)
+	public ContextImage(String id, String contextRelativePath)
 	{
-		super(id, model);
+		super(id);
+		add(new ContextPathGenerator(contextRelativePath));
 	}
 
+
 	/**
 	 * @see org.apache.wicket.Component#onComponentTag(ComponentTag)
 	 */
@@ -57,6 +68,5 @@
 	{
 		checkComponentTag(tag, "img");
 		super.onComponentTag(tag);
-		tag.put("src", getRequest().getRelativePathPrefixToContextRoot() + getModelObjectAsString());
 	}
 }

Added: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java?rev=669996&view=auto
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java (added)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java Fri Jun 20 10:23:02 2008
@@ -0,0 +1,86 @@
+/*
+ * 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.image;
+
+import org.apache.wicket.Component;
+import org.apache.wicket.behavior.AbstractBehavior;
+import org.apache.wicket.markup.ComponentTag;
+import org.apache.wicket.model.IModel;
+import org.apache.wicket.model.Model;
+import org.apache.wicket.util.string.Strings;
+
+/**
+ * A behavior that converts the provider url fragment to a context-relative url. For example if the
+ * current url is <code>http://localhost/context/product/1231</code> and the specified url is
+ * <code>images/border.jpg</code> the generated url will be <code>../../images/border.jpg</code>
+ * 
+ * @author Igor Vaynberg (ivaynberg)
+ */
+public class ContextPathGenerator extends AbstractBehavior
+{
+	private static final long serialVersionUID = 1L;
+
+	private final IModel<String> contextRelativePath;
+
+	/**
+	 * Constructor
+	 * 
+	 * @param contextRelativePath
+	 *            context-relative path, eg <code>images/border.jpg</code>
+	 */
+	public ContextPathGenerator(IModel<String> contextRelativePath)
+	{
+		this.contextRelativePath = contextRelativePath;
+	}
+
+	/**
+	 * Constructor
+	 * 
+	 * @param contextRelativePath
+	 *            context-relative path, eg <code>images/border.jpg</code>
+	 */
+	public ContextPathGenerator(String contextRelativePath)
+	{
+		this.contextRelativePath = new Model<String>(contextRelativePath);
+	}
+
+	/** {@inheritDoc} **/
+	@Override
+	public void onComponentTag(Component<?> component, ComponentTag tag)
+	{
+		// get the path
+		final String path = contextRelativePath.getObject();
+
+		// generate prefix that will make path context relative
+		final String prefix = component.getRequest().getRelativePathPrefixToContextRoot();
+
+		// join the two path pieces
+		final String contextRelativePath = Strings.join("/", path, prefix);
+
+		tag.put("src", contextRelativePath);
+	}
+
+	/** {@inheritDoc} **/
+	@Override
+	public void detach(Component<?> component)
+	{
+		contextRelativePath.detach();
+		super.detach(component);
+	}
+
+
+}

Propchange: wicket/trunk/wicket/src/main/java/org/apache/wicket/markup/html/image/ContextPathGenerator.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: wicket/trunk/wicket/src/main/java/org/apache/wicket/util/string/Strings.java
URL: http://svn.apache.org/viewvc/wicket/trunk/wicket/src/main/java/org/apache/wicket/util/string/Strings.java?rev=669996&r1=669995&r2=669996&view=diff
==============================================================================
--- wicket/trunk/wicket/src/main/java/org/apache/wicket/util/string/Strings.java (original)
+++ wicket/trunk/wicket/src/main/java/org/apache/wicket/util/string/Strings.java Fri Jun 20 10:23:02 2008
@@ -621,7 +621,7 @@
 	 * @param fragments
 	 * @return combined fragments
 	 */
-	public static String join(String separator, String[] fragments)
+	public static String join(String separator, String... fragments)
 	{
 		if (fragments.length < 1)
 		{
@@ -1093,9 +1093,9 @@
 	}
 
 	/**
-	 * Converts the given object to a string. Does special conversion for
-	 * {@link Throwable throwables} and String arrays of length 1 (in which case it just returns to
-	 * string in that array, as this is a common thing to have in the Servlet API).
+	 * Converts the given object to a string. Does special conversion for {@link Throwable
+	 * throwables} and String arrays of length 1 (in which case it just returns to string in that
+	 * array, as this is a common thing to have in the Servlet API).
 	 * 
 	 * @param object
 	 *            The object