📄 editorviewer.java
字号:
/*** $Id: EditorViewer.java,v 1.3 2001/05/07 12:37:21 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.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.IOException;import javax.mail.internet.ContentType;import javax.mail.internet.ParseException;import javax.mail.internet.MimeUtility;import javax.activation.DataHandler;import javax.activation.CommandObject;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.util.Enumeration;import javax.swing.JPanel;import javax.swing.JTextPane;import javax.swing.JEditorPane;import javax.swing.JScrollPane;import javax.swing.text.Document;import javax.swing.text.StyledDocument;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.Style;import javax.swing.text.EditorKit;import javax.swing.text.BadLocationException;/** * Class EditorViewer implements a CommandObject for the Java Activation Framework. * The only verb supported is "view", loading the contents of the data into * an EditorPane for display. And as you've already guessed, allows the data to * be viewed by activating the pane. */public class EditorViewer extends JPanel implements CommandObject{ JScrollPane scroller = null; JEditorPane editorPane = null; /** * Construct an empty viewer, which can be loaded later. */ public EditorViewer() { this( null, null ); } /** * Construct a viewer for the given content and type. * <p> * @param content data contents, possibly character encoded * @param contentType MIME formatted Content-Type header string */ public EditorViewer( InputStream content, String contentType ) { this.setDoubleBuffered( true ); this.setBackground( Color.white ); this.setLayout( new BorderLayout() ); this.scroller = new JScrollPane(); this.scroller.setPreferredSize( new Dimension( 480, 320 ) ); this.add( this.scroller ); // now display our message if ( content != null && contentType != null ) { this.setMessage( content, contentType ); } } /** * Sets the contents of the viewer from the given content and type. * <p> * There are two distinct ways to read the data, from a text reader * for documents of type text/plain, and directly from a stream for * others. (Don't ask me why! I belive it to be another GOTCHA from SUN * because the Editor Kits should determin wether to make a text reader * based on the content type set! OH HUM...) * <p> * One other point is that the contentType passed is MIME formatted and * can contain a charset parameter which is set to a MIME value. The charset * needs to be converted to a JAVA value before the EditorPane can use it. * <p> * @param content data contents, possibly character encoded * @param contentType MIME formatted Content-Type header string */ public void setMessage( InputStream content, String contentType ) { // remove any other components this.scroller.getViewport().removeAll(); // check the content type and convert to JAVA charset ContentType xct = new ContentType(); try { xct = new ContentType( contentType ); } catch ( ParseException ex ) { // assume text/plain xct.setPrimaryType( "text" ); xct.setSubType( "plain" ); } String xjcharset = "ISO-8859-1"; // US-ASCII in MIME terms if ( xct.getParameter( "charset" ) != null ) { xjcharset = MimeUtility.javaCharset( xct.getParameter( "charset" ) ); } xct.setParameter( "charset", xjcharset ); // determine the editor based on content type this.editorPane = new JEditorPane(); this.editorPane.setEditable( false ); this.editorPane.setContentType( xct.toString().toLowerCase() ); // set the contents of the editor try { EditorKit xeditor = this.editorPane.getEditorKit(); Document xdoc = xeditor.createDefaultDocument(); if ( xct.match( "text/plain" ) ) { BufferedReader xreader = new BufferedReader( new InputStreamReader( content, xjcharset ) ); xeditor.read( xreader, xdoc, 0 ); } else { xeditor.read( content, xdoc, 0 ); } this.editorPane.setDocument( xdoc ); } catch ( IOException ex ) { ex.printStackTrace( System.err ); } catch ( BadLocationException ex ) { ex.printStackTrace( System.err ); } this.scroller.getViewport().add( this.editorPane ); this.invalidate(); this.validate(); } /** * 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 { String contentType = dh.getContentType(); InputStream content = dh.getInputStream(); this.setMessage( content, contentType ); content.close(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -