001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.wicket.markup.html.image; 018 019import org.apache.wicket.markup.ComponentTag; 020import org.apache.wicket.markup.html.WebComponent; 021import org.apache.wicket.markup.parser.filter.RelativePathPrefixHandler; 022import org.apache.wicket.model.IModel; 023 024/** 025 * Provides a context-relative image. 026 * <p> 027 * Provide a String in this component's model which will be prefixed such that the image is relative 028 * to the context root, no matter what URL the page the ContextImage is on is rendered at. 029 * 030 * @author Alastair Maw 031 * @author Igor Vaynberg (ivaynberg) 032 */ 033public class ContextImage extends WebComponent 034{ 035 private static final long serialVersionUID = 1L; 036 037 /** 038 * Constructor 039 * 040 * @param id 041 * the component id 042 * @param contextRelativePath 043 * context-relative path eg <code>images/border.jpg</code> 044 */ 045 public ContextImage(String id, IModel<String> contextRelativePath) 046 { 047 super(id); 048 add(new ContextPathGenerator(contextRelativePath)); 049 } 050 051 /** 052 * Constructor 053 * 054 * @param id 055 * the component id 056 * @param contextRelativePath 057 * context-relative path eg <code>images/border.jpg</code> 058 */ 059 public ContextImage(String id, String contextRelativePath) 060 { 061 super(id); 062 add(new ContextPathGenerator(contextRelativePath)); 063 } 064 065 /** 066 * Constructor. 067 * 068 * Uses the url from the <em>src</em> markup attribute and makes 069 * it relative to the context path 070 * 071 * @param id 072 * the component id 073 */ 074 public ContextImage(String id) 075 { 076 super(id); 077 add(RelativePathPrefixHandler.RELATIVE_PATH_BEHAVIOR); 078 } 079 080 /** 081 * @see org.apache.wicket.Component#onComponentTag(ComponentTag) 082 */ 083 @Override 084 protected void onComponentTag(final ComponentTag tag) 085 { 086 checkComponentTag(tag, "img"); 087 super.onComponentTag(tag); 088 } 089}