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

📄 ftpippanel.java

📁 Ftp服务1.0
💻 JAVA
字号:
package ranab.server.ftp.gui;

import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import java.awt.Font;
import java.awt.Color;
import java.awt.Insets;
import java.awt.FlowLayout;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JButton;
import javax.swing.JTextArea;
import javax.swing.BorderFactory;

import ranab.io.IoUtils;
import ranab.io.LogFile;
import ranab.io.StreamConnector;
import ranab.gui.GuiUtils;
import ranab.gui.TextAreaIo;
import ranab.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 + -