?? ftpippanel.java
字號:
/*
* Copyright (C) The Apache Software Foundation. All rights reserved.
*
* This software is published under the terms of the Apache Software License
* version 1.1, a copy of which has been included with this distribution in
* the LICENSE file.
*/
package server.ftp.gui;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import gui.GuiUtils;
import gui.TextAreaIo;
import io.IoUtils;
import io.StreamConnector;
import server.ftp.FtpConfig;
/**
* Ip restrictor panel
*/
public
class FtpIpPanel extends PluginPanel {
private JTextArea mjIpTxt;
private FtpConfig mConfig;
private JTextField mHeaderLab;
/**
* Instantiate IP restrictor panel
*/
public FtpIpPanel(FtpTree tree) {
super(tree);
initComponents();
}
/**
* Initialize UI components
*/
private void initComponents() {
setLayout(new GridBagLayout());
GridBagConstraints gc = null;
int yindex = -1;
// header
mHeaderLab = new JTextField();
mHeaderLab.setHorizontalAlignment(JTextField.CENTER);
mHeaderLab.setColumns(12);
mHeaderLab.setEditable(false);
mHeaderLab.setBorder(BorderFactory.createEmptyBorder(0, 0, 0, 0));
mHeaderLab.setFont(new Font(null, Font.BOLD, 12));
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = ++yindex;
gc.gridwidth = 1;
gc.insets = new Insets(3, 0, 0, 3);
add(mHeaderLab, gc);
// text area
mjIpTxt = new JTextArea(16, 12);
JScrollPane txtPane = new JScrollPane(mjIpTxt,
JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = ++yindex;
gc.gridwidth = 1;
gc.insets = new Insets(3, 0, 0, 3);
add(txtPane, gc);
// buttons
JPanel btnPane = new JPanel();
btnPane.setLayout(new FlowLayout(FlowLayout.CENTER));
JButton jSaveBtn = new JButton("Save");
btnPane.add(jSaveBtn);
JButton jResetBtn = new JButton("Reload");
btnPane.add(jResetBtn);
gc = new GridBagConstraints();
gc.gridx = 0;
gc.gridy = ++yindex;
gc.gridwidth = 1;
add(btnPane, gc);
// event handlers
jSaveBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
save();
}
});
jResetBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
refresh(mConfig);
}
});
}
/**
* Save IP data
*/
public void save() {
FtpConfig cfg = mConfig;
if (cfg == null) {
return;
}
InputStream is = null;
OutputStream os = null;
try {
is = TextAreaIo.getInputStream(mjIpTxt);
os = new FileOutputStream(cfg.getIpRestrictor().getFile());
StreamConnector sc = new StreamConnector(is, os);
sc.connect();
if(sc.hasException()) {
throw sc.getException();
}
cfg.getLogger().info("Saved ip restrictor file.");
}
catch(Exception ex) {
GuiUtils.showErrorMessage(getTree().getRootPanel(), ex.getMessage());
cfg.getLogger().error(ex);
}
finally {
IoUtils.close(is);
IoUtils.close(os);
}
cfg.getIpRestrictor().readFile();
}
/**
* Refresh ip data
*/
public void refresh(FtpConfig cfg) {
mjIpTxt.setText("");
if (cfg != null) {
mjIpTxt.setText("");
Iterator ipRestrictorIt = cfg.getIpRestrictor().iterator();
while(ipRestrictorIt.hasNext()) {
mjIpTxt.append(ipRestrictorIt.next().toString());
mjIpTxt.append("\n");
}
String headerStr = "";
if (cfg.isAllowIp()) {
headerStr = "Allow IP listed";
}
else {
headerStr = "Ban IP listed";
}
mHeaderLab.setText(headerStr);
}
mConfig = cfg;
}
/**
* Not displayable when server stopped
*/
public boolean isDisplayable() {
return mConfig != null;
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -