Skip to content

Commit 90ab588

Browse files
committed
Refactor window persistence into interface
so enterprise
1 parent 22c1b43 commit 90ab588

File tree

6 files changed

+173
-70
lines changed

6 files changed

+173
-70
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,7 @@ public void openFile(String name, String container, byte[] contents)
268268
public static Dimension defaultDimension = new Dimension(200, -1);
269269
public static Point defaultPosition = new Point(0, 0);
270270
@Override
271-
public Dimension getDefaultDimensions()
271+
public Dimension getDefaultSize()
272272
{
273273
return defaultDimension;
274274
}

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

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import the.bytecode.club.jda.decompilers.ProcyonDecompiler;
1111
import the.bytecode.club.jda.decompilers.bytecode.ClassNodeDecompiler;
1212
import the.bytecode.club.jda.settings.DecompilerSettings;
13+
import the.bytecode.club.jda.settings.IPersistentWindow;
1314
import the.bytecode.club.jda.settings.Settings;
1415

1516
import javax.swing.*;
@@ -25,7 +26,7 @@
2526
*
2627
* @author Konloch
2728
*/
28-
public class MainViewerGUI extends JFrame implements FileChangeNotifier
29+
public class MainViewerGUI extends JFrame implements FileChangeNotifier, IPersistentWindow
2930
{
3031
public void setOptionalLibrary()
3132
{
@@ -405,7 +406,7 @@ public void resetWindows()
405406

406407
for (VisibleComponent f : rfComps)
407408
{
408-
Dimension size = f.getDefaultDimensions();
409+
Dimension size = f.getDefaultSize();
409410
if (size.width < 0 || size.height < 0)
410411
size = new Dimension(
411412
size.width < 0 ? clientSize.width + size.width : size.width,
@@ -414,6 +415,7 @@ public void resetWindows()
414415
f.pack();
415416
Point pos = f.getDefaultPosition();
416417
f.setLocation(pos);
418+
f.setVisible(true);
417419
desktop.getDesktopManager().resizeFrame(f, pos.x, pos.y, size.width, size.height);
418420
}
419421
}
@@ -1014,4 +1016,59 @@ private void exitPrompt()
10141016
System.exit(0);
10151017
}
10161018
}
1019+
1020+
@Override
1021+
public String getWindowId()
1022+
{
1023+
return "JDA";
1024+
}
1025+
1026+
@Override
1027+
public int getState()
1028+
{
1029+
return getExtendedState();
1030+
}
1031+
1032+
@Override
1033+
public void restoreState(int state)
1034+
{
1035+
setExtendedState(state);
1036+
}
1037+
1038+
@Override
1039+
public Point getPersistentPosition()
1040+
{
1041+
return unmaximizedPos;
1042+
}
1043+
1044+
@Override
1045+
public void restorePosition(Point pos)
1046+
{
1047+
unmaximizedPos = pos;
1048+
if (isNormalState())
1049+
setLocation(pos);
1050+
}
1051+
1052+
@Override
1053+
public Dimension getPersistentSize()
1054+
{
1055+
return unmaximizedSize;
1056+
}
1057+
1058+
@Override
1059+
public void restoreSize(Dimension size)
1060+
{
1061+
unmaximizedSize = size;
1062+
if (isNormalState())
1063+
{
1064+
setPreferredSize(size);
1065+
pack();
1066+
}
1067+
}
1068+
1069+
@Override
1070+
public boolean isNormalState()
1071+
{
1072+
return (getExtendedState() & MAXIMIZED_BOTH) != MAXIMIZED_BOTH && (getExtendedState() & ICONIFIED) != ICONIFIED;
1073+
}
10171074
}

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

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,13 @@
22

33
import org.objectweb.asm.tree.ClassNode;
44
import the.bytecode.club.jda.FileChangeNotifier;
5+
import the.bytecode.club.jda.settings.IPersistentWindow;
56

67
import javax.swing.*;
78
import java.awt.*;
89
import java.awt.event.ComponentAdapter;
910
import java.awt.event.ComponentEvent;
11+
import java.beans.PropertyVetoException;
1012

1113
/**
1214
* Used to represent all the panes inside of Bytecode Viewer, this is temp code
@@ -16,7 +18,7 @@
1618
* @author WaterWolf
1719
*/
1820

19-
public abstract class VisibleComponent extends JInternalFrame implements FileChangeNotifier
21+
public abstract class VisibleComponent extends JInternalFrame implements FileChangeNotifier, IPersistentWindow
2022
{
2123
private String windowId;
2224

@@ -29,8 +31,8 @@ public VisibleComponent(final String id, final String title, final Icon icon)
2931
windowId = id;
3032
setFrameIcon(icon);
3133

32-
unmaximizedPos = getLocation();
33-
unmaximizedSize = getSize();
34+
unmaximizedPos = getDefaultPosition();
35+
unmaximizedSize = getDefaultSize();
3436

3537
addComponentListener(new ComponentAdapter()
3638
{
@@ -52,7 +54,7 @@ public void componentMoved(ComponentEvent e)
5254
});
5355

5456
addPropertyChangeListener(evt -> {
55-
if (!isMaximum() && !isIcon())
57+
if (isNormalState())
5658
{
5759
setSize(unmaximizedSize);
5860
setLocation(unmaximizedPos);
@@ -69,11 +71,82 @@ public void componentMoved(ComponentEvent e)
6971
protected static Dimension defaultDimensions;
7072
protected static Point defaultPosition;
7173

72-
public abstract Dimension getDefaultDimensions();
74+
public abstract Dimension getDefaultSize();
7375
public abstract Point getDefaultPosition();
7476

77+
@Override
7578
public String getWindowId()
7679
{
7780
return windowId;
7881
}
82+
83+
private static int
84+
MAXIMIZED = 1 << 0,
85+
MINIMIZED = 1 << 1,
86+
VISIBLE = 1 << 2;
87+
88+
@Override
89+
public int getState()
90+
{
91+
int state = 0;
92+
if (isMaximum())
93+
state |= MAXIMIZED;
94+
if (isIcon())
95+
state |= MINIMIZED;
96+
if (isVisible())
97+
state |= VISIBLE;
98+
return state;
99+
}
100+
101+
@Override
102+
public void restoreState(int state)
103+
{
104+
try
105+
{
106+
setMaximum((state & MAXIMIZED) != 0);
107+
setIcon((state & MINIMIZED) != 0);
108+
setVisible((state & VISIBLE) != 0);
109+
}
110+
catch (PropertyVetoException e)
111+
{
112+
e.printStackTrace();
113+
}
114+
}
115+
116+
@Override
117+
public Point getPersistentPosition()
118+
{
119+
return unmaximizedPos;
120+
}
121+
122+
@Override
123+
public void restorePosition(Point pos)
124+
{
125+
unmaximizedPos = pos;
126+
if (isNormalState())
127+
setLocation(pos);
128+
}
129+
130+
@Override
131+
public Dimension getPersistentSize()
132+
{
133+
return unmaximizedSize;
134+
}
135+
136+
@Override
137+
public void restoreSize(Dimension size)
138+
{
139+
unmaximizedSize = size;
140+
if (isNormalState())
141+
{
142+
setPreferredSize(size);
143+
pack();
144+
}
145+
}
146+
147+
@Override
148+
public boolean isNormalState()
149+
{
150+
return !isMaximum() && !isIcon();
151+
}
79152
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public void componentRemoved(final ContainerEvent e)
9090
public static Dimension defaultDimension = new Dimension(-FileNavigationPane.defaultDimension.width, -1);
9191
public static Point defaultPosition = new Point(FileNavigationPane.defaultDimension.width, 0);
9292
@Override
93-
public Dimension getDefaultDimensions()
93+
public Dimension getDefaultSize()
9494
{
9595
return defaultDimension;
9696
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package the.bytecode.club.jda.settings;
2+
3+
import java.awt.*;
4+
5+
public interface IPersistentWindow
6+
{
7+
int getState();
8+
void restoreState(int state);
9+
10+
Point getPersistentPosition();
11+
void restorePosition(Point pos);
12+
13+
Dimension getPersistentSize();
14+
void restoreSize(Dimension size);
15+
16+
boolean isNormalState(); // not maximized/minimized/etc
17+
18+
String getWindowId();
19+
}

src/main/java/the/bytecode/club/jda/settings/Settings.java

Lines changed: 15 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
import the.bytecode.club.jda.gui.MainViewerGUI;
77
import the.bytecode.club.jda.gui.VisibleComponent;
88

9-
import javax.swing.*;
109
import java.awt.*;
11-
import java.beans.PropertyVetoException;
1210
import java.io.FileOutputStream;
1311
import java.io.FileReader;
1412
import java.util.HashMap;
@@ -53,8 +51,8 @@ public static void saveGUI()
5351
settings.add("windows", new JsonObject());
5452
JsonObject windowsSection = settings.get("windows").asObject();
5553
for (VisibleComponent f : MainViewerGUI.rfComps)
56-
saveFrame(windowsSection, f, f.getWindowId());
57-
saveFrame(windowsSection, JDA.viewer, "JDA");
54+
saveFrame(windowsSection, f);
55+
saveFrame(windowsSection, JDA.viewer);
5856

5957
FileOutputStream out = new FileOutputStream(JDA.settingsFile);
6058
out.write(settings.toString().getBytes("UTF-8"));
@@ -66,32 +64,20 @@ public static void saveGUI()
6664
}
6765
}
6866

69-
public static void saveFrame(JsonObject windowsSection, Component f, String name)
67+
public static void saveFrame(JsonObject windowsSection, IPersistentWindow f)
7068
{
69+
String name = f.getWindowId();
7170
if (windowsSection.get(name) == null)
7271
windowsSection.add(name, new JsonObject());
7372

7473
JsonObject windowSettings = windowsSection.get(name).asObject();
75-
boolean jda = name.equals("JDA");
76-
Point pos = jda ? JDA.viewer.unmaximizedPos : ((VisibleComponent) f).unmaximizedPos;
77-
Dimension size = jda ? JDA.viewer.unmaximizedSize : ((VisibleComponent) f).unmaximizedSize;
74+
Point pos = f.getPersistentPosition();
75+
Dimension size = f.getPersistentSize();
7876
windowSettings.add("x", pos.x);
7977
windowSettings.add("y", pos.y);
8078
windowSettings.add("width", size.width);
8179
windowSettings.add("height", size.height);
82-
if (jda)
83-
windowSettings.add("state", ((JFrame) f).getExtendedState());
84-
else if (f instanceof VisibleComponent)
85-
{
86-
int state = 0;
87-
if (((VisibleComponent) f).isMaximum())
88-
state |= 1;
89-
if (((VisibleComponent) f).isIcon())
90-
state |= 2;
91-
if (((VisibleComponent) f).isVisible())
92-
state |= 4;
93-
windowSettings.add("state", state);
94-
}
80+
windowSettings.add("state", f.getState());
9581
}
9682

9783
public static void loadGUI()
@@ -130,10 +116,8 @@ public static void loadWindows()
130116
{
131117
JsonObject windowsSection = rootSettings.get("windows").asObject();
132118
for (VisibleComponent f : MainViewerGUI.rfComps)
133-
{
134-
loadFrame(windowsSection, f, f.getWindowId());
135-
}
136-
loadFrame(windowsSection, JDA.viewer, "JDA");
119+
loadFrame(windowsSection, f);
120+
loadFrame(windowsSection, JDA.viewer);
137121
}
138122
}
139123
catch (Exception e)
@@ -142,46 +126,16 @@ public static void loadWindows()
142126
}
143127
}
144128

145-
private static void loadFrame(JsonObject windowsSection, Component f, String name)
129+
private static void loadFrame(JsonObject windowsSection, IPersistentWindow f)
146130
{
147-
if (windowsSection.get(name) != null)
131+
if (windowsSection.get(f.getWindowId()) != null)
148132
{
149-
JsonObject settings = windowsSection.get(name).asObject();
150-
boolean jda = name.equals("JDA");
151-
133+
JsonObject settings = windowsSection.get(f.getWindowId()).asObject();
152134
Point pos = new Point(settings.get("x").asInt(), settings.get("y").asInt());
153135
Dimension size = new Dimension(settings.get("width").asInt(), settings.get("height").asInt());
154-
if (jda) // fuck this shit make it an interfaceeeeeeeeeee
155-
{
156-
JDA.viewer.setExtendedState(settings.get("state").asInt());
157-
JDA.viewer.unmaximizedPos = pos;
158-
JDA.viewer.unmaximizedSize = size;
159-
if ((JDA.viewer.getExtendedState() & JFrame.MAXIMIZED_BOTH) != JFrame.MAXIMIZED_BOTH)
160-
JDA.viewer.pack();
161-
}
162-
else if (f instanceof VisibleComponent)
163-
{
164-
VisibleComponent comp = (VisibleComponent) f;
165-
int state = settings.get("state").asInt();
166-
try
167-
{
168-
comp.setMaximum((state & 1) != 0);
169-
comp.setIcon((state & 2) != 0);
170-
comp.setVisible((state & 4) != 0);
171-
}
172-
catch (PropertyVetoException e)
173-
{
174-
e.printStackTrace();
175-
}
176-
comp.unmaximizedPos = pos;
177-
comp.unmaximizedSize = size;
178-
if (!comp.isMaximum() && !comp.isIcon())
179-
{
180-
comp.setLocation(pos);
181-
comp.setPreferredSize(size);
182-
comp.pack();
183-
}
184-
}
136+
f.restoreState(settings.get("state").asInt());
137+
f.restorePosition(pos);
138+
f.restoreSize(size);
185139
}
186140
}
187141
}

0 commit comments

Comments
 (0)