Skip to content

Commit 9ac0bd3

Browse files
committed
Refactor (FileContainer, String name) into ViewerFile pair
Partially fix reload bug
1 parent 9c37183 commit 9ac0bd3

File tree

11 files changed

+140
-113
lines changed

11 files changed

+140
-113
lines changed
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package club.bytecode.the.jda;
22

3+
import club.bytecode.the.jda.gui.fileviewer.ViewerFile;
34
import org.objectweb.asm.tree.ClassNode;
45

56
/**
@@ -9,7 +10,7 @@
910
*/
1011

1112
public interface FileChangeNotifier {
12-
void openClassFile(String name, FileContainer container, ClassNode cn);
13+
void openClassFile(ViewerFile file, ClassNode cn);
1314

14-
void openFile(String name, FileContainer container, byte[] contents);
15+
void openFile(ViewerFile file, byte[] contents);
1516
}

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import club.bytecode.the.jda.gui.MainViewerGUI;
77
import club.bytecode.the.jda.gui.fileviewer.BytecodeFoldParser;
88
import club.bytecode.the.jda.gui.fileviewer.BytecodeTokenizer;
9+
import club.bytecode.the.jda.gui.fileviewer.ViewerFile;
910
import club.bytecode.the.jda.gui.navigation.FileNavigationPane;
1011
import club.bytecode.the.jda.settings.Settings;
1112
import org.apache.commons.io.FileUtils;
@@ -152,15 +153,15 @@ private static void onExit() {
152153
Settings.saveGUI();
153154
}
154155

