jNetClient/src/main/java/jnet/client/gui/Window.java

349 lines
11 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package jnet.client.gui;
import jnet.client.Client;
import jnet.client.gui.dialog.NewSiteDialog;
import jnet.client.gui.maptree.MapTree;
import jnet.client.gui.serversettings.ServerSettingsPanel;
import jnet.lib.LogFile;
import jnet.lib.ResourceLoader;
import jnet.lib.object.Site;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.IOException;
import jnet.client.gui.mapview.MapView;
import jnet.lib.object.Node;
/**
* Hlavní okno aplikace – implementováno jako singleton.
*/
public class Window extends JFrame {
private static Window instance;
private JSplitPane splitPane;
private StatusBar statusBar;
private MapTree mapTree;
private MenuPanel menuPanel;
private static ServerSettingsPanel currentServerSettingsPanel;
private static EventsPanel currentEventsPanel;
// Nové: reference na aktuální MapView a jeho scroll panel
private static MapView currentMapView;
private static JScrollPane currentMapViewScrollPane;
private Window() {
super(Client.APP_NAME);
setIcon();
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
handleClosing();
}
});
initComponents();
pack();
setLocationRelativeTo(null);
}
private void initComponents() {
setLayout(new BorderLayout());
menuPanel = new MenuPanel();
add(menuPanel, BorderLayout.NORTH);
splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT);
splitPane.setDividerSize(5);
JPanel leftPanel = new JPanel(new BorderLayout());
leftPanel.setBackground(Color.WHITE);
leftPanel.setPreferredSize(new Dimension(200, 250));
mapTree = new MapTree();
leftPanel.add(mapTree, BorderLayout.CENTER);
if (Client.user != null && Client.user.isAddMap()) {
JButton addSiteButton = new JButton("Přidat mapu");
addSiteButton.addActionListener(e -> new NewSiteDialog().setVisible(true));
leftPanel.add(addSiteButton, BorderLayout.SOUTH);
}
splitPane.add(leftPanel, JSplitPane.LEFT);
// Pravý panel – prázdný
splitPane.add(new JPanel(), JSplitPane.RIGHT);
add(splitPane, BorderLayout.CENTER);
if (statusBar == null) {
statusBar = new StatusBar();
}
add(statusBar, BorderLayout.SOUTH);
currentServerSettingsPanel = null;
currentEventsPanel = null;
}
public static Window getInstance() {
if (instance == null) {
instance = new Window();
}
return instance;
}
public void reloadWindow() {
getContentPane().removeAll();
initComponents();
revalidate();
repaint();
currentEventsPanel = null;
// Po znovunačtení okna zrušíme referenci na MapView, protože byl zničen
currentMapView = null;
currentMapViewScrollPane = null;
}
// ------------------------------------------------------------------------
// Soukromé pomocné metody
// ------------------------------------------------------------------------
private void setIcon() {
try {
var icon = ImageIO.read(ResourceLoader.getFile("img/app.png"));
setIconImage(icon);
} catch (IOException ex) {
LogFile.printErr("APP icon exception");
}
}
private void handleClosing() {
int n = JOptionPane.showOptionDialog(this,
"Ukončit nebo minimalizovat do lišty?",
"Ukončit",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
new Object[]{"Ukončit", "Minimalizovat"},
"Minimalizovat");
if (n == 0) {
LogFile.printInfo("Ukončit");
System.exit(0);
} else if (n == 1) {
// Skryjeme okno – zůstane pouze tray ikona
setVisible(false);
// Volitelně můžeme zobrazit bublinu s informací
Tray.showMessage("jNet client", "Aplikace byla minimalizována do lišty.");
}
}
// ------------------------------------------------------------------------
// Statické přístupové metody pro ostatní komponenty
// ------------------------------------------------------------------------
public static void setStatusBarMessage(String message) {
if (instance != null && instance.statusBar != null) {
instance.statusBar.setMessage(message);
}
}
public static void reloadOnlineClients() {
if (instance != null && instance.statusBar != null) {
instance.statusBar.setOnlineCount();
}
}
/**
* Nastaví nebo aktualizuje MapView pro daný Site. Odstraní starý pravý
* panel a vloží nový MapView.
*/
public static void setMapView(Site site) {
if (instance == null) {
return;
}
SwingUtilities.invokeLater(() -> {
// Odstraníme starý pravý panel (pokud existuje)
Component right = instance.splitPane.getRightComponent();
if (right != null) {
instance.splitPane.remove(right);
}
// Vytvoříme nový MapView
currentMapView = new MapView(site);
currentMapViewScrollPane = new JScrollPane(currentMapView);
instance.splitPane.add(currentMapViewScrollPane, JSplitPane.RIGHT);
instance.splitPane.revalidate();
instance.splitPane.repaint();
});
}
/**
* Aktualizuje stav uzlu v aktuálním MapView. Volá se z handleru po příchodu
* zprávy o změně stavu.
*/
public static void updateNodeStatusInMapView(int nodeId, int newStatus) {
if (currentMapView != null) {
currentMapView.updateNodeStatus(nodeId, newStatus);
}
}
/**
* Aktualizuje pozici uzlu v aktuálním MapView. Volá se z handleru po
* příchodu zprávy o změně stavu.
*/
public static void updateNodePositionInMapView(int nodeId, int x, int y) {
if (currentMapView != null) {
currentMapView.updateNodePosition(nodeId, x, y);
}
}
/**
* Aktualizuje pozici uzlu v aktuálním MapView. Volá se z handleru po
* příchodu zprávy o změně stavu.
*/
public static void addNodeInMapView(Node node) {
if (currentMapView != null) {
currentMapView.addNodeFromServer(node);
}
}
/**
* Kompletně obnoví MapView z nového Site objektu (např. po hromadné změně).
*/
public static void refreshMapView(Site site) {
if (currentMapView != null) {
currentMapView.refreshFromServer(site);
}
}
/**
* Obnoví aktuální zobrazení mapy podle dat ze serveru.
* Pokud není žádná mapa otevřena, nic neprovede.
*/
public static void refreshCurrentMapView() {
if (currentMapView == null) {
return;
}
SwingUtilities.invokeLater(() -> {
Site currentSite = currentMapView.getModel().getSite();
if (currentSite == null) {
return;
}
// Najdeme aktuální verzi site v Client.site
for (Site s : Client.site) {
if (s.getId() == currentSite.getId()) {
currentMapView.refreshFromServer(s);
break;
}
}
});
}
/**
* odebere node z mapview
*/
public static void removeNodeFromMapView(int nodeId) {
if (currentMapView != null) {
currentMapView.removeNodeFromServer(nodeId);
}
}
public static void showSettings() {
if (instance == null) {
return;
}
SwingUtilities.invokeLater(() -> {
JScrollPane panel = new JScrollPane(new SettingsPanel());
// Rychlejší posouvání šipkami a kolečkem
panel.getVerticalScrollBar().setUnitIncrement(20);
// Rychlejší posouvání Page Up / Page Down
panel.getVerticalScrollBar().setBlockIncrement(100);
// Povolení rychlejšího kolečka (volitelné)
panel.putClientProperty("JScrollBar.fastWheelScrolling", Boolean.TRUE);
instance.splitPane.add(panel, JSplitPane.RIGHT);
instance.splitPane.revalidate();
});
}
public static void showLog() {
if (instance == null) {
return;
}
SwingUtilities.invokeLater(() -> {
Component right = instance.splitPane.getRightComponent();
if (right != null) {
instance.splitPane.remove(right);
}
if (currentEventsPanel == null) {
currentEventsPanel = new EventsPanel();
} else {
currentEventsPanel.refresh();
}
instance.splitPane.add(currentEventsPanel, JSplitPane.RIGHT);
instance.splitPane.revalidate();
instance.splitPane.repaint();
});
}
public static void showServerSettings(int tab) {
if (instance == null) {
return;
}
SwingUtilities.invokeLater(() -> {
if (currentServerSettingsPanel == null) {
currentServerSettingsPanel = new ServerSettingsPanel();
JScrollPane scroll = new JScrollPane(currentServerSettingsPanel);
instance.splitPane.add(scroll, JSplitPane.RIGHT);
instance.splitPane.revalidate();
}
currentServerSettingsPanel.setTable(tab);
if (tab == ServerSettingsPanel.TAB_DEVICE_TYPE) {
currentServerSettingsPanel.refreshDeviceTypes();
} else if (tab == ServerSettingsPanel.TAB_USERS) {
currentServerSettingsPanel.refreshUsers();
} else if (tab == ServerSettingsPanel.TAB_SNMP_PROFILE) {
// zatím neimplementováno
}
});
}
public static void refreshDeviceTypes() {
if (currentServerSettingsPanel != null) {
currentServerSettingsPanel.refreshDeviceTypes();
}
}
public static void refreshUsers() {
if (currentServerSettingsPanel != null) {
currentServerSettingsPanel.refreshUsers();
}
}
public static void refreshSnmpProfiles() {
if (currentServerSettingsPanel != null) {
currentServerSettingsPanel.refreshSnmpProfiles();
}
}
public static void refreshMapTree() {
if (instance != null) {
instance.mapTree.refresh();
}
}
public static MapTree getMapTree() {
return instance != null ? instance.mapTree : null;
}
public static void refreshEventsIfVisible() {
if (currentEventsPanel != null && currentEventsPanel.isShowing()) {
currentEventsPanel.refresh();
}
}
}