package window;
|
import java.awt.*;
|
import java.awt.event.*;
|
import java.io.*;
|
import java.sql.Connection;
|
import java.util.Enumeration;
|
import java.util.HashMap;
|
import java.util.Locale;
|
import java.util.Map;
|
import java.util.PropertyResourceBundle;
|
import java.util.ResourceBundle;
|
import javax.swing.*;
|
import javax.swing.border.*;
|
import javax.swing.event.InternalFrameAdapter;
|
import javax.swing.event.InternalFrameEvent;
|
import javax.swing.tree.*;
|
|
import dell_map.Dell_Map;
|
import dell_map.MapViewer;
|
import targets.Mapdata;
|
|
public class Windows extends JFrame {
|
private static final long serialVersionUID = 1L;
|
private Connection conn;
|
private JDesktopPane desktopPane;
|
private ResourceBundle messages;
|
private Locale currentLocale;
|
private Map<String, JInternalFrame> frameCache = new HashMap<>();
|
private Map<String, Boolean> nodeOpenStatus = new HashMap<>(); // ¸ú×Ù½Úµã´ò¿ª×´Ì¬
|
private JTree navTree; // µ¼º½Ê÷ÒýÓÃ
|
|
public Windows(String title, Connection conn, Locale locale) {
|
super(title);
|
this.conn = conn;
|
this.currentLocale = locale;
|
this.messages = loadResourceBundle(currentLocale);
|
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
setExtendedState(JFrame.MAXIMIZED_BOTH);
|
// Ìí¼Ó´°¿Ú״̬¼àÌýÆ÷
|
addWindowStateListener(new WindowAdapter() {
|
@Override
|
public void windowStateChanged(WindowEvent e) {
|
if ((e.getNewState() & Frame.MAXIMIZED_BOTH) == 0) { // ·Ç×î´ó»¯×´Ì¬
|
setSize(1200, 850); // ÉèÖÃгߴç
|
setLocationRelativeTo(null); // ¾ÓÖÐÏÔʾ
|
}
|
}
|
});
|
|
JSplitPane splitPane = new JSplitPane();
|
splitPane.setDividerLocation(200);
|
splitPane.setDividerSize(3);
|
|
JPanel navPanel = createNavigationPanel();
|
navPanel.setPreferredSize(new Dimension(200, 0));
|
|
desktopPane = new JDesktopPane();
|
desktopPane.setBorder(new EmptyBorder(10, 10, 10, 10));
|
desktopPane.setBackground(new Color(240, 240, 240));
|
|
JInternalFrame welcomeFrame = WindowFactory.createWelcomeFrame(messages, conn);
|
frameCache.put("welcome", welcomeFrame);
|
desktopPane.add(welcomeFrame);
|
try {
|
welcomeFrame.setSelected(true);
|
welcomeFrame.setMaximum(true);
|
} catch (java.beans.PropertyVetoException e) {
|
e.printStackTrace();
|
}
|
|
splitPane.setLeftComponent(navPanel);
|
splitPane.setRightComponent(new JScrollPane(desktopPane));
|
|
add(splitPane);
|
|
addWindowListener(new WindowAdapter() {
|
@Override
|
public void windowClosing(WindowEvent e) {
|
closeDatabaseConnection();
|
}
|
});
|
|
createLanguageMenu();
|
}
|
|
private ResourceBundle loadResourceBundle(Locale locale) {
|
String fileName = locale.equals(Locale.ENGLISH) ?
|
"Messages_en.properties" : "Messages_zh.properties";
|
|
File langFile = new File("systemfile/" + fileName);
|
|
if (!langFile.exists()) {
|
System.err.println("ĬÈÏ×ÊÔ´ÎļþδÕÒµ½: " + langFile.getAbsolutePath());
|
return ResourceBundle.getBundle("systemfile.Messages");
|
}
|
|
try (InputStream inputStream = new FileInputStream(langFile)) {
|
return new PropertyResourceBundle(inputStream);
|
} catch (IOException e) {
|
System.err.println("ÎÞ·¨¼ÓÔØ×ÊÔ´Îļþ: " + e.getMessage());
|
return ResourceBundle.getBundle("systemfile.Messages");
|
}
|
}
|
|
private void createLanguageMenu() {
|
JMenuBar menuBar = new JMenuBar();
|
|
JMenu languageMenu = new JMenu(messages.getString("LANGUAGE"));
|
JMenuItem chineseItem = new JMenuItem("ÖÐÎÄ");
|
JMenuItem englishItem = new JMenuItem("English");
|
|
chineseItem.addActionListener(e -> switchLanguage(Locale.SIMPLIFIED_CHINESE));
|
englishItem.addActionListener(e -> switchLanguage(Locale.ENGLISH));
|
|
languageMenu.add(chineseItem);
|
languageMenu.add(englishItem);
|
menuBar.add(languageMenu);
|
|
JMenu helpMenu = new JMenu(messages.getString("HELP"));
|
JMenuItem aboutItem = new JMenuItem(messages.getString("ABOUT"));
|
aboutItem.addActionListener(e -> showAboutDialog());
|
helpMenu.add(aboutItem);
|
menuBar.add(helpMenu);
|
|
setJMenuBar(menuBar);
|
}
|
|
private void switchLanguage(Locale newLocale) {
|
this.currentLocale = newLocale;
|
this.messages = loadResourceBundle(currentLocale);
|
frameCache.clear();
|
nodeOpenStatus.clear(); // Çå¿Õ״̬»º´æ
|
|
getJMenuBar().getMenu(0).setText(messages.getString("LANGUAGE"));
|
getJMenuBar().getMenu(1).setText(messages.getString("HELP"));
|
getJMenuBar().getMenu(1).getItem(0).setText(messages.getString("ABOUT"));
|
|
getContentPane().removeAll();
|
|
JSplitPane splitPane = new JSplitPane();
|
splitPane.setDividerLocation(200);
|
|
JPanel navPanel = createNavigationPanel();
|
navPanel.setPreferredSize(new Dimension(200, 0));
|
|
desktopPane = new JDesktopPane();
|
desktopPane.setBorder(new EmptyBorder(10, 10, 10, 10));
|
desktopPane.setBackground(new Color(240, 240, 240));
|
|
JInternalFrame welcomeFrame = WindowFactory.createWelcomeFrame(messages, conn);
|
frameCache.put("welcome", welcomeFrame);
|
desktopPane.add(welcomeFrame);
|
try {
|
welcomeFrame.setSelected(true);
|
} catch (java.beans.PropertyVetoException e) {
|
e.printStackTrace();
|
}
|
|
splitPane.setLeftComponent(navPanel);
|
splitPane.setRightComponent(new JScrollPane(desktopPane));
|
|
add(splitPane);
|
|
createLanguageMenu();
|
|
revalidate();
|
repaint();
|
}
|
|
private void showAboutDialog() {
|
String title = messages.getString("ABOUT");
|
String message = messages.getString("APP_NAME") + " v1.0\n" +
|
"°æÈ¨ËùÓÐ 2023\n" +
|
"»ùÓÚλÖ÷þÎñµÄ×ۺϹÜÀíϵͳ\n" +
|
"ÌṩÉ豸¶¨Î»¡¢Î§À¸¹ÜÀí¡¢Í³¼Æ·ÖÎöµÈ¹¦ÄÜ";
|
|
JOptionPane.showMessageDialog(this, message, title, JOptionPane.INFORMATION_MESSAGE);
|
}
|
|
private void closeDatabaseConnection() {
|
try {
|
if (conn != null && !conn.isClosed()) {
|
conn.close();
|
System.out.println(messages.getString("DB_CLOSED"));
|
}
|
} catch (Exception ex) {
|
System.err.println(messages.getString("DB_CLOSE_ERROR") + ": " + ex.getMessage());
|
}
|
}
|
|
private JPanel createNavigationPanel() {
|
JPanel panel = new JPanel(new BorderLayout());
|
panel.setBackground(new Color(240, 240, 240));
|
panel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY));
|
|
JLabel titleLabel = new JLabel(messages.getString("NAVIGATION"), SwingConstants.CENTER);
|
titleLabel.setFont(new Font("΢ÈíÑźÚ", Font.BOLD, 16));
|
titleLabel.setForeground(new Color(0, 120, 215));
|
titleLabel.setBorder(new EmptyBorder(15, 10, 15, 10));
|
panel.add(titleLabel, BorderLayout.NORTH);
|
|
// ʹÓõ¼º½Ê÷¹¤³§´´½¨µ¼º½Ê÷
|
navTree = NavigationTreeFactory.createNavigationTree(messages);
|
navTree = NavigationTreeFactory.createNavigationTree(messages);
|
|
// Ô¤³õʼ»¯ËùÓнڵã״̬Ϊ¹Ø±Õ
|
Enumeration<?> nodes = ((DefaultMutableTreeNode)navTree.getModel().getRoot()).depthFirstEnumeration();
|
while (nodes.hasMoreElements()) {
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) nodes.nextElement();
|
if (node.isLeaf()) {
|
nodeOpenStatus.put(node.getUserObject().toString(), false);
|
}
|
}
|
// ÉèÖÃ×Ô¶¨ÒåäÖȾÆ÷
|
navTree.setCellRenderer(new StatusTreeCellRenderer());
|
|
navTree.addTreeSelectionListener(e -> {
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) navTree.getLastSelectedPathComponent();
|
if (node == null || !node.isLeaf()) return;
|
|
String nodeName = node.getUserObject().toString();
|
openOrFocusFrame(nodeName);
|
});
|
|
JScrollPane scrollPane = new JScrollPane(navTree);
|
scrollPane.setBorder(null);
|
panel.add(scrollPane, BorderLayout.CENTER);
|
|
return panel;
|
}
|
|
private JInternalFrame createInternalFrame(String title) {
|
try {
|
JInternalFrame frame = WindowFactory.createFunctionFrame(title);
|
|
// ʹÓù¤³§Àà´´½¨ÄÚÈÝÃæ°å
|
JPanel contentPanel = ContentPanelFactory.createContentPanel(title, messages);
|
frame.setContentPane(contentPanel);
|
frame.pack(); // ×Ô¶¯µ÷Õû´óС
|
|
// ¼ÆËã¾ÓÖÐλÖÃ
|
Dimension desktopSize = desktopPane.getSize();
|
Dimension frameSize = frame.getSize();
|
int x = (desktopSize.width - frameSize.width) / 2;
|
int y = (desktopSize.height - frameSize.height) / 2;
|
|
// È·±£Î»ÖÃÔÚ¿ÉÊÓÇøÓòÄÚ
|
x = Math.max(x, 0);
|
y = Math.max(y, 0);
|
|
frame.setLocation(x, y);
|
frame.setVisible(true);
|
frame.setMaximum(true);
|
|
// Ìí¼Ó´°¿Ú¼àÌýÆ÷ÒÔ¸ú×ٹرÕ״̬ - ÐÞ¸´µã
|
frame.addInternalFrameListener(new InternalFrameAdapter() {
|
@Override
|
public void internalFrameClosed(InternalFrameEvent e) {
|
// ʹÓô°Ìå±êÌâ×÷Ϊ¼ü¸üÐÂ״̬
|
nodeOpenStatus.put(frame.getTitle(), false);
|
navTree.repaint(); // ¸üÐÂÊ÷äÖȾ
|
}
|
});
|
|
return frame;
|
} catch (Exception e) {
|
JOptionPane.showMessageDialog(this,
|
"´´½¨´°¿Úʧ°Ü: " + e.getMessage(),
|
"´íÎó",
|
JOptionPane.ERROR_MESSAGE
|
);
|
return null;
|
}
|
}
|
private void openOrFocusFrame(String frameName) {
|
|
// ÐÂÔö£º¼ì²éÊÇ·ñÊǵØÍ¼Ãû³Æ½Úµã
|
Mapdata mapData = Dell_Map.getMapByName(frameName);
|
if (mapData != null) {
|
// ´¦ÀíµØÍ¼½Úµã
|
MapViewer.createMapViewer(desktopPane, frameName,messages);
|
|
// ¸üнڵã´ò¿ª×´Ì¬
|
nodeOpenStatus.put(frameName, true);
|
navTree.repaint();
|
return;
|
}
|
|
// ¼ì²é¸Ã´°¿ÚÊÇ·ñÒÑ´æÔÚ
|
JInternalFrame existingFrame = null;
|
for (JInternalFrame frame : desktopPane.getAllFrames()) {
|
if (frame.getTitle().equals(frameName)) {
|
existingFrame = frame;
|
break;
|
}
|
}
|
|
if (existingFrame != null) {
|
// ´°¿ÚÒÑ´æÔÚ£¬½«ÆäÖÃÓÚ×îÉϲã
|
try {
|
existingFrame.setVisible(true);
|
existingFrame.toFront();
|
existingFrame.setSelected(true);
|
if (existingFrame.isIcon()) {
|
existingFrame.setIcon(false);
|
}
|
} catch (java.beans.PropertyVetoException e) {
|
e.printStackTrace();
|
}
|
} else {
|
// ´°¿Ú²»´æÔÚ£¬´´½¨Ð´°¿Ú
|
JInternalFrame frame = createInternalFrame(frameName);
|
if (frame != null) {
|
desktopPane.add(frame);
|
try {
|
frame.setSelected(true);
|
// ¸üÐÂ״̬Ϊ´ò¿ª
|
nodeOpenStatus.put(frameName, true);
|
navTree.repaint();
|
} catch (Exception e) {
|
e.printStackTrace();
|
}
|
}
|
}
|
|
desktopPane.revalidate();
|
desktopPane.repaint();
|
}
|
|
// ×Ô¶¨ÒåÊ÷µ¥Ôª¸ñäÖȾÆ÷
|
@SuppressWarnings("serial")
|
private class StatusTreeCellRenderer extends DefaultTreeCellRenderer {
|
@Override
|
public Component getTreeCellRendererComponent(JTree tree, Object value,
|
boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) {
|
|
super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
|
|
DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
|
if (node.isLeaf()) {
|
String nodeName = node.getUserObject().toString();
|
// ¸ù¾Ý´ò¿ª×´Ì¬ÉèÖÃÑÕÉ«
|
if (nodeOpenStatus.containsKey(nodeName) && nodeOpenStatus.get(nodeName)) {
|
setForeground(Color.RED); // ´ò¿ª×´Ì¬ÏÔʾºìÉ«
|
} else {
|
setForeground(Color.BLACK); // ¹Ø±Õ״̬ÏÔʾºÚÉ«
|
}
|
}
|
return this;
|
}
|
}
|
|
|
}
|