001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.client.gui.file;
019:
020: import java.io.File;
021: import java.util.HashMap;
022: import java.util.Map;
023:
024: import javax.swing.ImageIcon;
025:
026: import de.finix.contelligent.client.i18n.Resources;
027:
028: public final class Utils {
029:
030: public final static String jpeg = "jpeg";
031:
032: public final static String jpg = "jpg";
033:
034: public final static String gif = "gif";
035:
036: public final static String tiff = "tiff";
037:
038: public final static String tif = "tif";
039:
040: public final static String png = "png";
041:
042: private static Map<String, ImageIcon> mimeTypeToIconMapping;
043:
044: public final static String[][] EXT_TO_MIME = {
045: { jpeg, "image/jpeg" }, { jpg, "image/jpeg" },
046: { gif, "image/gif" }, { tiff, "image/tiff" },
047: { tif, "image/tiff" }, { png, "image/png" },
048: { "bmp", "image/bmp" }, { "pdf", "application/pdf" },
049: { "doc", "application/msword" },
050: { "zip", "application/zip" },
051: { "xls", "application/vnd.ms-excel" },
052: { "ppt", "application/vnd.ms-powerpoint" },
053: { "swf", "application/x-shockwave-flash" },
054: { "mid", "audio/midi" }, { "mp3", "audio/mpeg" },
055: { "ram", "audio/x-pn-realaudio" },
056: { "rm", "audio/x-pn-realaudio" },
057: { "ra", "audio/x-realaudio" }, { "wav", "audio/x-wav" },
058: { "txt", "text/plain" }, { "rtf", "text/rtf" },
059: { "htm", "text/html" }, { "html", "text/html" },
060: { "mht", "message/rfc822" }, { "mpeg", "video/mpeg" },
061: { "mpe", "video/mpeg" }, { "mpg", "video/mpeg" },
062: { "qt", "video/quicktime" }, { "mov", "video/quicktime" },
063: { "avi", "video/x-msvideo" }, };
064:
065: public final static String getMimeForExtension(String ext) {
066: for (int i = 0; i < EXT_TO_MIME.length; i++) {
067: if (EXT_TO_MIME[i][0].equals(ext))
068: return EXT_TO_MIME[i][1];
069: }
070: return "";
071: }
072:
073: public final static ImageIcon getIconForMimeType(String mimetype) {
074: if (mimeTypeToIconMapping == null) {
075: mimeTypeToIconMapping = new HashMap<String, ImageIcon>();
076: mimeTypeToIconMapping.put("application/pdf",
077: Resources.pdfIcon);
078: mimeTypeToIconMapping.put("application/postscript",
079: Resources.pdfIcon);
080: mimeTypeToIconMapping.put("image/jpeg", Resources.jpegIcon);
081: mimeTypeToIconMapping.put("image/pdf", Resources.pdfIcon);
082: mimeTypeToIconMapping.put("image/gif", Resources.gifIcon);
083: mimeTypeToIconMapping.put("image/tiff", Resources.tiffIcon);
084: mimeTypeToIconMapping.put("message/rfc822",
085: Resources.htmlIcon);
086: mimeTypeToIconMapping.put("text/html", Resources.htmlIcon);
087: mimeTypeToIconMapping.put("text/plain", Resources.textIcon);
088: mimeTypeToIconMapping.put("text/richtext",
089: Resources.textIcon);
090: mimeTypeToIconMapping.put("image/x-shockwave-flash",
091: Resources.flashIcon);
092: mimeTypeToIconMapping.put("video/x-msvideo",
093: Resources.msmediaIcon);
094: mimeTypeToIconMapping.put("video/quicktime",
095: Resources.quicktimeIcon);
096: }
097: if (mimeTypeToIconMapping.containsKey(mimetype)) {
098: return (ImageIcon) mimeTypeToIconMapping.get(mimetype);
099: } else {
100: return Resources.fileIcon;
101: }
102: }
103:
104: public final static String[] BASIC_MIME_TYPES = {
105: // Put application/octet-stream first, so unknown extensions get mapped to
106: // that
107: "application/octet-stream", "application/msword",
108: "application/pdf", "application/pgp",
109: "application/postscript", "application/rtf",
110: "application/vnd.ms-excel",
111: "application/vnd.ms-powerpoint",
112: "application/x-shockwave-flash", "application/zip",
113: "audio/midi", "audio/mpeg", "audio/basic",
114: "audio/x-pn-realaudio", "audio/x-realaudio", "audio/x-wav",
115: "image/bmp", "image/cgm", "image/gif", "image/jpeg",
116: "image/png", "image/tiff", "message/rfc822",
117: "text/enriched", "text/html", "text/plain",
118: "text/richtext", "text/tab-separated-values", "text/rtf",
119: "video/mpeg", "video/quicktime", "video/x-msvideo" };
120:
121: public final static String[] BASIC_IMAGE_MIME_TYPES = {
122: "image/jpeg", "image/gif", "image/tiff", "image/cgm",
123: "image/png" };
124:
125: /*
126: * Get the extension of a file.
127: */
128: public static String getExtension(File f) {
129: String ext = null;
130: String s = f.getName();
131: int i = s.lastIndexOf('.');
132:
133: if (i > 0 && i < s.length() - 1) {
134: ext = s.substring(i + 1).toLowerCase();
135: }
136: return ext;
137: }
138: }
|