📄 bsfiletransferdialog.java
字号:
fileChooser = new JFileChooser(path);
File f = new File(path);
fileChooser.setSelectedFile(f);
/*else
fileChooser = new JFileChooser("");*/
/*ExampleFileFilter filter = new ExampleFileFilter();
filter.addExtension("jpg");
filter.addExtension("gif");
filter.setDescription("JPG & GIF Images");
chooser.setFileFilter(filter);*/
int returnVal;
if (receiving) {
returnVal = fileChooser.showSaveDialog(this);
}
else {
returnVal = fileChooser.showOpenDialog(this);
}
if (returnVal == JFileChooser.APPROVE_OPTION) {
fileTextField.setText(fileChooser.getSelectedFile().getPath());
}
}
else if (c == cancelButton) {
setVisible(false);
dispose();
}
// up/downloads the file
else if (c == okButton) {
if (receiving) {
tryDownload();
}
else {
if (tryUpload())
mainFrame.sendOOB(jid, urlStr);
}
}
}//GEN-LAST:event_actionPerformed
/** Closes the dialog */
private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
setVisible(false);
dispose();
}//GEN-LAST:event_closeDialog
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JPanel jidPanel;
private javax.swing.JTextField proxyTextField;
private javax.swing.JTextField proxyPortTextField;
private javax.swing.JPanel proxyPanel;
private javax.swing.JPanel buttonPanel;
private javax.swing.JTextField fileTextField;
private javax.swing.JLabel jidLabel;
private javax.swing.JButton okButton;
private javax.swing.JCheckBox useProxyCheckBox;
private javax.swing.JPanel filePanel;
private javax.swing.JLabel proxyLabel;
private javax.swing.JLabel proxyPortLabel;
private javax.swing.JButton browseButton;
private javax.swing.JLabel fileLabel;
private javax.swing.JButton cancelButton;
private javax.swing.JTextField jidTextField;
private javax.swing.JPanel proxyParamPanel;
// End of variables declaration//GEN-END:variables
private javax.swing.JPanel serverPanel = null;
private javax.swing.JLabel hostLabel = null;
private javax.swing.JLabel portLabel = null;
private javax.swing.JLabel pathLabel = null;
private javax.swing.JTextField hostTextField = null;
private javax.swing.JTextField portTextField = null;
private javax.swing.JTextField pathTextField = null;
private javax.swing.JPanel urlPanel = null;
private javax.swing.JLabel urlLabel = null;
private javax.swing.JTextField urlTextField = null;
/** Adds GUI for web server information. */
protected void addServerPanel() {
java.awt.GridBagConstraints gridBagConstraints;
serverPanel = new javax.swing.JPanel();
hostLabel = new javax.swing.JLabel();
portLabel = new javax.swing.JLabel();
pathLabel = new javax.swing.JLabel();
hostTextField = new javax.swing.JTextField();
portTextField = new javax.swing.JTextField();
pathTextField = new javax.swing.JTextField();
serverPanel.setLayout(new java.awt.GridBagLayout());
serverPanel.setBorder(new javax.swing.border.TitledBorder("File server"));
hostLabel.setText("WWW host");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
serverPanel.add(hostLabel, gridBagConstraints);
portLabel.setText("Port");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 2;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
serverPanel.add(portLabel, gridBagConstraints);
pathLabel.setText("Remote file path");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
serverPanel.add(pathLabel, gridBagConstraints);
hostTextField.setColumns(30);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
serverPanel.add(hostTextField, gridBagConstraints);
portTextField.setColumns(30);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 2;
serverPanel.add(portTextField, gridBagConstraints);
pathTextField.setColumns(30);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 3;
serverPanel.add(pathTextField, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 0.5;
getContentPane().add(serverPanel, gridBagConstraints);
}
/** Adds GUI for URL information */
protected void addUrlPanel() {
java.awt.GridBagConstraints gridBagConstraints;
urlPanel = new javax.swing.JPanel();
urlLabel = new javax.swing.JLabel();
urlTextField = new javax.swing.JTextField();
urlPanel.setLayout(new java.awt.GridBagLayout());
urlPanel.setBorder(new javax.swing.border.TitledBorder("File server"));
urlLabel.setText("URL");
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 1;
gridBagConstraints.insets = new java.awt.Insets(0, 5, 0, 10);
gridBagConstraints.anchor = java.awt.GridBagConstraints.WEST;
urlPanel.add(urlLabel, gridBagConstraints);
urlTextField.setColumns(30);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 2;
gridBagConstraints.gridy = 1;
gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
urlPanel.add(urlTextField, gridBagConstraints);
gridBagConstraints = new java.awt.GridBagConstraints();
gridBagConstraints.gridx = 1;
gridBagConstraints.gridy = 3;
gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
gridBagConstraints.weightx = 1.0;
gridBagConstraints.weighty = 0.5;
getContentPane().add(urlPanel, gridBagConstraints);
}
/** Enables proxy GUI, when proxy enabled */
protected void enableProxyPanel(boolean enable) {
//proxyParamPanel.setEnabled(enable);
proxyTextField.setEnabled(enable);
proxyLabel.setEnabled(enable);
proxyPortTextField.setEnabled(enable);
proxyPortLabel.setEnabled(enable);
}
/**
* Tries to download file.
* Returns if successfull.
*/
protected boolean tryDownload() {
File file;
URL url;
String proxy;
String proxyPort;
try {
file = new File(fileTextField.getText());
url = new URL(urlTextField.getText());
if (useProxyCheckBox.isSelected()) {
proxy = proxyTextField.getText();
proxyPort = proxyPortTextField.getText();
}
else
proxy = proxyPort = null;
URLFileTransfer.downloadURL(url, file, proxy, proxyPort);
}
catch (MalformedURLException e) {
JOptionPane.showMessageDialog(this, "Cannot open URL",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
catch (NullPointerException e) {
JOptionPane.showMessageDialog(this, "Cannot open file",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
JOptionPane.showMessageDialog(this, "File saved",
"Success", JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
dispose();
return true;
}
/**
* Tries to upload file.
* Returns if successfull.
*/
protected boolean tryUpload() {
File file;
URL url;
String proxy;
String proxyPort;
//String urlStr;
try {
file = new File(fileTextField.getText());
urlStr = new String("http://" + hostTextField.getText() +
":" + portTextField.getText() +
pathTextField.getText());
if (urlStr.charAt(urlStr.length()-1) != '/')
urlStr += "/";
urlStr += file.getName();
url = new URL(urlStr);
if (useProxyCheckBox.isSelected()) {
proxy = proxyTextField.getText();
proxyPort = proxyPortTextField.getText();
}
else
proxy = proxyPort = null;
URLFileTransfer.uploadURL(file, url, proxy, proxyPort);
}
catch (MalformedURLException e) {
JOptionPane.showMessageDialog(this, "Cannot open URL",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
catch (NullPointerException e) {
JOptionPane.showMessageDialog(this, "Cannot open file",
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
catch (IOException e) {
JOptionPane.showMessageDialog(this, e.getMessage(),
"Error", JOptionPane.ERROR_MESSAGE);
return false;
}
JOptionPane.showMessageDialog(this, "File uploaded",
"Success", JOptionPane.INFORMATION_MESSAGE);
setVisible(false);
dispose();
return true;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -