Skip to content

Commit 132939d

Browse files
committed
refactorings
1 parent 817d3d2 commit 132939d

11 files changed

Lines changed: 275 additions & 248 deletions

src/main/java/pixelitor/automate/AutoPaintPanel.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ public class AutoPaintPanel extends ValidatedPanel implements DialogMenuOwner {
7575
strokeCountTF.setName("strokeCountTF");
7676
gbh.addLabelAndControl(STROKE_COUNT_TEXT + ":",
7777
createPositiveIntLayer(
78-
STROKE_COUNT_TEXT, strokeCountTF, false));
78+
STROKE_COUNT_TEXT, strokeCountTF));
7979

8080
strokeLengthTF = new JTextField("100");
8181
gbh.addLabelAndControl(STROKE_LENGTH_TEXT + ":",
82-
createPositiveIntLayer(
83-
STROKE_LENGTH_TEXT, strokeLengthTF, false));
82+
createPositiveIntLayer(STROKE_LENGTH_TEXT, strokeLengthTF));
8483

8584
gbh.addParam(lengthVariation);
8685
gbh.addParam(curvature);
@@ -122,8 +121,8 @@ private int getStrokeLength() {
122121
@Override
123122
public ValidationResult validateSettings() {
124123
return ValidationResult.valid()
125-
.validatePositiveInt(strokeCountTF.getText(), STROKE_COUNT_TEXT)
126-
.validatePositiveInt(strokeLengthTF.getText(), STROKE_LENGTH_TEXT);
124+
.requirePositiveInt(strokeCountTF.getText(), STROKE_COUNT_TEXT)
125+
.requirePositiveInt(strokeLengthTF.getText(), STROKE_LENGTH_TEXT);
127126
}
128127

129128
@Override

src/main/java/pixelitor/automate/BatchFilterWizardPage.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,7 @@ public JComponent createPanel(Wizard wizard, Drawable dr) {
5959

6060
var mainPanel = new JPanel(new VerticalLayout());
6161
mainPanel.add(searchPanel);
62-
if (openSaveDirsPanel == null) {
63-
openSaveDirsPanel = new OpenSaveDirsPanel();
64-
}
62+
openSaveDirsPanel = new OpenSaveDirsPanel();
6563
mainPanel.add(openSaveDirsPanel);
6664

6765
return mainPanel;
@@ -88,7 +86,7 @@ public void onComplete(Wizard wizard, Drawable dr) {
8886

8987
((BatchFilterWizard) wizard).setFilter(filter);
9088

91-
openSaveDirsPanel.rememberValues();
89+
openSaveDirsPanel.rememberSettings();
9290
}
9391
}, FILTER_GUI {
9492
@Override
@@ -105,8 +103,7 @@ public Optional<WizardPage> getNextPage() {
105103
public JComponent createPanel(Wizard wizard, Drawable dr) {
106104
Filter filter = ((BatchFilterWizard) wizard).getFilter();
107105

108-
// This page will be shown only if
109-
// the selected filter is a filter with GUI.
106+
// this page is shown only if the selected filter has a GUI
110107
FilterWithGUI guiFilter = (FilterWithGUI) filter;
111108

112109
dr.startPreviewing();
@@ -121,8 +118,7 @@ public void onWizardCanceled(Drawable dr) {
121118

122119
@Override
123120
public void onComplete(Wizard wizard, Drawable dr) {
124-
// cancel the previewing
125-
onWizardCanceled(dr);
121+
dr.stopPreviewing();
126122
}
127123
}
128124
}

src/main/java/pixelitor/automate/BatchResize.java

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -29,7 +29,7 @@
2929
import static pixelitor.gui.utils.TextFieldValidator.createPositiveIntLayer;
3030

3131
/**
32-
* The batch resize functionality
32+
* The batch resize functionality.
3333
*/
3434
public 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
}

src/main/java/pixelitor/automate/OpenSaveDirsPanel.java

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
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
@@ -28,12 +28,10 @@
2828
import java.awt.GridBagLayout;
2929
import java.io.File;
3030

31-
import static java.lang.String.format;
3231
import static pixelitor.gui.utils.BrowseFilesSupport.SelectionMode.DIRECTORY;
3332

3433
/**
35-
* A panel for selecting an input folder,
36-
* an output folder, and a saving file format.
34+
* A panel for selecting input/output folders and the save file format.
3735
*/
3836
class OpenSaveDirsPanel extends ValidatedPanel {
3937
private final BrowseFilesSupport inputChooser
@@ -74,30 +72,17 @@ public ValidationResult validateSettings() {
7472
File inputDir = inputChooser.getSelectedFile();
7573
File outputDir = outputChooser.getSelectedFile();
7674

77-
var result = ValidationResult.valid();
78-
result = validateDirExists(result, inputDir, "input");
79-
result = validateDirExists(result, outputDir, "output");
80-
81-
if (inputDir.equals(outputDir)) {
82-
ValidationResult sameDirError = ValidationResult.invalid(
75+
return ValidationResult.valid()
76+
.requireExistingDir(inputDir, "input")
77+
.requireExistingDir(outputDir, "output")
78+
.addErrorIf(inputDir.equals(outputDir),
8379
"The input and output folders must be different.");
84-
return result.and(sameDirError);
85-
}
86-
return result;
87-
}
88-
89-
private static ValidationResult validateDirExists(ValidationResult currentResult,
90-
File dir,
91-
String directoryType) {
92-
if (!dir.exists()) {
93-
String msg = format("The selected %s folder %s doesn't exist.",
94-
directoryType, dir.getAbsolutePath());
95-
currentResult = currentResult.and(ValidationResult.invalid(msg));
96-
}
97-
return currentResult;
9880
}
9981

100-
public void rememberValues() {
82+
/**
83+
* Saves the chosen directories and format for future use.
84+
*/
85+
public void rememberSettings() {
10186
Dirs.setLastOpen(inputChooser.getSelectedFile());
10287
Dirs.setLastSave(outputChooser.getSelectedFile());
10388
FileFormat.setLastSaved(getSelectedFormat());

0 commit comments

Comments
 (0)