-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
270 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
Allay-Server/src/main/java/org/allaymc/server/gui/Dashboard.form
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="org.allaymc.server.gui.Dashboard"> | ||
<grid id="27dc6" binding="rootPane" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="8" left="8" bottom="8" right="8"/> | ||
<constraints> | ||
<xy x="20" y="20" width="500" height="400"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<tabbedpane id="7cced" binding="tabbedPane"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"> | ||
<preferred-size width="200" height="200"/> | ||
</grid> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<grid id="a3704" binding="perfTab" layout-manager="FormLayout"> | ||
<constraints> | ||
<tabbedpane title="Performance"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children/> | ||
</grid> | ||
<grid id="64ec5" binding="playerTab" layout-manager="GridLayoutManager" row-count="2" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<tabbedpane title="Players"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="8a0bd" class="javax.swing.JLabel" binding="onlinePlayerCount"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="1" fill="1" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties> | ||
<enabled value="true"/> | ||
<horizontalAlignment value="2"/> | ||
<text value="Online: 0"/> | ||
</properties> | ||
</component> | ||
<scrollpane id="18991"> | ||
<constraints> | ||
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="585f4" class="javax.swing.JTable" binding="playerTable"> | ||
<constraints/> | ||
<properties> | ||
<autoCreateRowSorter value="true"/> | ||
</properties> | ||
</component> | ||
</children> | ||
</scrollpane> | ||
</children> | ||
</grid> | ||
<grid id="32e90" binding="pluginTab" layout-manager="GridLayoutManager" row-count="1" column-count="1" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1"> | ||
<margin top="0" left="0" bottom="0" right="0"/> | ||
<constraints> | ||
<tabbedpane title="Plugins"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<scrollpane id="7c0e2"> | ||
<constraints> | ||
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="7" hsize-policy="7" anchor="0" fill="3" indent="0" use-parent-layout="false"/> | ||
</constraints> | ||
<properties/> | ||
<border type="none"/> | ||
<children> | ||
<component id="6da8f" class="javax.swing.JTable" binding="pluginTable"> | ||
<constraints/> | ||
<properties> | ||
<cellSelectionEnabled value="false"/> | ||
</properties> | ||
</component> | ||
</children> | ||
</scrollpane> | ||
</children> | ||
</grid> | ||
</children> | ||
</tabbedpane> | ||
</children> | ||
</grid> | ||
</form> |
161 changes: 161 additions & 0 deletions
161
Allay-Server/src/main/java/org/allaymc/server/gui/Dashboard.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
package org.allaymc.server.gui; | ||
|
||
import com.formdev.flatlaf.themes.FlatMacDarkLaf; | ||
import com.intellij.uiDesigner.core.GridConstraints; | ||
import com.intellij.uiDesigner.core.GridLayoutManager; | ||
import com.jgoodies.forms.layout.FormLayout; | ||
import org.allaymc.api.eventbus.EventHandler; | ||
import org.allaymc.api.eventbus.event.server.player.PlayerInitializedEvent; | ||
import org.allaymc.api.eventbus.event.server.player.PlayerQuitEvent; | ||
import org.allaymc.api.server.Server; | ||
|
||
import javax.swing.*; | ||
import javax.swing.table.DefaultTableModel; | ||
import java.awt.*; | ||
import java.util.stream.Collectors; | ||
|
||
/** | ||
* java-swing-playground Project 2024/5/19 | ||
* | ||
* @author daoge_cmd | ||
*/ | ||
public class Dashboard { | ||
private static Dashboard INSTANCE; | ||
|
||
private JPanel rootPane; | ||
private JTabbedPane tabbedPane; | ||
private JPanel playerTab; | ||
private JPanel perfTab; | ||
private JTable playerTable; | ||
private JPanel pluginTab; | ||
private JTable pluginTable; | ||
private JLabel onlinePlayerCount; | ||
|
||
public static Dashboard getInstance() { | ||
if (INSTANCE != null) { | ||
return INSTANCE; | ||
} | ||
FlatMacDarkLaf.setup(); | ||
|
||
// Init the frame | ||
INSTANCE = new Dashboard(); | ||
JFrame frame = new JFrame("Dashboard"); | ||
frame.setContentPane(INSTANCE.rootPane); | ||
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | ||
frame.setSize(600, 600); | ||
frame.setLocationRelativeTo(null); | ||
frame.setVisible(true); | ||
|
||
var server = Server.getInstance(); | ||
server.getEventBus().registerListener(INSTANCE); | ||
// TODO: Update the table if any plugin disabled or enabled | ||
INSTANCE.updatePluginTable(); | ||
|
||
return INSTANCE; | ||
} | ||
|
||
@EventHandler | ||
protected void onPlayerJoin(PlayerInitializedEvent event) { | ||
updateOnlinePlayerCount(); | ||
updateOnlinePlayerTable(); | ||
} | ||
|
||
@EventHandler | ||
protected void onPlayerQuit(PlayerQuitEvent event) { | ||
updateOnlinePlayerCount(); | ||
updateOnlinePlayerTable(); | ||
} | ||
|
||
protected void updateOnlinePlayerCount() { | ||
SwingUtilities.invokeLater(() -> onlinePlayerCount.setText("Online: " + Server.getInstance().getOnlinePlayerCount())); | ||
} | ||
|
||
protected void updateOnlinePlayerTable() { | ||
var title = new String[] {"Name", "Address", "UUID"}; | ||
var players = Server.getInstance().getOnlinePlayers().values(); | ||
var data = new String[3][players.size() - 1]; | ||
int row = 0; | ||
for (var player : players) { | ||
data[row] = new String[] { | ||
player.getOriginName(), | ||
player.getClientSession().getSocketAddress().toString(), | ||
player.getUUID().toString() | ||
}; | ||
row++; | ||
} | ||
var model = new UneditableDefaultTableModel(data, title); | ||
SwingUtilities.invokeLater(() -> playerTable.setModel(model)); | ||
} | ||
|
||
protected void updatePluginTable() { | ||
var title = new String[] {"Name", "Description", "Version", "Author"}; | ||
var plugins = Server.getInstance().getPluginManager().getPlugins().values(); | ||
var data = new String[4][plugins.size() - 1]; | ||
int row = 0; | ||
for (var plugin : plugins) { | ||
var descriptor = plugin.descriptor(); | ||
data[row] = new String[] { | ||
descriptor.getName(), | ||
descriptor.getDescription().isBlank() ? "N/A" : descriptor.getDescription(), | ||
descriptor.getVersion(), | ||
String.join(", ", descriptor.getAuthors()) | ||
}; | ||
row++; | ||
} | ||
var model = new UneditableDefaultTableModel(data, title); | ||
SwingUtilities.invokeLater(() -> pluginTable.setModel(model)); | ||
} | ||
|
||
{ | ||
// GUI initializer generated by IntelliJ IDEA GUI Designer | ||
// >>> IMPORTANT!! <<< | ||
// DO NOT EDIT OR ADD ANY CODE HERE! | ||
$$$setupUI$$$(); | ||
} | ||
|
||
/** | ||
* Method generated by IntelliJ IDEA GUI Designer | ||
* >>> IMPORTANT!! <<< | ||
* DO NOT edit this method OR call it in your code! | ||
* | ||
* @noinspection ALL | ||
*/ | ||
private void $$$setupUI$$$() { | ||
rootPane = new JPanel(); | ||
rootPane.setLayout(new GridLayoutManager(1, 1, new Insets(8, 8, 8, 8), -1, -1)); | ||
tabbedPane = new JTabbedPane(); | ||
rootPane.add(tabbedPane, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, new Dimension(200, 200), null, 0, false)); | ||
playerTab = new JPanel(); | ||
playerTab.setLayout(new GridLayoutManager(2, 1, new Insets(0, 0, 0, 0), -1, -1)); | ||
tabbedPane.addTab("Players", playerTab); | ||
final JLabel label1 = new JLabel(); | ||
label1.setEnabled(true); | ||
label1.setHorizontalAlignment(2); | ||
label1.setText("Online: "); | ||
playerTab.add(label1, new GridConstraints(0, 0, 1, 1, GridConstraints.ANCHOR_NORTH, GridConstraints.FILL_HORIZONTAL, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_CAN_GROW, null, null, null, 0, false)); | ||
final JScrollPane scrollPane1 = new JScrollPane(); | ||
playerTab.add(scrollPane1, new GridConstraints(1, 0, 1, 1, GridConstraints.ANCHOR_CENTER, GridConstraints.FILL_BOTH, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, GridConstraints.SIZEPOLICY_CAN_SHRINK | GridConstraints.SIZEPOLICY_WANT_GROW, null, null, null, 0, false)); | ||
playerTable = new JTable(); | ||
playerTable.setAutoCreateRowSorter(true); | ||
scrollPane1.setViewportView(playerTable); | ||
perfTab = new JPanel(); | ||
perfTab.setLayout(new FormLayout("", "")); | ||
tabbedPane.addTab("Performance", perfTab); | ||
} | ||
|
||
/** | ||
* @noinspection ALL | ||
*/ | ||
public JComponent $$$getRootComponent$$$() { | ||
return rootPane; | ||
} | ||
|
||
private static class UneditableDefaultTableModel extends DefaultTableModel { | ||
public UneditableDefaultTableModel(String[][] data, String[] title) {super(data, title);} | ||
|
||
@Override | ||
public boolean isCellEditable(int row, int column) { | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters