Skip to content

Commit 2c57424

Browse files
committed
Added New Class
Right-click New>Class
1 parent 4d863a6 commit 2c57424

1 file changed

Lines changed: 45 additions & 14 deletions

File tree

  • src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/contextmenu/impl

src/main/java/the/bytecode/club/bytecodeviewer/gui/resourcelist/contextmenu/impl/New.java

Lines changed: 45 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package the.bytecode.club.bytecodeviewer.gui.resourcelist.contextmenu.impl;
22

33
import org.apache.commons.io.FilenameUtils;
4+
import org.objectweb.asm.tree.ClassNode;
45
import the.bytecode.club.bytecodeviewer.BytecodeViewer;
6+
import the.bytecode.club.bytecodeviewer.Constants;
7+
import the.bytecode.club.bytecodeviewer.api.ASMUtil;
58
import the.bytecode.club.bytecodeviewer.decompilers.Decompiler;
69
import the.bytecode.club.bytecodeviewer.gui.resourcelist.contextmenu.ContextMenuItem;
710
import the.bytecode.club.bytecodeviewer.gui.resourcelist.contextmenu.ContextMenuType;
@@ -43,8 +46,16 @@ public New()
4346
{
4447
JMenu quickOpen = new JMenu(TranslatedStrings.NEW.toString());
4548
quickOpen.add(createMenu("Class", FileType.CLASS, selPath));
46-
quickOpen.add(createMenu("File", FileType.FILE, selPath));
47-
//quickOpen.add(createMenu("Directory", FileType.DIRECTORY, selPath));
49+
50+
//TODO
51+
// + directory isn't finished
52+
// + file has no purpose until the plugin writer code is added for newly created resources
53+
// ^ this will allow users to edit the files they have created
54+
if(Constants.DEV_MODE)
55+
{
56+
quickOpen.add(createMenu("File", FileType.FILE, selPath));
57+
quickOpen.add(createMenu("Directory", FileType.DIRECTORY, selPath));
58+
}
4859
menu.add(quickOpen);
4960
}));
5061
}
@@ -53,28 +64,47 @@ private static JMenuItem createMenu(String name, FileType fileType, TreePath sel
5364
{
5465
JMenuItem menu = new JMenuItem(name);
5566

56-
String separator = "/"; //fileType == FileType.CLASS ? "." : "/";
57-
String firstPath = buildPath(0, 2, selPath, separator);
67+
String separator = fileType == FileType.CLASS ? "." : "/";
68+
String firstPath = buildPath(0, 2, selPath, "/");
5869
String path = buildPath(2, selPath.getPathCount(), selPath, separator);
5970
String containerName = selPath.getPathComponent(1).toString();
6071

6172
menu.addActionListener((e)->{
62-
String newPath = BytecodeViewer.showInput("Name", "Enter the file name", path);
73+
String newPath = BytecodeViewer.showInput("Name",
74+
fileType == FileType.CLASS ? "Enter the class name" : "Enter the file name",
75+
FilenameUtils.removeExtension(path));
6376

6477
if(newPath == null || newPath.isEmpty())
6578
return;
6679

80+
byte[] contents = new byte[0];
81+
String resourcePath = newPath;
82+
6783
switch(fileType)
6884
{
69-
case FILE:
70-
BytecodeViewer.resourceContainers.get(containerName).resourceFiles.put(newPath, new byte[0]);
71-
searchAndInsert(firstPath+"/"+newPath, BytecodeViewer.resourceContainers.get(containerName).treeNode);
72-
break;
73-
7485
case CLASS:
75-
//TODO needs to be an empty class file, asm can help with this
86+
ClassNode cn = new ClassNode();
87+
88+
//TODO this should be a dialog
89+
cn.version = 52;
90+
91+
//TODO santize newPath and remove extension if added
92+
cn.name = newPath;
93+
resourcePath = resourcePath.replace(".", "/") + ".class";
94+
95+
contents = ASMUtil.nodeToBytes(cn);
96+
97+
BytecodeViewer.resourceContainers.get(containerName).resourceFiles.put(resourcePath, contents);
98+
searchAndInsert(firstPath + "/" + resourcePath, BytecodeViewer.resourceContainers.get(containerName).treeNode, "/");
99+
100+
break;
101+
case FILE:
102+
BytecodeViewer.resourceContainers.get(containerName).resourceFiles.put(resourcePath, contents);
103+
searchAndInsert(firstPath + separator +newPath, BytecodeViewer.resourceContainers.get(containerName).treeNode, separator);
76104
break;
77105
}
106+
107+
BytecodeViewer.viewer.resourcePane.tree.updateUI();
78108
});
79109

80110
return menu;
@@ -109,12 +139,12 @@ public static String buildPath(int startsAt, int max, DefaultMutableTreeNode tre
109139
}
110140

111141
//TODO this needs to be rewritten to support creating parent nodes that don't exist
112-
public static boolean searchAndInsert(String path, DefaultMutableTreeNode treeNode)
142+
public static boolean searchAndInsert(String path, DefaultMutableTreeNode treeNode, String separator)
113143
{
114144
Enumeration<DefaultMutableTreeNode> children = treeNode.children();
115145

116146
String findPath = FilenameUtils.getPath(path);
117-
String currentPath = buildPath(0, treeNode.getPath().length, treeNode, "/");
147+
String currentPath = buildPath(0, treeNode.getPath().length, treeNode, separator);
118148
String directory = FilenameUtils.getPath(currentPath);
119149

120150
if(currentPath.startsWith(findPath))
@@ -127,7 +157,7 @@ public static boolean searchAndInsert(String path, DefaultMutableTreeNode treeNo
127157
while(children.hasMoreElements())
128158
{
129159
DefaultMutableTreeNode child = children.nextElement();
130-
if(searchAndInsert(path, child))
160+
if(searchAndInsert(path, child, separator))
131161
return true;
132162
}
133163

@@ -138,5 +168,6 @@ public enum FileType
138168
{
139169
CLASS,
140170
FILE,
171+
DIRECTORY,
141172
}
142173
}

0 commit comments

Comments
 (0)