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

Last change on this file since 25374 was 25110, checked in by guggis, 14 years ago

Fixed help context

File size: 5.4 KB
Line 
1/*
2 * This scripts sets a sequence of consecutive house numbers on the currently selected nodes.
3 *
4 */
5import java.awt.event.WindowAdapter;
6
7
8import java.awt.BorderLayout;
9import javax.swing.JComponent;
10
11import java.awt.event.KeyEvent;
12import java.awt.event.WindowAdapter;
13import java.awt.event.WindowListener;
14
15import javax.swing.KeyStroke;
16
17import groovy.swing.SwingBuilder;
18import groovy.util.ProxyGenerator;
19
20import javax.swing.JOptionPane;
21import org.openstreetmap.josm.Main;
22import org.openstreetmap.josm.command.ChangeCommand;
23import org.openstreetmap.josm.command.SequenceCommand;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.gui.HelpAwareOptionPane;
26import org.openstreetmap.josm.gui.layer.Layer;
27import org.openstreetmap.josm.gui.layer.OsmDataLayer;
28import org.openstreetmap.josm.gui.widgets.HtmlPanel;
29import org.openstreetmap.josm.gui.widgets.SelectAllOnFocusGainedDecorator;
30import org.openstreetmap.josm.tools.ImageProvider;
31import org.openstreetmap.josm.tools.WindowGeometry;
32import org.openstreetmap.josm.data.osm.DataSet
33import org.openstreetmap.josm.data.osm.Node;
34import javax.swing.JDialog;
35import javax.swing.JPanel;
36import java.awt.BorderLayout;
37import java.awt.FlowLayout;
38import java.awt.GridBagConstraints;
39import java.awt.GridBagLayout;
40import javax.swing.JLabel;
41import java.awt.event.FocusListener;
42
43import javax.swing.Action;
44import javax.swing.BorderFactory;
45import javax.swing.JTextField;
46import javax.swing.AbstractAction;
47import javax.swing.JButton;
48import java.awt.event.ActionListener;
49import java.awt.Dimension;
50import java.awt.Dialog.ModalityType;
51import java.awt.event.WindowEvent;
52
53class 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
156AddHouseNumberDialog.instance.setVisible(true)
157
Note: See TracBrowser for help on using the repository browser.