Package ch.rakudave.jnetmap.util

Source Code of ch.rakudave.jnetmap.util.DeviceEventFilter

package ch.rakudave.jnetmap.util;

import java.awt.GridLayout;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.BorderFactory;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import ch.rakudave.jnetmap.model.IF.NetworkIF;
import ch.rakudave.jnetmap.model.device.Device;
import ch.rakudave.jnetmap.model.device.DeviceEvent;
import ch.rakudave.jnetmap.model.device.DeviceEvent.Type;

public class DeviceEventFilter {
  private boolean onDeviceChange = true, onIFChange;
  private String deviceNameMatcher = "", statusMatcher = "", ipMatcher = "", subnetMatcher = "";
 
  public DeviceEventFilter() {}
 
  public DeviceEventFilter(boolean onDeviceChange, boolean onIFChange) {
    this.onDeviceChange = onDeviceChange;
    this.onIFChange = onIFChange;
  }
 
  public boolean matches(DeviceEvent e) {
    if (!onDeviceChange && !onIFChange) return false;
    Device d = (Device) e.getSource();
    if (!deviceNameMatcher.isEmpty() && !d.getName().matches(deviceNameMatcher)) return false;
    if (statusMatcher == null) statusMatcher = "";
    if (onDeviceChange && Type.STATUS_CHANGED.equals(e.getType())) {
      for (NetworkIF nif : d.getInterfaces()) {
        if (!statusMatcher.isEmpty() && !d.getStatus().getMessage().matches(statusMatcher)) return false;
        if (!ipMatcher.isEmpty() && nif.getAddress() != null
            && !nif.getAddress().getHostAddress().matches(ipMatcher)) return false;
        if (!subnetMatcher.isEmpty() && nif.getSubnet() != null
            && !nif.getSubnet().getInfo().getBroadcastAddress().matches(ipMatcher))  return false;
      }
    } else if (onIFChange && Type.INTERFACE_STATUS_CHANGED.equals(e.getType())) {
      NetworkIF nif = (NetworkIF) e.getSubject();
      if (!statusMatcher.isEmpty() && !nif.getStatus().getMessage().matches(statusMatcher)) return false;
      if (!ipMatcher.isEmpty() && nif.getAddress() != null
          && !nif.getAddress().getHostAddress().matches(ipMatcher)) return false;
      if (!subnetMatcher.isEmpty() && nif.getSubnet() != null
          && !nif.getSubnet().getInfo().getBroadcastAddress().matches(ipMatcher)) return false;
      return statusMatcher.isEmpty() && ipMatcher.isEmpty() && subnetMatcher.isEmpty();
    } else {
      return false;
    }
    return true;
  }
 
  public JPanel settingsPanel() {
    JPanel p = new JPanel(new GridLayout(0, 2, 5, 5));
      p.setBorder(BorderFactory.createTitledBorder(Lang.getNoHTML("filter")));
      final JCheckBox deviceChange = new JCheckBox(Lang.get("device"), onDeviceChange);
      final JCheckBox ifChange = new JCheckBox(Lang.get("interface"), onIFChange);
      final JTextField deviceFilter = new JTextField(deviceNameMatcher);
      final JTextField statusFilter = new JTextField(statusMatcher);
      final JTextField ipFilter = new JTextField(ipMatcher);
      final JTextField subFilter = new JTextField(subnetMatcher);
      FocusListener f = new FocusListener() {
        @Override public void focusGained(FocusEvent e) { }
        @Override
        public void focusLost(FocusEvent e) {
          onDeviceChange = deviceChange.isSelected();
          onIFChange = ifChange.isSelected();
          deviceNameMatcher = deviceFilter.getText();
          statusMatcher = statusFilter.getText();
          ipMatcher = ipFilter.getText();
          subnetMatcher = subFilter.getText();
        }
      };
        deviceChange.addFocusListener(f);
        ifChange.addFocusListener(f);
        deviceFilter.addFocusListener(f);
        ipFilter.addFocusListener(f);
        subFilter.addFocusListener(f);
      p.add(new JLabel(Lang.get("event.from"))); p.add(deviceChange);
      p.add(new JLabel(Lang.get("event.from"))); p.add(ifChange);
      p.add(new JLabel()); p.add(new JLabel("Regular Expression"));
      p.add(new JLabel(Lang.get("filter.name"))); p.add(deviceFilter);
      p.add(new JLabel(Lang.get("filter.status"))); p.add(statusFilter);
      p.add(new JLabel(Lang.get("filter.ip"))); p.add(ipFilter);
      p.add(new JLabel(Lang.get("filter.subnet"))); p.add(subFilter);
    return p;
  }

}
TOP

Related Classes of ch.rakudave.jnetmap.util.DeviceEventFilter

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.