upravy a editovani connection
parent
635f301b1d
commit
da11df8f29
|
|
@ -55,6 +55,7 @@ public class ClientMessageParser {
|
|||
handlers.put(Message.SNMP_CONNECTION_INTERFACE, ClientMessageParser::handleSnmpConnectionInterface);
|
||||
handlers.put(Message.CONNECTION_NEW, ClientMessageParser::handleConnectionNew);
|
||||
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) {
|
||||
if (map.getId() == c.getMap()) {
|
||||
map.getConnection().add(c);
|
||||
System.out.println("jnet.client.ClientMessageParser.handleConnectionNew()");
|
||||
}
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -13,9 +13,11 @@ import javax.swing.JCheckBox;
|
|||
import javax.swing.JComboBox;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import jnet.client.Client;
|
||||
import jnet.client.network.NettyClient;
|
||||
import jnet.lib.Message;
|
||||
import jnet.lib.object.Connection;
|
||||
import jnet.lib.object.Map;
|
||||
import jnet.lib.object.MapObject;
|
||||
import jnet.lib.snmp.Interface;
|
||||
|
||||
|
|
@ -25,6 +27,7 @@ public class ConnectionDialog extends JFrame {
|
|||
private MapObject endObj;
|
||||
private Boolean newObject;
|
||||
private List<Interface> iface = new ArrayList<>();
|
||||
private Connection connection = new Connection();
|
||||
|
||||
private JComboBox<String> typComboBox = new JComboBox<String>(new DefaultComboBoxModel(new String[]{
|
||||
"Neznámé",
|
||||
|
|
@ -47,15 +50,35 @@ public class ConnectionDialog extends JFrame {
|
|||
public ConnectionDialog(MapObject startObj, MapObject endObj) throws HeadlessException {
|
||||
this.startObj = startObj;
|
||||
this.endObj = endObj;
|
||||
this.newObject = true;
|
||||
|
||||
init();
|
||||
}
|
||||
|
||||
public ConnectionDialog(Connection connection) throws HeadlessException {
|
||||
this.connection = connection;
|
||||
this.newObject = false;
|
||||
|
||||
// identifikator okna
|
||||
this.setName("connection_detail_" + Math.random());
|
||||
// nastaveni pocatecniho a koncoveho objektu podle id
|
||||
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();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
|
||||
// identifikator okna
|
||||
this.setName("connection_detail_" + Math.random());
|
||||
|
||||
if (newObject) {
|
||||
setTitle("Nové spojení");
|
||||
} else {
|
||||
|
|
@ -147,6 +170,14 @@ public class ConnectionDialog extends JFrame {
|
|||
});
|
||||
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() {
|
||||
|
|
@ -166,22 +197,36 @@ public class ConnectionDialog extends JFrame {
|
|||
sourceInterfaceComboBox.removeAllItems();
|
||||
|
||||
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() {
|
||||
Item sellectedIface = (Item) sourceInterfaceComboBox.getSelectedItem();
|
||||
Connection c = new Connection(
|
||||
startObj.getId(),
|
||||
endObj.getId(),
|
||||
typComboBox.getSelectedIndex(),
|
||||
startObj.getMap(),
|
||||
readTraffic.isSelected(),
|
||||
sourceDeviceComboBox.getSelectedIndex() == 0 ? startObj.getId() : endObj.getId(),
|
||||
sellectedIface.getId()
|
||||
);
|
||||
NettyClient.send(Message.CONNECTION_NEW, c);
|
||||
|
||||
connection.setType(typComboBox.getSelectedIndex());
|
||||
connection.setReadTraffic(readTraffic.isSelected());
|
||||
if (readTraffic.isSelected()) {
|
||||
// pokud je zapnute cteni nastavi zdrojovy objekt a interface
|
||||
connection.setTrafficObject(sourceDeviceComboBox.getSelectedIndex() == 0 ? startObj.getId() : endObj.getId());
|
||||
connection.setTrafficIface(sellectedIface.getId());
|
||||
}
|
||||
|
||||
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 {
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
@ -47,6 +47,7 @@ public class MapView extends JPanel {
|
|||
|
||||
this.addMouseMotionListener(new MapViewAction(map));
|
||||
this.addMouseListener(new MapViewAction(map));
|
||||
this.addMouseListener(new ConnectionListener());
|
||||
|
||||
// periodicke prekreslovani
|
||||
Timer timer = new Timer(100, (ActionEvent ae) -> {
|
||||
|
|
@ -91,15 +92,20 @@ public class MapView extends JPanel {
|
|||
|
||||
public static void updateMapView(Map map) {
|
||||
if (map.equals(MapView.map)) {
|
||||
// obnoveni propojeni
|
||||
paintConnection.clear();
|
||||
loadConnection(map.getId());
|
||||
// obnoveni objektu
|
||||
paintObjects.clear();
|
||||
// nahrat objekty
|
||||
loadObject(map.getId());
|
||||
}
|
||||
}
|
||||
|
||||
public static void updateMapView() {
|
||||
if (map.equals(MapView.map)) {
|
||||
// obnoveni propojeni
|
||||
paintConnection.clear();
|
||||
loadConnection(map.getId());
|
||||
// obnoveni objektu
|
||||
paintObjects.clear();
|
||||
// nahrat objekty
|
||||
|
|
|
|||
|
|
@ -180,4 +180,16 @@ public class PaintConnection extends Rectangle {
|
|||
return fm.stringWidth(text);
|
||||
}
|
||||
|
||||
public Point getFrom() {
|
||||
return this.from;
|
||||
}
|
||||
|
||||
public Point getTo() {
|
||||
return this.to;
|
||||
}
|
||||
|
||||
public Connection getConnection() {
|
||||
return this.con;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue