Skip to content

Commit 70ca268

Browse files
committed
Folder functionality
1 parent 3768831 commit 70ca268

File tree

5 files changed

+81
-97
lines changed

5 files changed

+81
-97
lines changed

src/main/java/club/bytecode/the/jda/FileContainer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public String findClassfile(String className) {
5252
return "";
5353
}
5454

55-
public Map<String, byte[]> getData() {
55+
public Map<String, byte[]> getFiles() {
5656
return files;
5757
}
5858

src/main/java/club/bytecode/the/jda/JDA.java

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ private static void onExit() {
154154

155155
public static byte[] getFileBytes(FileContainer container, String name) {
156156
if (container != null)
157-
return container.getData().get(name);
157+
return container.getFiles().get(name);
158158
else
159159
return null;
160160
}
@@ -244,67 +244,66 @@ public static ArrayList<ClassNode> loadAllClasses() {
244244
public static Map<String, byte[]> getLoadedBytes() {
245245
Map<String, byte[]> data = new HashMap<>();
246246
for (FileContainer container : files) {
247-
data.putAll(container.getData());
247+
data.putAll(container.getFiles());
248248
}
249249
return data;
250250
}
251251

252-
private static boolean update = true;
253-
254252
/**
255253
* Opens a file, optional if it should append to the recent files menu
256254
*
257255
* @param files the file(s) you wish to open
258256
* @param recentFiles if it should append to the recent files menu
259257
*/
260258
public static void openFiles(final File[] files, boolean recentFiles) {
259+
openFiles(files, recentFiles, null);
260+
}
261+
262+
public static void openFiles(final File[] files, boolean recentFiles, FileNavigationPane.FileNode parent) {
261263
if (recentFiles)
262264
for (File f : files)
263265
if (f.exists())
264266
JDA.addRecentFile(f);
265267

266268
JDA.viewer.setIcon(true);
267-
update = true;
269+
270+
FileNavigationPane fnp = MainViewerGUI.getComponent(FileNavigationPane.class);
268271

269272
(new Thread(() -> {
270273
try {
271274
for (final File fileToOpen : files) {
272275
final String fn = fileToOpen.getName();
273276
if (!fileToOpen.exists()) {
274-
update = false;
275277
showMessage("The file " + fileToOpen.getAbsolutePath() + " could not be found.");
276278
} else if (fileToOpen.isDirectory()) {
277-
openFiles(fileToOpen.listFiles(), false);
279+
FileNavigationPane.FileNode newNode = fnp.addTreeElement(new FileContainer(fileToOpen), parent);
280+
openFiles(fileToOpen.listFiles(), false, newNode);
278281
} else if (fn.endsWith(".jar") || fn.endsWith(".zip")) {
279282
try {
280-
JarUtils.put(fileToOpen);
283+
FileContainer newContainer = JarUtils.load(fileToOpen);
284+
addFile(newContainer);
285+
fnp.addTreeElement(newContainer, parent);
281286
} catch (final Exception e) {
282287
new ExceptionUI(e);
283-
update = false;
284288
}
285289
} else if (fn.endsWith(".class")) {
286290
FileContainer container = loadClassfile(fileToOpen, fn);
287291
addFile(container);
292+
fnp.addTreeElement(container, parent);
288293
} else {
289294
HashMap<String, byte[]> files1 = new HashMap<>();
290295
byte[] bytes = JarUtils.getBytes(new FileInputStream(fileToOpen));
291296
files1.put(fileToOpen.getName(), bytes);
292297
FileContainer container = new FileContainer(fileToOpen);
293298
container.files = files1;
294299
addFile(container);
300+
fnp.addTreeElement(container, parent);
295301
}
296302
}
297303
} catch (final Exception e) {
298304
new ExceptionUI(e);
299305
} finally {
300306
JDA.viewer.setIcon(false);
301-
if (update) {
302-
try {
303-
MainViewerGUI.getComponent(FileNavigationPane.class).updateTree();
304-
} catch (NullPointerException e) {
305-
e.printStackTrace();
306-
}
307-
}
308307
}
309308
})).start();
310309
}
@@ -321,11 +320,9 @@ public static FileContainer loadClassfile(File fileToOpen, String fn) {
321320
return container;
322321
} else {
323322
showMessage(fn + ": Header does not start with CAFEBABE, ignoring.");
324-
update = false;
325323
}
326324
} catch (final Exception e) {
327325
new ExceptionUI(e);
328-
update = false;
329326
}
330327
return null;
331328
}
@@ -575,8 +572,8 @@ public void run() {
575572
};
576573
t.start();
577574
} else if ((e.getKeyCode() == KeyEvent.VK_W) && isCtrlDown(e)) {
578-
if (viewer.FileViewerPane.getCurrentViewer() != null)
579-
viewer.FileViewerPane.tabs.remove(viewer.FileViewerPane.getCurrentViewer());
575+
if (viewer.fileViewerPane.getCurrentViewer() != null)
576+
viewer.fileViewerPane.tabs.remove(viewer.fileViewerPane.getCurrentViewer());
580577
}
581578
}
582579

src/main/java/club/bytecode/the/jda/JarUtils.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,7 @@
2222
*/
2323

2424
public class JarUtils {
25-
26-
/**
27-
* Loads the classes and resources from the input jar file
28-
*
29-
* @param jarFile the input jar file
30-
* @param clazzList the existing map of loaded classes
31-
* @throws IOException
32-
*/
33-
public static void put(final File jarFile) throws IOException {
25+
public static FileContainer load(final File jarFile) throws IOException {
3426
FileContainer container = new FileContainer(jarFile);
3527
HashMap<String, byte[]> files = new HashMap<>();
3628

@@ -56,8 +48,7 @@ public static void put(final File jarFile) throws IOException {
5648
}
5749
jis.close();
5850
container.files = files;
59-
JDA.files.add(container);
60-
51+
return container;
6152
}
6253

6354

src/main/java/club/bytecode/the/jda/gui/MainViewerGUI.java

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -51,18 +51,9 @@ public class MainViewerGUI extends JFrame implements FileChangeNotifier, IPersis
5151

5252
public JDesktopPane desktop;
5353
public FileNavigationPane navigator;
54-
public club.bytecode.the.jda.gui.fileviewer.FileViewerPane FileViewerPane;
54+
public FileViewerPane fileViewerPane;
5555
public static ArrayList<JDAWindow> windows = new ArrayList<>();
56-
private final ActionListener listener = new ActionListener() {
57-
@Override
58-
public void actionPerformed(ActionEvent arg0) {
59-
if (Settings.REFRESH_ON_VIEW_CHANGE.getBool()) {
60-
if (FileViewerPane.getCurrentViewer() == null)
61-
return;
62-
FileViewerPane.refreshClass.doClick();
63-
}
64-
}
65-
};
56+
6657
public AboutWindow aboutWindow = new AboutWindow();
6758
public IntroWindow introWindow = new IntroWindow();
6859
public List<ButtonGroup> allPanes = Collections.unmodifiableList(Arrays.asList(panelGroup1, panelGroup2, panelGroup3));
@@ -285,15 +276,15 @@ private void initializeMenubar() {
285276

286277
mnShowContainer.setSelected(Settings.SHOW_CONTAINER_NAME.getBool());
287278
mnShowContainer.addItemListener(e -> {
288-
JTabbedPane tabs = FileViewerPane.tabs;
279+
JTabbedPane tabs = fileViewerPane.tabs;
289280
Component[] components = tabs.getComponents();
290281
for (int i = 0; i < components.length; i++) {
291282
Component c = components[i];
292283
if (c instanceof Viewer) {
293284
((Viewer) c).updateName();
294285
int idx = tabs.indexOfComponent(c);
295286
tabs.setTabComponentAt(idx, new TabbedPane(c.getName(), tabs));
296-
FileViewerPane.tabs.setTitleAt(idx, c.getName());
287+
fileViewerPane.tabs.setTitleAt(idx, c.getName());
297288
}
298289
}
299290
Settings.SHOW_CONTAINER_NAME.set(mnShowContainer.isSelected());
@@ -314,17 +305,17 @@ public static <T> T getComponent(final Class<T> clazz) {
314305

315306
private void initializeWindows() {
316307
navigator = new FileNavigationPane(this);
317-
FileViewerPane = new FileViewerPane(this);
308+
fileViewerPane = new FileViewerPane(this);
318309

319310
desktop = new JDesktopPane();
320311
setContentPane(desktop);
321312
desktop.add(navigator);
322-
desktop.add(FileViewerPane);
313+
desktop.add(fileViewerPane);
323314
desktop.setDesktopManager(new WorkspaceDesktopManager());
324315
desktop.setBackground(COLOR_DESKTOP_BACKGROUND);
325316

326317
windows.add(navigator);
327-
windows.add(FileViewerPane);
318+
windows.add(fileViewerPane);
328319
}
329320

330321
public void resetWindows() {
@@ -386,7 +377,7 @@ private JMenu generatePane(int id) {
386377

387378
public void closeResources() {
388379
navigator.resetWorkspace();
389-
FileViewerPane.resetWorkspace();
380+
fileViewerPane.resetWorkspace();
390381
}
391382

392383
public void setIcon(final boolean busy) {
@@ -421,7 +412,7 @@ public void openFile(final String name, FileContainer container, byte[] content)
421412
}
422413

423414
public void refreshView() {
424-
FileViewerPane.refreshClass.doClick();
415+
fileViewerPane.refreshClass.doClick();
425416
}
426417

427418
public void reloadResources() {
@@ -467,7 +458,7 @@ private void saveAsRunnableJar() {
467458
}
468459

469460
private void decompileSaveOpenedClasses() {
470-
if (FileViewerPane.getCurrentViewer() == null) {
461+
if (fileViewerPane.getCurrentViewer() == null) {
471462
JDA.showMessage("First open a class, jar, or zip file.");
472463
return;
473464
}

src/main/java/club/bytecode/the/jda/gui/navigation/FileNavigationPane.java

Lines changed: 51 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -217,49 +217,50 @@ public void filesDropped(final File[] files) {
217217
JDA.openFiles(files, true);
218218
}
219219

220-
public void updateTree() {
221-
try {
222-
treeRoot.removeAllChildren();
223-
for (FileContainer container : JDA.files) {
224-
FileNode root = new FileNode(container);
225-
treeRoot.add(root);
226-
JDATreeCellRenderer renderer = new JDATreeCellRenderer();
227-
tree.setCellRenderer(renderer);
228-
229-
if (!container.files.isEmpty()) {
230-
for (final Entry<String, byte[]> entry : container.files.entrySet()) {
231-
String name = entry.getKey();
232-
final String[] spl = name.split("/");
233-
if (spl.length < 2) {
234-
root.add(new FileNode(name));
235-
} else {
236-
FileNode parent = root;
237-
for (final String s : spl) {
238-
FileNode child = null;
239-
for (int i = 0; i < parent.getChildCount(); i++) {
240-
if (((FileNode) parent.getChildAt(i)).getUserObject().equals(s)) {
241-
child = (FileNode) parent.getChildAt(i);
242-
break;
243-
}
244-
}
245-
if (child == null) {
246-
child = new FileNode(s);
247-
parent.add(child);
248-
}
249-
parent = child;
220+
/**
221+
* Add tree element.
222+
* If parent is null, it will be added to the root node.
223+
*/
224+
public FileNode addTreeElement(FileContainer container, FileNode parent) {
225+
if (parent == null)
226+
parent = treeRoot;
227+
228+
FileNode root = new FileNode(container);
229+
parent.add(root);
230+
JDATreeCellRenderer renderer = new JDATreeCellRenderer();
231+
tree.setCellRenderer(renderer);
232+
233+
if (!container.files.isEmpty()) {
234+
for (final Entry<String, byte[]> entry : container.files.entrySet()) {
235+
String name = entry.getKey();
236+
final String[] spl = name.split("/");
237+
if (spl.length <= 1) {
238+
root.add(new FileNode(name));
239+
} else {
240+
FileNode parentNode = root;
241+
for (final String s : spl) {
242+
FileNode child = null;
243+
for (int i = 0; i < parentNode.getChildCount(); i++) {
244+
if (((FileNode) parentNode.getChildAt(i)).getUserObject().equals(s)) {
245+
child = (FileNode) parentNode.getChildAt(i);
246+
break;
250247
}
251248
}
249+
if (child == null) {
250+
child = new FileNode(s);
251+
parentNode.add(child);
252+
}
253+
parentNode = child;
252254
}
253255
}
254-
255256
}
256-
257-
treeRoot.sort();
258-
tree.expandPath(new TreePath(tree.getModel().getRoot()));
259-
tree.updateUI();
260-
} catch (java.util.ConcurrentModificationException e) {
261-
//ignore, the last file will reset everything
262257
}
258+
259+
parent.sort();
260+
tree.expandPath(new TreePath(tree.getModel().getRoot()));
261+
tree.updateUI();
262+
263+
return root;
263264
}
264265

265266
@SuppressWarnings("rawtypes")
@@ -313,6 +314,8 @@ public void paint(final Graphics g) {
313314

314315
public class FileNode extends DefaultMutableTreeNode {
315316

317+
public final boolean isJava = false;
318+
316319
private static final long serialVersionUID = -8817777566176729571L;
317320

318321
public FileNode(final Object o) {
@@ -399,17 +402,11 @@ public void resetWorkspace() {
399402
public void openPath(TreePath path) {
400403
if (path == null)
401404
return;
402-
final StringBuilder nameBuffer = new StringBuilder();
403-
for (int i = 2; i < path.getPathCount(); i++) {
404-
nameBuffer.append(path.getPathComponent(i));
405-
if (i < path.getPathCount() - 1) {
406-
nameBuffer.append("/");
407-
}
408-
}
409405

410406
FileContainer container = null;
411-
for (int i = path.getPathCount() - 1; i > 0; i--) {
412-
Object o = ((FileNode) path.getPathComponent(1)).getUserObject();
407+
int containerLevel;
408+
for (containerLevel = path.getPathCount() - 1; containerLevel > 0; containerLevel--) {
409+
Object o = ((FileNode) path.getPathComponent(containerLevel)).getUserObject();
413410
if (o != null && o instanceof FileContainer) {
414411
container = (FileContainer) o;
415412
break;
@@ -420,6 +417,14 @@ public void openPath(TreePath path) {
420417
throw new IllegalStateException("Path isn't parented to a container?");
421418
}
422419

420+
final StringBuilder nameBuffer = new StringBuilder();
421+
for (int i = containerLevel + 1; i < path.getPathCount(); i++) {
422+
nameBuffer.append(path.getPathComponent(i));
423+
if (i < path.getPathCount() - 1) {
424+
nameBuffer.append("/");
425+
}
426+
}
427+
423428
String name = nameBuffer.toString();
424429
if (name.endsWith(".class")) {
425430
final ClassNode cn = container.loadClass(name);

0 commit comments

Comments
 (0)