From a62a4041a1f876a9f3e1d8e5f64073e03a2b57c7 Mon Sep 17 00:00:00 2001 From: ShadelessFox Date: Sun, 17 Mar 2024 19:43:29 +0100 Subject: [PATCH] Model Viewer: Use spinners instead of text fields in the teleport dialog --- .../viewer/model/menu/TeleportToItem.java | 64 ++++++------------- 1 file changed, 19 insertions(+), 45 deletions(-) diff --git a/modules/decima-ext-model-exporter/src/main/java/com/shade/decima/ui/data/viewer/model/menu/TeleportToItem.java b/modules/decima-ext-model-exporter/src/main/java/com/shade/decima/ui/data/viewer/model/menu/TeleportToItem.java index d9386842f..abe602244 100644 --- a/modules/decima-ext-model-exporter/src/main/java/com/shade/decima/ui/data/viewer/model/menu/TeleportToItem.java +++ b/modules/decima-ext-model-exporter/src/main/java/com/shade/decima/ui/data/viewer/model/menu/TeleportToItem.java @@ -3,8 +3,6 @@ import com.shade.decima.model.viewer.Camera; import com.shade.decima.model.viewer.ModelViewport; import com.shade.platform.ui.controls.Mnemonic; -import com.shade.platform.ui.controls.validation.InputValidator; -import com.shade.platform.ui.controls.validation.Validation; import com.shade.platform.ui.dialogs.BaseDialog; import com.shade.platform.ui.dialogs.BaseEditDialog; import com.shade.platform.ui.menus.MenuItem; @@ -17,7 +15,6 @@ import org.joml.Vector3fc; import javax.swing.*; -import javax.swing.text.JTextComponent; import static com.shade.decima.ui.menu.MenuConstants.*; @@ -35,81 +32,58 @@ public void perform(@NotNull MenuItemContext ctx) { } private static class TeleportToDialog extends BaseEditDialog { - private final JTextField xField; - private final JTextField yField; - private final JTextField zField; + private final JSpinner xSpinner; + private final JSpinner ySpinner; + private final JSpinner zSpinner; public TeleportToDialog(@NotNull Vector3fc v) { super("Teleport To"); - this.xField = new JTextField(String.valueOf(v.x())); - this.yField = new JTextField(String.valueOf(v.y())); - this.zField = new JTextField(String.valueOf(v.z())); + this.xSpinner = new JSpinner(new SpinnerNumberModel(v.x(), null, null, 0.1f)); + this.ySpinner = new JSpinner(new SpinnerNumberModel(v.y(), null, null, 0.1f)); + this.zSpinner = new JSpinner(new SpinnerNumberModel(v.z(), null, null, 0.1f)); } @NotNull @Override protected JComponent createContentsPane() { final JPanel panel = new JPanel(); - panel.setLayout(new MigLayout("ins 0", "[][grow,fill][][grow,fill][][grow,fill]")); - - UIUtils.installInputValidator(xField, new DoubleValidator(xField), this); - UIUtils.installInputValidator(yField, new DoubleValidator(yField), this); - UIUtils.installInputValidator(zField, new DoubleValidator(zField), this); + panel.setLayout(new MigLayout("ins 0,wrap", "[][grow,fill]")); final JLabel xLabel = Mnemonic.resolve(new JLabel("&X:")); final JLabel yLabel = Mnemonic.resolve(new JLabel("&Y:")); final JLabel zLabel = Mnemonic.resolve(new JLabel("&Z:")); panel.add(xLabel); - panel.add(xField); + panel.add(xSpinner); panel.add(yLabel); - panel.add(yField); + panel.add(ySpinner); panel.add(zLabel); - panel.add(zField); + panel.add(zSpinner); - xLabel.setLabelFor(xField); - yLabel.setLabelFor(yField); - zLabel.setLabelFor(zField); + xLabel.setLabelFor(xSpinner); + yLabel.setLabelFor(ySpinner); + zLabel.setLabelFor(zSpinner); return panel; } @Override protected boolean isComplete() { - return UIUtils.isValid(xField) - && UIUtils.isValid(yField) - && UIUtils.isValid(zField); + return UIUtils.isValid(xSpinner) + && UIUtils.isValid(ySpinner) + && UIUtils.isValid(zSpinner); } @NotNull public Vector3f getPosition() { return new Vector3f( - Float.parseFloat(xField.getText().trim()), - Float.parseFloat(yField.getText().trim()), - Float.parseFloat(zField.getText().trim()) + (float) xSpinner.getValue(), + (float) ySpinner.getValue(), + (float) zSpinner.getValue() ); } } - - private static class DoubleValidator extends InputValidator { - public DoubleValidator(@NotNull JComponent component) { - super(component); - } - - @NotNull - @Override - protected Validation validate(@NotNull JComponent input) { - final String text = ((JTextComponent) input).getText().trim(); - - try { - Float.parseFloat(text); - return Validation.ok(); - } catch (NumberFormatException e) { - return Validation.error("Not a number"); - } - } - } }