From fa57d664461fd9a38542ea4df9f9eef81225b57d Mon Sep 17 00:00:00 2001 From: Jeroen Hoek Date: Tue, 5 May 2020 07:17:41 +0200 Subject: [PATCH] Re-add foundation of settings dialogue --- .../josm/gridify/GridifyAction.java | 57 +++--- .../josm/gridify/GridifySettings.java | 75 ++++++++ .../gridify/ui/GridifySettingsDialog.java | 167 ++++++++++++++++++ 3 files changed, 277 insertions(+), 22 deletions(-) create mode 100644 src/nl/jeroenhoek/josm/gridify/GridifySettings.java create mode 100644 src/nl/jeroenhoek/josm/gridify/ui/GridifySettingsDialog.java diff --git a/src/nl/jeroenhoek/josm/gridify/GridifyAction.java b/src/nl/jeroenhoek/josm/gridify/GridifyAction.java index dce5e62..1b44396 100644 --- a/src/nl/jeroenhoek/josm/gridify/GridifyAction.java +++ b/src/nl/jeroenhoek/josm/gridify/GridifyAction.java @@ -2,25 +2,38 @@ package nl.jeroenhoek.josm.gridify; import nl.jeroenhoek.josm.gridify.exception.GridifyException; -import nl.jeroenhoek.josm.gridify.exception.UserCancelledException; import nl.jeroenhoek.josm.gridify.exception.UserInputException; +import nl.jeroenhoek.josm.gridify.exception.UserCancelledException; +import nl.jeroenhoek.josm.gridify.ui.GridifySettingsDialog; import org.openstreetmap.josm.actions.JosmAction; -import org.openstreetmap.josm.command.*; +import org.openstreetmap.josm.command.AddCommand; +import org.openstreetmap.josm.command.ChangePropertyCommand; +import org.openstreetmap.josm.command.Command; +import org.openstreetmap.josm.command.DeleteCommand; +import org.openstreetmap.josm.command.SelectCommand; +import org.openstreetmap.josm.command.SequenceCommand; import org.openstreetmap.josm.data.UndoRedoHandler; import org.openstreetmap.josm.data.coor.EastNorth; -import org.openstreetmap.josm.data.osm.*; +import org.openstreetmap.josm.data.osm.DataSet; +import org.openstreetmap.josm.data.osm.Node; +import org.openstreetmap.josm.data.osm.OsmPrimitive; +import org.openstreetmap.josm.data.osm.TagMap; +import org.openstreetmap.josm.data.osm.Way; +import org.openstreetmap.josm.data.preferences.BooleanProperty; +import org.openstreetmap.josm.data.preferences.IntegerProperty; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Shortcut; import org.openstreetmap.josm.tools.SubclassFilteredCollection; import org.openstreetmap.josm.tools.Utils; -import javax.swing.*; +import javax.swing.JOptionPane; import java.awt.event.ActionEvent; import java.awt.event.KeyEvent; import java.util.*; import static nl.jeroenhoek.josm.gridify.exception.UserInputException.error; +import static org.openstreetmap.josm.tools.I18n.set; import static org.openstreetmap.josm.tools.I18n.tr; /** @@ -87,34 +100,34 @@ Collection performGridifyAction(DataSet dataSet) throws GridifyExceptio InputData inputData = inputDataFromSelection(selection) .orElseThrow(error(tr("Select four nodes, or a way consisting of four nodes."))); -// -// GridifySettings settings = new GridifySettings(); -// -// GridifySettingsDialog dialog = new GridifySettingsDialog(inputData, settings); -// dialog.showDialog(); + + GridifySettings settings = new GridifySettings(); + + GridifySettingsDialog dialog = new GridifySettingsDialog(inputData, settings); + dialog.showDialog(); // Only the OK button returns 1, the rest means 'Cancel' or a closed dialog window. -// if (dialog.getValue() != 1) { -// throw new UserCancelledException(); -// } + if (dialog.getValue() != 1) { + throw new UserCancelledException(); + } GridExtrema extrema = inputData.getGridExtrema(); Collection commands = new ArrayList<>(); // Read the user provided settings. - int numRows = 2; - int numColumns = 3; - Operation operation = Operation.BLOCKS; - boolean deleteSourceWay = true; - boolean copyTags = true; + int numRows = dialog.getRowCount(); + int numColumns = dialog.getColumnCount(); + Operation operation = dialog.getOperation(); + boolean deleteSourceWay = dialog.deleteSourceWay(); + boolean copyTags = dialog.copyTags(); // Update settings properties now that we are about to commence the operation. // This way the user gets to keep the last settings they entered. -// settings.setNumRows(numRows); -// settings.setNumColumns(numColumns); -// settings.setDeleteSource(deleteSourceWay); -// settings.setCopyTagsFromSource(copyTags); -// settings.setOperation(operation); + settings.setNumRows(numRows); + settings.setNumColumns(numColumns); + settings.setDeleteSource(deleteSourceWay); + settings.setCopyTagsFromSource(copyTags); + settings.setOperation(operation); List nodesTopBetween = nodesBetween(extrema.one, extrema.two, numColumns - 1); addToDataSet(commands, dataSet, nodesTopBetween); diff --git a/src/nl/jeroenhoek/josm/gridify/GridifySettings.java b/src/nl/jeroenhoek/josm/gridify/GridifySettings.java new file mode 100644 index 0000000..ccf04ff --- /dev/null +++ b/src/nl/jeroenhoek/josm/gridify/GridifySettings.java @@ -0,0 +1,75 @@ +package nl.jeroenhoek.josm.gridify; + +import org.openstreetmap.josm.data.preferences.BooleanProperty; +import org.openstreetmap.josm.data.preferences.EnumProperty; +import org.openstreetmap.josm.data.preferences.IntegerProperty; + +/** + * All settings used by Gridify. + */ +public class GridifySettings { + final IntegerProperty numRowsSetting = new IntegerProperty("gridify.num_rows", 2); + final IntegerProperty numColsSetting = new IntegerProperty("gridify.num_cols", 4); + final BooleanProperty copyTagsFromSource = new BooleanProperty("gridify.copy_tags_from_source", true); + final BooleanProperty deleteSource = new BooleanProperty("gridify.delete_source", true); + + final EnumProperty operationSetting = new EnumProperty<>( + "gridify.operation", Operation.class, Operation.BLOCKS + ); + + public int getNumRows() { + int rows = numRowsSetting.get(); + + // Limit number of rows to permissible values. + if (rows < 1 || rows > 1000) { + setNumRows(2); + return 2; + } + + return rows; + } + + public void setNumRows(int numRows) { + numRowsSetting.put(numRows); + } + + public int getNumColumns() { + int columns = numColsSetting.get(); + + // Limit number of columns to permissible values. + if (columns < 1 || columns > 1000) { + setNumColumns(4); + return 4; + } + + return columns; + } + + public void setNumColumns(int numColumns) { + numColsSetting.put(numColumns); + } + + public boolean copyTagsFromSource() { + return copyTagsFromSource.get(); + } + + public void setCopyTagsFromSource(boolean enabled) { + copyTagsFromSource.put(enabled); + } + + public boolean deleteSource() { + return deleteSource.get(); + } + + public void setDeleteSource(boolean enabled) { + deleteSource.put(enabled); + } + + public Operation getOperation() { + return operationSetting.get(); + } + + public void setOperation(Operation operation) { + operationSetting.put(operation); + } +} diff --git a/src/nl/jeroenhoek/josm/gridify/ui/GridifySettingsDialog.java b/src/nl/jeroenhoek/josm/gridify/ui/GridifySettingsDialog.java new file mode 100644 index 0000000..c16a8c6 --- /dev/null +++ b/src/nl/jeroenhoek/josm/gridify/ui/GridifySettingsDialog.java @@ -0,0 +1,167 @@ +// License: GPL. For details, see LICENSE file. +package nl.jeroenhoek.josm.gridify.ui; + +import nl.jeroenhoek.josm.gridify.GridifySettings; +import nl.jeroenhoek.josm.gridify.InputData; +import nl.jeroenhoek.josm.gridify.Operation; +//import nl.jeroenhoek.josm.gridify.ui.GridSizePanel.Nudge; +import org.openstreetmap.josm.gui.ExtendedDialog; +import org.openstreetmap.josm.gui.MainApplication; + +import javax.swing.BorderFactory; +import javax.swing.Box; +import javax.swing.BoxLayout; +import javax.swing.JLabel; +import javax.swing.JPanel; +import javax.swing.border.Border; +import java.awt.Color; +import java.awt.Dimension; +import java.awt.GridBagConstraints; +import java.awt.GridBagLayout; +import java.awt.Insets; + +import static org.openstreetmap.josm.tools.I18n.set; +import static org.openstreetmap.josm.tools.I18n.tr; + +/** + * The modal dialog presented to the user when executing the Gridify action. + */ +public class GridifySettingsDialog extends ExtendedDialog { + private final InputData inputData; + private final GridifySettings settings; + +// private GridSizePanel gridSizePanel; +// private OperationChooser operationChooser; +// private SourceWayPanel sourceWayPanel; + + public GridifySettingsDialog(InputData inputData, GridifySettings settings) { + super(MainApplication.getMainFrame(), tr("Gridify preview"), tr("Gridify"), tr("Cancel")); + this.inputData = inputData; + this.settings = settings; + } + @Override + public void setupDialog() { + final Insets insetsDefault = new Insets(0, 0, 10, 0); + final Insets insetsIndent = new Insets(0, 30, 10, 0); + final Border underline = BorderFactory.createMatteBorder(0, 0, 1, 0, Color.black); + + + //Preview preview = new Preview(inputData.getGridExtrema(), this); + JLabel preview = new JLabel("Preview"); + + JPanel rootPanel = new JPanel(); + setMinimumSize(new Dimension(550, 360)); + rootPanel.setLayout(new BoxLayout(rootPanel, BoxLayout.X_AXIS)); + + JPanel controlPanel = new JPanel(); + controlPanel.setLayout(new GridBagLayout()); + GridBagConstraints constraints = new GridBagConstraints(); + constraints.fill = GridBagConstraints.HORIZONTAL; + + JLabel operationChooserLabel = new JLabel(tr("Output shape")); + operationChooserLabel.setBorder(underline); + controlPanel.add(operationChooserLabel, constraints); + +// operationChooser = new OperationChooser(settings.getOperation()); + JLabel operationChooser = new JLabel("Chooser"); + + constraints.gridy = 1; + constraints.insets = insetsIndent; + controlPanel.add(operationChooser, constraints); + + JLabel gridSizePanelLabel = new JLabel(tr("Grid size")); + gridSizePanelLabel.setBorder(underline); + constraints.gridy = 2; + constraints.insets = insetsDefault; + controlPanel.add(gridSizePanelLabel, constraints); + +// gridSizePanel = new GridSizePanel( +// preview::updateRowsColumns, +// settings.getNumRows(), +// settings.getNumColumns() +// ); + JLabel gridSizePanel = new JLabel("Size"); + + constraints.gridy = 3; + constraints.insets = insetsIndent; + controlPanel.add(gridSizePanel, constraints); + + if (inputData.getSourceWay().isPresent()) { + JLabel wayLabel = new JLabel(tr("Source way")); + wayLabel.setBorder(underline); + constraints.gridy = 4; + constraints.insets = insetsDefault; + controlPanel.add(wayLabel, constraints); + + // 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(); + +// sourceWayPanel = new SourceWayPanel( +// settings.copyTagsFromSource(), +// deleteSourceWay || settings.deleteSource() +// ); + JLabel sourceWayPanel = new JLabel("Source"); + constraints.gridy = 5; + constraints.insets = insetsIndent; + controlPanel.add(sourceWayPanel, constraints); + } + + rootPanel.add(controlPanel); + rootPanel.add(Box.createHorizontalGlue()); + + rootPanel.add(preview); + + setContent(rootPanel, false); + setButtonIcons("ok.png", "cancel.png"); + setDefaultButton(1); + + super.setupDialog(); + + gridSizePanel.requestFocusInWindow(); + } + + @Override + public Dimension getMinimumSize() { + return getPreferredSize(); + } + + public int getRowCount() { + return settings.getNumRows(); +// return gridSizePanel == null +// ? settings.getNumRows() +// : gridSizePanel.getRowCount(); + } + + public int getColumnCount() { + return settings.getNumColumns(); +// return gridSizePanel == null +// ? settings.getNumColumns() +// : gridSizePanel.getColumnCount(); + } + +// public void nudgeRowCount(Nudge direction) { +// if (gridSizePanel != null) { +// gridSizePanel.nudgeRowCount(direction); +// } +// } +// +// public void nudgeColumnCount(Nudge direction) { +// if (gridSizePanel != null) { +// gridSizePanel.nudgeColumnCount(direction); +// } +// } + + public Operation getOperation() { + return Operation.BLOCKS; +// return operationChooser.getSelected(); + } + + public boolean copyTags() { + return inputData.getSourceWay().isPresent();// && sourceWayPanel.copyTags(); + } + + public boolean deleteSourceWay() { + return inputData.getSourceWay().isPresent();// && sourceWayPanel.deleteSourceWay(); + } +}