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
RevLine 
[25019]1/*
[25110]2 * This scripts sets a sequence of consecutive house numbers on the currently selected nodes.
3 *
[25019]4 */
[25110]5import java.awt.event.WindowAdapter;
[25019]6
[25110]7
[25019]8import java.awt.BorderLayout;
[25107]9import javax.swing.JComponent;
[25019]10
[25107]11import java.awt.event.KeyEvent;
[25110]12import java.awt.event.WindowAdapter;
13import java.awt.event.WindowListener;
[25107]14
15import javax.swing.KeyStroke;
16
[25019]17import groovy.swing.SwingBuilder;
[25110]18import groovy.util.ProxyGenerator;
19
[25019]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;
[25110]41import java.awt.event.FocusListener;
[25019]42
[25107]43import javax.swing.Action;
[25019]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;
[25110]50import java.awt.Dialog.ModalityType;
51import java.awt.event.WindowEvent;
[25019]52
53class AddHouseNumberDialog extends JDialog {
54
[25107]55 static private AddHouseNumberDialog instance;
56 static def AddHouseNumberDialog getInstance() {
57 if (instance == null){
58 instance = new AddHouseNumberDialog()
59 }
60 return instance
61 }
62
[25019]63 private JTextField tfStart;
64 private JTextField tfIncrement;
[25110]65 private def actApply;
[25019]66
67 public AddHouseNumberDialog(){
[25110]68 super(Main.parent,true)
[25019]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
[25107]77 and the <strong>increment</strong> between consecutive house numbers.
[25019]78 </html>
79 """
80 )
81 }
82
83 def buildInputPanel() {
84 SwingBuilder swing = new SwingBuilder()
85 return swing.panel(){
[25107]86 emptyBorder([5,5,5,5],parent:true)
[25019]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]))
[25107]93 SelectAllOnFocusGainedDecorator.decorate(tfStart)
[25019]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]))
[25107]96 SelectAllOnFocusGainedDecorator.decorate(tfIncrement)
[25019]97 panel(constraints: gbc(gridx:0,gridy:2,weightx:1.0, weighty:1.0, gridwidth:2, fill: GridBagConstraints.BOTH, insets:[2,2,2,2]))
[25107]98 tfIncrement.text = "2"
[25019]99 }
100 }
101
102 def buildControlButtonPanel() {
103 SwingBuilder swing = new SwingBuilder()
104 return swing.panel(layout: new FlowLayout(FlowLayout.CENTER)) {
[25110]105 actApply = action(name: "Apply", smallIcon: ImageProvider.get("ok"), closure: {apply(); setVisible(false)})
[25107]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
[25019]110 button(text: "Cancel", icon: ImageProvider.get("cancel"), actionPerformed: {setVisible(false)})
111 }
112 }
[25107]113
[25019]114 def apply() {
115 def start
[25107]116 def incr
[25019]117 try {
118 start = tfStart.text.trim().toInteger()
[25107]119 incr = tfIncrement.text.trim().toInteger()
[25019]120 } catch(NumberFormatException e){
121 e.printStackTrace()
122 return
123 }
[25110]124 def nodes = Main?.map?.mapView?.editLayer?.data?.getSelectedNodes()?.asList()
125 if (nodes == null || nodes.isEmpty()) return
126 def cmds = nodes.collect { Node n ->
[25019]127 Node nn = new Node(n)
128 nn.put("addr:housenumber", start.toString())
129 start += incr
[25107]130 return new ChangeCommand(n, nn)
[25019]131 }
132 Main.main.undoRedo.add(new SequenceCommand("Setting house numbers", cmds))
133 }
134
135 def build() {
[25107]136 title = "Set house numbers"
[25019]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)
[25110]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 }
[25019]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
[25107]156AddHouseNumberDialog.instance.setVisible(true)
157
Note: See TracBrowser for help on using the repository browser.