Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions src/nl/jeroenhoek/josm/gridify/ui/GridSizePanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,36 @@ void flipRowsColumns() {
}

void setRowCount(int rows) {
if (rows < 1 || rows > 1000) return;
if (rows < PositiveSpinner.SPINNER_MIN_VALUE || rows > PositiveSpinner.SPINNER_MAX_VALUE) return;
this.rows = rows;
this.spinnerRows.setValue(rows);
changeCallback.changed(getRowCount(), getColumnCount());
}

void setColumnCount(int columns) {
if (columns < 1 || columns > 1000) return;
if (columns < PositiveSpinner.SPINNER_MIN_VALUE || columns > PositiveSpinner.SPINNER_MAX_VALUE) return;
this.columns = columns;
this.spinnerColumns.setValue(columns);
changeCallback.changed(getRowCount(), getColumnCount());
}

void nudgeRowCount(Nudge direction) {
if (direction == Nudge.INCREMENT || this.rows > 1) {
this.rows += direction == Nudge.INCREMENT ? 1 : -1;
this.spinnerRows.setValue(rows);
int newRows = this.rows + (direction == Nudge.INCREMENT ? 1 : -1);
if (newRows >= PositiveSpinner.SPINNER_MIN_VALUE && newRows <= PositiveSpinner.SPINNER_MAX_VALUE) {
this.rows = newRows;
this.spinnerRows.setValue(newRows);
this.spinnerRows.caretToEnd();
changeCallback.changed(getRowCount(), getColumnCount());
changeCallback.changed(this.rows, this.columns);
}
}

void nudgeColumnCount(Nudge direction) {
if (direction == Nudge.INCREMENT || this.columns > 1) {
this.columns += direction == Nudge.INCREMENT ? 1 : -1;
this.spinnerColumns.setValue(columns);
int newCols = this.columns + (direction == Nudge.INCREMENT ? 1 : -1);
if (newCols >= PositiveSpinner.SPINNER_MIN_VALUE && newCols <= PositiveSpinner.SPINNER_MAX_VALUE) {
this.columns = newCols;
this.spinnerColumns.setValue(newCols);
this.spinnerColumns.caretToEnd();
changeCallback.changed(getRowCount(), getColumnCount());
changeCallback.changed(this.rows, this.columns);
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/nl/jeroenhoek/josm/gridify/ui/GridifySettingsDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public GridifySettingsDialog(InputData inputData, GridifySettings settings) {
constraints.insets = insetsIndent;
controlPanel.add(gridSizePanel, constraints);

if (inputData.getSourceWay().isPresent()) {
inputData.getSourceWay().ifPresent(sourceWay -> {
JLabel wayLabel = new JLabel(tr("Source way"));
wayLabel.setBorder(underline);
constraints.gridy = 4;
Expand All @@ -92,18 +92,18 @@ public GridifySettingsDialog(InputData inputData, GridifySettings settings) {

// Always check the 'delete source way' option when a new way is used as template.
// It tends to have been drawn specifically to cut up.
boolean deleteSourceWay = inputData.getSourceWay().get().isNew();
boolean deleteSourceWay = sourceWay.isNew();

sourceWayPanel = new SourceWayPanel(
settings.copyTagsFromSource(),
deleteSourceWay || settings.deleteSource(),
!inputData.getSourceWay().get().getReferrers().isEmpty(),
!sourceWay.getReferrers().isEmpty(),
this::fireChangeEvent
);
constraints.gridy = 5;
constraints.insets = insetsIndent;
controlPanel.add(sourceWayPanel, constraints);
}
});

JLabel statsLabel = new JLabel(tr("Statistics"));
constraints.gridy = 6;
Expand Down
47 changes: 23 additions & 24 deletions src/nl/jeroenhoek/josm/gridify/ui/PositiveSpinner.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.FocusEvent;
import java.text.ParseException;
import java.util.Objects;

/**
Expand Down Expand Up @@ -42,24 +43,19 @@ public PositiveSpinner(int defaultValue, ValueChanged changeCallback) {

// Kind of weird that the default JSpinner doesn't do this.
addMouseWheelListener(e -> {
int ticks = e.getWheelRotation();
Object newValue = null;
if (ticks > 0) {
for (int tick = 0; tick < ticks; tick++) {
Object previous = getPreviousValue();
if (previous == null) break;
newValue = previous;
}
} else if (ticks < 0) {
for (int tick = 0; tick > ticks; tick--) {
Object next = getNextValue();
if (next == null) break;
newValue = next;
}
try {
commitEdit();
} catch (ParseException ignore) {
}
int ticks = e.getWheelRotation();
int current = (Integer) getValue();

// e.getWheelRotation() is positive when scrolling down (decrease) and negative when scrolling up (increase)
int newValue = Math.max(SPINNER_MIN_VALUE, Math.min(SPINNER_MAX_VALUE, current - ticks));

if (newValue != null) {
if (newValue != current) {
setValue(newValue);
caretToEnd();
}
});

Expand Down Expand Up @@ -88,27 +84,30 @@ public void changedUpdate(DocumentEvent e) {
}

void callbackIfChanged() {
Integer newValue;
String text = field.getText();
if (text.isBlank()) {
return;
}

int newValue;
try {
newValue = Integer.parseInt(field.getText());
newValue = Integer.parseInt(text);
} catch (NumberFormatException e) {
// Ignore.
return;
}

if (!Objects.equals(newValue, lastValue)) {
SwingUtilities.invokeLater(() -> {
this.lastValue = newValue;
changeCallback.onChange(newValue);
});
this.lastValue = newValue;
SwingUtilities.invokeLater(() -> changeCallback.onChange(newValue));
}
}

@Override
public void setValue(Object value) {
// No need to update the value if nothing changes. This prevents the caret being placed at an awkward position.
if (Objects.equals(value, lastValue)) return;
// Check against the actual model value instead of lastValue so the model doesn't get stuck.
if (Objects.equals(value, getValue())) return;

this.lastValue = (Integer) value;
super.setValue(value);
}

Expand Down
Loading