📄 composeframe.java
字号:
/*** $Id: ComposeFrame.java,v 1.13 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.awt.Color;import java.awt.Container;import java.awt.Dimension;import java.awt.Event;import java.awt.FileDialog;import java.awt.GridBagLayout;import java.awt.GridBagConstraints;import java.awt.Image;import java.awt.Point;import java.awt.Rectangle;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.KeyEvent;import java.awt.event.MouseEvent;import java.awt.event.MouseAdapter;import java.awt.event.WindowEvent;import java.awt.event.WindowAdapter;import java.io.BufferedReader;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileReader;import java.io.FileOutputStream;import java.io.IOException;import java.io.PrintWriter;import java.io.StringReader;import java.io.StringWriter;import java.text.FieldPosition;import java.text.SimpleDateFormat;import java.util.Date;import java.util.Hashtable;import java.util.Enumeration;import java.util.NoSuchElementException;import java.util.MissingResourceException;import java.util.StringTokenizer;import java.util.Vector;import javax.activation.FileTypeMap;import javax.mail.Address;import javax.mail.Folder;import javax.mail.Header;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Part;import javax.mail.Store;import javax.mail.internet.InternetAddress;import javax.mail.internet.InternetHeaders;import javax.mail.internet.MimeMessage;import javax.mail.internet.MimeMultipart;import javax.swing.AbstractAction;import javax.swing.Action;import javax.swing.ImageIcon;import javax.swing.KeyStroke;import javax.swing.JButton;import javax.swing.JCheckBox;import javax.swing.JComboBox;import javax.swing.JDialog;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JMenu;import javax.swing.JMenuItem;import javax.swing.JMenuBar;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JPopupMenu;import javax.swing.JScrollPane;import javax.swing.JTextPane;import javax.swing.JTextField;import javax.swing.JToolBar;import javax.swing.SwingUtilities;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.UndoableEditEvent;import javax.swing.event.UndoableEditListener;import javax.swing.text.Document;import javax.swing.text.DefaultStyledDocument;import javax.swing.text.JTextComponent;import javax.swing.undo.UndoManager;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.FormatResourceBundle;import org.icemail.util.JFCUtilities;import org.icemail.util.RotateButton;import org.icemail.util.StringUtilities;import org.icemail.util.UserProperties;/** * This class composes and edits messages, * allowing messages to be sent, printed, and saved. * <p> * A message can contain: * <pre> * -sender and receipient addresses * -attachments * -extra headers * </pre> * <p> * Additionally, messages can be encrypted or signed when * the sMIME library is available. * <p> * At this time, JFrame is known to leak memory, so all ComposeFrames * are managed, and re-used. * * @see ComposeMgr */public class ComposeFrame extends JFrame{ private static final int Debug_ = Package.DEBUG ? Package.getLevel( "ComposeFrame" ) : 0;// state of mail editing public static final int CREATING = 0; public static final int EDITING = 1; public static final int REPLYING = 2; public static final int REPLYINGTOALL = 3; public static final int FORWARDING = 4; private static int[] AccKeys = { KeyEvent.VK_1, KeyEvent.VK_2, KeyEvent.VK_3, KeyEvent.VK_4, KeyEvent.VK_5, KeyEvent.VK_6, KeyEvent.VK_7, KeyEvent.VK_8, KeyEvent.VK_9, KeyEvent.VK_0, }; private static String LastAttachDir = null; private int state_ = ComposeFrame.CREATING; private Message originalMessage_ = null; private boolean modified_ = false; private String signature_; /** * The headers preserved from the original message in a forwarding. */ private InternetHeaders headers_; /** * The extra headers configured by the composer. */ private InternetHeaders extraHeaders_; /* * controls to maintain choice of character set */ private Vector charsets_; private JPopupMenu charsetMenu_; private JLabel charsetLbl_; private JMenuBar menuBar_; private JTextField fromText_; private JTextField toText_; private JTextField ccText_; private JTextField bccText_; private JTextField subjectText_; private JTextPane messageText_; private UndoManager messageUndo_; private JButton sendButton_; private JButton printButton_; private JButton attachButton_; private JButton detachButton_; private RotateButton priorityButton_; private RotateButton encryptButton_; private RotateButton signButton_; private JComboBox attachComboBox_; private Vector attachments_; private int textAttachmentSequence_ = 0; private IActionListener actionListener_ = new IActionListener(); private IFocusListener focusListener_ = new IFocusListener(); private JTextComponent currentFocus_ = null; /** * Default constructor, creating a composition frame for a new message. */ public ComposeFrame() { super(); if ( Package.DEBUG && Package.isTraceable( "ComposeFrame" ) ) { System.out.println( "ComposeFrame()" ); } setDefaultCloseOperation( DO_NOTHING_ON_CLOSE ); signature_ = null; headers_ = null; extraHeaders_ = null; attachments_ = null; currentFocus_ = null; charsets_ = Configuration.getCharsets(); establishMenuBar(); establishContents(); addWindowListener( new IWindowAdapter() ); pack(); charsetMenu_.pack(); // // FIXME try to select input method of selected locale // Removed until we decide to only support Java 2. // // getInputContext().selectInputMethod( locale ); // loadLayoutProperties(); } /** * Dispose of the window and release resources. * <p> * Implementation of java.awt.Window.dispose() */ public void dispose( boolean automatically ) { if ( Package.DEBUG && Package.isTraceable( "ComposeFrame" ) ) { System.out.println( "ComposeFrame.dispose()" ); } if ( automatically ) { dismissWindow(); } else { closeWindow(); } } /** * Edit the given message using the composition frame. * * @param state the state of the composition * @param msg the message to edit * @param body the optionally selected body of the message */ public void editMessage( int state, Message msg, String body ) { if ( Package.DEBUG && Package.isTraceable( "ComposeFrame" ) ) { System.out.println( "ComposeFrame.editMessage(i,m,s): " + state ); } setEnabled( true ); state_ = state; originalMessage_ = msg; textAttachmentSequence_ = 0; signature_ = null; headers_ = new InternetHeaders(); extraHeaders_ = new InternetHeaders(); attachments_ = new Vector(); currentFocus_ = null; // title the frame accordingly String rsrcName = "Compose.title.creating"; switch ( state_ ) { case EDITING: rsrcName = "Compose.title.editing"; break; case REPLYING: rsrcName = "Compose.title.replying"; break; case REPLYINGTOALL: rsrcName = "Compose.title.replyingtoall"; break; case FORWARDING: rsrcName = "Compose.title.forwarding"; break; case CREATING: default: break; } setTitle( ICEMail.getBundle().getString( rsrcName ) ); // clear previous controls if ( ICEMail.getSMIMELibrary() != null ) { priorityButton_.rotate( 0 ); encryptButton_.rotate( 0 ); signButton_.rotate( 0 ); } setMessage( body ); if ( state_ == REPLYING || state_ == REPLYINGTOALL ) { int end = messageText_.getDocument().getEndPosition().getOffset(); messageText_.setCaretPosition( end - 1 ); messageText_.moveCaretPosition( end - 1 ); } else { messageText_.setCaretPosition( 0 ); messageText_.moveCaretPosition( 0 ); } modified_ = false; setVisible( true ); toFront(); if ( state_ == CREATING || state_ == FORWARDING ) { toText_.requestFocus(); } else { messageText_.requestFocus(); } }//............................................................// PRIVATE METHODS private void closeWindow() { if ( Package.DEBUG && Package.isTraceable( "ComposeFrame" ) ) { System.out.println( "ComposeFrame.closeWindow()" ); } boolean xdismiss = true; String to = toText_.getText().trim(); String cc = ccText_.getText().trim(); String bcc = bccText_.getText().trim(); String subject = subjectText_.getText().trim(); if ( to.length() > 0 || cc.length() > 0 || bcc.length() > 0 || subject.length() > 0 ) { modified_ = true; } if ( modified_ ) { int option = ComponentFactory.showDialog( ICEMail.getBundle(), "Compose.Save", JOptionPane.YES_NO_CANCEL_OPTION, JOptionPane.QUESTION_MESSAGE, null ); if ( option == JOptionPane.YES_OPTION ) { try { MimeMessage xmessage = buildMessage(); buildHeader( xmessage ); xmessage.saveChanges(); saveComposition( xmessage ); } catch ( MessagingException xex ) { xdismiss = false; ByteArrayOutputStream xbaos = new ByteArrayOutputStream(); xex.printStackTrace( new PrintWriter( xbaos ) ); Object[] xargs = new Object[2]; xargs[0] = xex.getMessage(); xargs[1] = xbaos.toString(); ComponentFactory.showDialog( ICEMail.getBundle(), "Compose.SavingException", 0, JOptionPane.ERROR_MESSAGE, xargs );
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -