001: /*
002: * Copyright 2000,2005 wingS development team.
003: *
004: * This file is part of wingS (http://wingsframework.org).
005: *
006: * wingS is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * Please see COPYING for the complete licence.
012: */
013: package org.wings;
014:
015: import org.wings.resource.FileResource;
016: import org.wings.util.ImageInfo;
017:
018: import java.io.File;
019: import java.io.FileInputStream;
020: import java.io.FileNotFoundException;
021: import java.io.IOException;
022:
023: /**
024: * SIcon implementation that is externalized globally
025: * and is not bound to a session.
026: *
027: * @author <a href="mailto:haaf@mercatis.de">Armin Haaf</a>
028: */
029: public class SFileIcon extends FileResource implements SIcon {
030:
031: /**
032: * The width to display the icon. This overwrites the real width of the icon. Ignored, if <0
033: */
034: private int width = -1;
035:
036: /**
037: * The height to display the icon. This overwrites the real height of the icon. Ignored, if <0
038: */
039: private int height = -1;
040:
041: /**
042: * Title of icon, <code>""</code> if not set.
043: */
044: private String title = null;
045:
046: /**
047: * Create a new SFileIcon from the File. This constructor extracts
048: * the extension from the file to be appended to the externalized resource
049: * name.
050: */
051: public SFileIcon(String fileName) throws FileNotFoundException {
052: this (new File(fileName));
053: }
054:
055: /**
056: * crates a new SFileIcon from the given file. The extension and
057: * mimetype are taken from the parameters given.
058: *
059: * @param file the file to construct a SFileIcon from
060: * @param extension user provided extension. The original extension of
061: * the file is ignored, unless this paramter is
062: * 'null'.
063: * @param mimetype the user provided mimetype. If this is 'null', then
064: * the mimetype is guessed from the extension.
065: */
066: public SFileIcon(File file, String extension, String mimetype)
067: throws FileNotFoundException {
068: super (file, extension, mimetype);
069:
070: ImageInfo tImageInfo = new ImageInfo();
071: FileInputStream tImageInput = new FileInputStream(file);
072: tImageInfo.setInput(tImageInput);
073:
074: if (tImageInfo.check()) {
075: // if either of the extension or mimetype is missing, try to guess it.
076: if (this .mimeType == null || this .mimeType.length() == 0) {
077: this .mimeType = tImageInfo.getMimeType();
078: } else if (this .extension == null
079: || this .extension.length() == 0) {
080: this .extension = tImageInfo.getFormatName();
081: }
082:
083: width = tImageInfo.getWidth();
084: height = tImageInfo.getHeight();
085: }
086:
087: try {
088: tImageInput.close();
089: } catch (IOException ex) {
090: // ignore close exception, we don't need it anymore
091: }
092: }
093:
094: public SFileIcon(File file) throws FileNotFoundException {
095: this (file, null, null);
096: }
097:
098: public int getIconWidth() {
099: return width;
100: }
101:
102: public int getIconHeight() {
103: return height;
104: }
105:
106: public void setIconWidth(int width) {
107: this .width = width;
108: }
109:
110: public void setIconHeight(int height) {
111: this .height = height;
112: }
113:
114: public String getIconTitle() {
115: return (title != null) ? title : "";
116: }
117:
118: public void setIconTitle(String title) {
119: this.title = title;
120: }
121: }
|