upravy a editovani connection

main
Michal 2025-02-24 20:04:25 +01:00
parent 635f301b1d
commit da11df8f29
5 changed files with 161 additions and 17 deletions

View File

@ -55,6 +55,7 @@ public class ClientMessageParser {
handlers.put(Message.SNMP_CONNECTION_INTERFACE, ClientMessageParser::handleSnmpConnectionInterface); handlers.put(Message.SNMP_CONNECTION_INTERFACE, ClientMessageParser::handleSnmpConnectionInterface);
handlers.put(Message.CONNECTION_NEW, ClientMessageParser::handleConnectionNew); handlers.put(Message.CONNECTION_NEW, ClientMessageParser::handleConnectionNew);
handlers.put(Message.TRAFFIC, ClientMessageParser::handlerTraffic); handlers.put(Message.TRAFFIC, ClientMessageParser::handlerTraffic);
handlers.put(Message.CONNECTION_UPDATE, ClientMessageParser::handlerConnectionUpdate);
} }
@ -255,6 +256,7 @@ public class ClientMessageParser {
for (Map map : Client.maps) { for (Map map : Client.maps) {
if (map.getId() == c.getMap()) { if (map.getId() == c.getMap()) {
map.getConnection().add(c); map.getConnection().add(c);
System.out.println("jnet.client.ClientMessageParser.handleConnectionNew()");
} }
} }
UIUpdater.updateMapView(); UIUpdater.updateMapView();
@ -272,4 +274,19 @@ public class ClientMessageParser {
} }
} }
} }
private static void handlerConnectionUpdate(Message msg) {
Connection c = (Connection) msg.getMsg();
// upravi v seznamu
for (Map map : Client.maps) {
List<Connection> connections = map.getConnection();
for (int i = 0; i < connections.size(); i++) {
if (connections.get(i).getId() == c.getId()) {
connections.set(i, c);
}
}
}
// update map view
UIUpdater.updateMapView();
}
} }

View File

