/* dIntProg Browser. A webbrowser written in Java.
 * Copyright (C) 2001 Martin Geisler <gimpster@gimpster.com>
 *  
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the
 * 
 * Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

import java.net.*;
import java.awt.*;
import javax.swing.*;

/** An image. */
public class ImageBox extends AbstractRigidBox {

    private JComponent component;
    private Image img;
    private URL url;

    /** Constructs an ImageBox.
     *  @param img_url the URL of the image. The image isn't
     *  downloaded until {@link #doLayout(Graphics, JComponent, int)}
     *  is called.
     *  @param link_url the URL which this image links too. If the
     *  image doesn't link to anywhere, <code>link_url</code> should
     *  be <code>null</code>. */
    public ImageBox(URL img_url, URL link_url) {
	img = Toolkit.getDefaultToolkit().getImage(img_url);
        url = link_url;
    }

    /** Calculates the width and height of the image. The image is
     *  downloaded and then meassured. */
    public void doLayout(Graphics g, JComponent c, int w) {
        pen = g;
        component = c;
	MediaTracker tracker = new MediaTracker(component);
	tracker.addImage(img, 0);
	try {
	    tracker.waitForAll();
	} catch (InterruptedException e) { }
        /* We set the width and height. Since an image can't be
         * resized, we set the min_width and pref_width too. */
	width = min_width = pref_width = img.getWidth(component);
	height = img.getHeight(component);
    }

    public void drawAt(int x, int y, DocumentView v) {
        if (url != null) {
            v.drawImage(x, y, img, new HyperLink(x, y, width, height, url));
        } else {
            v.drawImage(x, y, img, null);
        }
    }

}