| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.plugins.buildings_tools;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.awt.GridBagConstraints;
|
|---|
| 7 | import java.text.NumberFormat;
|
|---|
| 8 | import java.text.ParseException;
|
|---|
| 9 |
|
|---|
| 10 | import javax.swing.ButtonGroup;
|
|---|
| 11 | import javax.swing.JButton;
|
|---|
| 12 | import javax.swing.JCheckBox;
|
|---|
| 13 | import javax.swing.JFormattedTextField;
|
|---|
| 14 | import javax.swing.JRadioButton;
|
|---|
| 15 |
|
|---|
| 16 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * A dialog for setting building sizes and other information
|
|---|
| 20 | */
|
|---|
| 21 | public class BuildingSizeDialog extends MyDialog {
|
|---|
| 22 | private final JFormattedTextField twidth = new JFormattedTextField(NumberFormat.getInstance());
|
|---|
| 23 | private final JFormattedTextField tlenstep = new JFormattedTextField(NumberFormat.getInstance());
|
|---|
| 24 | {
|
|---|
| 25 | ((NumberFormat) tlenstep.getFormatter()).setMaximumFractionDigits(4);
|
|---|
| 26 | }
|
|---|
| 27 | private final JCheckBox caddr = new JCheckBox(tr("Use Address dialog"));
|
|---|
| 28 | private final JCheckBox cAutoSelect = new JCheckBox(tr("Auto-select building"));
|
|---|
| 29 | private final JCheckBox cAutoSelectReplaceSelection = new JCheckBox(tr("Auto-select replaces existing selection"));
|
|---|
| 30 | private final JCheckBox cAddrNode = new JCheckBox(tr("Use address nodes under buildings"));
|
|---|
| 31 | private final JRadioButton circleRadio = new JRadioButton(tr("Circle"));
|
|---|
| 32 | private final JRadioButton rectangleRadio = new JRadioButton(tr("Rectangle"));
|
|---|
| 33 |
|
|---|
| 34 | /**
|
|---|
| 35 | * Create a new dialog for building sizes
|
|---|
| 36 | */
|
|---|
| 37 | public BuildingSizeDialog() {
|
|---|
| 38 | super(tr("Set buildings size and shape"));
|
|---|
| 39 |
|
|---|
| 40 | ButtonGroup shapeGroup = new ButtonGroup();
|
|---|
| 41 | shapeGroup.add(circleRadio);
|
|---|
| 42 | shapeGroup.add(rectangleRadio);
|
|---|
| 43 | circleRadio.setSelected(ToolSettings.Shape.CIRCLE == ToolSettings.getShape());
|
|---|
| 44 | rectangleRadio.setSelected(ToolSettings.Shape.RECTANGLE == ToolSettings.getShape());
|
|---|
| 45 |
|
|---|
| 46 | panel.add(rectangleRadio, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 47 | panel.add(circleRadio, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 48 |
|
|---|
| 49 | addLabelled(tr("Buildings width/diameter:"), twidth);
|
|---|
| 50 | addLabelled(tr("Length step:"), tlenstep);
|
|---|
| 51 | panel.add(caddr, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 52 | panel.add(cAutoSelect, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 53 | panel.add(cAutoSelectReplaceSelection, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 54 | panel.add(cAddrNode, GBC.eol().fill(GridBagConstraints.HORIZONTAL));
|
|---|
| 55 |
|
|---|
| 56 | twidth.setValue(ToolSettings.getWidth());
|
|---|
| 57 | tlenstep.setValue(ToolSettings.getLenStep());
|
|---|
| 58 | caddr.setSelected(ToolSettings.isUsingAddr());
|
|---|
| 59 | cAutoSelect.setSelected(ToolSettings.isAutoSelect());
|
|---|
| 60 | cAutoSelectReplaceSelection.setSelected(ToolSettings.isAutoSelectReplaceSelection());
|
|---|
| 61 | cAddrNode.setSelected(ToolSettings.PROP_USE_ADDR_NODE.get());
|
|---|
| 62 |
|
|---|
| 63 | JButton bAdv = new JButton(tr("Advanced..."));
|
|---|
| 64 | bAdv.addActionListener(ignored -> {
|
|---|
| 65 | AdvancedSettingsDialog dlg = new AdvancedSettingsDialog();
|
|---|
| 66 | if (dlg.getValue() == 1) {
|
|---|
| 67 | dlg.saveSettings();
|
|---|
| 68 | }
|
|---|
| 69 | });
|
|---|
| 70 | panel.add(bAdv, GBC.eol().insets(0, 5, 0, 0).anchor(GridBagConstraints.EAST));
|
|---|
| 71 |
|
|---|
| 72 | setupDialog();
|
|---|
| 73 | showDialog();
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | /**
|
|---|
| 77 | * Get the specified max width/diameter
|
|---|
| 78 | * @return The maximum width/diameter for rectangles/circles
|
|---|
| 79 | */
|
|---|
| 80 | public final double width() {
|
|---|
| 81 | try {
|
|---|
| 82 | return NumberFormat.getInstance().parse(twidth.getText()).doubleValue();
|
|---|
| 83 | } catch (ParseException e) {
|
|---|
| 84 | return 0;
|
|---|
| 85 | }
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | /**
|
|---|
| 89 | * Get the step length for drawing rectangular buildings
|
|---|
| 90 | * @return The discrete steps to increase rectanglular building sizes
|
|---|
| 91 | */
|
|---|
| 92 | public double lenstep() {
|
|---|
| 93 | try {
|
|---|
| 94 | return NumberFormat.getInstance().parse(tlenstep.getText()).doubleValue();
|
|---|
| 95 | } catch (ParseException e) {
|
|---|
| 96 | return 0;
|
|---|
| 97 | }
|
|---|
| 98 | }
|
|---|
| 99 |
|
|---|
| 100 | /**
|
|---|
| 101 | * Check if the user wants to use addresses from underlying nodes
|
|---|
| 102 | * @return {@code true} if the user wants to use addresses
|
|---|
| 103 | */
|
|---|
| 104 | public final boolean useAddr() {
|
|---|
| 105 | return caddr.isSelected();
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | /**
|
|---|
| 109 | * Save the settings for this dialog
|
|---|
| 110 | */
|
|---|
| 111 | public final void saveSettings() {
|
|---|
| 112 | if (circleRadio.isSelected()) {
|
|---|
| 113 | ToolSettings.saveShape(ToolSettings.Shape.CIRCLE);
|
|---|
| 114 | } else if (rectangleRadio.isSelected()) {
|
|---|
| 115 | ToolSettings.saveShape(ToolSettings.Shape.RECTANGLE);
|
|---|
| 116 | }
|
|---|
| 117 | ToolSettings.setSizes(width(), lenstep());
|
|---|
| 118 | ToolSettings.setAddrDialog(useAddr());
|
|---|
| 119 | ToolSettings.setAutoSelect(cAutoSelect.isSelected());
|
|---|
| 120 | ToolSettings.setAutoSelectReplaceSelection(cAutoSelectReplaceSelection.isSelected());
|
|---|
| 121 | ToolSettings.PROP_USE_ADDR_NODE.put(cAddrNode.isSelected());
|
|---|
| 122 | }
|
|---|
| 123 | }
|
|---|