pridání, editace a smazani snmp profilu

verze3
Michal 2026-08-29 00:25:33 +02:00
parent 90760ec544
commit 7aa79c0b04
9 changed files with 380 additions and 6 deletions

View File

@ -5,6 +5,7 @@ import java.util.List;
import jnet.client.client.NettyClientManager;
import jnet.client.gui.Tray;
import jnet.client.gui.Window;
import jnet.client.gui.dialog.DialogSnmpProfile;
import jnet.lib.ApacheConfig;
import jnet.lib.BuilddDate;
import jnet.lib.LogFile;
@ -24,7 +25,7 @@ import org.apache.commons.configuration2.ex.ConfigurationException;
public class Client {
// verze klienta
public static final int VERSION = 1;
public static final int VERSION = 3;
// jméno aplikace
public static final String APP_NAME = "jNet client";
@ -120,7 +121,10 @@ public class Client {
remoteTools.closeAll(); // ZAVŘE VŠECHNY SPUŠTĚNÉ PROCESY
}
}));
}
public static void sendMessage(Message msg) {
nettyClient.sendMessage(msg);

View File

@ -323,6 +323,12 @@ public class Window extends JFrame {
currentServerSettingsPanel.refreshUsers();
}
}
public static void refreshSnmpProfiles() {
if (currentServerSettingsPanel != null) {
currentServerSettingsPanel.refreshSnmpProfiles();
}
}
public static void refreshMapTree() {
if (instance != null) {

View File

@ -0,0 +1,214 @@
package jnet.client.gui.dialog;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import jnet.client.client.NettyClientManager;
import jnet.lib.Message;
import jnet.lib.object.SnmpProfile;
public class DialogSnmpProfile extends JDialog {
private SnmpProfile profile;
private boolean newProfile;
private JTextField nameField = new JTextField(15);
private JTextField readField = new JTextField(15);
private JTextField portField = new JTextField(15);
private JComboBox<Item> vesrionComboBox = new JComboBox<>();
public DialogSnmpProfile(SnmpProfile profile, boolean newProfile) {
this.profile = profile;
this.newProfile = newProfile;
prepare();
}
public DialogSnmpProfile(SnmpProfile profile) {
this.profile = profile;
this.newProfile = false;
prepare();
}
private void prepare() {
if (!newProfile) {
nameField.setText(profile.getName());
readField.setText(profile.getCommunityRead());
portField.setText(profile.getPort());
} else {
profile = new SnmpProfile();
portField.setText("161");
}
// nastaveni okna
setModal(true);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
init();
pack();
setLocationRelativeTo(null);
setSize(400, getHeight()); // zachová výšku z pack(), ale šířku nastaví pevně
setResizable(false);
}
private void init() {
if (newProfile) {
setTitle("Přidat SNMP profil");
} else {
setTitle(profile.getName());
}
JPanel panel = new JPanel(new GridBagLayout());
// Přidá vnitřní okraj 10 pixelů od všech okrajů dialogu
panel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
GridBagConstraints gbc = new GridBagConstraints();
gbc.insets = new Insets(5, 10, 5, 10);
gbc.fill = GridBagConstraints.HORIZONTAL;
gbc.anchor = GridBagConstraints.WEST;
int row = 0;
JLabel nameLabel = new JLabel("Název");
gbc.gridx = 0;
gbc.gridy = row;
gbc.weightx = 1.0;
gbc.gridwidth = 1;
panel.add(nameLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
panel.add(nameField, gbc);
row++;
JLabel readLabel = new JLabel("Community read");
gbc.gridx = 0;
gbc.gridy = row;
gbc.weightx = 1.0;
gbc.gridwidth = 1;
panel.add(readLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
panel.add(readField, gbc);
row++;
JLabel portLabel = new JLabel("Port");
gbc.gridx = 0;
gbc.gridy = row;
gbc.weightx = 1.0;
gbc.gridwidth = 1;
panel.add(portLabel, gbc);
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
panel.add(portField, gbc);
row++;
JLabel verzeLabel = new JLabel("Port");
gbc.gridx = 0;
gbc.gridy = row;
gbc.weightx = 1.0;
gbc.gridwidth = 1;
panel.add(verzeLabel, gbc);
vesrionComboBox.addItem(new Item(SnmpProfile.version1, "Verze 1"));
vesrionComboBox.addItem(new Item(SnmpProfile.version2c, "Verze 2c"));
vesrionComboBox.addItem(new Item(SnmpProfile.version3, "Verze 3"));
gbc.gridx = 1;
gbc.gridwidth = 2;
gbc.weightx = 0.0;
gbc.gridwidth = 2;
panel.add(vesrionComboBox, gbc);
row++;
JButton actionBtn = new JButton(newProfile ? "Přidat" : "Upravit");
JButton cancelBtn = new JButton("Storno");
actionBtn.addActionListener(e -> performAction());
cancelBtn.addActionListener(e -> dispose());
gbc.gridwidth = 1;
gbc.gridx = 0;
gbc.gridy = row;
panel.add(actionBtn, gbc);
gbc.gridx = 2;
panel.add(cancelBtn, gbc);
add(panel);
}
private void performAction() {
String name = nameField.getText().trim();
if (name.length() < 3) {
JOptionPane.showMessageDialog(this, "Název musí mít alespoň 3 znaky.", "Chyba", JOptionPane.WARNING_MESSAGE);
return;
}
String read = readField.getText().trim();
if (read.isEmpty()) {
JOptionPane.showMessageDialog(this, "Community read nesmí být prázdné.", "Chyba", JOptionPane.WARNING_MESSAGE);
return;
}
String port = portField.getText().trim();
if (read.isEmpty()) {
JOptionPane.showMessageDialog(this, "Port nesmí být prázdné.", "Chyba", JOptionPane.WARNING_MESSAGE);
return;
}
profile.setName(name);
profile.setCommunityRead(read);
profile.setPort(port);
Item version = (Item) vesrionComboBox.getSelectedItem();
profile.setVersion(version.getId());
if (newProfile) {
NettyClientManager.sendMessage(Message.SNMP_PROFILE_NEW, profile);
} else{
NettyClientManager.sendMessage(Message.SNMP_PROFILE_EDIT, profile);
}
dispose();
}
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;
}
public String getName() {
return name;
}
@Override
public String toString() {
return name; // Zobrazí se pouze název
}
}
}

View File

@ -13,6 +13,7 @@ import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import jnet.client.gui.dialog.DialogSnmpProfile;
import org.snmp4j.mp.SnmpConstants;
public class ServerSettingsPanel extends JTabbedPane {
@ -215,10 +216,10 @@ public class ServerSettingsPanel extends JTabbedPane {
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
//SwingUtilities.invokeLater(() -> new AddDeviceTypeDialog().setVisible(true));
SwingUtilities.invokeLater(() -> new DialogSnmpProfile(null, true).setVisible(true));
}
});
//panel.add(button);
panel.add(button);
return panel;
}

