Skip to content

Commit

Permalink
Merge pull request #5102 from bubblobill/TokenLayout
Browse files Browse the repository at this point in the history
Token layout: GenericDialog improvements
  • Loading branch information
cwisniew authored Dec 11, 2024
2 parents f8d0672 + 2970e71 commit ab1ebe3
Showing 1 changed file with 115 additions and 16 deletions.
131 changes: 115 additions & 16 deletions src/main/java/net/rptools/maptool/client/swing/GenericDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,15 @@
*/
package net.rptools.maptool.client.swing;

import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.AbstractAction;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import static com.formdev.flatlaf.FlatClientProperties.*;

import com.formdev.flatlaf.ui.FlatUIUtils;
import com.formdev.flatlaf.util.UIScale;
import java.awt.*;
import java.awt.event.*;
import java.util.Arrays;
import javax.accessibility.AccessibleContext;
import javax.swing.*;

public class GenericDialog extends JDialog {
private static final long serialVersionUID = 6739665491287916519L;
Expand All @@ -37,19 +35,48 @@ public GenericDialog(String title, Frame parent, JPanel panel) {

public GenericDialog(String title, Frame parent, JPanel panel, boolean modal) {
super(parent, title, modal);
setResizable(true);

setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);

this.panel = panel;
setLayout(new GridLayout());
JScrollPane scrollPane = new JScrollPane(this.panel);
add(scrollPane);

add(this.panel);
addWindowListener(
new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
closeDialog();
}
});
addComponentListener(
new ComponentAdapter() {
private void placeButtons() {
if (getSize().width == 0) {
return;
}
if (maximiseBtn == null || restoreBtn == null) {
addResizeButtons();
}
positionResizeButtons();
}

@Override
public void componentResized(ComponentEvent e) {
super.componentResized(e);
placeButtons();
}

@Override
public void componentShown(ComponentEvent e) {
super.componentShown(e);
dBounds = getBounds();
placeButtons();
}
});

// ESCAPE cancels the window without committing
this.panel
.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW)
Expand All @@ -70,10 +97,6 @@ public void closeDialog() {
dispose();
}

protected void positionInitialView() {
SwingUtil.centerOver(this, getOwner());
}

public void showDialog() {
// We want to center over our parent, but only the first time.
// If this dialog is reused, we want it to show up where it was last.
Expand All @@ -84,4 +107,80 @@ public void showDialog() {
}
setVisible(true);
}

protected void positionInitialView() {
SwingUtil.centerOver(this, getOwner());
}

// Resize Button code
JButton maximiseBtn, restoreBtn;
JLayeredPane layeredPane = getLayeredPane();
Dimension buttonSize = FlatUIUtils.getSubUIDimension("TitlePane.buttonSize", null);
Window w = SwingUtilities.getWindowAncestor(SwingUtilities.getRoot(layeredPane));
String defaultWindowStyle =
(w != null && w.getType() == Window.Type.UTILITY) ? WINDOW_STYLE_SMALL : null;
String windowStyle =
clientProperty(layeredPane.getRootPane(), WINDOW_STYLE, defaultWindowStyle, String.class);
int buttonMinimumWidth = FlatUIUtils.getSubUIInt("TitlePane.buttonMinimumWidth", windowStyle, 30);
Rectangle dBounds = new Rectangle();
Rectangle screenBounds =
GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
ActionListener resizeListener =
e -> {
System.out.println(e);
if (e.getActionCommand().equals("maximise")) {
setBounds(screenBounds);
swapResizeButtons();
} else if (e.getActionCommand().equals("restore")) {
setBounds(dBounds);
swapResizeButtons();
}
};

private void addResizeButtons() {
maximiseBtn = createButtons("TitlePane.maximizeIcon", "maximise");
restoreBtn = createButtons("TitlePane.restoreIcon", "restore");
layeredPane.add(maximiseBtn, Integer.valueOf(900), 1);
}

protected JButton createButtons(String iconKey, String accessibleName) {
JButton button =
new JButton(FlatUIUtils.getSubUIIcon(iconKey, windowStyle)) {
@Override
public Dimension getMinimumSize() {
// allow the button to shrink if space is rare
return new Dimension(UIScale.scale(buttonMinimumWidth), super.getMinimumSize().height);
}
};
button.setSize(UIScale.scale(buttonSize.width), UIScale.scale(buttonSize.height));
button.setFocusable(false);
button.setContentAreaFilled(false);
button.setBorder(BorderFactory.createEmptyBorder());
button.putClientProperty(AccessibleContext.ACCESSIBLE_NAME_PROPERTY, accessibleName);
button.setActionCommand(accessibleName);
button.addActionListener(resizeListener);
return button;
}

private void swapResizeButtons() {
if (Arrays.stream(layeredPane.getComponents()).toList().contains(maximiseBtn)) {
layeredPane.remove(maximiseBtn);
layeredPane.add(restoreBtn, Integer.valueOf(900), 1);
} else {
layeredPane.remove(restoreBtn);
layeredPane.add(maximiseBtn, Integer.valueOf(900), 1);
}
positionResizeButtons();
Rectangle currentBounds = layeredPane.getBounds();
RepaintManager.currentManager(layeredPane.getRootPane())
.addDirtyRegion(
this, currentBounds.x, currentBounds.y, currentBounds.width, buttonSize.height + 2);
}

private void positionResizeButtons() {
int x = layeredPane.getBounds().width - maximiseBtn.getWidth() * 2;
int y = 0;
maximiseBtn.setLocation(x, y);
restoreBtn.setLocation(x, y);
}
}

0 comments on commit ab1ebe3

Please sign in to comment.