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.CrossOrigin; 021import org.apache.wicket.model.IModel; 022import org.apache.wicket.request.mapper.parameter.PageParameters; 023import org.apache.wicket.request.resource.IResource; 024import org.apache.wicket.request.resource.ResourceReference; 025 026/** 027 * A component which displays localizable image resources within a picture tag. 028 * 029 * The source tag is the same as the image element, but it is also possible to set the media 030 * attribute with setMedia(String media). The second difference is that there is no src attribute, 031 * so every ResourceReference and ImageResource is added directly to the srcset attribute. 032 * 033 * @see org.apache.wicket.markup.html.image.Image 034 * @author Tobias Soloschenko 035 * 036 */ 037public class Source extends Image 038{ 039 private static final long serialVersionUID = 1L; 040 041 private String media = null; 042 043 /** 044 * Creates a source for a picture 045 * 046 * @param id 047 * the component id 048 * @see org.apache.wicket.markup.html.image.Image 049 */ 050 protected Source(final String id) 051 { 052 super(id); 053 } 054 055 /** 056 * Creates a source for a picture 057 * 058 * @param id 059 * the component id 060 * @param resourceReferences 061 * the resource references applied to the source in the given order 062 * @see org.apache.wicket.markup.html.image.Image 063 */ 064 public Source(final String id, final ResourceReference... resourceReferences) 065 { 066 super(id, null, resourceReferences); 067 } 068 069 /** 070 * Creates a source for a picture 071 * 072 * @param id 073 * the component id 074 * @param resourceParameters 075 * the resource parameters applied to the localized image resource 076 * @param resourceReferences 077 * the resource references applied to the source in the given order 078 * @see org.apache.wicket.markup.html.image.Image 079 */ 080 public Source(final String id, PageParameters resourceParameters, 081 final ResourceReference... resourceReferences) 082 { 083 super(id, null, resourceParameters, resourceReferences); 084 } 085 086 /** 087 * Creates a source for a picture 088 * 089 * @param id 090 * the component id 091 * @param imageResources 092 * the image resources applied to the source in the given order 093 * @see org.apache.wicket.markup.html.image.Image 094 */ 095 public Source(final String id, final IResource... imageResources) 096 { 097 super(id, null, imageResources); 098 } 099 100 /** 101 * Creates a source for a picture 102 * 103 * @param id 104 * the component id 105 * @param model 106 * the internally used model 107 * @see org.apache.wicket.Component#Component(String, IModel) 108 */ 109 public Source(final String id, final IModel<?> model) 110 { 111 super(id, model); 112 } 113 114 /** 115 * Creates a source for a picture 116 * 117 * @param id 118 * the component id 119 * @param string 120 * the string used as model 121 * @see org.apache.wicket.markup.html.image.Image 122 */ 123 public Source(final String id, final String string) 124 { 125 super(id, string); 126 } 127 128 @Override 129 protected void onComponentTag(ComponentTag tag) 130 { 131 checkComponentTag(tag, "source"); 132 super.onComponentTag(tag); 133 if (getMedia() != null) 134 { 135 tag.put("media", getMedia()); 136 } 137 } 138 139 /** 140 * Sets the media attribute information 141 * 142 * @param media 143 * the media attribute information 144 */ 145 public void setMedia(String media) 146 { 147 this.media = media; 148 } 149 150 /** 151 * Gets the media attribute information 152 * 153 * @return the media attribute information 154 */ 155 public String getMedia() 156 { 157 return media; 158 } 159 160 /** 161 * Unsupported for source tag 162 */ 163 @Override 164 public void setCrossOrigin(CrossOrigin crossorigin) 165 { 166 throw new UnsupportedOperationException( 167 "It is not allowed to set the crossorigin attribute for source tag"); 168 } 169 170 /** 171 * Unsupported for source tag 172 */ 173 @Override 174 public final CrossOrigin getCrossOrigin() 175 { 176 return null; 177 } 178}