View File

@ -1,19 +1,30 @@
package jnet.client.gui.serversettings;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.table.DefaultTableModel;
import jnet.client.Client;
import jnet.client.gui.dialog.DialogSnmpProfile;
import jnet.lib.LogFile;
import jnet.lib.Message;
import jnet.lib.ResourceLoader;
import jnet.lib.object.SnmpProfile;
import org.snmp4j.mp.SnmpConstants;
public class TableSnmpProfile extends JTable {
private DefaultTableModel model;
private static final String[] COLUMN_NAMES = {"Název", "Verze", "Community Read", "Port"};
private static final String[] COLUMN_NAMES = {"#", "Název", "Verze", "Community Read", "Port"};
public TableSnmpProfile() {
initTable();
@ -33,6 +44,59 @@ public class TableSnmpProfile extends JTable {
loadData();
// Dvojklik pro editaci
addMouseListener(new MouseAdapter() {
private void zobrazMenu(MouseEvent e, int id) {
try {
JPopupMenu menu = new JPopupMenu();
JMenuItem delete = new JMenuItem("Smazat", new ImageIcon(ImageIO.read(ResourceLoader.getFile("/img/trash.png"))));
delete.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
int option = JOptionPane.showConfirmDialog(
null,
"Smazat profil ID " + id + " ?",
"Potvrzení akce",
JOptionPane.YES_NO_OPTION
);
if (option == JOptionPane.YES_OPTION) {
Client.sendMessage(new Message(Message.SNMP_PROFILE_DELETE, id));
}
}
});
menu.add(delete);
menu.show(e.getComponent(), e.getX(), e.getY());
} catch (IOException ex) {
LogFile.printErr("Load icon error: " + ex.getMessage());
}
}
@Override
public void mouseClicked(MouseEvent e) {
int row = getSelectedRow();
int id = (int) getValueAt(row, 0);
// Ověříme, zda bylo kliknuto pravým tlačítkem (BUTTON3)
if (e.getButton() == MouseEvent.BUTTON3) {
zobrazMenu(e, id);
} else if (e.getClickCount() == 2) {
if (row != -1) {
Client.snmpProfile.stream()
.filter(u -> u.getId() == id)
.findFirst()
.ifPresent(profile -> {
SwingUtilities.invokeLater(() -> {
new DialogSnmpProfile(profile).show();
});
});
}
}
}
}
);
}
private void loadData() {
@ -40,6 +104,7 @@ public class TableSnmpProfile extends JTable {
for (SnmpProfile sp : Client.snmpProfile) {
model.addRow(new Object[]{
sp.getId(),
sp.getName(),
getSnmpVersion(sp.getVersion()),
sp.getCommunityRead(),

View File

@ -44,6 +44,11 @@ public class MessageRegister {
register.put(Message.SNMP_TEMPLATE_VALUE_MAP_LIST, new RegisterSnmpValueMapList());
register.put(Message.SNMP_PROBE_VALUE, new RegisterSnmpProbeResult());
register.put(Message.NODE_CONNECTION_REMOVE, new RegisterNodeConnectionRemove());
register.put(Message.SNMP_PROFILE_NEW_RESULT, new RegisterSnmpProfileNewResult());
register.put(Message.SNMP_PROFILE_EDIT_RESULT, new RegisterSnmpProfileEditResult());
register.put(Message.SNMP_PROFILE_DELETE_RESULT, new RegisterSnmpProfileDeleteResult());
}

View File

@ -0,0 +1,24 @@
package jnet.client.message;
import io.netty.channel.ChannelHandlerContext;
import jnet.client.Client;
import jnet.client.gui.Window;
import jnet.lib.Message;
public class RegisterSnmpProfileDeleteResult implements Register {
public RegisterSnmpProfileDeleteResult() {
}
@Override
public void processing(ChannelHandlerContext ctx, Message msg) {
int profileId = (int) msg.getMsg();
// odebere ze seznamu
Client.snmpProfile.removeIf(snmpProfile -> snmpProfile.getId() == profileId);
Window.refreshSnmpProfiles();
}
}

View File

@ -0,0 +1,32 @@
package jnet.client.message;
import io.netty.channel.ChannelHandlerContext;
import jnet.client.Client;
import jnet.client.gui.Window;
import jnet.lib.Message;
import jnet.lib.object.SnmpProfile;
public class RegisterSnmpProfileEditResult implements Register {
public RegisterSnmpProfileEditResult() {
}
@Override
public void processing(ChannelHandlerContext ctx, Message msg) {
SnmpProfile profile = (SnmpProfile) msg.getMsg();
// upravit v lokalnim seznamu
for (int i = 0; i < Client.snmpProfile.size(); i++) {
if (Client.snmpProfile.get(i).getId() == profile.getId()) {
// nahradi node v seznamu
Client.snmpProfile.set(i, profile);
break;
}
}
Window.refreshSnmpProfiles();
}
}

View File

@ -0,0 +1,23 @@
package jnet.client.message;
import io.netty.channel.ChannelHandlerContext;
import jnet.client.Client;
import jnet.client.gui.Window;
import jnet.lib.Message;
import jnet.lib.object.SnmpProfile;
public class RegisterSnmpProfileNewResult implements Register {
public RegisterSnmpProfileNewResult() {
}
@Override
public void processing(ChannelHandlerContext ctx, Message msg) {
SnmpProfile profile = (SnmpProfile) msg.getMsg();
Client.snmpProfile.add(profile);
Window.refreshSnmpProfiles();
}
}