001    // ===========================================================================
002    // Copyright (c) 1996 Mort Bay Consulting Pty. Ltd. All rights reserved.
003    // $Id: Image.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
004    // ---------------------------------------------------------------------------
005    
006    package org.objectweb.jac.aspects.gui.web.html;
007    
008    // Laurent Martelli <laurent@aopsys.com> - 21 mar. 2004:
009    // 
010    
011    import java.awt.Dimension;
012    import java.io.File;
013    import java.io.FileInputStream;
014    import java.io.IOException;
015    import org.mortbay.util.Code;
016    
017    /* ---------------------------------------------------------------- */
018    /** HTML Image Tag.
019     * @see org.mortbay.html.Block
020     * @version $Id: Image.java,v 1.1 2004/03/23 13:59:49 laurent Exp $
021     * @author Greg Wilkins
022    */
023    public class Image extends Tag
024    {
025        /* ------------------------------------------------------------ */
026        public Image(String src, String alt)
027        {
028            super("img");
029            attribute("src",src);
030            alt(alt);
031        }
032    
033        /* ------------------------------------------------------------ */
034        public Image(String src, String alt, Dimension size)
035        {
036            super("img");
037            attribute("src",src);
038            alt(alt);
039            if (size!=null) {
040                width(size.width);
041                height(size.height);
042            }
043        }
044    
045        /* ------------------------------------------------------------ */
046        /** Construct from GIF file.
047         */
048        /*
049        public Image(String dirname, String src)
050        {
051            super("img");
052            attribute("src",src);
053            setSizeFromGif(dirname,src);
054        }
055        */
056        
057        /* ------------------------------------------------------------ */
058        /** Construct from GIF file.
059         */
060        public Image(File gif)
061        {
062            super("img");
063            attribute("src",gif.getName());
064            setSizeFromGif(gif);
065        }
066    
067        /* ------------------------------------------------------------ */
068        /*    public Image(String src,int width, int height, int border)
069        {
070            this(src);
071            width(width);
072            height(height);
073            border(border);
074        }
075        */
076    
077        /* ------------------------------------------------------------ */
078        public Image border(int b)
079        {
080            attribute("border",b);
081            return this;
082        }
083        
084        /* ------------------------------------------------------------ */
085        public Image alt(String alt)
086        {
087            attribute("alt",alt);
088            return this;
089        }
090        
091        /* ------------------------------------------------------------ */
092        /** Set the image size from the header of a GIF file.
093         * @param dirname The directory name, expected to be in OS format
094         * @param pathname The image path name relative to the directory.
095         *                 Expected to be in WWW format (i.e. with slashes)
096         *                 and will be converted to OS format.
097         */
098        public Image setSizeFromGif(String dirname,
099                                    String pathname)
100        {
101            String filename =dirname + pathname.replace('/',File.separatorChar);
102            return setSizeFromGif(filename);
103        }
104        
105        /* ------------------------------------------------------------ */
106        /** Set the image size from the header of a GIF file.
107         */
108        public Image setSizeFromGif(String filename)
109        {
110            return setSizeFromGif(new File(filename));
111        }
112        
113        /* ------------------------------------------------------------ */
114        /** Set the image size from the header of a GIF file.
115         */
116        public Image setSizeFromGif(File gif)
117        {
118            if (gif.canRead())
119            {
120                try{
121                    byte [] buf = new byte[10];
122                    FileInputStream in = new FileInputStream(gif);
123                    if (in.read(buf,0,10)==10)
124                    {
125                        Code.debug("Image "+gif.getName()+
126                                   " is " +
127                                   ((0x00ff&buf[7])*256+(0x00ff&buf[6])) +
128                                   " x " +
129                                   (((0x00ff&buf[9])*256+(0x00ff&buf[8]))));
130                        width((0x00ff&buf[7])*256+(0x00ff&buf[6]));
131                        height(((0x00ff&buf[9])*256+(0x00ff&buf[8])));
132                    }
133                }
134                catch (IOException e){
135                    Code.ignore(e);
136                }
137            }
138            
139            return this;
140        }
141        
142    }