155-
public static byte[] getFileBytes(FileContainer container, String name) {
156-
if (container != null)
157-
return container.getFiles().get(name);
156+
public static byte[] getFileBytes(ViewerFile file) {
157+
if (file.container != null)
158+
return file.container.getFiles().get(file.name);
158159
else
159160
return null;
160161
}
161162

162163
public static byte[] getClassBytes(FileContainer container, ClassNode cn) {
163-
byte[] bytes = getFileBytes(container, container.findClassfile(cn.name));
164+
byte[] bytes = getFileBytes(new ViewerFile(container, container.findClassfile(cn.name)));
164165
if (bytes == null)
165166
return null;
166167
if (cn.version < 49)

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package club.bytecode.the.jda.gui;
22

33
import club.bytecode.the.jda.FileChangeNotifier;
4-
import club.bytecode.the.jda.FileContainer;
4+
import club.bytecode.the.jda.gui.fileviewer.ViewerFile;
55
import club.bytecode.the.jda.settings.IPersistentWindow;
66
import org.objectweb.asm.tree.ClassNode;
77

@@ -19,6 +19,8 @@
1919
* @author WaterWolf
2020
*/
2121

22+
// TODO: why does this implement FileChangeNotifier?
23+
// that ought to be refactored
2224
public abstract class JDAWindow extends JInternalFrame implements FileChangeNotifier, IPersistentWindow {
2325
private String windowId;
2426

@@ -88,10 +90,10 @@ public void onJDAMaximized() {
8890
}
8991

9092
@Override
91-
public abstract void openClassFile(final String name, FileContainer container, final ClassNode cn);
93+
public abstract void openClassFile(ViewerFile file, final ClassNode cn);
9294

9395
@Override
94-
public abstract void openFile(final String name, FileContainer container, byte[] contents);
96+
public abstract void openFile(ViewerFile file, byte[] contents);
9597

9698
protected static Dimension defaultDimensions;
9799
protected static Point defaultPosition;

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import club.bytecode.the.jda.gui.dialogs.TabbedPane;
1313
import club.bytecode.the.jda.gui.fileviewer.FileViewerPane;
1414
import club.bytecode.the.jda.gui.fileviewer.Viewer;
15+
import club.bytecode.the.jda.gui.fileviewer.ViewerFile;
1516
import club.bytecode.the.jda.gui.navigation.FileNavigationPane;
1617
import club.bytecode.the.jda.settings.IPersistentWindow;
1718
import club.bytecode.the.jda.settings.Settings;
@@ -401,15 +402,15 @@ public void calledAfterLoad() {
401402
}
402403

403404
@Override
404-
public void openClassFile(final String name, FileContainer container, final ClassNode cn) {
405+
public void openClassFile(ViewerFile file, final ClassNode cn) {
405406
for (final JDAWindow vc : windows)
406-
vc.openClassFile(name, container, cn);
407+
vc.openClassFile(file, cn);
407408
}
408409

409410
@Override
410-
public void openFile(final String name, FileContainer container, byte[] content) {
411+
public void openFile(ViewerFile file, byte[] content) {
411412
for (final JDAWindow vc : windows)
412-
vc.openFile(name, container, content);
413+
vc.openFile(file, content);
413414
}
414415

415416
public void refreshView() {
@@ -434,8 +435,9 @@ public void reloadResources() {
434435
reopen.add(container.file);
435436

436437
JDA.files.clear();
437-
JDA.openFiles(reopen.toArray(new File[reopen.size()]), false);
438+
closeResources();
438439

440+
JDA.openFiles(reopen.toArray(new File[reopen.size()]), false);
439441
refreshView();
440442
}
441443
}

src/main/java/club/bytecode/the/jda/gui/fileviewer/ClassViewer.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package club.bytecode.the.jda.gui.fileviewer;
22

3-
import club.bytecode.the.jda.FileContainer;
43
import club.bytecode.the.jda.JDA;
54
import club.bytecode.the.jda.decompilers.JDADecompiler;
65
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
@@ -99,9 +98,8 @@ public void hierarchyChanged(HierarchyEvent e) {
9998
public List<RSyntaxTextArea> javas = Arrays.asList(null, null, null);
10099
public List<SearchPanel> searches = Arrays.asList(null, null, null);
101100

102-
public ClassViewer(final String name, final FileContainer container, final ClassNode cn) {
103-
this.name = name;
104-
this.container = container;
101+
public ClassViewer(ViewerFile file, final ClassNode cn) {
102+
super(file);
105103
this.cn = cn;
106104
updateName();
107105
this.setLayout(new BorderLayout());
@@ -111,7 +109,7 @@ public ClassViewer(final String name, final FileContainer container, final Class
111109
this.add(sp2, BorderLayout.CENTER);
112110

113111
JDA.viewer.setIcon(true);
114-
startPaneUpdater(null);
112+
refresh(null);
115113
this.addComponentListener(new ComponentAdapter() {
116114
public void componentResized(ComponentEvent e) {
117115
resetDivider();
@@ -163,8 +161,9 @@ public void resetDivider() {
163161
}
164162
}
165163

166-
public void startPaneUpdater(final JButton button) {
167-
this.cn = container.getClassNode(cn.name); //update the classnode
164+
@Override
165+
public void refresh(final JButton button) {
166+
this.cn = getFile().container.getClassNode(cn.name); //update the classnode
168167
setPanes();
169168

170169
for (JPanel jpanel : panels) {

src/main/java/club/bytecode/the/jda/gui/fileviewer/FileViewer.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package club.bytecode.the.jda.gui.fileviewer;
22

3-
import club.bytecode.the.jda.FileContainer;
43
import club.bytecode.the.jda.api.ExceptionUI;
54
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
65
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
@@ -34,7 +33,7 @@ public class FileViewer extends Viewer {
3433
boolean canRefresh = false;
3534

3635
public void setContents() {
37-
String name = this.name.toLowerCase();
36+
String name = getFile().name.toLowerCase();
3837
panelArea.setCodeFoldingEnabled(true);
3938
panelArea.setAntiAliasingEnabled(true);
4039
RTextScrollPane scrollPane = new RTextScrollPane(panelArea);
@@ -148,9 +147,8 @@ public static boolean isPureAscii(String v) {
148147
return asciiEncoder.canEncode(v);
149148
}
150149

151-
public FileViewer(final String name, final FileContainer container, final byte[] contents) {
152-
this.name = name;
153-
this.container = container;
150+
public FileViewer(ViewerFile file, final byte[] contents) {
151+
super(file);
154152
this.contents = contents;
155153
updateName();
156154
this.setLayout(new BorderLayout());
@@ -160,6 +158,7 @@ public FileViewer(final String name, final FileContainer container, final byte[]
160158
setContents();
161159
}
162160

161+
@Override
163162
public void refresh(JButton src) {
164163
if (!canRefresh) {
165164
src.setEnabled(true);

src/main/java/club/bytecode/the/jda/gui/fileviewer/FileViewerPane.java

Lines changed: 45 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package club.bytecode.the.jda.gui.fileviewer;
22

33
import club.bytecode.the.jda.FileChangeNotifier;
4-
import club.bytecode.the.jda.FileContainer;
54
import club.bytecode.the.jda.JDA;
65
import club.bytecode.the.jda.Resources;
76
import club.bytecode.the.jda.gui.JDAWindow;
@@ -12,11 +11,11 @@
1211

1312
import javax.swing.*;
1413
import java.awt.*;
15-
import java.awt.event.ActionEvent;
16-
import java.awt.event.ActionListener;
1714
import java.awt.event.ContainerEvent;
1815
import java.awt.event.ContainerListener;
19-
import java.util.HashMap;
16+
import java.util.ArrayList;
17+
import java.util.List;
18+
import java.util.function.Supplier;
2019

2120
/**
2221
* The pane that contains all of the classes as tabs.
@@ -25,7 +24,7 @@
2524
* @author WaterWolf
2625
*/
2726

28-
public class FileViewerPane extends JDAWindow implements ActionListener {
27+
public class FileViewerPane extends JDAWindow {
2928

3029
private static final long serialVersionUID = 6542337997679487946L;
3130

@@ -35,7 +34,8 @@ public class FileViewerPane extends JDAWindow implements ActionListener {
3534
JPanel buttonPanel;
3635
public JButton refreshClass;
3736

38-
HashMap<String, Integer> workingOn = new HashMap<>();
37+
// todo: once we move to mapleir, we can convert this to an indexedlist!
38+
List<ViewerFile> workingOn = new ArrayList<>();
3939

4040
public FileViewerPane(final FileChangeNotifier fcn) {
4141
super("WorkPanel", "Work Space", Resources.fileNavigatorIcon, (MainViewerGUI) fcn);
@@ -50,7 +50,16 @@ public FileViewerPane(final FileChangeNotifier fcn) {
5050
buttonPanel = new JPanel(new FlowLayout());
5151

5252
refreshClass = new JButton("Refresh");
53-
refreshClass.addActionListener(this);
53+
refreshClass.addActionListener(e -> (new Thread(() -> {
54+
final Component tabComp = tabs.getSelectedComponent();
55+
if (tabComp != null) {
56+
assert(tabComp instanceof Viewer);
57+
Viewer viewer = (Viewer) tabComp;
58+
JDA.viewer.setIcon(true);
59+
viewer.refresh(refreshClass);
60+
JDA.viewer.setIcon(false);
61+
}
62+
})).start());
5463

5564
buttonPanel.add(refreshClass);
5665

@@ -66,13 +75,9 @@ public void componentAdded(final ContainerEvent e) {
6675
@Override
6776
public void componentRemoved(final ContainerEvent e) {
6877
final Component c = e.getChild();
69-
if (c instanceof ClassViewer) {
70-
ClassViewer cv = (ClassViewer) c;
71-
workingOn.remove(cv.container + "$" + cv.name);
72-
}
73-
if (c instanceof FileViewer) {
74-
FileViewer fv = (FileViewer) c;
75-
workingOn.remove(fv.container + "$" + fv.name);
78+
if (c instanceof Viewer) {
79+
Viewer v = (Viewer) c;
80+
workingOn.remove(v.getFile());
7681
}
7782
}
7883

@@ -82,7 +87,7 @@ public void componentRemoved(final ContainerEvent e) {
8287
this.setVisible(true);
8388

8489
}
85-
90+
8691
public static Dimension defaultDimension = new Dimension(-FileNavigationPane.defaultDimension.width, -35);
8792
public static Point defaultPosition = new Point(FileNavigationPane.defaultDimension.width, 0);
8893

@@ -96,82 +101,52 @@ public Point getDefaultPosition() {
96101
return defaultPosition;
97102
}
98103

99-
int tabCount = 0;
100-
101-
public void addWorkingFile(final String name, FileContainer container, final ClassNode cn) {
102-
String key = container + "$" + name;
103-
if (!workingOn.containsKey(key)) {
104-
final JPanel tabComp = new ClassViewer(name, container, cn);
104+
private void addFile(ViewerFile file, Supplier<Viewer> viewerFactory) {
105+
if (!workingOn.contains(file)) {
106+
final JPanel tabComp = viewerFactory.get();
105107
tabs.add(tabComp);
106108
final int tabCount = tabs.indexOfComponent(tabComp);
107-
workingOn.put(key, tabCount);
108-
tabs.setTabComponentAt(tabCount, new TabbedPane(name, tabs));
109+
workingOn.add(tabCount, file);
110+
tabs.setTabComponentAt(tabCount, new TabbedPane(file.name, tabs));
109111
tabs.setSelectedIndex(tabCount);
110112
} else {
111-
tabs.setSelectedIndex(workingOn.get(key));
113+
tabs.setSelectedIndex(workingOn.indexOf(file));
112114
}
113115
}
116+
117+
public void addWorkingFile(ViewerFile file, final ClassNode cn) {
118+
addFile(file, () -> new ClassViewer(file, cn));
119+
}
114120

115-
public void addFile(final String name, FileContainer container, byte[] contents) {
116-
if (contents == null) //a directory
117-
return;
118-
119-
String key = container + "$" + name;
120-
if (!workingOn.containsKey(key)) {
121-
final Component tabComp = new FileViewer(name, container, contents);
122-
tabs.add(tabComp);
123-
final int tabCount = tabs.indexOfComponent(tabComp);
124-
workingOn.put(key, tabCount);
125-
tabs.setTabComponentAt(tabCount, new TabbedPane(name, tabs));
126-
tabs.setSelectedIndex(tabCount);
127-
} else {
128-
tabs.setSelectedIndex(workingOn.get(key));
129-
}
121+
public void addFile(ViewerFile file, byte[] contents) {
122+
addFile(file, () -> new FileViewer(file, contents));
130123
}
131124

132125
@Override
133-
public void openClassFile(final String name, FileContainer container, final ClassNode cn) {
134-
addWorkingFile(name, container, cn);
126+
public void openClassFile(ViewerFile file, final ClassNode cn) {
127+
addWorkingFile(file, cn);
135128
}
136129

137130
@Override
138-
public void openFile(final String name, FileContainer container, byte[] content) {
139-
addFile(name, container, content);
131+
public void openFile(ViewerFile file, byte[] content) {
132+
addFile(file, content);
140133
}
141134

142135
public Viewer getCurrentViewer() {
143136
return (Viewer) tabs.getSelectedComponent();
144137
}
145-
146-
public java.awt.Component[] getLoadedViewers() {
147-
return tabs.getComponents();
138+
139+
public Viewer[] getLoadedViewers() {
140+
return (Viewer[]) tabs.getComponents();
148141
}
149142

150-
@Override
151-
public void actionPerformed(final ActionEvent arg0) {
152-
Thread t = new Thread() {
153-
public void run() {
154-
final JButton src = (JButton) arg0.getSource();
155-
if (src == refreshClass) {
156-
final Component tabComp = tabs.getSelectedComponent();
157-
if (tabComp != null) {
158-
if (tabComp instanceof ClassViewer) {
159-
JDA.viewer.setIcon(true);
160-
((ClassViewer) tabComp).startPaneUpdater(src);
161-
JDA.viewer.setIcon(false);
162-
} else if (tabComp instanceof FileViewer) {
163-
src.setEnabled(false);
164-
JDA.viewer.setIcon(true);
165-
((FileViewer) tabComp).refresh(src);
166-
JDA.viewer.setIcon(false);
167-
}
168-
}
169-
}
170-
}
171-
};
172-
t.start();
143+
/**
144+
* @return a copy of the files currently open
145+
*/
146+
public List<ViewerFile> getOpenFiles() {
147+
return new ArrayList<>(workingOn);
173148
}
174-
149+
175150
public void resetWorkspace() {
176151
for (Component component : tabs.getComponents()) {
177152
if (component instanceof ClassViewer)

0 commit comments

Comments
 (0)