|
| 1 | +/* |
| 2 | + * Copyright (C) 2026 The MegaMek Team. All Rights Reserved. |
| 3 | + * |
| 4 | + * This file is part of MegaMek. |
| 5 | + * |
| 6 | + * MegaMek is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License (GPL), |
| 8 | + * version 3 or (at your option) any later version, |
| 9 | + * as published by the Free Software Foundation. |
| 10 | + * |
| 11 | + * MegaMek is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty |
| 13 | + * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 14 | + * See the GNU General Public License for more details. |
| 15 | + * |
| 16 | + * A copy of the GPL should have been included with this project; |
| 17 | + * if not, see <https://www.gnu.org/licenses/>. |
| 18 | + * |
| 19 | + * NOTICE: The MegaMek organization is a non-profit group of volunteers |
| 20 | + * creating free software for the BattleTech community. |
| 21 | + * |
| 22 | + * MechWarrior, BattleMech, `Mech and AeroTech are registered trademarks |
| 23 | + * of The Topps Company, Inc. All Rights Reserved. |
| 24 | + * |
| 25 | + * Catalyst Game Labs and the Catalyst Game Labs logo are trademarks of |
| 26 | + * InMediaRes Productions, LLC. |
| 27 | + * |
| 28 | + * MechWarrior Copyright Microsoft Corporation. MegaMek was created under |
| 29 | + * Microsoft's "Game Content Usage Rules" |
| 30 | + * <https://www.xbox.com/en-US/developers/rules> and it is not endorsed by or |
| 31 | + * affiliated with Microsoft. |
| 32 | + */ |
| 33 | +package megamek.client.ui.dialogs.buttonDialogs; |
| 34 | + |
| 35 | +import java.awt.Container; |
| 36 | +import java.awt.Dimension; |
| 37 | +import java.awt.event.ActionEvent; |
| 38 | +import java.awt.event.ActionListener; |
| 39 | +import java.io.BufferedReader; |
| 40 | +import java.io.InputStreamReader; |
| 41 | +import java.io.Serial; |
| 42 | +import java.net.InetAddress; |
| 43 | +import java.net.URL; |
| 44 | +import javax.swing.Box; |
| 45 | +import javax.swing.BoxLayout; |
| 46 | +import javax.swing.JButton; |
| 47 | +import javax.swing.JFrame; |
| 48 | +import javax.swing.JLabel; |
| 49 | +import javax.swing.JPanel; |
| 50 | + |
| 51 | +import megamek.client.ui.Messages; |
| 52 | +import megamek.client.ui.clientGUI.ClientGUI; |
| 53 | + |
| 54 | +/** |
| 55 | + * A JPanel that holds the networking information about the network information of the current session. |
| 56 | + */ |
| 57 | +public class NetworkInformationDialog extends AbstractButtonDialog implements ActionListener { |
| 58 | + |
| 59 | + private JFrame frame; |
| 60 | + |
| 61 | + @Serial |
| 62 | + private static final long serialVersionUID = -4754010220963493049L; |
| 63 | + |
| 64 | + private final JLabel lblLocalIP = new JLabel(Messages.getString("NetworkInformation.localIP")); |
| 65 | + private JLabel localIP = new JLabel(Messages.getString("NetworkInformation.blankIP")); |
| 66 | + private final JLabel lblRemoteIP = new JLabel(Messages.getString("NetworkInformation.remoteIP")); |
| 67 | + private JLabel remoteIP = new JLabel(Messages.getString("NetworkInformation.blankIP")); |
| 68 | + ; |
| 69 | + private final JLabel lblConnectedIP = new JLabel(Messages.getString("NetworkInformation.connectedIP")); |
| 70 | + private JLabel connectedIP = new JLabel(Messages.getString("NetworkInformation.blankIP")); |
| 71 | + ; |
| 72 | + private final JButton butShowLocalIPs = new JButton(" " + Messages.getString("NetworkInformation.buttonShowIPs")); |
| 73 | + private final JButton butShowRemoteIPs = new JButton(" " + Messages.getString("NetworkInformation.buttonShowIPs")); |
| 74 | + private final JButton butShowHostIPs = new JButton(" " + Messages.getString("NetworkInformation.buttonShowIPs")); |
| 75 | + private final JLabel lblBlank = new JLabel(""); |
| 76 | + private ClientGUI clientGui; |
| 77 | + |
| 78 | + /** |
| 79 | + * Constructs the network overview panel; the given ClientGUI is used to access the game data. |
| 80 | + */ |
| 81 | + public NetworkInformationDialog(ClientGUI cg) { |
| 82 | + super(cg.getFrame(), "NetworkInformationDialog", "NetworkInformation.title"); |
| 83 | + clientGui = cg; |
| 84 | + init(cg.getFrame()); |
| 85 | + } |
| 86 | + |
| 87 | + public NetworkInformationDialog(JFrame frame) { |
| 88 | + super(frame, "NetworkInformationDialog", "NetworkInformation.title"); |
| 89 | + init(frame); |
| 90 | + } |
| 91 | + |
| 92 | + private void init(JFrame frame) { |
| 93 | + this.frame = frame; |
| 94 | + refresh(); |
| 95 | + initialize(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void actionPerformed(ActionEvent e) { |
| 100 | + if (e.getSource() == butShowLocalIPs) { |
| 101 | + if (localIP.isVisible()) { |
| 102 | + localIP.setVisible(false); |
| 103 | + } else { |
| 104 | + localIP.setVisible(true); |
| 105 | + } |
| 106 | + } |
| 107 | + if (e.getSource() == butShowRemoteIPs) { |
| 108 | + if (remoteIP.isVisible()) { |
| 109 | + remoteIP.setVisible(false); |
| 110 | + } else { |
| 111 | + remoteIP.setVisible(true); |
| 112 | + } |
| 113 | + } |
| 114 | + if (e.getSource() == butShowHostIPs) { |
| 115 | + if (connectedIP.isVisible()) { |
| 116 | + connectedIP.setVisible(false); |
| 117 | + } else { |
| 118 | + connectedIP.setVisible(true); |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Override |
| 124 | + protected Container createCenterPane() { |
| 125 | + var mainPanel = new JPanel(); |
| 126 | + mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS)); |
| 127 | + refresh(); |
| 128 | + localIP.setVisible(false); |
| 129 | + remoteIP.setVisible(false); |
| 130 | + connectedIP.setVisible(false); |
| 131 | + |
| 132 | + JPanel row1 = new JPanel(); |
| 133 | + row1.setLayout(new BoxLayout(row1, BoxLayout.X_AXIS)); |
| 134 | + row1.add(lblLocalIP); |
| 135 | + row1.add(Box.createHorizontalStrut(10)); |
| 136 | + row1.add(localIP); |
| 137 | + row1.add(Box.createHorizontalGlue()); |
| 138 | + row1.add(butShowLocalIPs); |
| 139 | + |
| 140 | + JPanel row2 = new JPanel(); |
| 141 | + row2.setLayout(new BoxLayout(row2, BoxLayout.X_AXIS)); |
| 142 | + row2.add(lblRemoteIP); |
| 143 | + row2.add(Box.createHorizontalStrut(10)); |
| 144 | + row2.add(remoteIP); |
| 145 | + row2.add(Box.createHorizontalGlue()); |
| 146 | + row2.add(butShowRemoteIPs); |
| 147 | + |
| 148 | + JPanel row3 = new JPanel(); |
| 149 | + row3.setLayout(new BoxLayout(row3, BoxLayout.X_AXIS)); |
| 150 | + row3.add(lblConnectedIP); |
| 151 | + row3.add(Box.createHorizontalStrut(10)); |
| 152 | + row3.add(connectedIP); |
| 153 | + row3.add(Box.createHorizontalGlue()); |
| 154 | + row3.add(butShowHostIPs); |
| 155 | + |
| 156 | + mainPanel.add(row1); |
| 157 | + mainPanel.add(row2); |
| 158 | + mainPanel.add(row3); |
| 159 | + |
| 160 | + butShowLocalIPs.addActionListener(this); |
| 161 | + butShowHostIPs.addActionListener(this); |
| 162 | + butShowRemoteIPs.addActionListener(this); |
| 163 | + mainPanel.setPreferredSize(new Dimension(400, 300)); |
| 164 | + return mainPanel; |
| 165 | + } |
| 166 | + |
| 167 | + public void refresh() { |
| 168 | + localIP.setText(" " + getIPaddress(true)); |
| 169 | + remoteIP.setText(" " + getIPaddress(false)); |
| 170 | + if (clientGui != null) { |
| 171 | + connectedIP.setText(" " + clientGui.getClient().getHost()); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + private String getIPaddress(boolean localIP) { |
| 176 | + String thisIpAddress = ""; |
| 177 | + |
| 178 | + if (localIP) { |
| 179 | + try { |
| 180 | + InetAddress thisIp = InetAddress.getLocalHost(); |
| 181 | + thisIpAddress = thisIp.getHostAddress(); |
| 182 | + } catch (Exception e) { |
| 183 | + } |
| 184 | + if (thisIpAddress.length() == 0 || thisIpAddress == null) { |
| 185 | + thisIpAddress = "Could not obtain local IP address"; |
| 186 | + } |
| 187 | + } else { |
| 188 | + try { |
| 189 | + URL whatismyip = new URL("http://checkip.amazonaws.com"); |
| 190 | + BufferedReader in = new BufferedReader(new InputStreamReader(whatismyip.openStream())); |
| 191 | + thisIpAddress = in.readLine(); |
| 192 | + in.close(); |
| 193 | + } catch (Exception e) { |
| 194 | + } |
| 195 | + if (thisIpAddress.length() == 0 || thisIpAddress == null) { |
| 196 | + thisIpAddress = "Could not obtain public IP address"; |
| 197 | + } |
| 198 | + } |
| 199 | + return thisIpAddress; |
| 200 | + } |
| 201 | +} |
0 commit comments