jp.xet.uncommons.wicket.gp.SizedImage.java Source code

Java tutorial

Introduction

Here is the source code for jp.xet.uncommons.wicket.gp.SizedImage.java

Source

/*
 * Copyright 2011 Daisuke Miyamoto.
 * Created on 2011/11/25
 *
 * Licensed 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 jp.xet.uncommons.wicket.gp;

import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.image.Image;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import org.apache.wicket.request.resource.IResource;
import org.apache.wicket.request.resource.ResourceReference;
import org.apache.wicket.util.lang.Args;
import org.apache.wicket.util.string.Strings;

/**
 * TODO for daisuke
 * 
 * @since 1.0
 * @version $Id$
 * @author daisuke
 */
@SuppressWarnings("serial")
public class SizedImage extends Image {

    private final int width;

    private final int height;

    private final String alt;

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param imageResource The image resource
     * @param width 
     * @param height ?
     * @throws IllegalArgumentException {@code width}???{@code height}????
     */
    public SizedImage(String id, IResource imageResource, int width, int height) {
        this(id, imageResource, width, height, null);
    }

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param imageResource The image resource
     * @param width 
     * @param height ?
     * @param alt alt
     */
    public SizedImage(String id, IResource imageResource, int width, int height, String alt) {
        super(id, imageResource);
        Args.isTrue(width >= 0, "width must not to be negative: %d", width);
        Args.isTrue(height >= 0, "height must not to be negative: %d", height);
        this.width = width;
        this.height = height;
        this.alt = alt;
    }

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param resourceReference The image resource reference
     * @param width 
     * @param height ?
     */
    public SizedImage(String id, ResourceReference resourceReference, int width, int height) {
        this(id, resourceReference, width, height, null);
    }

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param resourceReference The image resource reference
     * @param width 
     * @param height ?
     * @param alt alt
     */
    public SizedImage(String id, ResourceReference resourceReference, int width, int height, String alt) {
        super(id, resourceReference);
        Args.isTrue(width >= 0, "width must not to be negative: %d", width);
        Args.isTrue(height >= 0, "height must not to be negative: %d", height);
        this.width = width;
        this.height = height;
        this.alt = alt;
    }

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param resourceReference The image resource reference
     * @param resourceParameters The resource parameters
     * @param width 
     * @param height ?
     */
    public SizedImage(String id, ResourceReference resourceReference, PageParameters resourceParameters, int width,
            int height) {
        this(id, resourceReference, resourceParameters, width, height, null);
    }

    /**
     * ??
     * 
     * @param id The non-null id of this component
     * @param resourceReference The image resource reference
     * @param resourceParameters The resource parameters
     * @param width 
     * @param height ?
     * @param alt alt
     */
    public SizedImage(String id, ResourceReference resourceReference, PageParameters resourceParameters, int width,
            int height, String alt) {
        super(id, resourceReference, resourceParameters);
        Args.isTrue(width >= 0, "width must not to be negative: %d", width);
        Args.isTrue(height >= 0, "height must not to be negative: %d", height);
        this.width = width;
        this.height = height;
        this.alt = alt;
    }

    @Override
    protected void onComponentTag(ComponentTag tag) {
        super.onComponentTag(tag);

        tag.put("width", width);
        tag.put("height", height);
        if (Strings.isEmpty(alt) == false) {
            CharSequence escapedAlt = Strings.escapeMarkup(alt);
            tag.put("alt", escapedAlt);
            tag.put("title", escapedAlt);
        }
    }
}