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