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

📄 iprestrictortable.java

📁 用java写的ftp服务器程序
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
// $Id: IPRestrictorTable.java 348197 2005-11-22 10:33:33 -0700 (Tue, 22 Nov 2005) rana_b $
/*
 * Copyright 2004 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.ftpserver.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.event.EventListenerList;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
import javax.swing.event.TableModelEvent;
import javax.swing.event.TableModelListener;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableModel;

/**
 * IP restrictor table.
 * 
 * @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
 */
public 
class IPRestrictorTable extends JPanel implements TableModel, ListSelectionListener {

    private static final long serialVersionUID = -7517200112683933776L;

    private final static String HEADER[] = {
            "IP Pattern",
            "Permission"
    };
    
    private Vector m_entries = new Vector();
    private EventListenerList m_modelListeners  = new EventListenerList();
    
    private JTable m_table;
    private JButton m_addButton;
    private JButton m_insertButton;
    private JButton m_removeButton;
    private JButton m_moveUpButton;
    private JButton m_moveDownButton;
    
    /**
     * Default constructor.
     */
    public IPRestrictorTable() {
        initComponents();
    }
    
    /**
     * Initialize UI components.
     */
    private void initComponents() {
        
        setLayout(new BorderLayout());
        
        m_table = new JTable(this);
        m_table.setColumnSelectionAllowed(false);
        m_table.setRowSelectionAllowed(true);
        m_table.setRowHeight(20);
        m_table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        m_table.getSelectionModel().addListSelectionListener(this);
        m_table.getColumnModel().getSelectionModel().addListSelectionListener(this);
        
        JScrollPane scrollPane = new JScrollPane(m_table);
        add(scrollPane, BorderLayout.CENTER);
        
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        add(buttonPanel, BorderLayout.SOUTH);
        
        m_addButton = new JButton("Add");
        m_addButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                addRow();
            }
        });
        buttonPanel.add(m_addButton);
        
        m_insertButton = new JButton("Insert");
        m_insertButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                insertRow();
            }
        });
        buttonPanel.add(m_insertButton);
        
        m_removeButton = new JButton("Remove");
        m_removeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                removeRow();
            }
        });
        buttonPanel.add(m_removeButton);
        
        m_moveUpButton = new JButton("Move Up");
        m_moveUpButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                moveUpRow();
            }
        });
        buttonPanel.add(m_moveUpButton);
        
        m_moveDownButton = new JButton("Move Down");
        m_moveDownButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                moveDownRow();
            }
        });
        buttonPanel.add(m_moveDownButton);
        setButtonStatus();
    }
    
    /**
     * Set button enable/disable status
     */
    private void setButtonStatus() {
        m_addButton.setEnabled(canBeAdded());
        m_insertButton.setEnabled(canBeInserted());
        m_removeButton.setEnabled(canBeRemoved());
        m_moveUpButton.setEnabled(canBeMovedUp());
        m_moveDownButton.setEnabled(canBeMovedDown());
    }
    
    /**
     * Can a new row be added?
     */
    private boolean canBeAdded() {
        return true;
    }
    
    /**
     * Can a row be inserted?
     */
    private boolean canBeInserted() {
        return true;
    }
    
    /**
     * Can a row be removed?
     */
    private boolean canBeRemoved() {
        
        // no data - nothing to be removed
        if( (m_entries == null) || (m_entries.size() == 0) ) {
            return false;
        }
        
        // no row selection - nothing to be removed
        int selRow = m_table.getSelectedRow();
        return (selRow >= 0);
    }
    
    /**
     * Can the selected row be moved up?
     */
    private boolean canBeMovedUp() {
        
        // no data - nothing to move up
        if( (m_entries == null) || (m_entries.size() == 0) ) {
            return false;
        }
        
        // no selection or the first row has been selected
        int selRow = m_table.getSelectedRow();
        return (selRow > 0);
    }
    
    /**
     * Can the selected row be moved down?
     */
    private boolean canBeMovedDown() {
        
        // no data - nothing to move down
        if( (m_entries == null) || (m_entries.size() == 0) ) {
            return false;
        }
        
        // no selection - nothing to move down
        int selRow = m_table.getSelectedRow();
        if(selRow == -1) {
            return false;
        }
        
        // last row cannot be moved down
        return ( selRow != (m_entries.size()-1) );
    }
    
    /**
     * Add a new row.
     */
    public void addRow() {
        
        // new row cannot be added
        if(!canBeAdded()) {
            return;
        }
        
        // save the changed data if any 
        stopCellEditing();
        
        // get row data and add
        Entry entry = new Entry();
        m_entries.add(entry);
        
        // notify listeners and select the newly added row
        int lastRow = m_entries.size() - 1;
        fireTableChanged( new TableModelEvent(this, lastRow, lastRow,
              TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT) );
        m_table.setRowSelectionInterval(lastRow, lastRow);
    }
    
    
    /**
     * Inser a new row entry an the selected (or last position)
     */
    public void insertRow() {
        
        // new row cannot be inserted
        if(!canBeInserted()) {
            return;
        }
        
        // save the changed data if any 
        stopCellEditing(); 
        
        // get the selected row 
        int selRow = m_table.getSelectedRow();
        if(selRow == -1) {
            selRow = m_entries.size() - 1;
        }
        if(selRow == -1) {
            selRow = 0;
        }
        
        // get row data and add
        Entry entry = new Entry();
        m_entries.add(selRow, entry);
        
        // notify listeners
        fireTableChanged( new TableModelEvent(this, selRow, selRow,
                TableModelEvent.ALL_COLUMNS, TableModelEvent.INSERT) );
        m_table.setRowSelectionInterval(selRow, selRow);
    }
    
    /**
     * Delete the selected (or the last) row
     */
    public void removeRow() {
        
        // row cannot be removed

⌨️ 快捷键说明

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