⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 printmessagethread.java

📁 一个用java写的mail.里面的代码值得我们去研究!学习。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*** $Id: PrintMessageThread.java,v 1.5 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.Container;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Frame;import java.awt.Graphics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.Image;import java.awt.Point;import java.awt.PrintJob;import java.awt.Toolkit;import java.util.Date;import javax.mail.Address;import javax.mail.Message;import javax.mail.MessagingException;import javax.mail.Multipart;import javax.mail.Part;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JProgressBar;import org.icemail.Package; import org.icemail.util.AWTUtilities; import org.icemail.util.ComponentFactory; /** * Class PrintMessageThread * */public class PrintMessageThread  extends Thread{  private static final int Debug_ = Package.DEBUG ? Package.getLevel( "PrintMessageThread" ) : 0;  private Frame         frame = null;  private Message[]     msgs = null;  private PrintJob      job = null;  private Graphics      pg = null;  private Font          boldF  = new Font( "Serif", Font.BOLD, 12 );  private Font          plainF = new Font( "Serif", Font.PLAIN, 12 );  private Font          titleF = new Font( "SansSerif", Font.BOLD, 12 );  private FontMetrics   boldFM;  private FontMetrics   plainFM;  private FontMetrics   titleFM;  private int           xPosition;  private int           yPosition;  private int           xMargin = 6;  private int           yMargin = 4;  private int           lineHeight;  private Dimension     pageSz;  private JDialog       dialog;  private JProgressBar  progress;  public  PrintMessageThread( Frame frame, Message[] msgs ) {    super();    if ( Package.DEBUG && Package.isTraceable( "PrintMessageThread" ) ) {      System.out.println( "PrintMessageThread(): " + msgs.length );    }    this.msgs = msgs;    this.frame = frame;  }  public void  run() {    if ( Package.DEBUG && Package.isTraceable( "PrintMessageThread" ) ) {      System.out.println( "PrintMessageThread.run()" );    }    if ( this.msgs != null && this.msgs.length > 0 ) {      Toolkit tk = Toolkit.getDefaultToolkit();      this.job = tk.getPrintJob( this.frame, "ICEMail Job", null );      if ( this.job != null ) {        printMessages( this.msgs );        this.job.end();      }    }  // FIX ME; clean up everything to be sure things are disposed properly  }  private void  printMessages( Message[] msgs ) {    if ( Package.DEBUG && Package.isTraceable( "PrintMessageThread" ) ) {      System.out.println( "PrintMessageThread.printMessages(m[]): " + msgs.length );    }    this.dialog = new JDialog( this.frame, false );    this.dialog.setTitle( ICEMail.getBundle().getString( "PrintMessage.Title" ) );    Container content = this.dialog.getContentPane();    content.setLayout( new GridBagLayout() );    JLabel lbl;    lbl = ComponentFactory.getLabel( ICEMail.getBundle(), "PrintMessage.Printing" );    lbl.setHorizontalAlignment( JLabel.CENTER );    AWTUtilities.constrain( content, lbl, GridBagConstraints.HORIZONTAL,                            GridBagConstraints.CENTER, 0, 0, 0, 1, 1.0, 0.0 );    this.progress = new JProgressBar();    this.progress.setValue( 0 );    this.progress.setMinimum( 0 );    this.progress.setMaximum( 100 );    AWTUtilities.constrain( content, this.progress, GridBagConstraints.HORIZONTAL,                            GridBagConstraints.CENTER, 0, 1, 0, 1, 1.0, 0.0 );    this.dialog.pack();    Dimension sz = this.dialog.getSize();    if ( sz.width < 350 ) {      sz.width = 350;      this.dialog.setSize( sz );    }    Point loc = AWTUtilities.computeDialogLocation( this.dialog );    this.dialog.setLocation( loc.x, loc.y );    this.dialog.show();    for ( int mIdx = 0 ; mIdx < msgs.length ; ++mIdx ) {      Message msg = msgs[ mIdx ];      if ( msg == null )        continue;      printMessage( this.job, msg );      this.progress.setValue( mIdx * 100 / msgs.length );    }    this.progress.setValue( 100 );    try {      sleep( 750 );    } catch ( InterruptedException ex ) {    }    this.dialog.dispose();  }  private void  printMessage( PrintJob job, Message msg ) {    if ( Package.DEBUG && Package.isTraceable( "PrintMessageThread" ) ) {      System.out.println( "PrintMessageThread.printMessage(pj,m)" );    }    this.pg = job.getGraphics();    this.boldFM = pg.getFontMetrics( this.boldF );    this.plainFM = pg.getFontMetrics( this.plainF );    this.titleFM = pg.getFontMetrics( this.titleF );    this.pageSz = job.getPageDimension();    int res = job.getPageResolution();    this.pageSz.width -= 36;    this.pageSz.height -= 72;    this.lineHeight = 20;    this.xPosition = xMargin;    this.yPosition = yMargin + 6 + this.titleFM.getHeight();    try {      Address[] adds;      adds = msg.getFrom();      printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.From" ), adds );      this.xPosition = this.xMargin;      this.yPosition += this.titleFM.getHeight();      adds = msg.getRecipients( Message.RecipientType.TO );      printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.To" ), adds );      this.xPosition = this.xMargin;      this.yPosition += this.titleFM.getHeight();      adds = msg.getRecipients( Message.RecipientType.CC );      if ( adds != null && adds.length > 0 ) {        printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.Cc" ), adds );        this.xPosition = this.xMargin;        this.yPosition += this.titleFM.getHeight();      }      adds = msg.getRecipients( Message.RecipientType.BCC );      if ( adds != null && adds.length > 0 ) {        printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.Bcc" ), adds );        this.xPosition = this.xMargin;        this.yPosition += this.titleFM.getHeight();      }      Date date = msg.getSentDate();      if ( date != null ) {        String dStr = ICEMail.getDateFormat().format( date );        printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.Date" ), dStr );        this.xPosition = this.xMargin;        this.yPosition += this.titleFM.getHeight();      }      String subject = msg.getSubject();      if ( subject != null ) {        printHeaderLine( ICEMail.getBundle().getString( "PrintMessage.Subject" ), subject );        this.xPosition = this.xMargin;      }      this.yPosition += this.plainFM.getHeight() / 3;      this.pg.fillRect( this.xPosition, this.yPosition, (this.pageSz.width - (2 * this.xMargin)),                                        (this.plainFM.getHeight() / 5) );      this.yPosition += this.plainFM.getHeight() / 3;      StringBuffer textbuf = new StringBuffer();      getMessageText( textbuf, msg );            printBody( textbuf.toString() );    } catch ( MessagingException ex ) {      Object[] xargs = new Object[1];      xargs[0] = ex.getMessage();      ComponentFactory.showDialog( ICEMail.getBundle(), "PrintMessage.MessageError",                                   0, JOptionPane.ERROR_MESSAGE, xargs );    }    this.pg.dispose();  }  private void  printBody( String bodyText ) {    if ( Package.DEBUG && Package.isTraceable( "PrintMessageThread" ) ) {      System.out.println( "PrintMessageThread.printBody(s)" );    }    String line = null;    String text = bodyText;    this.lineHeight = this.plainFM.getHeight();    newLine();    int lineW = this.pageSz.width - ( 2 * this.xMargin );    this.pg.setFont( this.plainF );    for ( ; text.length() > 0 ; ) {      int endi;      int skip = 1;      int ni = text.indexOf( "\n" );      int ri = text.indexOf( "\r" );      if ( ri != -1 && ni == (ri + 1) ) {      // CR/NL        endi = ri;        skip = 2;      } else if ( ri == -1 && ni == -1 ) {        endi = -1;      } else if ( ri == -1 ) {        endi = ni;      } else if ( ni == -1 ) {        endi = ri;      } else {        endi = ( ri < ni ? ri : ni );      }      if ( endi == -1 ) {        line = text;        text = "";

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -