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