KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > SFileIcon


1 /*
2  * $Id: SFileIcon.java,v 1.6 2005/06/07 12:55:58 neurolabs Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings;
15
16 import org.wings.resource.FileResource;
17 import org.wings.util.ImageInfo;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.io.FileNotFoundException JavaDoc;
22 import java.io.IOException JavaDoc;
23
24 /**
25  * An SIcon of this type is externalized globally. It is not bound
26  * to a session.
27  *
28  * @author <a HREF="mailto:haaf@mercatis.de">Armin Haaf</a>
29  * @version $Revision: 1.6 $
30  */

31 public class SFileIcon extends FileResource implements SIcon {
32
33     /**
34      * The width to display the icon. This overwrites the real width of the icon. Ignored, if &lt;0
35      */

36     private int width = -1;
37
38     /**
39      * The height to display the icon. This overwrites the real height of the icon. Ignored, if &lt;0
40      */

41     private int height = -1;
42
43     /**
44      * Title of icon, <code>""</code> if not set.
45      */

46     private String JavaDoc title = null;
47
48
49     /**
50      * Create a new SFileIcon from the File. This constructor extracts
51      * the extension from the file to be appended to the externalized resource
52      * name.
53      */

54     public SFileIcon(String JavaDoc fileName) throws FileNotFoundException JavaDoc {
55         this(new File JavaDoc(fileName));
56     }
57
58     /**
59      * crates a new SFileIcon from the given file. The extension and
60      * mimetype are taken from the parameters given.
61      *
62      * @param file the file to construct a SFileIcon from
63      * @param extension user provided extension. The original extension of
64      * the file is ignored, unless this paramter is
65      * 'null'.
66      * @param mimetype the user provided mimetype. If this is 'null', then
67      * the mimetype is guessed from the extension.
68      */

69     public SFileIcon(File JavaDoc file, String JavaDoc extension, String JavaDoc mimetype) throws FileNotFoundException JavaDoc {
70         super(file, extension, mimetype);
71
72         ImageInfo tImageInfo = new ImageInfo();
73         FileInputStream JavaDoc tImageInput = new FileInputStream JavaDoc(file);
74         tImageInfo.setInput(tImageInput);
75
76         if (tImageInfo.check()) {
77             // if either of the extension or mimetype is missing, try to guess it.
78
if (this.mimeType == null || this.mimeType.length() == 0) {
79                 this.mimeType = tImageInfo.getMimeType();
80             } else if (this.extension == null || this.extension.length() == 0) {
81                 this.extension = tImageInfo.getFormatName();
82             }
83
84             width = tImageInfo.getWidth();
85             height = tImageInfo.getHeight();
86         }
87
88         try {
89             tImageInput.close();
90         } catch (IOException JavaDoc ex) {
91             // ignore close exception, we don't need it anymore
92
}
93     }
94
95     public SFileIcon(File JavaDoc file) throws FileNotFoundException JavaDoc {
96         this(file, null, null);
97     }
98
99     public int getIconWidth() {
100         return width;
101     }
102
103     public int getIconHeight() {
104         return height;
105     }
106
107     public void setIconWidth(int width) {
108         this.width = width;
109     }
110
111     public void setIconHeight(int height) {
112         this.height = height;
113     }
114
115     public String JavaDoc getIconTitle() {
116         return (title!=null) ? title : "";
117     }
118     
119     public void setIconTitle(String JavaDoc title) {
120         this.title = title;
121     }
122 }
123
124
125
Popular Tags