source: osm/applications/editors/josm/plugins/scripting/scripts/AddHouseNumbers.groovy@ 25072

Last change on this file since 25072 was 25019, checked in by guggis, 15 years ago
File size: 4.6 KB
Line 
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
7import java.awt.BorderLayout;
8
9import groovy.swing.SwingBuilder;
10import javax.swing.JOptionPane;
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.command.ChangeCommand;
13import org.openstreetmap.josm.command.SequenceCommand;
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.gui.HelpAwareOptionPane;
16import org.openstreetmap.josm.gui.layer.Layer;
17import org.openstreetmap.josm.gui.layer.OsmDataLayer;
18import org.openstreetmap.josm.gui.widgets.HtmlPanel;
19import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
20import org.openstreetmap.josm.tools.ImageProvider;
21import org.openstreetmap.josm.tools.WindowGeometry;
22import org.openstreetmap.josm.data.osm.DataSet
23import org.openstreetmap.josm.data.osm.Node;
24import javax.swing.JDialog;
25import javax.swing.JPanel;
26import java.awt.BorderLayout;
27import java.awt.FlowLayout;
28import java.awt.GridBagConstraints;
29import java.awt.GridBagLayout;
30import javax.swing.JLabel;
31
32import javax.swing.BorderFactory;
33import javax.swing.JTextField;
34import javax.swing.AbstractAction;
35import javax.swing.JButton;
36import java.awt.event.ActionListener;
37import java.awt.Dimension;
38
39class 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
144def dialog = new AddHouseNumberDialog()
145dialog.setVisible(true)
Note: See TracBrowser for help on using the repository browser.