diff options
| author | 2020-07-18 14:51:09 +0200 | |
|---|---|---|
| committer | 2020-07-18 14:51:09 +0200 | |
| commit | a562f7ebc0065da229c39153b90ffa6fa739f31b (patch) | |
| tree | 29555aafaf0c22e66dbafec488f9acbff69366f3 /enigma-swing/src | |
| parent | Make GridBagConstraintsBuilder.insets respect scale (diff) | |
| download | enigma-fork-a562f7ebc0065da229c39153b90ffa6fa739f31b.tar.gz enigma-fork-a562f7ebc0065da229c39153b90ffa6fa739f31b.tar.xz enigma-fork-a562f7ebc0065da229c39153b90ffa6fa739f31b.zip | |
Improve layout for ProgressDialog
Diffstat (limited to 'enigma-swing/src')
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java | 63 | ||||
| -rw-r--r-- | enigma-swing/src/main/java/cuchaz/enigma/gui/util/GridBagConstraintsBuilder.java | 19 |
2 files changed, 54 insertions, 28 deletions
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java index 4f47a24..f97c1c2 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/dialog/ProgressDialog.java | |||
| @@ -11,56 +11,63 @@ | |||
| 11 | 11 | ||
| 12 | package cuchaz.enigma.gui.dialog; | 12 | package cuchaz.enigma.gui.dialog; |
| 13 | 13 | ||
| 14 | import java.awt.BorderLayout; | ||
| 15 | import java.awt.Container; | 14 | import java.awt.Container; |
| 16 | import java.awt.FlowLayout; | 15 | import java.awt.Dimension; |
| 16 | import java.awt.GridBagConstraints; | ||
| 17 | import java.awt.GridBagLayout; | ||
| 17 | import java.util.concurrent.CompletableFuture; | 18 | import java.util.concurrent.CompletableFuture; |
| 18 | 19 | ||
| 19 | import javax.swing.*; | 20 | import javax.swing.*; |
| 20 | 21 | ||
| 21 | import cuchaz.enigma.Enigma; | 22 | import cuchaz.enigma.Enigma; |
| 22 | import cuchaz.enigma.ProgressListener; | 23 | import cuchaz.enigma.ProgressListener; |
| 24 | import cuchaz.enigma.gui.util.GridBagConstraintsBuilder; | ||
| 23 | import cuchaz.enigma.gui.util.GuiUtil; | 25 | import cuchaz.enigma.gui.util.GuiUtil; |
| 24 | import cuchaz.enigma.gui.util.ScaleUtil; | 26 | import cuchaz.enigma.gui.util.ScaleUtil; |
| 25 | import cuchaz.enigma.utils.I18n; | 27 | import cuchaz.enigma.utils.I18n; |
| 26 | 28 | ||
| 27 | public class ProgressDialog implements ProgressListener, AutoCloseable { | 29 | public class ProgressDialog implements ProgressListener, AutoCloseable { |
| 28 | 30 | ||
| 29 | private JDialog dialog; | 31 | private final JDialog dialog; |
| 30 | private JLabel labelTitle; | 32 | private final JLabel labelTitle = new JLabel(); |
| 31 | private JLabel labelText; | 33 | private final JLabel labelText = GuiUtil.unboldLabel(new JLabel()); |
| 32 | private JProgressBar progress; | 34 | private final JProgressBar progress = new JProgressBar(); |
| 33 | 35 | ||
| 34 | public ProgressDialog(JFrame parent) { | 36 | public ProgressDialog(JFrame parent) { |
| 35 | |||
| 36 | // init frame | 37 | // init frame |
| 37 | this.dialog = new JDialog(parent, String.format(I18n.translate("progress.operation"), Enigma.NAME)); | 38 | this.dialog = new JDialog(parent, String.format(I18n.translate("progress.operation"), Enigma.NAME)); |
| 38 | final Container pane = this.dialog.getContentPane(); | 39 | Container pane = this.dialog.getContentPane(); |
| 39 | FlowLayout layout = new FlowLayout(); | 40 | pane.setLayout(new GridBagLayout()); |
| 40 | layout.setAlignment(FlowLayout.LEFT); | 41 | |
| 41 | pane.setLayout(layout); | 42 | GridBagConstraintsBuilder cb = GridBagConstraintsBuilder.create() |
| 42 | 43 | .insets(2) | |
| 43 | this.labelTitle = new JLabel(); | 44 | .anchor(GridBagConstraints.WEST) |
| 44 | pane.add(this.labelTitle); | 45 | .fill(GridBagConstraints.BOTH) |
| 45 | 46 | .weight(1.0, 0.0); | |
| 46 | // set up the progress bar | 47 | |
| 47 | JPanel panel = new JPanel(); | 48 | // Set label text since otherwise the label height is 0, which makes the |
| 48 | pane.add(panel); | 49 | // window size get set incorrectly |
| 49 | panel.setLayout(new BorderLayout()); | 50 | this.labelTitle.setText("Idle"); |
| 50 | this.labelText = GuiUtil.unboldLabel(new JLabel()); | 51 | this.labelText.setText("Idle"); |
| 51 | this.progress = new JProgressBar(); | 52 | this.progress.setPreferredSize(ScaleUtil.getDimension(0, 30)); |
| 52 | this.labelText.setBorder(BorderFactory.createEmptyBorder(0, 0, 10, 0)); | 53 | |
| 53 | panel.add(this.labelText, BorderLayout.NORTH); | 54 | pane.add(this.labelTitle, cb.pos(0, 0).build()); |
| 54 | panel.add(this.progress, BorderLayout.CENTER); | 55 | pane.add(this.labelText, cb.pos(0, 1).build()); |
| 55 | panel.setPreferredSize(ScaleUtil.getDimension(360, 50)); | 56 | // set padding because otherwise the progress bar gets cut off when |
| 57 | // using the Darkula theme | ||
| 58 | pane.add(this.progress, cb.pos(0, 2).weight(1.0, 1.0).padding(10).build()); | ||
| 56 | 59 | ||
| 57 | // show the frame | 60 | // show the frame |
| 58 | pane.doLayout(); | 61 | this.dialog.pack(); |
| 59 | this.dialog.setSize(ScaleUtil.getDimension(400, 120)); | 62 | Dimension size = this.dialog.getSize(); |
| 63 | this.dialog.setMinimumSize(size); | ||
| 64 | size.width = ScaleUtil.scale(400); | ||
| 65 | this.dialog.setSize(size); | ||
| 66 | |||
| 60 | this.dialog.setResizable(false); | 67 | this.dialog.setResizable(false); |
| 61 | this.dialog.setLocationRelativeTo(parent); | 68 | this.dialog.setLocationRelativeTo(parent); |
| 62 | this.dialog.setVisible(true); | ||
| 63 | this.dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); | 69 | this.dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE); |
| 70 | this.dialog.setVisible(true); | ||
| 64 | } | 71 | } |
| 65 | 72 | ||
| 66 | public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) { | 73 | public static CompletableFuture<Void> runOffThread(final JFrame parent, final ProgressRunnable runnable) { |
diff --git a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GridBagConstraintsBuilder.java b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GridBagConstraintsBuilder.java index 65b0f33..6a75686 100644 --- a/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GridBagConstraintsBuilder.java +++ b/enigma-swing/src/main/java/cuchaz/enigma/gui/util/GridBagConstraintsBuilder.java | |||
| @@ -109,6 +109,25 @@ public final class GridBagConstraintsBuilder { | |||
| 109 | return copy; | 109 | return copy; |
| 110 | } | 110 | } |
| 111 | 111 | ||
| 112 | public GridBagConstraintsBuilder padding(int pad) { | ||
| 113 | return this.paddingUnscaled(ScaleUtil.scale(pad)); | ||
| 114 | } | ||
| 115 | |||
| 116 | public GridBagConstraintsBuilder padding(int padX, int padY) { | ||
| 117 | return this.paddingUnscaled(ScaleUtil.scale(padX), ScaleUtil.scale(padY)); | ||
| 118 | } | ||
| 119 | |||
| 120 | public GridBagConstraintsBuilder paddingUnscaled(int pad) { | ||
| 121 | return this.paddingUnscaled(pad, pad); | ||
| 122 | } | ||
| 123 | |||
| 124 | public GridBagConstraintsBuilder paddingUnscaled(int padX, int padY) { | ||
| 125 | GridBagConstraintsBuilder copy = this.copy(); | ||
| 126 | copy.inner.ipadx = padX; | ||
| 127 | copy.inner.ipady = padY; | ||
| 128 | return copy; | ||
| 129 | } | ||
| 130 | |||
| 112 | public GridBagConstraintsBuilder copy() { | 131 | public GridBagConstraintsBuilder copy() { |
| 113 | GridBagConstraints c = (GridBagConstraints) this.inner.clone(); | 132 | GridBagConstraints c = (GridBagConstraints) this.inner.clone(); |
| 114 | return new GridBagConstraintsBuilder(c); | 133 | return new GridBagConstraintsBuilder(c); |