Skip to content

Commit 3f9cf19

Browse files
committed
Make reload resources actually update viewer panes' contents
Refactor MainViewerGUI#setIcon into JDA#setBusy and make thread-safe
1 parent 9ac0bd3 commit 3f9cf19

File tree

9 files changed

+95
-30
lines changed

9 files changed

+95
-30
lines changed

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

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.HashMap;
2929
import java.util.List;
3030
import java.util.Map;
31+
import java.util.concurrent.atomic.AtomicInteger;
3132

3233
public class JDA {
3334
/*per version*/
@@ -49,6 +50,7 @@ public class JDA {
4950
public static String lastDirectory = "";
5051
public static List<Process> createdProcesses = new ArrayList<>();
5152
public static List<Plugin> plugins = new ArrayList<>();
53+
private static AtomicInteger jobCount = new AtomicInteger(0);
5254

5355
/**
5456
* Main startup
@@ -152,6 +154,34 @@ private static void onExit() {
152154
viewer.unmaximizedPos = viewer.getLocation();
153155
Settings.saveGUI();
154156
}
157+
158+
/**
159+
* Waits for all busy-setting tasks to complete.
160+
*/
161+
public static void waitForTasks() {
162+
while (jobCount.get() > 0) {
163+
try {
164+
Thread.sleep(10L);
165+
} catch (InterruptedException e) {
166+
e.printStackTrace();
167+
}
168+
}
169+
}
170+
171+
/**
172+
* Sets the busy state, and toggles the spinner icon
173+
* Make sure to call me OUTSIDE of your worker thread for busy=true!
174+
* Then, you must call again busy=false once your worker thread finishes!
175+
* @param busy whether a background task is running
176+
*/
177+
public static void setBusy(boolean busy) {
178+
if (busy)
179+
jobCount.incrementAndGet();
180+
else
181+
jobCount.decrementAndGet();
182+
assert (jobCount.get() >= 0);
183+
viewer.setIcon(busy);
184+
}
155185

156186
public static byte[] getFileBytes(ViewerFile file) {
157187
if (file.container != null)
@@ -261,13 +291,13 @@ public static void openFiles(final File[] files, boolean recentFiles) {
261291
}
262292

263293
public static void openFiles(final File[] files, boolean recentFiles, FileNavigationPane.FileNode parent) {
294+
JDA.setBusy(true);
295+
264296
if (recentFiles)
265297
for (File f : files)
266298
if (f.exists())
267299
JDA.addRecentFile(f);
268300

269-
JDA.viewer.setIcon(true);
270-
271301
FileNavigationPane fnp = MainViewerGUI.getComponent(FileNavigationPane.class);
272302

273303
(new Thread(() -> {
@@ -304,7 +334,7 @@ public static void openFiles(final File[] files, boolean recentFiles, FileNaviga
304334
} catch (final Exception e) {
305335
new ExceptionUI(e);
306336
} finally {
307-
JDA.viewer.setIcon(false);
337+
JDA.setBusy(false);
308338
}
309339
})).start();
310340
}
@@ -559,12 +589,12 @@ public String getDescription() {
559589

560590
final File file2 = file;
561591

562-
JDA.viewer.setIcon(true);
592+
JDA.setBusy(true);
563593
Thread t = new Thread() {
564594
@Override
565595
public void run() {
566596
JarUtils.saveAsJar(JDA.getLoadedBytes(), file2.getAbsolutePath());
567-
JDA.viewer.setIcon(false);
597+
JDA.setBusy(false);
568598
}
569599
};
570600
t.start();
@@ -595,9 +625,9 @@ public static void openFileChooser() {
595625
if (returnVal == JFileChooser.APPROVE_OPTION) {
596626
JDA.lastDirectory = fc.getSelectedFile().getAbsolutePath();
597627
try {
598-
JDA.viewer.setIcon(true);
628+
JDA.setBusy(true);
599629
JDA.openFiles(new File[]{fc.getSelectedFile()}, true);
600-
JDA.viewer.setIcon(false);
630+
JDA.setBusy(false);
601631
} catch (Exception e1) {
602632
new ExceptionUI(e1);
603633
}

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

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,11 @@ public void closeResources() {
382382
fileViewerPane.resetWorkspace();
383383
}
384384

385+
/**
386+
* Toggles the spinner icon on and off.
387+
* DON'T CALL ME DIRECTLY. CALL JDA.setBusy INSTEAD!!!!
388+
* @param busy whether to show the busy spinner icon or not
389+
*/
385390
public void setIcon(final boolean busy) {
386391
SwingUtilities.invokeLater(() -> {
387392
if (busy) {
@@ -430,14 +435,26 @@ public void reloadResources() {
430435
result = k;
431436

432437
if (result == 0) {
433-
ArrayList<File> reopen = new ArrayList<>();
438+
List<File> reopenContainers = new ArrayList<>();
434439
for (FileContainer container : JDA.files)
435-
reopen.add(container.file);
440+
reopenContainers.add(container.file);
436441

437442
JDA.files.clear();
438-
closeResources();
439-
440-
JDA.openFiles(reopen.toArray(new File[reopen.size()]), false);
443+
navigator.resetWorkspace();
444+
445+
JDA.openFiles(reopenContainers.toArray(new File[reopenContainers.size()]), false);
446+
JDA.waitForTasks(); // this is not really ideal, but whatever.
447+
assert(JDA.files.size() > 0);
448+
for (Viewer v : fileViewerPane.getLoadedViewers()) {
449+
for (FileContainer newContainer : JDA.files) {
450+
if (newContainer.file.equals(v.getFile().container.file)) {
451+
v.setFile(new ViewerFile(newContainer, v.getFile().name));
452+
v.refresh(null);
453+
System.out.println("Found it");
454+
break;
455+
}
456+
}
457+
}
441458
refreshView();
442459
}
443460
}

src/main/java/club/bytecode/the/jda/gui/dialogs/ExportJar.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ public ExportJar(final String jarPath) {
3636
getContentPane().add(btnNewButton);
3737

3838
btnNewButton.addActionListener(arg0 -> {
39-
JDA.viewer.setIcon(true);
39+
JDA.setBusy(true);
4040
Thread t = new Thread() {
4141
@Override
4242
public void run() {
4343
JarUtils.saveAsJar(JDA.getLoadedClasses(), jarPath, mani.getText());
44-
JDA.viewer.setIcon(false);
44+
JDA.setBusy(false);
4545
}
4646
};
4747
t.start();

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import club.bytecode.the.jda.JDA;
44
import club.bytecode.the.jda.decompilers.JDADecompiler;
5+
import com.strobel.annotations.Nullable;
56
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
67
import org.objectweb.asm.tree.ClassNode;
78

@@ -108,7 +109,6 @@ public ClassViewer(ViewerFile file, final ClassNode cn) {
108109
this.sp2 = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, sp, panels.get(2));
109110
this.add(sp2, BorderLayout.CENTER);
110111

111-
JDA.viewer.setIcon(true);
112112
refresh(null);
113113
this.addComponentListener(new ComponentAdapter() {
114114
public void componentResized(ComponentEvent e) {
@@ -162,7 +162,7 @@ public void resetDivider() {
162162
}
163163

164164
@Override
165-
public void refresh(final JButton button) {
165+
public void refresh(@Nullable final JButton button) {
166166
this.cn = getFile().container.getClassNode(cn.name); //update the classnode
167167
setPanes();
168168

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

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

33
import club.bytecode.the.jda.api.ExceptionUI;
4+
import com.strobel.annotations.Nullable;
45
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
56
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
67
import org.fife.ui.rtextarea.RTextScrollPane;
@@ -159,9 +160,11 @@ public FileViewer(ViewerFile file, final byte[] contents) {
159160
}
160161

161162
@Override
162-
public void refresh(JButton src) {
163+
public void refresh(@Nullable JButton src) {
163164
if (!canRefresh) {
164-
src.setEnabled(true);
165+
if (src != null) { // this is such a kludge
166+
src.setEnabled(true);
167+
}
165168
return;
166169
}
167170

@@ -175,6 +178,8 @@ public void refresh(JButton src) {
175178
panel2.add(label, BorderLayout.CENTER);
176179
panel2.updateUI();
177180

178-
src.setEnabled(true);
181+
if (src != null) {
182+
src.setEnabled(true);
183+
}
179184
}
180185
}

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,9 @@ public FileViewerPane(final FileChangeNotifier fcn) {
5555
if (tabComp != null) {
5656
assert(tabComp instanceof Viewer);
5757
Viewer viewer = (Viewer) tabComp;
58-
JDA.viewer.setIcon(true);
58+
JDA.setBusy(true);
5959
viewer.refresh(refreshClass);
60-
JDA.viewer.setIcon(false);
60+
JDA.setBusy(false);
6161
}
6262
})).start());
6363

@@ -136,8 +136,13 @@ public Viewer getCurrentViewer() {
136136
return (Viewer) tabs.getSelectedComponent();
137137
}
138138

139-
public Viewer[] getLoadedViewers() {
140-
return (Viewer[]) tabs.getComponents();
139+
public List<Viewer> getLoadedViewers() {
140+
ArrayList<Viewer> result = new ArrayList<>();
141+
for (Component c : tabs.getComponents()) {
142+
if (c instanceof Viewer)
143+
result.add((Viewer) c);
144+
}
145+
return result;
141146
}
142147

143148
/**

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import club.bytecode.the.jda.decompilers.JDADecompiler;
66
import club.bytecode.the.jda.decompilers.bytecode.BytecodeDecompiler;
77
import club.bytecode.the.jda.settings.Settings;
8+
import com.strobel.annotations.Nullable;
89
import org.fife.ui.rsyntaxtextarea.RSyntaxTextArea;
910
import org.fife.ui.rsyntaxtextarea.SyntaxConstants;
1011
import org.fife.ui.rtextarea.RTextScrollPane;
@@ -23,14 +24,15 @@ public class PaneUpdaterThread extends Thread {
2324
private int paneId;
2425
private JPanel target;
2526
private ClassViewer viewer;
26-
private JButton button; // this needs to be refactored into something event-based, not a stupid hack like this!
27+
@Nullable private JButton button; // this needs to be refactored into something event-based, not a stupid hack like this!
2728

28-
public PaneUpdaterThread(ClassViewer viewer, JDADecompiler decompiler, int paneId, JPanel target, JButton button) {
29+
public PaneUpdaterThread(ClassViewer viewer, JDADecompiler decompiler, int paneId, JPanel target, @Nullable JButton button) {
2930
this.decompiler = decompiler;
3031
this.paneId = paneId;
3132
this.target = target;
3233
this.viewer = viewer;
3334
this.button = button;
35+
JDA.setBusy(true);
3436
}
3537

3638
public void run() {
@@ -58,7 +60,7 @@ public void run() {
5860
new ExceptionUI(e);
5961
} finally {
6062
viewer.resetDivider();
61-
JDA.viewer.setIcon(false);
63+
JDA.setBusy(false);
6264
if (button != null)
6365
button.setEnabled(true);
6466
}
@@ -70,14 +72,14 @@ private String stripUndisplayableChars(String s) {
7072
while (idx < s.length()) {
7173
char c = s.charAt(idx);
7274
if (isUndisplayable(c)) {
73-
result.append(s.substring(startIdx, idx));
75+
result.append(s, startIdx, idx);
7476
result.append("\\u").append(Integer.toHexString(c));
7577
startIdx = idx + 1;
7678
}
7779
idx++;
7880
}
7981
if (idx > startIdx)
80-
result.append(s.substring(startIdx, idx));
82+
result.append(s, startIdx, idx);
8183
return result.toString();
8284
}
8385

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

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

33
import club.bytecode.the.jda.settings.Settings;
4+
import com.strobel.annotations.Nullable;
45

56
import javax.swing.*;
67

@@ -25,5 +26,5 @@ public void updateName() {
2526
this.setName(getFile().name + (Settings.SHOW_CONTAINER_NAME.getBool() ? "(" + getFile().container + ")" : ""));
2627
}
2728

28-
public abstract void refresh(JButton button);
29+
public abstract void refresh(@Nullable JButton button);
2930
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,12 @@ public void openPath(TreePath path) {
448448
openClassFileToWorkSpace(file, cn);
449449
}
450450
} else {
451-
openFileToWorkSpace(file, JDA.getFileBytes(file));
451+
byte[] fileContents = JDA.getFileBytes(file);
452+
if (fileContents != null) { // if it's null, it's a directory or some non-leaf tree node
453+
openFileToWorkSpace(file, fileContents);
454+
} else {
455+
tree.expandPath(path);
456+
}
452457
}
453458
}
454459

0 commit comments

Comments
 (0)