fileexchangeview.java
来自「JAVA Servlet2.3外文书籍源码」· Java 代码 · 共 181 行
JAVA
181 行
package firewall.client;
import firewall.common.FileValidator;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import java.net.URL;
import java.net.MalformedURLException;
/**
* Title: FileExchangeView
* Description: Contains the application view/gui elements and setup
* Copyright: Copyright (c) 2001
* Company:
* @author Andrew Harbourne-Thomas
* @version 1.0
*/
public class FileExchangeView extends JFrame {
/** servers address */
URL hostServlet;
/** reference to the model */
FileExchangeModel model = new FileExchangeModel(this);
/** message area */
private final JTextArea messages = new JTextArea(5, 20);
/** JList used to hold list of files */
final JList fileList = new JList();
/** File chooser to select file to upload to server */
final JFileChooser fc = new JFileChooser();
/** Refresh button */
JButton refreshButton = new JButton("Refresh Filelist");
/** Upload button */
JButton uploadButton = new JButton("Upload File");
/** Download button */
JButton downloadButton = new JButton("Download File");
/** Delete button */
JButton deleteButton = new JButton("Delete File");
/** Panel to hold buttons */
JPanel buttonPanel = new JPanel();
/** Panel to hold file list and messages */
JPanel displayPanel = new JPanel();
/**
* Constructor prepares and setups up the applications gui
*
* @param server Reference to the servers address
*/
public FileExchangeView(URL server, String name) {
super(name);
this.hostServlet = server;
//create the message window and the filelist
messages.setMargin(new Insets(5, 5, 5, 5));
messages.setEditable(false);
JScrollPane messageScrollPane = new JScrollPane(messages);
fileList.setSize(50, 100);
fileList.setEnabled(true);
fileList
.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
JScrollPane fileListScrollPane = new JScrollPane();
fileListScrollPane.getViewport().setView(fileList);
//add listener to the refresh button
refreshButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//call refresh method
model.refreshRequest();
messages.setText("");
}
});
//add listener to the upload button
uploadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
//show a dialog to allow user to select file to send to server
fc.setDialogTitle("Select file to Upload..........");
FileExchangeFilter filter =
new FileExchangeFilter(FileValidator.VALID_EXTENSIONS, "Text files");
fc.setFileFilter(filter);
int returnVal = fc.showSaveDialog(FileExchangeView.this);
//perform users selected choice
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
messages.append("Uploading: " + file.getName() + "\n");
model.uploadRequest(file);
}
else {
messages.append("Upload command cancelled by user.\n");
}
//refresh list of files after deletion
model.refreshRequest();
}
});
//add listener to the download button
downloadButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.downloadRequest((String) fileList.getSelectedValue());
}
});
//add listener to the delete button
deleteButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
model.deleteRequest((String) fileList.getSelectedValue());
}
});
//prepare buttons
buttonPanel.add(refreshButton);
buttonPanel.add(uploadButton);
buttonPanel.add(downloadButton);
buttonPanel.add(deleteButton);
//set the focus order
refreshButton.setNextFocusableComponent(uploadButton);
uploadButton.setNextFocusableComponent(downloadButton);
downloadButton.setNextFocusableComponent(deleteButton);
deleteButton.setNextFocusableComponent(refreshButton);
//finish the gui setup
displayPanel.setLayout(new GridLayout(1, 2));
displayPanel.add(fileListScrollPane);
displayPanel.add(messageScrollPane);
Container contentPane = getContentPane();
contentPane.setLayout(new BorderLayout());
contentPane.add(buttonPanel, BorderLayout.CENTER);
contentPane.add(displayPanel, BorderLayout.NORTH);
//setup the filelist
model.refreshRequest();
}
/**
* Accesses the URL for the server
*
* @return URL for the server
*/
public URL getHostServlet() {
return hostServlet;
}
/**
* Appends text to our message box
*
* @param message - text to be appended
*/
public void appendMessage(String message) {
messages.append(message + "\n");
}
/**
*Refreshes the filelist
*
* @param files - String array of files
*/
public void refreshFileList(String[] files) {
fileList.setListData(files);
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?