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

📄 filereceivedialog.java

📁 网站即时通讯系统
💻 JAVA
字号:
package com.valhalla.jbother;import com.valhalla.jbother.jabber.smack.*;import org.jivesoftware.smack.packet.IQ;import org.jivesoftware.smack.packet.XMPPError;import org.jivesoftware.smack.packet.Packet;import org.jivesoftware.smack.PacketListener;import javax.swing.*;import java.awt.event.KeyEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.awt.*;import java.util.ResourceBundle;import java.util.Locale;/** * Created by luke on Jan 19, 2005 4:25:58 PM *//** * Dialog for receiving files. It's called each time <code><streamhost></code> packet * arrives. User can accept or reject incoming file by pressing "Accept" and "Reject" buttons. */public class FileReceiveDialog extends FileDialog implements PacketListener{  private static String fId = "$Id$";  private ReceiveFile receiveFile;  private Streamhost streamHost;  private FileReceiveDialog thisPointer = this;  private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );  public FileReceiveDialog(StreamInitiation aSi)  {    super(aSi);    setTitle( "Receive file" );    fromToLabel.setText( "From:" );    fromToTF.setText( si.getFrom() );    ayeButton.setText( "Accept" );    ayeButton.setMnemonic( KeyEvent.VK_A );    nayButton.setText( "Reject" );    nayButton.setMnemonic( KeyEvent.VK_R );    descriptionArea.setEditable( false );    fileChooser = BuddyList.getInstance().getReceiveFileChooser();    setFileDetails(si.getFileDetails());  }  /** "Accept" button pressed */  protected void doAye()  {    // try really hard to make sure that file will not be overwritten    // and can be written to    boolean done = false;    while( !done )    {      fileChooser.setSelectedFile(si.getFileDetails().getFile());      int retval = fileChooser.showSaveDialog(this);      if(retval == JFileChooser.APPROVE_OPTION)      {        if(fileChooser.getSelectedFile().exists())        {          int choice = JOptionPane.showConfirmDialog(this,            "File exists - overwrite?","Receive File",JOptionPane.YES_NO_CANCEL_OPTION);          if(choice == JOptionPane.CANCEL_OPTION)          {            doNay();            return;          }          else if(choice == JOptionPane.NO_OPTION) {            done = false;          }          else if(choice == JOptionPane.YES_OPTION)          {            done = true;            if( ! fileChooser.getSelectedFile().canWrite() )            {              JOptionPane.showMessageDialog(this,                resources.getString( "writingToFileNotPermitted" ),                resources.getString( "receiveFileError" ),                JOptionPane.ERROR_MESSAGE              );              done = false;            }          }        }        else        {          si.getFileDetails().setDestFile(fileChooser.getSelectedFile());          done = true;        }      }      else if(retval == JFileChooser.CANCEL_OPTION) {        doNay();        return;      }    }    // send confirmation message indicating that we want to receive the file    sendPacket(si.createConfirmationMessage());    statusLabel.setText("Waiting for sender...");    disableAll();  }  /** "Reject" button pressed */  protected void doNay()  {    // we don't want to accept the file so we need to send the IQ error message    // to the Initiator    IQ errorPacket = new IQ() {      public String getChildElementXML()      {        return null;      }    };    errorPacket.setTo(si.getFrom());    errorPacket.setFrom(si.getTo());    errorPacket.setType(IQ.Type.ERROR);    errorPacket.setPacketID(si.getPacketID());    errorPacket.setError(new XMPPError(403,"forbidden"));    sendPacket( errorPacket );  }  /**   * implementation of cleanUp() method. Closes in- and output stream and removes this dialog   * from the list of packet listeners   */  protected void cleanUp() {    receiveFile.cleanUp();    BuddyList.getInstance().getConnection().removePacketListener(this);  }  /**   * helper method for making sure that packet will be sent   * @param packet packet to send   */  private void sendPacket(Packet packet)  {    if(BuddyList.getInstance().checkConnection()) {      BuddyList.getInstance().getConnection().sendPacket( packet );    }  }  /**   * implementation of processPacket() method   * checks if incoming packet is really of Streamhost type <b>and</b> its stream id (SID) is the same as   * preceding StreamInitiation object's (only then it is sure that those two messages are about same file)   * @param packet   */  public void processPacket(Packet packet)  {    if(packet instanceof Streamhost && ((IQ)packet).getType() == IQ.Type.SET )    {      streamHost = (Streamhost)packet;      if( streamHost.getSId().equals(si.getId()) )      {        // prepare to receive file        receiveFile = new ReceiveFile(          si.getFileDetails(),          streamHost.getHostname(),          streamHost.getPort(),          streamHost.getSId(),          si.getFrom(),          si.getTo());        // receive file in separate thread        // this will not block the other communication        ReceiveFileThread receiveFileThread = new ReceiveFileThread(receiveFile,this);        receiveFileThread.start();      }    }  }  /**   * Thread for receiving files   * by putting receiving files into separate thread, connection is not blocked for other   * messages, so that users can send chat/message/file transfer packages and don't need   * to wait for the filetransfer to finish   */  class ReceiveFileThread extends Thread  {    private ReceiveFile receiveFile;    private JDialog parentDialog;    public ReceiveFileThread(ReceiveFile aReceiveFile, JDialog aParentDialog) {      receiveFile = aReceiveFile;      parentDialog = aParentDialog;    }    public void run()    {      if( receiveFile.authenticate() == false )      {        JOptionPane.showMessageDialog(parentDialog,          resources.getString( "couldNotAuthenticateWithInitiator" ),          resources.getString( "receiveFileError" ),          JOptionPane.ERROR_MESSAGE        );        return;      }      fileProgressDialog = new FileProgressDialog(null,        resources.getString( "receivingFile" ) + " " + receiveFile.getFileDetails().getFileName(),receiveFile.getFileDetails());      fileProgressDialog.getButton().addActionListener( new ActionListener () {        public void actionPerformed(ActionEvent e)        {          fileProgressDialog.delete();          cleanUp();        }      });      receiveFile.addProgressListener( fileProgressDialog );      receiveFile.addProgressListener(thisPointer);      // prepare and send <streamhost-used> message to activate the bytestream      // note: this packet's id needs to be the same as calling <streamhost>'s      StreamhostUsed streamHostuReply = new StreamhostUsed(streamHost.getFrom());      streamHostuReply.setPacketID(streamHost.getPacketID());      streamHostuReply.setTo(streamHost.getFrom());      streamHostuReply.setFrom(streamHost.getTo());      sendPacket(streamHostuReply);      if ( receiveFile.getFile() == false )      {        JOptionPane.showMessageDialog(parentDialog,          resources.getString( "connectionLostCancelled" ),          resources.getString( "receiveFileError" ),          JOptionPane.ERROR_MESSAGE);        return;      }      receiveFile.removeProgressListener( fileProgressDialog );      fileProgressDialog.getButton().setText("Done");      cleanUp();    }  }}

⌨️ 快捷键说明

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