| 1 | /*
|
|---|
| 2 | * This scripts sets a sequence of house numbers on the currently selected nodes.
|
|---|
| 3 | *
|
|---|
| 4 | * The user can enter a start number and and an increment.
|
|---|
| 5 | */
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.BorderLayout;
|
|---|
| 8 |
|
|---|
| 9 | import groovy.swing.SwingBuilder;
|
|---|
| 10 | import javax.swing.JOptionPane;
|
|---|
| 11 | import org.openstreetmap.josm.Main;
|
|---|
| 12 | import org.openstreetmap.josm.command.ChangeCommand;
|
|---|
| 13 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 15 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
|---|
| 16 | import org.openstreetmap.josm.gui.layer.Layer;
|
|---|
| 17 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
|---|
| 18 | import org.openstreetmap.josm.gui.widgets.HtmlPanel;
|
|---|
| 19 | import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
|
|---|
| 20 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 21 | import org.openstreetmap.josm.tools.WindowGeometry;
|
|---|
| 22 | import org.openstreetmap.josm.data.osm.DataSet
|
|---|
| 23 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 24 | import javax.swing.JDialog;
|
|---|
| 25 | import javax.swing.JPanel;
|
|---|
| 26 | import java.awt.BorderLayout;
|
|---|
| 27 | import java.awt.FlowLayout;
|
|---|
| 28 | import java.awt.GridBagConstraints;
|
|---|
| 29 | import java.awt.GridBagLayout;
|
|---|
| 30 | import javax.swing.JLabel;
|
|---|
| 31 |
|
|---|
| 32 | import javax.swing.BorderFactory;
|
|---|
| 33 | import javax.swing.JTextField;
|
|---|
| 34 | import javax.swing.AbstractAction;
|
|---|
| 35 | import javax.swing.JButton;
|
|---|
| 36 | import java.awt.event.ActionListener;
|
|---|
| 37 | import java.awt.Dimension;
|
|---|
| 38 |
|
|---|
| 39 | class AddHouseNumberDialog extends JDialog {
|
|---|
| 40 |
|
|---|
| 41 | private JTextField tfStart;
|
|---|
| 42 | private JTextField tfIncrement;
|
|---|
| 43 |
|
|---|
| 44 | public AddHouseNumberDialog(){
|
|---|
| 45 | super(Main.parent, true /* modal */)
|
|---|
| 46 | build();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | def buildInfoPanel() {
|
|---|
| 50 | def info = new HtmlPanel(
|
|---|
| 51 | """
|
|---|
| 52 | <html>
|
|---|
| 53 | Enter the <strong>first house number</strong> to be applied to the currently selected nodes
|
|---|
| 54 | and the amount by which the house number is <strong>incremented</strong>.
|
|---|
| 55 | </html>
|
|---|
| 56 | """
|
|---|
| 57 | )
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | def buildInputPanel() {
|
|---|
| 61 | SwingBuilder swing = new SwingBuilder()
|
|---|
| 62 | return swing.panel(){
|
|---|
| 63 | gridBagLayout()
|
|---|
| 64 | label(text: "Start:",
|
|---|
| 65 | horizontalAlignment: JLabel.LEFT,
|
|---|
| 66 | constraints: gbc(gridx:0,gridy:0,weightx:0.0, weighty:0.0, fill: GridBagConstraints.NONE, anchor: GridBagConstraints.WEST)
|
|---|
| 67 | )
|
|---|
| 68 | tfStart = textField(constraints: gbc(gridx:1,gridy:0,weightx:1.0, weighty:0.0, fill: GridBagConstraints.HORIZONTAL, insets:[2,2,2,2]))
|
|---|
| 69 | label(text: "Increment:", horizontalAlignment: JLabel.LEFT, constraints: gbc(gridx:0,gridy:1,weightx:0.0, weighty:0.0, anchor: GridBagConstraints.WEST, insets:[2,2,2,2]))
|
|---|
| 70 | tfIncrement = textField(constraints: gbc(gridx:1,gridy:1,weightx:1.0, weighty:0.0, fill: GridBagConstraints.HORIZONTAL, anchor: GridBagConstraints.WEST, insets:[2,2,2,2]))
|
|---|
| 71 | panel(constraints: gbc(gridx:0,gridy:2,weightx:1.0, weighty:1.0, gridwidth:2, fill: GridBagConstraints.BOTH, insets:[2,2,2,2]))
|
|---|
| 72 | }
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | def buildControlButtonPanel() {
|
|---|
| 76 | SwingBuilder swing = new SwingBuilder()
|
|---|
| 77 | return swing.panel(layout: new FlowLayout(FlowLayout.CENTER)) {
|
|---|
| 78 | button(text: "Cancel", icon: ImageProvider.get("cancel"), actionPerformed: {setVisible(false)})
|
|---|
| 79 | button(text: "Apply", icon: ImageProvider.get("ok"), actionPerformed: {
|
|---|
| 80 | apply()
|
|---|
| 81 | setVisible(false)
|
|---|
| 82 | })
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | def apply() {
|
|---|
| 87 | def start
|
|---|
| 88 | try {
|
|---|
| 89 | start = tfStart.text.trim().toInteger()
|
|---|
| 90 | } catch(NumberFormatException e){
|
|---|
| 91 | e.printStackTrace()
|
|---|
| 92 | return
|
|---|
| 93 | }
|
|---|
| 94 | def incr
|
|---|
| 95 | try {
|
|---|
| 96 | incr = tfIncrement.text.trim().toInteger()
|
|---|
| 97 | } catch(NumberFormatException e){
|
|---|
| 98 | e.printStackTrace()
|
|---|
| 99 | return
|
|---|
| 100 | }
|
|---|
| 101 | def nodes = getCurrentlySelectedNodes()
|
|---|
| 102 | def cmds = []
|
|---|
| 103 | nodes.each {Node n ->
|
|---|
| 104 | Node nn = new Node(n)
|
|---|
| 105 | nn.put("addr:housenumber", start.toString())
|
|---|
| 106 | start += incr
|
|---|
| 107 | cmds << new ChangeCommand(n, nn)
|
|---|
| 108 | }
|
|---|
| 109 | if (cmds.isEmpty()) return
|
|---|
| 110 | Main.main.undoRedo.add(new SequenceCommand("Setting house numbers", cmds))
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | def build() {
|
|---|
| 114 | setTitle("Set house numbers")
|
|---|
| 115 | def cp = getContentPane()
|
|---|
| 116 | cp.setLayout(new BorderLayout())
|
|---|
| 117 | cp.add(buildInfoPanel(), BorderLayout.NORTH)
|
|---|
| 118 | cp.add(buildInputPanel(), BorderLayout.CENTER)
|
|---|
| 119 | cp.add(buildControlButtonPanel(), BorderLayout.SOUTH)
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | def getCurrentDataSet() {
|
|---|
| 123 | def layer = Main?.map?.mapView?.getActiveLayer()
|
|---|
| 124 | if (layer == null) return null
|
|---|
| 125 | if (! (layer instanceof OsmDataLayer)) return null
|
|---|
| 126 | return layer.data
|
|---|
| 127 | }
|
|---|
| 128 |
|
|---|
| 129 | def getCurrentlySelectedNodes() {
|
|---|
| 130 | def DataSet ds = getCurrentDataSet()
|
|---|
| 131 | if (ds == null) return []
|
|---|
| 132 | return ds.getSelectedNodes().asList()
|
|---|
| 133 | }
|
|---|
| 134 |
|
|---|
| 135 | @Override
|
|---|
| 136 | public void setVisible(boolean b) {
|
|---|
| 137 | if (b){
|
|---|
| 138 | WindowGeometry.centerInWindow(getParent(), new Dimension(400,200)).applySafe(this)
|
|---|
| 139 | }
|
|---|
| 140 | super.setVisible(b);
|
|---|
| 141 | }
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | def dialog = new AddHouseNumberDialog()
|
|---|
| 145 | dialog.setVisible(true)
|
|---|