@ -13,9 +13,11 @@ import javax.swing.JCheckBox;
import javax.swing.JComboBox; import javax.swing.JComboBox;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.JLabel; import javax.swing.JLabel;
import jnet.client.Client;
import jnet.client.network.NettyClient; import jnet.client.network.NettyClient;
import jnet.lib.Message; import jnet.lib.Message;
import jnet.lib.object.Connection; import jnet.lib.object.Connection;
import jnet.lib.object.Map;
import jnet.lib.object.MapObject; import jnet.lib.object.MapObject;
import jnet.lib.snmp.Interface; import jnet.lib.snmp.Interface;
@ -25,6 +27,7 @@ public class ConnectionDialog extends JFrame {
private MapObject endObj; private MapObject endObj;
private Boolean newObject; private Boolean newObject;
private List<Interface> iface = new ArrayList<>(); private List<Interface> iface = new ArrayList<>();
private Connection connection = new Connection();
private JComboBox<String> typComboBox = new JComboBox<String>(new DefaultComboBoxModel(new String[]{ private JComboBox<String> typComboBox = new JComboBox<String>(new DefaultComboBoxModel(new String[]{
"Neznámé", "Neznámé",
@ -47,15 +50,35 @@ public class ConnectionDialog extends JFrame {
public ConnectionDialog(MapObject startObj, MapObject endObj) throws HeadlessException { public ConnectionDialog(MapObject startObj, MapObject endObj) throws HeadlessException {
this.startObj = startObj; this.startObj = startObj;
this.endObj = endObj; this.endObj = endObj;
this.newObject = true;
init();
}
public ConnectionDialog(Connection connection) throws HeadlessException {
this.connection = connection;
this.newObject = false; this.newObject = false;
// identifikator okna // nastaveni pocatecniho a koncoveho objektu podle id
this.setName("connection_detail_" + Math.random()); for (Map map : Client.maps) {
for (MapObject object : map.getObjects()) {
if (object.getId() == connection.getSourceObj()) {
this.startObj = object;
}
if (object.getId() == connection.getDestinationObj()) {
this.endObj = object;
}
}
}
init(); init();
} }
private void init() { private void init() {
// identifikator okna
this.setName("connection_detail_" + Math.random());
if (newObject) { if (newObject) {
setTitle("Nové spojení"); setTitle("Nové spojení");
} else { } else {
@ -147,6 +170,14 @@ public class ConnectionDialog extends JFrame {
}); });
add(closeButton); add(closeButton);
if (!newObject) {
// nastaveni hodnot upravovaného spojení
typComboBox.setSelectedIndex(connection.getType());
readTraffic.setSelected(connection.isReadTraffic());
sourceDeviceComboBox.setSelectedIndex((connection.getTrafficObject() == startObj.getId() ? 0 : 1));
}
} }
private void loadIface() { private void loadIface() {
@ -166,22 +197,36 @@ public class ConnectionDialog extends JFrame {
sourceInterfaceComboBox.removeAllItems(); sourceInterfaceComboBox.removeAllItems();
for (Interface ifs : iface) { for (Interface ifs : iface) {
sourceInterfaceComboBox.addItem(new Item(ifs.getId(), ifs.getDescription())); 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() { private void save() {
Item sellectedIface = (Item) sourceInterfaceComboBox.getSelectedItem(); Item sellectedIface = (Item) sourceInterfaceComboBox.getSelectedItem();
Connection c = new Connection(
startObj.getId(), connection.setType(typComboBox.getSelectedIndex());
endObj.getId(), connection.setReadTraffic(readTraffic.isSelected());
typComboBox.getSelectedIndex(), if (readTraffic.isSelected()) {
startObj.getMap(), // pokud je zapnute cteni nastavi zdrojovy objekt a interface
readTraffic.isSelected(), connection.setTrafficObject(sourceDeviceComboBox.getSelectedIndex() == 0 ? startObj.getId() : endObj.getId());
sourceDeviceComboBox.getSelectedIndex() == 0 ? startObj.getId() : endObj.getId(), connection.setTrafficIface(sellectedIface.getId());
sellectedIface.getId() }
);
NettyClient.send(Message.CONNECTION_NEW, c); if (newObject) {
connection.setSourceObj(startObj.getId());
connection.setDestinationObj(endObj.getId());
connection.setMap(startObj.getMap());
NettyClient.send(Message.CONNECTION_NEW, connection);
} else {
NettyClient.send(Message.CONNECTION_UPDATE, connection);
}
} }
class Item { class Item {

View File

@ -0,0 +1,64 @@
package jnet.client.gui.mapview;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.Line2D;
import jnet.client.gui.dialog.ConnectionDialog;
public class ConnectionListener implements MouseListener {
private final double TOLERANCE = 10;
private PaintConnection selectedConnection;
@Override
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) {
// dvojité kliknutí
if (MapView.paintConnection == null) {
return;
}
selectedConnection = findObjectAt(e.getPoint());
if (selectedConnection != null) {
new ConnectionDialog(selectedConnection.getConnection());
}
}
}
@Override
public void mousePressed(MouseEvent e) {
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
private boolean isPointNearLine(Point p, Line2D line, double tolerance) {
return line.ptSegDist(p) <= tolerance; // Zkontroluje vzdálenost bodu od čáry
}
private PaintConnection findObjectAt(Point p) {
for (PaintConnection node : MapView.paintConnection) {
Line2D.Double line = new Line2D.Double(node.getFrom().getX(), node.getFrom().getY(), node.getTo().getX(), node.getTo().getY());
//node.setSelected(false);
if (isPointNearLine(p, line, TOLERANCE)) {
return node;
}
}
return null;
}
}

View File

@ -47,6 +47,7 @@ public class MapView extends JPanel {
this.addMouseMotionListener(new MapViewAction(map)); this.addMouseMotionListener(new MapViewAction(map));
this.addMouseListener(new MapViewAction(map)); this.addMouseListener(new MapViewAction(map));
this.addMouseListener(new ConnectionListener());
// periodicke prekreslovani // periodicke prekreslovani
Timer timer = new Timer(100, (ActionEvent ae) -> { Timer timer = new Timer(100, (ActionEvent ae) -> {
@ -91,15 +92,20 @@ public class MapView extends JPanel {
public static void updateMapView(Map map) { public static void updateMapView(Map map) {
if (map.equals(MapView.map)) { if (map.equals(MapView.map)) {
// obnoveni propojeni
paintConnection.clear();
loadConnection(map.getId());
// obnoveni objektu // obnoveni objektu
paintObjects.clear(); paintObjects.clear();
// nahrat objekty
loadObject(map.getId()); loadObject(map.getId());
} }
} }
public static void updateMapView() { public static void updateMapView() {
if (map.equals(MapView.map)) { if (map.equals(MapView.map)) {
// obnoveni propojeni
paintConnection.clear();
loadConnection(map.getId());
// obnoveni objektu // obnoveni objektu
paintObjects.clear(); paintObjects.clear();
// nahrat objekty // nahrat objekty

View File

@ -180,4 +180,16 @@ public class PaintConnection extends Rectangle {
return fm.stringWidth(text); return fm.stringWidth(text);
} }
public Point getFrom() {
return this.from;
}
public Point getTo() {
return this.to;
}
public Connection getConnection() {
return this.con;
}
} }