netcap.JcaptureConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for netcap.JcaptureConfiguration.java

Source

package netcap;

import java.awt.Component;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.JTextPane;

import org.apache.http.util.TextUtils;
import jpcap.NetworkInterface;
import jpcap.JpcapCaptor;
import netcap.view.ViewModules;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
@SuppressWarnings("restriction")
public class JcaptureConfiguration extends javax.swing.JDialog implements ActionListener {

    private static final long serialVersionUID = 1L;

    /**
     * Auto-generated main method to display this JDialog
     */
    private static JpcapCaptor jpcap = null;
    private JTextField caplenTextField, proFilterField, domainFilterField;
    private JCheckBox checkBox;
    private JComboBox<?> netJComboBox;
    private JFrame frame;

    private NetworkInterface[] devices;
    private static String domain;

    public JcaptureConfiguration(JFrame frame) {
        super(frame, "???", true);
        this.frame = frame;
        initGUI();
    }

    private void initGUI() {
        Container container = getContentPane();
        container.setLayout(ViewModules.createBoxLayout(container, BoxLayout.X_AXIS));
        try {
            JPanel jPanel_west = ViewModules.createSimplePanel(null, -1f, -1f, BoxLayout.Y_AXIS);
            JPanel netPanel = getNetDevicesPanel();
            checkBox = getPromiscCheckBox();
            JPanel filterPanel = getProtocolFilterPanel();
            ViewModules.addComponent(jPanel_west, netPanel, checkBox, filterPanel);

            JPanel jPanel_east = ViewModules.createSimplePanel(null, -1f, -1f, BoxLayout.Y_AXIS);
            JPanel caplenPanel = getCaplenPanel();
            JPanel buttonPanel = getOkCanelPanel();
            ViewModules.addComponent(jPanel_east, caplenPanel, buttonPanel);

            container.add(jPanel_west);
            container.add(jPanel_east);
            pack();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void actionPerformed(ActionEvent evt) {
        Command cmd = Command.getCommand(evt.getActionCommand());
        switch (cmd) {
        case WHOLE:
            caplenTextField.setText("1514");
            caplenTextField.setEnabled(false);
            break;
        case HEAD:
            caplenTextField.setText("68");
            caplenTextField.setEnabled(false);
            break;
        case OTHER:
            caplenTextField.setText("");
            caplenTextField.setEnabled(true);
            caplenTextField.requestFocus();
            break;
        case OK:
            saveConfiguration();
            break;
        case CANCEL:
            dispose();
            break;
        default:
            break;
        }
    }

    public static JpcapCaptor getJpcap(JFrame parent) {
        new JcaptureConfiguration(parent).setVisible(true);
        return jpcap;
    }

    /**
     * ????
     * @return
     */
    public static String getDomainFilter() {
        return domain;
    }

    /**
     * ??
     */
    private void saveConfiguration() {
        try {
            int caplen = Integer.parseInt(caplenTextField.getText());
            if (caplen < 68 || caplen > 1514) {
                ViewModules.showMessageDialog(null, "? 68  1514");
                return;
            }
            NetworkInterface netInter = devices[netJComboBox.getSelectedIndex()];
            boolean isPromisc = checkBox.isSelected();
            jpcap = JpcapCaptor.openDevice(netInter, caplen, isPromisc, 50);
            if (!TextUtils.isEmpty(proFilterField.getText()))
                jpcap.setFilter(proFilterField.getText(), true);
            if (!TextUtils.isEmpty(domainFilterField.getText()))
                domain = domainFilterField.getText().trim();
        } catch (NumberFormatException e) {
            ViewModules.showMessageDialog(null, "?");
        } catch (java.io.IOException e) {
            ViewModules.showMessageDialog(null, e.toString());
            jpcap = null;
        } finally {
            dispose();
        }
    }

    /**
     * ??
     * @return
     */
    private JPanel getNetDevicesPanel() {
        JPanel netPanel = ViewModules.createSimplePanel("?", Component.LEFT_ALIGNMENT, -1f, true);
        String[] names = getNetDeviceList();
        if (names.length == 0)
            ViewModules.showMessageDialog(frame, "?");
        netJComboBox = ViewModules.createComboBox(names);
        netPanel.add(netJComboBox);
        return netPanel;
    }

    /**
     * ????
     * @return
     */
    private String[] getNetDeviceList() {
        devices = JpcapCaptor.getDeviceList();
        if (devices == null) {
            dispose();
            return new String[0];
        } else {
            String[] names = new String[devices.length];
            for (int i = 0; i < names.length; i++) {
                names[i] = (devices[i].description == null ? devices[i].name : devices[i].description);
            }
            return names;
        }
    }

    /**
     * ????
     * @return
     */
    private JCheckBox getPromiscCheckBox() {
        FlowLayout checkBoxLayout = new FlowLayout();
        checkBoxLayout.setAlignOnBaseline(true);
        JCheckBox checkBox = ViewModules.createCheckBox("???", checkBoxLayout);
        return checkBox;
    }

    /**
     * ????
     * @return
     */
    private JPanel getProtocolFilterPanel() {
        JPanel filterPanel = ViewModules.createSimplePanel("?", Component.LEFT_ALIGNMENT, -1f,
                BoxLayout.Y_AXIS);
        JTextPane proType = ViewModules.createTextPane("???:");
        proFilterField = ViewModules.createTextField(20, "tcp", true);
        ViewModules.createSeparator(0);
        JTextPane domain = ViewModules.createTextPane("???/IP:");
        domainFilterField = ViewModules.createTextField(20, "", true);
        ViewModules.addComponent(filterPanel, proType, proFilterField, domain, domainFilterField);
        return filterPanel;
    }

    /**
     * ????
     * @return
     */
    private JPanel getCaplenPanel() {
        JPanel caplenPanel = ViewModules.createSimplePanel("", Component.CENTER_ALIGNMENT, -1f,
                BoxLayout.Y_AXIS);
        caplenTextField = ViewModules.createTextField(20, "1514", false);
        JRadioButton wholeRadioButton = ViewModules.createRadioButton("?", Command.WHOLE.getName(),
                this);
        wholeRadioButton.setSelected(true);
        JRadioButton headRadioButton = ViewModules.createRadioButton("", Command.HEAD.getName(), this);
        JRadioButton otherRadioButton = ViewModules.createRadioButton("", Command.OTHER.getName(),
                this);
        ViewModules.addComponent(caplenPanel, caplenTextField, wholeRadioButton, headRadioButton, otherRadioButton);
        // ?????
        ViewModules.createButtonGroup(wholeRadioButton, headRadioButton, otherRadioButton);
        return caplenPanel;
    }

    /**
     * ???
     * @return
     */
    private JPanel getOkCanelPanel() {
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
        JButton ok = ViewModules.createButton("", Command.OK.getName(), this);
        JButton cancel = ViewModules.createButton("?", Command.CANCEL.getName(), this);
        ViewModules.addComponent(buttonPanel, ok, cancel);
        return buttonPanel;
    }

    private enum Command {
        WHOLE("WHOLE"), HEAD("HEAD"), OTHER("OTHER"), OK("OK"), CANCEL("CANCEL");
        private String name;

        private Command(String name) {
            this.name = name;
        }

        public String getName() {
            return name;
        }

        public static Command getCommand(String name) {
            for (Command e : Command.values()) {
                if (e.getName().equals(name))
                    return e;
            }
            return null;
        }
    }

    //   public static void main(String[] args) {
    //      JFrame frame = new JFrame();
    //      JcaptureConfiguration inst = new JcaptureConfiguration(frame);
    //      inst.setVisible(true);
    //   }

}