284 lines
9.0 KiB
Java
284 lines
9.0 KiB
Java
package jnet.client.gui.dialog;
|
|
|
|
import java.awt.HeadlessException;
|
|
import java.awt.Image;
|
|
import java.awt.event.ActionEvent;
|
|
import java.awt.event.ActionListener;
|
|
import java.awt.event.ItemEvent;
|
|
import java.awt.event.ItemListener;
|
|
import java.io.File;
|
|
import java.io.IOException;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import javax.imageio.ImageIO;
|
|
import javax.swing.DefaultComboBoxModel;
|
|
import javax.swing.ImageIcon;
|
|
import javax.swing.JButton;
|
|
import javax.swing.JCheckBox;
|
|
import javax.swing.JComboBox;
|
|
import javax.swing.JFrame;
|
|
import javax.swing.JLabel;
|
|
import jnet.client.Client;
|
|
import jnet.client.client.NettyClientManager;
|
|
import jnet.lib.LogFile;
|
|
import jnet.lib.Message;
|
|
import jnet.lib.ResourceLoader;
|
|
import jnet.lib.object.Interface;
|
|
import jnet.lib.object.Node;
|
|
import jnet.lib.object.NodeConnection;
|
|
import jnet.lib.object.Site;
|
|
|
|
public class ConnectionDialog extends JFrame {
|
|
|
|
private Node startObj;
|
|
private Node endObj;
|
|
private Boolean newObject;
|
|
private List<Interface> iface = new ArrayList<>();
|
|
private NodeConnection connection = new NodeConnection();
|
|
private JLabel loadingLabel;
|
|
|
|
private JComboBox<String> typComboBox = new JComboBox<String>(new DefaultComboBoxModel(new String[]{
|
|
"Neznámé",
|
|
"Gigabit Ethernet",
|
|
"Fast Ethernet",
|
|
"Ethernet",
|
|
"10G Ethernet",
|
|
"100M Fiber",
|
|
"1G Fiber",
|
|
"10G Fiber",
|
|
"Bezdrát",
|
|
"Tunel",
|
|
"VLAN"
|
|
}));
|
|
|
|
private JCheckBox readTraffic = new JCheckBox("Číst traffic");
|
|
private JComboBox<String> sourceDeviceComboBox = new JComboBox<>();
|
|
private JComboBox<Item> sourceInterfaceComboBox = new JComboBox<>();
|
|
|
|
// public ConnectionDialog(Node startObj, Node endObj, boolean newObject) throws HeadlessException {
|
|
// this.startObj = startObj;
|
|
// this.endObj = endObj;
|
|
// this.newObject = newObject;
|
|
//
|
|
// init();
|
|
// }
|
|
public ConnectionDialog(NodeConnection connection, boolean newObject) throws HeadlessException {
|
|
this.connection = connection;
|
|
this.newObject = newObject;
|
|
|
|
// nastaveni pocatecniho a koncoveho objektu podle id
|
|
for (Site map : Client.site) {
|
|
for (Node object : map.getNode()) {
|
|
if (object.getId() == connection.getNodeFrom()) {
|
|
this.startObj = object;
|
|
}
|
|
if (object.getId() == connection.getNodeTo()) {
|
|
this.endObj = object;
|
|
}
|
|
}
|
|
}
|
|
|
|
sourceDeviceComboBox.setEnabled(false);
|
|
sourceInterfaceComboBox.setEnabled(false);
|
|
|
|
if (!newObject) {
|
|
if (connection.isReadTraffic()) {
|
|
readTraffic.setSelected(true);
|
|
sourceDeviceComboBox.setEnabled(true);
|
|
sourceDeviceComboBox.setSelectedItem(startObj.getName());
|
|
sourceInterfaceComboBox.setEnabled(true);
|
|
sourceInterfaceComboBox.setSelectedItem(connection.getTrafficIface());
|
|
}
|
|
}
|
|
|
|
init();
|
|
}
|
|
|
|
private void init() {
|
|
|
|
// identifikator okna
|
|
this.setName("connection_detail_" + Math.random());
|
|
|
|
if (newObject) {
|
|
setTitle("Nové spojení");
|
|
} else {
|
|
setTitle("Vlastnosti spojení");
|
|
}
|
|
|
|
setSize(500, 500);
|
|
setLocationRelativeTo(null);
|
|
setAlwaysOnTop(false);
|
|
setResizable(false);
|
|
setVisible(true);
|
|
|
|
setLayout(null);
|
|
|
|
int x = 20;
|
|
|
|
JLabel firstLabel = new JLabel("1: " + startObj.getName());
|
|
firstLabel.setBounds(x, 10, 500, 25);
|
|
add(firstLabel);
|
|
|
|
JLabel secondLabel = new JLabel("2: " + endObj.getName());
|
|
secondLabel.setBounds(x, 40, 500, 25);
|
|
add(secondLabel);
|
|
|
|
JLabel typLabel = new JLabel("Typ spojení");
|
|
typLabel.setBounds(x, 100, 140, 25);
|
|
add(typLabel);
|
|
|
|
try {
|
|
loadingLabel = new JLabel();
|
|
loadingLabel.setBounds(410, 300, 35, 35);
|
|
Image image = ImageIO.read(ResourceLoader.getFile("/img/wait.png"));
|
|
loadingLabel.setIcon(new ImageIcon(image));
|
|
loadingLabel.setVisible(false);
|
|
add(loadingLabel);
|
|
} catch (IOException ex) {
|
|
LogFile.printErr(ex.getMessage());
|
|
}
|
|
|
|
typComboBox.setBounds(200, 100, 200, 25);
|
|
add(typComboBox);
|
|
|
|
readTraffic.setBounds(x, 200, 200, 25);
|
|
readTraffic.addItemListener(new ItemListener() {
|
|
@Override
|
|
public void itemStateChanged(ItemEvent e) {
|
|
if (readTraffic.isSelected()) {
|
|
sourceDeviceComboBox.setEnabled(true);
|
|
sourceInterfaceComboBox.setEnabled(true);
|
|
loadIface();
|
|
} else {
|
|
sourceDeviceComboBox.setEnabled(false);
|
|
sourceInterfaceComboBox.setEnabled(false);
|
|
}
|
|
}
|
|
});
|
|
add(readTraffic);
|
|
|
|
JLabel sourceDeviceLabel = new JLabel("Zdrojové zařízení:");
|
|
sourceDeviceLabel.setBounds(x, 250, 140, 25);
|
|
add(sourceDeviceLabel);
|
|
|
|
sourceDeviceComboBox.addItem(startObj.getName());
|
|
sourceDeviceComboBox.addItem(endObj.getName());
|
|
sourceDeviceComboBox.setBounds(200, 250, 200, 25);
|
|
sourceDeviceComboBox.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
loadIface();
|
|
}
|
|
});
|
|
add(sourceDeviceComboBox);
|
|
|
|
JLabel sourceInterfaceLabel = new JLabel("Zdrojové rozhraní:");
|
|
sourceInterfaceLabel.setBounds(x, 300, 140, 25);
|
|
add(sourceInterfaceLabel);
|
|
|
|
sourceInterfaceComboBox.setBounds(200, 300, 200, 25);
|
|
add(sourceInterfaceComboBox);
|
|
|
|
JButton saveButton = new JButton("Uložit");
|
|
saveButton.setBounds((getWidth() / 2) - 120, 400, 90, 25);
|
|
saveButton.addActionListener(new ActionListener() {
|
|
@Override
|
|
public void actionPerformed(ActionEvent e) {
|
|
save();
|
|
dispose();
|
|
}
|
|
});
|
|
add(saveButton);
|
|
|
|
JButton closeButton = new JButton("Storno");
|
|
closeButton.setBounds((getWidth() / 2) + 30, 400, 90, 25);
|
|
closeButton.addActionListener(new ActionListener() {
|
|
public void actionPerformed(ActionEvent e) {
|
|
dispose();
|
|
}
|
|
});
|
|
add(closeButton);
|
|
|
|
if (!newObject) {
|
|
|
|
// nastaveni hodnot upravovaného spojení
|
|
typComboBox.setSelectedIndex(connection.getType());
|
|
readTraffic.setSelected(connection.isReadTraffic());
|
|
sourceDeviceComboBox.setSelectedIndex((connection.getTrafficNode() == startObj.getId() ? 0 : 1));
|
|
}
|
|
|
|
}
|
|
|
|
private void loadIface() {
|
|
loadingLabel.setVisible(true);
|
|
Node obj;
|
|
if (sourceDeviceComboBox.getSelectedIndex() == 0) {
|
|
obj = startObj;
|
|
} else {
|
|
obj = endObj;
|
|
}
|
|
Object[] o = {this.getName(), obj};
|
|
NettyClientManager.sendMessage(Message.NODE_CONNECTION_SNMP_INTERFACE, o);
|
|
}
|
|
|
|
public void updateInterface(List<Interface> iface) {
|
|
loadingLabel.setVisible(false);
|
|
this.iface = iface;
|
|
|
|
sourceInterfaceComboBox.removeAllItems();
|
|
|
|
for (Interface ifs : iface) {
|
|
Item item = new Item(ifs.getId(), ifs.getDescription());
|
|
sourceInterfaceComboBox.addItem(item);
|
|
// pokud se nejedna o nový objekt a souhlasi index rozhrani nastavit polozku v seznamu
|
|
if (!newObject && ifs.getId() == connection.getTrafficIface()) {
|
|
// po nacteni rozhrani nastav vybrane
|
|
sourceInterfaceComboBox.setSelectedItem(item);
|
|
}
|
|
}
|
|
}
|
|
|
|
private void save() {
|
|
Item sellectedIface = (Item) sourceInterfaceComboBox.getSelectedItem();
|
|
|
|
connection.setType(typComboBox.getSelectedIndex());
|
|
connection.setReadTraffic(readTraffic.isSelected());
|
|
if (readTraffic.isSelected()) {
|
|
// pokud je zapnute cteni nastavi zdrojovy objekt a interface
|
|
connection.setTrafficNode(sourceDeviceComboBox.getSelectedIndex() == 0 ? startObj.getId() : endObj.getId());
|
|
connection.setTrafficIface(sellectedIface.getId());
|
|
}
|
|
|
|
if (newObject) {
|
|
connection.setNodeFrom(startObj.getId());
|
|
connection.setNodeTo(endObj.getId());
|
|
connection.setSite(startObj.getSiteId());
|
|
NettyClientManager.sendMessage(Message.NODE_CONNECTION_NEW, connection);
|
|
} else {
|
|
NettyClientManager.sendMessage(Message.NODE_CONNECTION_EDIT, connection);
|
|
}
|
|
|
|
}
|
|
|
|
class Item {
|
|
|
|
private int id;
|
|
private String name;
|
|
|
|
public Item(int id, String name) {
|
|
this.id = id;
|
|
this.name = name;
|
|
}
|
|
|
|
public int getId() {
|
|
return id;
|
|
}
|
|
|
|
@Override
|
|
public String toString() {
|
|
return name; // Zobrazí se pouze název
|
|
}
|
|
}
|
|
|
|
}
|