📄 composeframe.java
字号:
} } else if ( option == JOptionPane.CANCEL_OPTION ) { xdismiss = false; } } if ( xdismiss ) { dismissWindow(); } } private void dismissWindow() { saveLayoutProperties(); originalMessage_ = null; signature_ = null; headers_ = null; extraHeaders_ = null; attachments_ = null; if ( attachComboBox_.getItemCount() > 0 ) { attachComboBox_.removeAllItems(); } fromText_.setText( "" ); toText_.setText( "" ); ccText_.setText( "" ); bccText_.setText( "" ); subjectText_.setText( "" ); messageText_.setText( "" ); // reasonable time to clean up memory. System.gc(); javax.swing.SwingUtilities.invokeLater( new IWindowDisposer() ); }//............................................................ private void loadLayoutProperties() { Dimension sz = getSize(); Rectangle bounds = Configuration.getBounds( "composeWindow", new Rectangle( 20, 20, sz.width, sz.height ) ); setBounds( bounds ); charsetLbl_.setText( Configuration.getCharset() ); } private void saveLayoutProperties() { Configuration.saveBounds( "composeWindow", getBounds() ); Configuration.setCharset( charsetLbl_.getText() ); } private Message buildHeader( MimeMessage message ) throws MessagingException { // add the addresses InternetAddress[] xaddresses; String xtext; String xcharset = charsetLbl_.getText(); xtext = fromText_.getText().trim(); xaddresses = MessageUtilities.encodeAddresses( xtext, xcharset ); if ( xaddresses.length > 0 ) message.setFrom( xaddresses[0] ); xtext = toText_.getText().trim(); xaddresses = MessageUtilities.encodeAddresses( xtext, xcharset ); message.addRecipients( Message.RecipientType.TO, xaddresses ); xtext = ccText_.getText().trim(); xaddresses = MessageUtilities.encodeAddresses( xtext, xcharset ); message.addRecipients( Message.RecipientType.CC, xaddresses ); xtext = bccText_.getText().trim(); xaddresses = MessageUtilities.encodeAddresses( xtext, xcharset ); message.addRecipients( Message.RecipientType.BCC, xaddresses ); // add the subject message.setSubject( subjectText_.getText().trim(), xcharset ); // add the extra headers MessageUtilities.addHeaders( message, headers_ ); MessageUtilities.addHeaders( message, extraHeaders_ ); return message; } private MimeMessage buildMessage() throws MessagingException { MimeMessage xnew = new MimeMessage( ICEMail.getDefaultSession() ); String xcharset = charsetLbl_.getText(); // add the content plus any attachments String xtext = messageText_.getText(); // WARNING // // Remove this translation and SMIME clear signed messages will // begin to generate invalid signatures. The problem lies in line // termination. The JTextPane likes to give us "\n" terminate lines. // // However, the mail standards define "\r\n" as the proper line // termination sequence. Thus, if we do not convert those "\n"'s // to "\r\n", then along the way they will be converted into // "\r\n"'s and at that point the signature will become invalid. // // This is only a problem with "clear signed" messages, since only // they can be munged by the intermediate transports. // try { StringBuffer buf = new StringBuffer(); BufferedReader in = new BufferedReader( new StringReader( xtext ) ); for ( ; ; ) { String inline = in.readLine(); if ( inline == null ) break; buf.append( inline ); buf.append( "\r\n" ); } xtext = buf.toString(); } catch ( IOException ex ) { throw new MessagingException( "newline conversion" + ex.getMessage() ); } if ( attachments_.size() > 0 ) { // make a multipart message with attachments MimeMultipart xmulti = new MimeMultipart(); if ( xtext != null ) { MessageUtilities.attach( xmulti, xtext, xcharset, Part.INLINE ); } MessageUtilities.attach( xmulti, attachments_, xcharset ); xnew.setContent( xmulti ); } else { // content is simple text xnew.setText( xtext, xcharset ); } return xnew; } private void editHeaders() { ExtraHeadersDialog dlg = new ExtraHeadersDialog( this, extraHeaders_ ); dlg.show(); InternetHeaders xheaders = dlg.getInternetHeaders(); if ( xheaders.getAllHeaders().hasMoreElements() ) { extraHeaders_ = xheaders; } } private void pasteAddress( String addrStr ) { if ( currentFocus_ != null ) { currentFocus_.replaceSelection( addrStr ); } }// FIXME Move to a utilities class... private String readFileToString( File f ) { String result = null; try { StringWriter out = new StringWriter(); BufferedReader in = new BufferedReader( new FileReader( f ) ); for ( ; ; ) { String ln = in.readLine(); if ( ln == null ) break; out.write( ln ); out.write( "\n" ); } out.close(); in.close(); result = out.toString(); } catch ( IOException ex ) { result = null; } return result; } private void pasteFile() { String title = ICEMail.getBundle().getString( "Compose.title.pastefile" ); FileDialog dialog = new FileDialog( this, title, FileDialog.LOAD ); if ( ComposeFrame.LastAttachDir != null ) dialog.setDirectory( ComposeFrame.LastAttachDir ); dialog.show(); String fileName = dialog.getFile(); String dirName = dialog.getDirectory(); if ( fileName != null && dirName != null ) { ComposeFrame.LastAttachDir = dirName; File dirFile = new File( dirName ); File pasteFile = new File( dirFile, fileName ); String text = readFileToString( pasteFile ); if ( text != null ) { messageText_.replaceSelection( text ); } } } private void attachItem( Object item, String itemName ) { if ( Package.DEBUG && Package.isTraceable( "ComposeFrame" ) ) { System.out.println( "ComposeFrame.attachItem(): o-" + item + ", in-[" + itemName + "]" ); } int idx = -1; if ( attachments_.size() > 0 ) idx = attachComboBox_.getSelectedIndex(); attachments_.insertElementAt( item, idx + 1 ); attachComboBox_.insertItemAt( itemName, idx + 1 ); attachComboBox_.setSelectedIndex( idx + 1 ); attachComboBox_.validate(); modified_ = true; } private void attachFile() { String title = ICEMail.getBundle().getString( "Compose.title.attachfile" ); FileDialog dialog = new FileDialog( this, title, FileDialog.LOAD ); if ( ComposeFrame.LastAttachDir != null ) { dialog.setDirectory( ComposeFrame.LastAttachDir ); } dialog.show(); String fileName = dialog.getFile(); String dirName = dialog.getDirectory(); if ( fileName != null && dirName != null ) { ComposeFrame.LastAttachDir = dirName; File dirFile = new File( dirName ); File attachFile = new File( dirFile, fileName ); String contentType = FileTypeMap.getDefaultFileTypeMap().getContentType( attachFile ); String attachName = attachFile.getName().trim(); //String attachName = attachFile.getName() + " (" + contentType + ")"; attachItem( attachFile, attachName ); } } private void detachFile() { int idx = attachComboBox_.getSelectedIndex(); if ( idx >= 0 ) { attachments_.removeElementAt( idx ); attachComboBox_.removeItemAt( idx ); attachComboBox_.validate(); modified_ = true; } } private void attachSelection( String text ) { if ( text != null && text.length() > 0 ) { textAttachmentSequence_++; String attachName = "Text_" + textAttachmentSequence_ + " (text/plain) " + text.length() + " bytes"; attachItem( text, attachName ); messageText_.cut(); } } private void prefixSelection() { String selectedText = messageText_.getSelectedText(); if ( selectedText != null && selectedText.length() > 0 ) { StringBuffer buf = new StringBuffer(); String xprefix = JOptionPane.showInputDialog( ICEMail.getBundle().getString( "Compose.Prefix.dialog.message" ) ); if ( xprefix != null ) { collate( buf, xprefix, selectedText ); messageText_.replaceSelection( buf.toString() ); modified_ = true; } } } private void printMessage() { try { MimeMessage[] msgs = new MimeMessage[1]; msgs[0] = buildMessage(); buildHeader( msgs[0] ); msgs[0].saveChanges(); PrintMessageThread pThread = new PrintMessageThread( this, msgs ); pThread.start(); } catch ( MessagingException xex ) { Object[] xargs = new Object[1]; xargs[0] = xex.getMessage(); ComponentFactory.showDialog( ICEMail.getBundle(), "Compose.PrintingException", 0, JOptionPane.ERROR_MESSAGE, xargs ); } } private void sendMessage() { boolean xencrypt = encryptButton_ == null ? false : encryptButton_.current() > 0; boolean xsign = signButton_ == null ? false : signButton_.current() > 0; boolean xclearsign = signButton_ == null ? false : signButton_.current() == 2; try { MimeMessage xmessage = buildMessage(); xmessage.saveChanges(); if ( xencrypt || xsign ) { String charSet = charsetLbl_.getText(); String fromStr = fromText_.getText().trim(); InternetAddress[] addrs = MessageUtilities.encodeAddresses( fromStr, charSet ); xmessage.setFrom( addrs[0] ); String addrStr; Vector addrV = new Vector(); InternetAddress[] recipAddrs = null; if ( xencrypt ) { addrStr = toText_.getText().trim(); recipAddrs = MessageUtilities.encodeAddresses( addrStr, charSet ); for ( int i = 0 ; i < recipAddrs.length ; ++i ) { addrV.addElement( recipAddrs[i] ); } addrStr = ccText_.getText().trim(); recipAddrs = MessageUtilities.encodeAddresses( addrStr, charSet ); for ( int i = 0 ; i < recipAddrs.length ; ++i ) { addrV.addElement( recipAddrs[i] ); } addrStr = bccText_.getText().trim(); recipAddrs = MessageUtilities.encodeAddresses( addrStr, charSet ); for ( int i = 0 ; i < recipAddrs.length ; ++i ) { addrV.addElement( recipAddrs[i] ); } recipAddrs = new InternetAddress[ addrV.size() ]; addrV.copyInto( recipAddrs ); } else { recipAddrs = new InternetAddress[0]; } if ( xencrypt ) { xclearsign = false; } xmessage = ICEMail.getSMIMELibrary().buildSMimeMessage( ICEMail.getDefaultSession(), xmessage, addrs[0], recipAddrs, xsign, xclearsign, xencrypt ); } buildHeader( xmessage ); xmessage.saveChanges(); SendMessageThread thread = new SendMessageThread( this, xmessage ); thread.start(); } catch ( MissingCertificateException ex ) { Object[] xargs = { ex.getMessage() }; if ( xsign ) { ComponentFactory.showDialog( ICEMail.getBundle(), "Compose.MissingCert1", 0, JOptionPane.WARNING_MESSAGE, xargs ); } else { ComponentFactory.showDialog( ICEMail.getBundle(), "Compose.MissingCert2", 0, JOptionPane.WARNING_MESSAGE, xargs ); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -