📄 quickviewer.java
字号:
/*** $Id: QuickViewer.java,v 1.10 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.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.FileDialog;import java.awt.Frame;import java.awt.Image;import java.awt.Insets;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.MouseAdapter;import java.awt.event.MouseEvent;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.InputStream;import java.io.IOException;import java.io.PrintWriter;import java.net.URL;import java.text.DecimalFormat;import java.text.SimpleDateFormat;import java.util.Enumeration;import java.util.Date;import javax.mail.Address;import javax.mail.Folder;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Part;import javax.mail.internet.ContentType;import javax.mail.internet.InternetHeaders;import javax.mail.internet.MimeMessage;import javax.mail.internet.ParseException;import javax.activation.CommandInfo;import javax.activation.DataHandler;import javax.activation.MimeType;import javax.activation.MimeTypeParseException;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.BoxLayout;import javax.swing.ImageIcon;import javax.swing.JButton;import javax.swing.JDialog;import javax.swing.JEditorPane;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTextArea;import javax.swing.JTextPane;import javax.swing.SwingUtilities;import javax.swing.event.HyperlinkEvent;import javax.swing.event.HyperlinkListener;import javax.swing.undo.UndoManager;import javax.swing.text.BadLocationException;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.Document;import javax.swing.text.EditorKit;import javax.swing.text.StyledDocument;import javax.swing.border.EmptyBorder;import org.icemail.Package;import org.icemail.smime.SMIMELibrary;import org.icemail.smime.MissingCertificateException;import org.icemail.smime.PasswordCancelException;import org.icemail.util.AWTUtilities;import org.icemail.util.ComponentFactory;import org.icemail.util.UserProperties;/** * QuickViewer allows the contents of a message to be viewed. * The message contents are determined, displaying the headers and body in * separate panes. If there are attachments (multi-part messages) then an * additional pane is added which contains a button for each part. * * FIX ME; at this time, QuickViewer is closely tied to MainPanel due to * the interworkings of buttons and actions. These need to be separate in * future versions. */public class QuickViewer extends JPanel implements ActionDistributor{ private static final int Debug_ = Package.DEBUG ? Package.getLevel( "QuickViewer" ) : 0; private static ImageIcon AttUnknownIcon = null; private boolean showFullHeaders_ = false; private boolean parseSMimeMessages_ = true; private int maxBodyPartSize_ = (48 * 1024); private JPanel attachPane_ = null; private JEditorPane editorPane_ = null; private JPanel theAttachPane_ = null; private UndoManager undo_ = null; private Message outerMessage_ = null; private Part message_ = null; private JScrollPane editorScroller_ = null; private JScrollPane attachScroller_ = null; private JSplitPane bodySplit_ = null; private int bodyDivLoc_ = 5; private Component body_ = null; private Part popupAttachPart_ = null; private JPopupMenu popupMenu_ = null; private JTextArea hdrTextArea_ = null; private JScrollPane hdrScroller_ = null; private JSplitPane hdrSplit_ = null; private Action signedAction_ = null; private Action envelopedAction_ = null; private IActionListener actionListener_ = new IActionListener(); static { try { Image iconImg = AWTUtilities.getImageResource( "/org/icemail/mail/images/attunknown.gif" ); QuickViewer.AttUnknownIcon = new ImageIcon( iconImg ); } catch ( Exception ex ) { System.err.println( "QuickViewer.static: Error " + "loading icon: " + ex.getMessage() ); } } public QuickViewer() { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer()" ); } parseSMimeMessages_ = ( ICEMail.getSMIMELibrary() != null ); setLayout( new BorderLayout() ); int row = 0; hdrTextArea_ = new JTextArea( "" ); hdrTextArea_.setBorder( new EmptyBorder( 1, 3, 1, 3 ) ); hdrTextArea_.setEditable( false ); hdrScroller_ = new JScrollPane(); hdrScroller_.getViewport().add( hdrTextArea_ ); hdrScroller_.setMinimumSize( new Dimension(50, 15) ); hdrScroller_.setPreferredSize( new Dimension(600, 78) ); editorScroller_ = new JScrollPane(); editorScroller_.getViewport().add( new JTextArea() ); editorScroller_.setMinimumSize( new Dimension(50, 75) ); editorScroller_.setPreferredSize( new Dimension(400, 400) ); theAttachPane_ = new JPanel(); theAttachPane_.setLayout( new BoxLayout( theAttachPane_, BoxLayout.Y_AXIS ) ); attachScroller_ = new JScrollPane(); attachScroller_.getViewport().add( theAttachPane_ ); attachScroller_.setMinimumSize( new Dimension(5, 50) ); bodySplit_ = new JSplitPane( JSplitPane.HORIZONTAL_SPLIT, editorScroller_, attachScroller_ ); bodySplit_.setMinimumSize( new Dimension(50, 50) ); bodySplit_.setContinuousLayout( true ); bodySplit_.setDividerSize( 3 ); bodySplit_.setDividerLocation( bodyDivLoc_ ); hdrSplit_ = new JSplitPane( JSplitPane.VERTICAL_SPLIT, hdrScroller_, editorScroller_ ); hdrSplit_.setMinimumSize( new Dimension(50, 50) ); hdrSplit_.setContinuousLayout( true ); hdrSplit_.setDividerSize( 3 ); add( hdrSplit_, BorderLayout.CENTER ); popupMenu_ = ComponentFactory.getPopupMenuAndItems( ICEMail.getBundle(), "QuickViewer.attachment", actionListener_ ); maxBodyPartSize_ = UserProperties.getProperty( "maxBodyPartSize", (128 * 1024) ); Image img = null; try { img = AWTUtilities.getImageResource( "/org/icemail/mail/images/signed.gif" ); } catch ( IOException ex ) { img = null; } signedAction_ = new AbstractAction() { public void actionPerformed( ActionEvent event ) { SwingUtilities.invokeLater( new Runnable() { public void run() { showSigners(); } } ); } }; signedAction_.setEnabled( false ); signedAction_.putValue( Action.SMALL_ICON, new ImageIcon( img ) ); try { img = AWTUtilities.getImageResource( "/org/icemail/mail/images/enveloped.gif" ); } catch ( IOException ex ) { img = null; } envelopedAction_ = new AbstractAction() { public void actionPerformed( ActionEvent event ) { SwingUtilities.invokeLater( new Runnable() { public void run() { showEnvelope(); } } ); } }; envelopedAction_.setEnabled( false ); envelopedAction_.putValue( Action.SMALL_ICON, new ImageIcon( img ) ); setMinimumSize( new Dimension( 50, 50 ) ); setPreferredSize( new Dimension( 600, 250 ) ); }//............................................................// Accessors public Action getSignedAction() { return signedAction_; } public Action getEnvelopedAction() { return envelopedAction_; } public String getTextSelection() { String result = ""; if ( editorPane_ != null ) { result = editorPane_.getSelectedText(); } return result; }//............................................................// Mutators public void setFullHeaders( boolean full ) { showFullHeaders_ = full; getHeaderComponent(); hdrScroller_.revalidate(); } public void setParseSMimeMessages( boolean parse ) { parseSMimeMessages_ = parse; setMessage( outerMessage_ ); } /** * sets the current message to be displayed in the viewer */ public void setMessage( Message msg ) { if ( Package.DEBUG && Package.isTraceable( "QuickViewer" ) ) { System.out.println( "QuickViewer.setMessage(m)" ); } // The outerMessage ONLY differs from the message when the // message being displayed is signed or encrypted. In this // case, the outerMessage is the PKCS SMIME message, while // message is the inner "wrapped MIME" message outerMessage_ = msg; message_ = outerMessage_; int divLoc = 50; int hdrDivLoc = hdrSplit_.getDividerLocation(); try { if ( outerMessage_ != null ) { Folder f = outerMessage_.getFolder(); MailUtilities.setFolderOpenAndReady( f, Folder.READ_WRITE ); } } catch ( MessagingException ex ) { // UNDONE what do upon failure??? } boolean wasSplit = attachPane_ != null; if ( wasSplit ) { bodyDivLoc_ = bodySplit_.getDividerLocation(); } undo_ = new UndoManager(); editorPane_ = null; attachPane_ = null; getHeaderComponent(); if ( parseSMimeMessages_ && outerMessage_ instanceof MimeMessage ) { ICEMail.getSMIMELibrary().clearState(); try { if ( ICEMail.getSMIMELibrary().isSMimeMessage( outerMessage_ ) ) { message_ = ICEMail.getSMIMELibrary().getContentMessage( ICEMail.getDefaultSession(), outerMessage_ ); } } catch ( MissingCertificateException ex ) { ComponentFactory.showDialog( ICEMail.getBundle(), "QuickViewer.SMimeError", 0, JOptionPane.WARNING_MESSAGE, null ); message_ = outerMessage_; } catch ( PasswordCancelException ex ) { ComponentFactory.showDialog( ICEMail.getBundle(), "QuickViewer.SMimeError", 0, JOptionPane.WARNING_MESSAGE, null ); message_ = outerMessage_; } catch ( MessagingException ex ) { message_ = outerMessage_; } signedAction_.setEnabled( ICEMail.getSMIMELibrary().isSigned( message_ ) ); envelopedAction_.setEnabled( ICEMail.getSMIMELibrary().isEnveloped( message_ ) ); } else { signedAction_.setEnabled( false ); envelopedAction_.setEnabled( false ); } parseMessagePart( message_ ); if ( editorPane_ == null ) { editorPane_ = new JTextPane( new DefaultStyledDocument() ); } editorScroller_.getViewport().removeAll(); editorScroller_.getViewport().add( editorPane_ ); editorScroller_.revalidate(); if ( attachPane_ == null ) { body_ = editorScroller_; } else { bodySplit_.setLeftComponent( editorScroller_ ); bodySplit_.setRightComponent( attachScroller_ ); bodySplit_.invalidate(); bodySplit_.validate(); body_ = bodySplit_; } editorPane_.setCaretPosition( 0 ); editorPane_.moveCaretPosition( 0 ); hdrScroller_.getViewport().removeAll(); hdrScroller_.getViewport().add( hdrTextArea_ ); hdrScroller_.revalidate(); hdrSplit_.setBottomComponent( body_ ); hdrSplit_.setDividerLocation( hdrDivLoc ); hdrSplit_.invalidate(); hdrSplit_.validate(); if ( attachPane_ != null ) { bodySplit_.setDividerLocation( bodyDivLoc_ ); attachPane_.invalidate();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -