IPAssignFrm.java~186~
上传用户:liming9091
上传日期:2014-10-27
资源大小:3376k
文件大小:11k
源码类别:

Java编程

开发平台:

Java

  1. package tsinghuaip;
  2. import java.awt.*;
  3. import java.awt.event.*;
  4. import javax.swing.*;
  5. import javax.swing.table.*;
  6. import javax.swing.event.*;
  7. import com.borland.jbcl.layout.*;
  8. import javax.swing.border.*;
  9. import java.util.*;
  10. import java.math.*;
  11. public class IPAssignFrm extends JFrame {
  12.     JPanel contentPane;
  13.     XYLayout xYLayout1 = new XYLayout();
  14.     DefaultTableColumnModel columnModel = new DefaultTableColumnModel();
  15.     TableColumn column = new TableColumn();
  16.     JTableHeader tableHeader = new JTableHeader(columnModel);
  17.     IPInfoDataModel ipInfoDataModal = new IPInfoDataModel();
  18.     JTable jTable1 = new JTable(ipInfoDataModal);
  19.     JLabel jLabel1 = new JLabel();
  20.     JButton queryButton = new JButton();
  21.     JButton exitButton = new JButton();
  22.     JFormattedTextField IPAddrTextField = new JFormattedTextField();
  23.     TitledBorder titledBorder1;
  24.     TitledBorder titledBorder2;
  25.     TitledBorder titledBorder3;
  26.     JLabel jLabel2 = new JLabel();
  27.     //Construct the frame
  28.     public IPAssignFrm() {
  29.         enableEvents(AWTEvent.WINDOW_EVENT_MASK);
  30.         try {
  31.             IPInfoInit(); // some initialization about IP info
  32.             jbInit();
  33.             addWindowListener(new WindowAdapter() {
  34.                 public void windowClosing(WindowEvent e) {
  35.                     dispose();
  36.                 }
  37.             });
  38.         }
  39.         catch (Exception e) {
  40.             e.printStackTrace();
  41.         }
  42.     }
  43.     private void IPInfoInit() throws Exception {
  44.         IPInfoToDB iPInfoToDB = new IPInfoToDB();
  45.         iPInfoToDB.SetTxtFileName("TsinghuaIPAssign.txt");
  46.         iPInfoToDB.SaveIPToDB();
  47.         //GenXML genXML = new GenXML();
  48.         //genXML.SetXmlFileName("TsinghuaIPAssign.xml");
  49.         //genXML.SaveIPFromDB();
  50.     }
  51.     //Component initialization
  52.     private void jbInit() throws Exception {
  53.         contentPane = (JPanel)this.getContentPane();
  54.         titledBorder1 = new TitledBorder("");
  55.         titledBorder2 = new TitledBorder("");
  56.         titledBorder3 = new TitledBorder("");
  57.         contentPane.setMaximumSize(new Dimension(480, 400));
  58.         contentPane.setMinimumSize(new Dimension(480, 400));
  59.         contentPane.setOpaque(true);
  60.         contentPane.setPreferredSize(new Dimension(480, 400));
  61.         contentPane.setLayout(xYLayout1);
  62.         this.setContentPane(contentPane);
  63.         this.setDefaultCloseOperation(HIDE_ON_CLOSE);
  64.         this.setSize(new Dimension(550, 400));
  65.         this.setTitle("清华大学IP地址分布查询器(野人制作)");
  66.         jTable1.setBackground(new Color(149, 165, 182));
  67.         jTable1.setToolTipText("清华大学IP地址分配表");
  68.         jTable1.setRowHeight(25);
  69.         jTable1.setRowMargin(3);
  70.         jLabel1.setFont(new java.awt.Font("Dialog", 0, 15));
  71.         jLabel1.setRequestFocusEnabled(false);
  72.         jLabel1.setText("请出入待查询的IP地址:");
  73.         queryButton.setFont(new java.awt.Font("DialogInput", 1, 16));
  74.         queryButton.setText("开始查询");
  75.         queryButton.addActionListener(new IPAssignFrm_queryButton_actionAdapter(this));
  76.         IPAddrTextField.setBackground(Color.white);
  77.         IPAddrTextField.setBorder(BorderFactory.createEtchedBorder());
  78.         IPAddrTextField.setHorizontalAlignment(SwingConstants.CENTER);
  79.         IPAddrTextField.setText("");
  80.         jLabel2.setFont(new java.awt.Font("Monospaced", 0, 13));
  81.         jLabel2.setForeground(Color.darkGray);
  82.         jLabel2.setToolTipText("");
  83.         jLabel2.setText("Savage制作@CopyRight 2003-2010...");
  84.         JScrollPane scrollpane = JTable.createScrollPaneForTable(jTable1);
  85.         exitButton.addActionListener(new IPAssignFrm_exitButton_actionAdapter(this));
  86.         exitButton.setText("退出系统");
  87.         exitButton.addActionListener(new IPAssignFrm_exitButton_actionAdapter(this));
  88.         exitButton.setFont(new java.awt.Font("DialogInput", 1, 16));
  89.         contentPane.add(scrollpane, new XYConstraints(16, 22, 510, 202));
  90.         contentPane.add(jLabel1, new XYConstraints(30, 249, 167, 37));
  91.         contentPane.add(IPAddrTextField, new XYConstraints(214, 250, 152, 34));
  92.         contentPane.add(jLabel2, new XYConstraints(114, 357, 288, -1));
  93.         contentPane.add(exitButton, new XYConstraints(312, 301, 136, 31));
  94.         contentPane.add(queryButton, new XYConstraints(66, 299, 136, 31));
  95.     }
  96.     //Overridden so we can exit when window is closed
  97.     protected void processWindowEvent(WindowEvent e) {
  98.         super.processWindowEvent(e);
  99.         if (e.getID() == WindowEvent.WINDOW_CLOSING) {
  100.             System.exit(0);
  101.         }
  102.     }
  103.     void queryButton_actionPerformed(ActionEvent e) {
  104.         String strIP = IPAddrTextField.getText();
  105.         if (strIP.trim().length() == 0) {
  106.             JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
  107.             anOptionPane.showMessageDialog(this, "请输入待查询得IP地址!", "输入提示",
  108.                                            JOptionPane.ERROR_MESSAGE);
  109.             return;
  110.         }
  111.         QueryIP(strIP);
  112.     }
  113.     /**********************************************
  114.      这个函数的作用就是查询输入的IP地址
  115.      **********************************************/
  116.     private void QueryIP(String strIP) {
  117.         strIP = strIP.trim();
  118.         String strResult;
  119.         //检查IP地址格式
  120.         if (CheckIPFormat(strIP) == false) {
  121.             strResult = "对不起,IP地址输入错误!";
  122.             JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
  123.             anOptionPane.showMessageDialog(this, strResult, "IP地址输入格式错误",
  124.                                            JOptionPane.ERROR_MESSAGE);
  125.             return;
  126.         }
  127.         //将IP地址字符串中的'.'去掉
  128.         strIP = IngnoreIPDot(strIP);
  129.         //将去掉'.'的IP地址字符串转换为长整数
  130.         BigInteger bigIntIP = StringToBigInt(strIP);
  131.         //查找IP地址所属单位
  132.         strResult = CheckIPInList(bigIntIP);
  133.         JOptionPane anOptionPane = new JOptionPane(); //弹出对话框
  134.         anOptionPane.showMessageDialog(this, strResult, "查询结果",
  135.                                        JOptionPane.PLAIN_MESSAGE);
  136.     }
  137.     /**********************************************
  138.      这个函数的作用就是检查输入的IP地址的格式是否正确
  139.      **********************************************/
  140.     private boolean CheckIPFormat(String strIP) {
  141.         int intLen = strIP.length();
  142.         int index = 0;
  143.         if (strIP.length() > 15)
  144.             return false;
  145.         while (index < intLen) {
  146.             if ( ( (strIP.charAt(index) >= '0') && (strIP.charAt(index) <= '9'))
  147.                 || (strIP.charAt(index) == '.')) {
  148.                 index++;
  149.             }
  150.             else
  151.                 break;
  152.         }
  153.         if (index < intLen)
  154.             return false;
  155.         return true;
  156.     }
  157.     /********************************************************
  158.      这个函数的作用就是将IP地址中间的‘.’去掉将其连成一纯数字字符串
  159.      *********************************************************/
  160.     private String IngnoreIPDot(String strIP) {
  161.         String strTmp = new String("");
  162.         strIP = strIP.trim();
  163.         StringTokenizer token = new StringTokenizer(strIP, ".");
  164.         while (token.hasMoreElements()) {
  165.             strTmp = strTmp.concat( (String) token.nextElement());
  166.         }
  167.         return strTmp;
  168.     }
  169.     /**********************************************
  170.          这个函数的作用就是将数字字符串转换为长整数
  171.      **********************************************/
  172.     private BigInteger StringToBigInt(String strIP) {
  173.         return new BigInteger(strIP);
  174.     }
  175.     /**********************************************
  176.          这个函数的作用就是在已有IP地址列表中查找此IP地址,如果
  177.          找到就返回该IP地址的所属单位,如果找不到就报错
  178.      **********************************************/
  179.     public String CheckIPInList(BigInteger bigIntIP) {
  180.         SAXParseIPInfo parseIPInfo = new SAXParseIPInfo();
  181.         try {
  182.             parseIPInfo.ParseIPInfo();
  183.         }
  184.         catch (Exception e) {
  185.             e.printStackTrace();
  186.         }
  187.         Vector startIPVector = parseIPInfo.GetStartIPVector();
  188.         Vector endIPVector = parseIPInfo.GetEndIPVector();
  189.         Vector localVector = parseIPInfo.GetLocalVector();
  190.         int intLen = endIPVector.size();
  191.         for (int i = 1; i < intLen; i++) {
  192.             BigInteger bigIntStartIP = StringToBigInt(
  193.                 IngnoreIPDot( (String) startIPVector.elementAt(i)));
  194.             BigInteger bigIntEndIP = StringToBigInt(
  195.                 IngnoreIPDot( (String) endIPVector.elementAt(i)));
  196.             if ( ( (bigIntIP.compareTo(bigIntStartIP) == 1) &&
  197.                   (bigIntIP.compareTo(bigIntEndIP) == -1))
  198.                 || (bigIntIP.compareTo(bigIntStartIP) == 0)
  199.                 || (bigIntIP.compareTo(bigIntEndIP) == 0)) {
  200.                 String strResult = "此IP属于";
  201.                 strResult = strResult.concat( (String) localVector.elementAt(i));
  202.                 return strResult;
  203.             }
  204.         }
  205.         return "对不起,此IP不属于清华大学!";
  206.     }
  207.     void exitButton_actionPerformed(ActionEvent e) {
  208.         System.exit(0);
  209.     }
  210. }
  211. class IPAssignFrm_queryButton_actionAdapter implements java.awt.event.
  212.     ActionListener {
  213.     IPAssignFrm adaptee;
  214.     IPAssignFrm_queryButton_actionAdapter(IPAssignFrm adaptee) {
  215.         this.adaptee = adaptee;
  216.     }
  217.     public void actionPerformed(ActionEvent e) {
  218.         adaptee.queryButton_actionPerformed(e);
  219.     }
  220. }
  221. class IPInfoDataModel extends AbstractTableModel {
  222.     private Object[][] arrIPInfo;
  223.     private Vector startIPVector;
  224.     private Vector endIPVector;
  225.     private Vector localVector;
  226.     public IPInfoDataModel() {
  227.         SAXParseIPInfo parseIPInfo = new SAXParseIPInfo();
  228.         try {
  229.             parseIPInfo.ParseIPInfo();
  230.         }
  231.         catch (Exception e) {
  232.             e.printStackTrace();
  233.         }
  234.         startIPVector = parseIPInfo.GetStartIPVector();
  235.         endIPVector = parseIPInfo.GetEndIPVector();
  236.         localVector = parseIPInfo.GetLocalVector();
  237.         int intLen = startIPVector.size();
  238.         arrIPInfo = new Object[intLen][3];
  239.         for (int i = 0; i < intLen; i++) {
  240.             arrIPInfo[i] = new Object[3];
  241.         }
  242.         for (int m = 0; m < intLen-1; m++) {
  243.             //System.out.print(m);
  244.             arrIPInfo[m][0] = startIPVector.elementAt(m);
  245.             arrIPInfo[m][1] = endIPVector.elementAt(m);
  246.             arrIPInfo[m][2] = localVector.elementAt(m);
  247.         }
  248.     }
  249.     public int getColumnCount() {
  250.         return arrIPInfo[0].length;
  251.     }
  252.     public int getRowCount() {
  253.         return arrIPInfo.length -1;
  254.     }
  255.     public Object getValueAt(int row, int col) {
  256.         return arrIPInfo[row][col];
  257.     }
  258.     public boolean isCellEditable(int row, int col) {
  259.         return false;
  260.     }
  261. }
  262. class IPAssignFrm_exitButton_actionAdapter implements java.awt.event.
  263.     ActionListener {
  264.     IPAssignFrm adaptee;
  265.     IPAssignFrm_exitButton_actionAdapter(IPAssignFrm adaptee) {
  266.         this.adaptee = adaptee;
  267.     }
  268.     public void actionPerformed(ActionEvent e) {
  269.         adaptee.exitButton_actionPerformed(e);
  270.     }
  271. }