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

📄 filesenddialog.java

📁 网站即时通讯系统
💻 JAVA
字号:
package com.valhalla.jbother;import com.valhalla.jbother.jabber.smack.*;import org.jivesoftware.smack.PacketListener;import org.jivesoftware.smack.PacketCollector;import org.jivesoftware.smack.SmackConfiguration;import org.jivesoftware.smack.filter.PacketIDFilter;import org.jivesoftware.smack.packet.Packet;import org.jivesoftware.smack.packet.IQ;import java.awt.*;import java.awt.event.KeyEvent;import java.awt.event.ActionListener;import java.awt.event.ActionEvent;import java.util.ResourceBundle;import java.util.Locale;import java.io.File;import com.valhalla.settings.Settings;import javax.swing.*;/** * Created by luke on Feb 2, 2005 4:52:15 PM */public class FileSendDialog extends FileDialog implements PacketListener{  private static String fId = "$Id$";  private SendFile sendFile = null;  private Streamhost streamHost = null;  private ResourceBundle resources = ResourceBundle.getBundle( "JBotherBundle", Locale.getDefault() );  public FileSendDialog(StreamInitiation aSi) throws HeadlessException  {    super( aSi );    setTitle( "Send file" );    fromToLabel.setText( "To:" );    fromToTF.setText( si.getTo() );    ayeButton.setText( "Send" );    ayeButton.setMnemonic( KeyEvent.VK_S );    nayButton.setText( "Cancel" );    nayButton.setMnemonic( KeyEvent.VK_C );    descriptionArea.setEditable( true );    fileChooser = BuddyList.getInstance().getSendFileChooser();    selectFile();  }  /**   * "Send" button pressed   * note: sending initial StreamInitiation message is done in separate thread, in order to prevent   * locking up JBother while waiting for the reply   */  protected void doAye()  {    if(fileDetails.getFileSize() == 0)    {      String windowTitle = resources.getString( "cantSendEmptyFilesWindowTitle" );      String message = resources.getString( "cantSendEmptyFilesMessage" );      JOptionPane.showMessageDialog(this,message,windowTitle,JOptionPane.ERROR_MESSAGE);      cleanUp();      dispose();      return;    }    // then, build <si> message out of file information    fileDetails.setDescription(descriptionArea.getText());    si.setFileDetails(fileDetails);    // this should catch up all errors    PacketErrorWatcher t = new PacketErrorWatcher(si.getPacketID());    t.start();    BuddyList.getInstance().getConnection().sendPacket(si);    statusLabel.setText("Waiting for recipient...");    disableAll();  }  /**   * "Cancel" button pressed   */  protected void doNay()  {    cleanUp();    dispose();  }  /**   * cleanup includes removing this dialog from the list of packet listeners   * and closing down all data streams   */  protected void cleanUp()  {    BuddyList.getInstance().getConnection().removePacketListener(this);    if(sendFile != null) {      sendFile.cleanUp();    }  }  /**   * as this is dialog that handles file sending, it should handle the replies from the   * other end. Those would include:   * . confirmation of whether the other side can handle <si> requests (<iq type="result"><si ...>)   * .   *   * @param packet   */  public void processPacket(Packet packet)  {    if (packet instanceof StreamInitiation && ((IQ)packet).getType() == IQ.Type.RESULT )    {      if ( si != null  && packet.getPacketID().equals(si.getPacketID()) )      {        StreamInitiation streamInitiation = (StreamInitiation)packet;        if(streamInitiation.getFeature().providesBytestreamOption())        {          // prepare <streamhost> message          streamHost = new Streamhost(Integer.parseInt(            Settings.getInstance().getProperty( "dataPort" )),            Settings.getInstance().getProperty( "dataInterface" ),            streamInitiation.getFrom(),si.getId());          streamHost.setTo(streamInitiation.getFrom());          BuddyList.getInstance().getConnection().sendPacket(streamHost);          sendFile = new SendFile(            si.getFileDetails(),            Settings.getInstance().getProperty( "dataInterface" ),            Integer.parseInt(Settings.getInstance().getProperty( "dataPort" )),            si.getId(),            si.getFrom(),            si.getTo()          );          // verify if connection was successful, inform & bail out if not          if(sendFile.isByteChannelNull())          {            JOptionPane.showMessageDialog(this,              resources.getString("couldNotAuthenticateWithTarget") +              "\n" + resources.getString("checkDataTransferSettings"),              resources.getString("sendFileError"),JOptionPane.ERROR_MESSAGE);            cleanUp();            dispose();            return;          }          // now as we are sure that connection is valid, we can start thread          // listening for error replies to <streamhost> packet          PacketErrorWatcher packetWatchdogThread = new PacketErrorWatcher( streamHost.getPacketID() );          packetWatchdogThread.start();          // show progress dialog          fileProgressDialog = new FileProgressDialog(null,resources.getString( "sendingFile" )              + " " + si.getFileDetails().getFileName(),sendFile.getFileDetails());          fileProgressDialog.getButton().addActionListener( new ActionListener () {            public void actionPerformed(ActionEvent e)            {              fileProgressDialog.delete();              cleanUp();            }          });          sendFile.addProgressListener( fileProgressDialog );          sendFile.addProgressListener(this);          if(sendFile.authenticate() == false)          {            System.out.println(resources.getString( "couldNotAuthenticateWithTarget" ));            cleanUp();            return;          }        }      }    }    else if(packet instanceof StreamhostUsed && ((IQ)packet).getType() == IQ.Type.RESULT )    {      if( streamHost != null && packet.getPacketID().equals(streamHost.getPacketID()) )      {        // other end is ready to receive data, send file now        sendFile.sendFile();        sendFile.removeProgressListener( fileProgressDialog );        fileProgressDialog.getButton().setText( "Done" );        cleanUp();        dispose();      }    }  }  /**   * displays file chooser window and lets user select file to send   */  private void selectFile()  {    int retval = fileChooser.showOpenDialog(this);    if( retval == JFileChooser.APPROVE_OPTION )    {      File selectedFile = fileChooser.getSelectedFile();      if( ! selectedFile.exists() )      {        JOptionPane.showMessageDialog(this,          resources.getString("fileNotFound"),          resources.getString("sendFileError"),          JOptionPane.ERROR_MESSAGE);        return;      }      setFileDetails(new StreamInitiation.FileDetails(selectedFile));      si.setFileDetails(getFileDetails());    }    else if( retval == JFileChooser.CANCEL_OPTION ) {      dispose();      return;    }  }  /**   * this thread is used to   */  class PacketErrorWatcher extends Thread  {    private FileSendDialog parentDialog;    private String packetID;    private PacketCollector collector;    public PacketErrorWatcher(String aPacketID)    {//      parentDialog = aParentDialog;      packetID = aPacketID;      collector = BuddyList.getInstance().getConnection().        createPacketCollector(new PacketIDFilter(packetID));    }    public PacketCollector getCollector() {      return collector;    }    public void run()    {      IQ reply = (IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout());      String errorMessages = "";      if( reply == null ) {        errorMessages += resources.getString( "targetTimedOut" );      }      else if ( reply.getType() == IQ.Type.ERROR ) {        switch(reply.getError().getCode())        {          case 403:            errorMessages += resources.getString( "targetRefusedToAcceptFile" ) + "\n";            break;          case 404:            errorMessages += resources.getString( "targetCouldNotConnect" ) + "\n";            break;          case 405:            errorMessages += resources.getString( "targetDidNotAllowFileTransfer" ) + "\n";            break;          default:            errorMessages = resources.getString( "sendFileError" );            break;        }      }      collector.cancel();      if( errorMessages.length() > 0 )      {        JOptionPane.showMessageDialog(null,          errorMessages, resources.getString( "sendFileError" ), JOptionPane.ERROR_MESSAGE);        cleanUp();        dispose();      }    }  }}

⌨️ 快捷键说明

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