11/*
2- * Copyright 2024 Laszlo Balazs-Csiki and Contributors
2+ * Copyright 2025 Laszlo Balazs-Csiki and Contributors
33 *
44 * This file is part of Pixelitor. Pixelitor is free software: you
55 * can redistribute it and/or modify it under the terms of the GNU
2929import static pixelitor .gui .utils .TextFieldValidator .createPositiveIntLayer ;
3030
3131/**
32- * The batch resize functionality
32+ * The batch resize functionality.
3333 */
3434public class BatchResize {
3535 private BatchResize () { // do not instantiate
@@ -44,18 +44,18 @@ public static void showDialog(String dialogTitle) {
4444 .show ();
4545 }
4646
47- private static void dialogAccepted (BatchResizePanel p ) {
48- p . saveValues ();
47+ private static void dialogAccepted (BatchResizePanel panel ) {
48+ panel . rememberSettings ();
4949
50- int maxWidth = p .getNewWidth ();
51- int maxHeight = p .getNewHeight ();
50+ int maxWidth = panel .getNewWidth ();
51+ int maxHeight = panel .getNewHeight ();
5252
5353 var resizeAction = new Resize (maxWidth , maxHeight , true );
5454 new BatchProcessor (resizeAction , "Batch Resize..." ).processFiles ();
5555 }
5656
5757 /**
58- * The GUI for batch resize
58+ * The panel for batch resize settings.
5959 */
6060 static class BatchResizePanel extends ValidatedPanel {
6161 private final OpenSaveDirsPanel openSaveDirsPanel ;
@@ -64,14 +64,16 @@ static class BatchResizePanel extends ValidatedPanel {
6464
6565 private static final int DEFAULT_WIDTH = 300 ;
6666 private static final int DEFAULT_HEIGHT = 300 ;
67+ private static final String WIDTH_LABEL = "Max Width" ;
68+ private static final String HEIGHT_LABEL = "Max Height" ;
6769
6870 private BatchResizePanel () {
6971 var sizePanel = new JPanel ();
7072
7173 IntDocumentFilter documentFilter = new IntDocumentFilter ();
7274
73- widthTF = addTextField ("Max Width :" , "widthTF" , DEFAULT_WIDTH , sizePanel , documentFilter );
74- heightTF = addTextField ("Max Height :" , "heightTF" , DEFAULT_HEIGHT , sizePanel , documentFilter );
75+ widthTF = addTextField (WIDTH_LABEL + " :" , "widthTF" , DEFAULT_WIDTH , sizePanel , documentFilter );
76+ heightTF = addTextField (HEIGHT_LABEL + " :" , "heightTF" , DEFAULT_HEIGHT , sizePanel , documentFilter );
7577
7678 setLayout (new BoxLayout (this , Y_AXIS ));
7779 add (sizePanel );
@@ -84,7 +86,8 @@ private static JTextField addTextField(String label, String name, int defaultVal
8486
8587 JTextField tf = new JTextField (String .valueOf (defaultValue ), 5 );
8688 tf .setName (name );
87- sizePanel .add (createPositiveIntLayer (label , tf , false ));
89+ // the JLayer shows immediate visual feedback for invalid input
90+ sizePanel .add (createPositiveIntLayer (label , tf ));
8891 documentFilter .applyOn (tf );
8992
9093 return tf ;
@@ -93,32 +96,22 @@ private static JTextField addTextField(String label, String name, int defaultVal
9396 @ Override
9497 public ValidationResult validateSettings () {
9598 return openSaveDirsPanel .validateSettings ()
96- .withErrorIf (widthTF .getText ().trim ().isEmpty (),
97- "The \" width\" field is empty" )
98- .withErrorIf (heightTF .getText ().trim ().isEmpty (),
99- "The \" height\" field is empty" );
99+ .requirePositiveInt (widthTF .getText (), WIDTH_LABEL )
100+ .requirePositiveInt (heightTF .getText (), HEIGHT_LABEL );
100101 }
101102
102- private void saveValues () {
103- openSaveDirsPanel .rememberValues ();
103+ private void rememberSettings () {
104+ openSaveDirsPanel .rememberSettings ();
104105 }
105106
106107 private int getNewWidth () {
107- try {
108- return Integer .parseInt (widthTF .getText ());
109- } catch (NumberFormatException e ) {
110- widthTF .setText (String .valueOf (DEFAULT_WIDTH ));
111- return DEFAULT_WIDTH ;
112- }
108+ // validation already ensured this is a valid positive integer
109+ return Integer .parseInt (widthTF .getText ().trim ());
113110 }
114111
115112 private int getNewHeight () {
116- try {
117- return Integer .parseInt (heightTF .getText ());
118- } catch (NumberFormatException e ) {
119- heightTF .setText (String .valueOf (DEFAULT_HEIGHT ));
120- return DEFAULT_HEIGHT ;
121- }
113+ // validation already ensured this is a valid positive integer
114+ return Integer .parseInt (heightTF .getText ().trim ());
122115 }
123116 }
124117}
0 commit comments