📄 iconmapper.java
字号:
/*** $Id: IconMapper.java,v 1.4 2001/05/07 12:37:22 kunugi Exp $**** Copyright (c) 2000-2001 Jeff Gay** on behalf of ICEMail.org <http://www.icemail.org>** Copyright (c) 1998-2000 by Timothy Gerard Endres** ** This program is free software.** ** You may redistribute it and/or modify it under the terms of the GNU** General Public License as published by the Free Software Foundation.** Version 2 of the license should be included with this distribution in** the file LICENSE, as well as License.html. If the license is not** included with this distribution, you may find a copy at the FSF web** site at 'www.gnu.org' or 'www.fsf.org', or you may write to the** Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139 USA.**** THIS SOFTWARE IS PROVIDED AS-IS WITHOUT WARRANTY OF ANY KIND,** NOT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY. THE AUTHOR** OF THIS SOFTWARE, ASSUMES _NO_ RESPONSIBILITY FOR ANY** CONSEQUENCE RESULTING FROM THE USE, MODIFICATION, OR** REDISTRIBUTION OF THIS SOFTWARE. ** */package org.icemail.mail;import java.awt.Image;import java.io.IOException;import java.util.Hashtable;import javax.activation.CommandObject;import javax.activation.DataHandler;import javax.activation.MimeType;import javax.activation.MimeTypeParseException;import javax.swing.ImageIcon;import org.icemail.Package;import org.icemail.util.AWTUtilities;/** * Class IconMapper implements a CommandObject for the Java Activation Framework. * IconMapper tries to become (load) the appropriate icon of the underlying datahandler. * <p> * The only verb supported is "view", becoming the appropriate icon for display. * And as you've already guessed, allows the mapper to be displayed. * * @see QuickViewer.getMailcapIcon() */public class IconMapper extends ImageIcon implements CommandObject{ private static final int Debug_ = Package.DEBUG ? Package.getLevel( "IconMapper" ) : 0; private static Hashtable ImageCache_ = new Hashtable(); private static String[] Paths_ = { "/", "/images/mime/", "/org/icemail/mail/images/mime/", }; /** * Default constructor. */ public IconMapper() { super(); } /** * Initialize the command object from the data handler as described by the verb. * <p> * Implements CommandObject.setCommandContext(). * <p> * @param verb the command from the activation framework * @param dh the datahandler containing the data contents and type * @see javax.activation.CommandObject */ public void setCommandContext( String verb, DataHandler dh ) throws IOException { setIconImage( dh.getContentType() ); } /** * Set the icon (IconMapper) appropriately by the given contentType. * * @param contentType the content type of the underlying data handler */ private void setIconImage( String contentType ) { if ( Package.DEBUG && Package.isTraceable( "IconMapper" ) ) { System.out.println( "IconMapper.setIconImage(s): " + contentType ); } Image iconImg = null; if ( contentType != null ) try { MimeType mime = new MimeType( contentType ); iconImg = (Image)IconMapper.ImageCache_.get( mime.getBaseType() ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Cached? '" + mime.getBaseType() + "' = '" + (iconImg==null?"NO":"YES") ); } if ( iconImg == null ) { iconImg = this.locateIconImage( this.getBaseName( mime ) ); if ( iconImg != null ) IconMapper.ImageCache_.put( mime.getBaseType(), iconImg ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Located '" + mime.getBaseType() + "' = '" + iconImg ); } } if ( iconImg == null ) { iconImg = (Image)IconMapper.ImageCache_.get( mime.getPrimaryType() ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Cached? '" + mime.getPrimaryType() + "' = '" + (iconImg==null?"NO":"YES") ); } } if ( iconImg == null ) { iconImg = this.locateIconImage( this.getPrimaryName( mime ) ); if ( iconImg != null ) IconMapper.ImageCache_.put( mime.getPrimaryType(), iconImg ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Located '" + mime.getPrimaryType() + "' = '" + iconImg ); } } if ( iconImg == null ) { String unknownType = "unknown/unknown"; String unknownName = "unknown.unknown.gif"; iconImg = (Image)IconMapper.ImageCache_.get( unknownType ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Cached? '" + unknownType + "' = '" + (iconImg==null?"NO":"YES") ); } if ( iconImg == null ) { iconImg = this.locateIconImage( unknownName ); if ( iconImg != null ) IconMapper.ImageCache_.put( unknownType, iconImg ); if ( Package.DEBUG && Debug_ > 0 ) { System.err.println( "IconMapper: Located '" + unknownType + "' = '" + iconImg ); } } if ( iconImg != null ) { IconMapper.ImageCache_.put( mime.getBaseType(), iconImg ); } } } catch ( MimeTypeParseException ex ) { } this.setImage( iconImg ); } private Image locateIconImage( String iconName ) { Image result = null; StringBuffer buf = new StringBuffer(); for ( int pi = 0; result == null && pi < IconMapper.Paths_.length; ++pi ) { try { buf.setLength(0); buf.append( IconMapper.Paths_[ pi ] ); buf.append( iconName ); result = AWTUtilities.getImageResource( buf.toString() ); } catch ( IOException ex ) { } } return result; } private String getPrimaryName( MimeType mime ) { StringBuffer buf = new StringBuffer(); buf.append( mime.getPrimaryType() ); buf.append( ".gif" ); return buf.toString(); } private String getBaseName( MimeType mime ) { StringBuffer buf = new StringBuffer(); buf.append( mime.getPrimaryType() ); buf.append( "." ); buf.append( mime.getSubType() ); buf.append( ".gif" ); return buf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -