Skip to content

Commit

Permalink
Start working on saving/loading panel state
Browse files Browse the repository at this point in the history
  • Loading branch information
2xsaiko committed Jul 9, 2021
1 parent a043d09 commit 993948a
Show file tree
Hide file tree
Showing 18 changed files with 230 additions and 105 deletions.
41 changes: 11 additions & 30 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
import cuchaz.enigma.gui.dialog.SearchDialog;
import cuchaz.enigma.gui.elements.*;
import cuchaz.enigma.gui.elements.rpanel.RPanel;
import cuchaz.enigma.gui.elements.rpanel.WorkspaceRPanelContainer;
import cuchaz.enigma.gui.elements.rpanel.WorkspaceRPanelContainer.DockPosition;
import cuchaz.enigma.gui.panels.*;
import cuchaz.enigma.gui.renderer.MessageListCellRenderer;
import cuchaz.enigma.gui.util.GuiUtil;
Expand All @@ -67,8 +67,8 @@ public class Gui {
private final Set<EditableType> editableTypes;
private boolean singleClassTree;

private final RPanel messagePanel = new RPanel();
private final RPanel userPanel = new RPanel();
private final RPanel messagePanel = new RPanel("Messages");
private final RPanel userPanel = new RPanel("Users");

private final MenuBar menuBar;
private final ObfPanel obfPanel;
Expand Down Expand Up @@ -158,33 +158,14 @@ public void actionPerformed(ActionEvent e) {
messagePanel.getContentPane().add(messageScrollPane, BorderLayout.CENTER);
messagePanel.getContentPane().add(chatPanel, BorderLayout.SOUTH);

// restore state
int[] layout = UiConfig.getLayout();
if (layout.length >= 4) {
// this.splitClasses.setDividerLocation(layout[0]);
// this.splitCenter.setDividerLocation(layout[1]);
// this.splitRight.setDividerLocation(layout[2]);
// this.logSplit.setDividerLocation(layout[3]);
}

WorkspaceRPanelContainer workspace = this.mainWindow.workspace();
workspace.getRightTop().attach(structurePanel.getPanel());
workspace.getRightTop().attach(inheritanceTree.getPanel());
workspace.getRightTop().attach(implementationsTree.getPanel());
workspace.getRightTop().attach(callsTree.getPanel());
workspace.getLeftTop().attach(obfPanel.getPanel());
workspace.getLeftBottom().attach(deobfPanel.getPanel());
workspace.getRightTop().attach(messagePanel);
workspace.getRightBottom().attach(userPanel);

workspace.addDragTarget(structurePanel.getPanel());
workspace.addDragTarget(inheritanceTree.getPanel());
workspace.addDragTarget(implementationsTree.getPanel());
workspace.addDragTarget(callsTree.getPanel());
workspace.addDragTarget(obfPanel.getPanel());
workspace.addDragTarget(deobfPanel.getPanel());
workspace.addDragTarget(messagePanel);
workspace.addDragTarget(userPanel);
this.mainWindow.addPanel(DockPosition.RIGHT_TOP, structurePanel.getPanel());
this.mainWindow.addPanel(DockPosition.RIGHT_TOP, inheritanceTree.getPanel());
this.mainWindow.addPanel(DockPosition.RIGHT_TOP, implementationsTree.getPanel());
this.mainWindow.addPanel(DockPosition.RIGHT_TOP, callsTree.getPanel());
this.mainWindow.addPanel(DockPosition.LEFT_TOP, obfPanel.getPanel());
this.mainWindow.addPanel(DockPosition.LEFT_BOTTOM, deobfPanel.getPanel());
this.mainWindow.addPanel(DockPosition.RIGHT_TOP, messagePanel);
this.mainWindow.addPanel(DockPosition.RIGHT_BOTTOM, userPanel);

this.mainWindow.statusBar().addPermanentComponent(this.connectionStatusLabel);

Expand Down
21 changes: 0 additions & 21 deletions enigma-swing/src/main/java/cuchaz/enigma/gui/config/UiConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -69,27 +69,6 @@ public static void setScaleFactor(float scale) {
swing.data().section("General").setDouble("Scale Factor", scale);
}

/**
* Gets the dimensions of the different panels of the GUI.
* <p>These dimensions are used to determine the location of the separators between these panels.</p>
*
* <ul>
* <li>[0] - The height of the obfuscated classes panel</li>
* <li>[1] - The width of the classes panel</li>
* <li>[2] - The width of the center panel</li>
* <li>[3] - The height of the tabs panel. Only used if the logs panel should appear</li>
* </ul>
*
* @return an integer array composed of these 4 dimensions
*/
public static int[] getLayout() {
return swing.data().section("Main Window").getIntArray("Layout").orElseGet(() -> new int[] { -1, -1, -1, -1 });
}

public static void setLayout(int leftV, int left, int right, int rightH) {
swing.data().section("Main Window").setIntArray("Layout", new int[] { leftV, left, right, rightH });
}

public static LookAndFeel getLookAndFeel() {
return swing.data().section("Themes").setIfAbsentEnum(LookAndFeel::valueOf, "Current", LookAndFeel.NONE);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
import cuchaz.enigma.translation.representation.entry.Entry;

public abstract class AbstractInheritanceTree {
private final RPanel panel = new RPanel();
private final RPanel panel;

private final JTree tree = new JTree();

protected final Gui gui;

public AbstractInheritanceTree(Gui gui, TreeCellRenderer cellRenderer) {
public AbstractInheritanceTree(String panelId, Gui gui, TreeCellRenderer cellRenderer) {
this.panel = new RPanel(panelId);
this.gui = gui;

this.tree.setModel(null);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import cuchaz.enigma.utils.I18n;

public class CallsTree {
private final RPanel panel = new RPanel();
private final RPanel panel = new RPanel("Calls");

private final JTree callsTree = new JTree();
private final JList<Token> tokens = new JList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class ImplementationsTree extends AbstractInheritanceTree {
public ImplementationsTree(Gui gui) {
super(gui, new ImplementationsTreeCellRenderer(gui));
super("Implementations", gui, new ImplementationsTreeCellRenderer(gui));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

public class InheritanceTree extends AbstractInheritanceTree {
public InheritanceTree(Gui gui) {
super(gui, new InheritanceTreeCellRenderer(gui));
super("Inheritance", gui, new InheritanceTreeCellRenderer(gui));
}

@Nullable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,15 @@
import javax.swing.JMenuBar;

import cuchaz.enigma.config.ConfigSection;
import cuchaz.enigma.gui.elements.rpanel.RPanel;
import cuchaz.enigma.gui.elements.rpanel.RPanelGroup;
import cuchaz.enigma.gui.elements.rpanel.WorkspaceRPanelContainer;
import cuchaz.enigma.gui.elements.rpanel.WorkspaceRPanelContainer.DockPosition;

public class MainWindow {
private final JFrame frame;
private final WorkspaceRPanelContainer workspace = new WorkspaceRPanelContainer();
private final RPanelGroup panelGroup = new RPanelGroup();

private final JMenuBar menuBar = new JMenuBar();
private final StatusBar statusBar = new StatusBar();
Expand All @@ -24,6 +28,8 @@ public MainWindow(String title) {
contentPane.setLayout(new BorderLayout());
contentPane.add(this.workspace.getUi(), BorderLayout.CENTER);
contentPane.add(this.statusBar.getUi(), BorderLayout.SOUTH);

this.workspace.addToGroup(this.panelGroup);
}

public void setVisible(boolean visible) {
Expand All @@ -45,6 +51,8 @@ public void saveState(ConfigSection section) {
section.setInt("Y %d".formatted(screenSize.height), location.y);
section.setInt("Width %d".formatted(screenSize.width), dim.width);
section.setInt("Height %d".formatted(screenSize.height), dim.height);

this.panelGroup.saveState(section.section("Dock Panels"));
}

public void restoreState(ConfigSection section) {
Expand All @@ -68,12 +76,19 @@ public void restoreState(ConfigSection section) {
this.frame.setLocation(ix, iy);
}
}

this.panelGroup.restoreState(section.section("Dock Panels"));
}

public WorkspaceRPanelContainer workspace() {
return this.workspace;
}

public void addPanel(DockPosition dp, RPanel panel) {
this.panelGroup.addPanel(panel);
this.workspace.get(dp).attach(panel);
}

public JMenuBar menuBar() {
return this.menuBar;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ public RPanelContainer getInner() {
return inner;
}

public void addDragTarget(RPanel panel) {
panel.addDragTarget(this.inner);
public void addToGroup(RPanelGroup group) {
group.addHost(this.inner);
}

public static void initListenerForButtonBar(JComponent ui, RPanelHost panelHost, Consumer<JToggleButton> addCallback, Consumer<JToggleButton> removeCallback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ public RPanelHost getRight() {
return right;
}

public void addDragTarget(RPanel panel) {
panel.addDragTarget(this.left);
panel.addDragTarget(this.right);
public void addToGroup(RPanelGroup group) {
group.addHost(this.left);
group.addHost(this.right);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,26 @@

import java.awt.Container;
import java.awt.Rectangle;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import javax.annotation.Nullable;
import javax.swing.JPanel;

public class RPanel {

private final String id;
private Container contentPane = new JPanel();
private String title;

private RPanelHost host;
private final Set<RPanelHost> dndContainers = new HashSet<>();
private RPanelGroup group;

private boolean visible = true;

public RPanel() {
this("");
this(null);
}

public RPanel(String title) {
this.title = title;
public RPanel(String id) {
this.id = id;
}

public Container getContentPane() {
Expand Down Expand Up @@ -80,7 +78,7 @@ public void attach(RPanelHost host) {
if (currentPos != null) host.tryMoveTo(this, currentPos);
}

public void setOwner(RPanelHost host) {
public void setOwner(@Nullable RPanelHost host) {
if (host != null && !host.owns(this)) throw new IllegalStateException(String.format("Received request to set owner of panel to %s, but that says it doesn't own this panel!", host));

if (this.host != null) {
Expand All @@ -90,6 +88,23 @@ public void setOwner(RPanelHost host) {
this.host = host;
}

@Nullable
public RPanelHost getHost() {
return this.host;
}

public void setGroup(@Nullable RPanelGroup group) {
if (group != null && !group.contains(this)) {
throw new IllegalStateException("Received request to set group of panel to %s, but that says it doesn't contain this panel!".formatted(group));
}

if (this.group != null) {
this.group.removePanel(this);
}

this.group = group;
}

public void setVisible(boolean visible) {
if (this.visible != visible) {
this.visible = visible;
Expand All @@ -115,16 +130,12 @@ public String getTitle() {
return title;
}

public void addDragTarget(RPanelHost host) {
dndContainers.add(host);
@Nullable
public String getId() {
return this.id;
}

public void removeDragTarget(RPanelHost host) {
dndContainers.remove(host);
public RPanelGroup getGroup() {
return this.group;
}

public Set<RPanelHost> getDragTargets() {
return Collections.unmodifiableSet(dndContainers);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,20 @@

public class RPanelContainer implements RPanelHost {

private final JPanel ui;
private final JPanel ui = new JPanel(new BorderLayout());
private final String id;

private final Map<RPanel, StandaloneRootPane> panels = new HashMap<>();
private RPanel openPanel = null;

private List<RPanelListener> listeners = new ArrayList<>();
private final List<RPanelListener> listeners = new ArrayList<>();

public RPanelContainer() {
ui = new JPanel();
ui.setLayout(new BorderLayout());
this(null);
}

public RPanelContainer(String id) {
this.id = id;
}

public JPanel getUi() {
Expand Down Expand Up @@ -164,7 +168,13 @@ public Rectangle getPanelLocation(RPanel panel) {
if (!owns(panel)) return null;

Rectangle bounds = this.ui.getBounds();
bounds.setLocation(this.ui.getLocationOnScreen());

if (this.ui.isShowing()) {
bounds.setLocation(this.ui.getLocationOnScreen());
} else {
// TODO fix panel location calculation
}

return bounds;
}

Expand All @@ -191,4 +201,9 @@ public boolean isDedicatedHost() {
return false;
}

@Nullable
@Override
public String getId() {
return this.id;
}
}
Loading

0 comments on commit 993948a

Please sign in to comment.