1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.plugins.fixAddresses;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.Collection;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
11 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.event.SelectionEventManager;
|
---|
14 | import org.openstreetmap.josm.plugins.fixAddresses.gui.AddressEditDialog;
|
---|
15 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
16 |
|
---|
17 | /**
|
---|
18 | * Action to find and fix addresses without (valid) streets. It launches an dialog
|
---|
19 | * instance of {@link AddressEditDialog}.
|
---|
20 | *
|
---|
21 | * @author Oliver Wieland <oliver.wieland@online.de>
|
---|
22 | */
|
---|
23 | public class FixUnresolvedStreetsAction extends JosmAction implements DataSelectionListener {
|
---|
24 | private AddressEditContainer addressEditContainer;
|
---|
25 | private Collection<? extends OsmPrimitive> newSelection;
|
---|
26 |
|
---|
27 | public FixUnresolvedStreetsAction() {
|
---|
28 | super(tr("Fix street addresses"), "fixstreets_24",
|
---|
29 | tr("Find and fix addresses without (valid) streets."),
|
---|
30 | Shortcut.registerShortcut("tools:AddressEdit", tr("Data: {0}",
|
---|
31 | tr("Fix street addresses")), KeyEvent.VK_X, Shortcut.CTRL_SHIFT), false);
|
---|
32 |
|
---|
33 | setEnabled(false);
|
---|
34 | addressEditContainer = new AddressEditContainer();
|
---|
35 | SelectionEventManager.getInstance().addSelectionListener(this);
|
---|
36 | }
|
---|
37 |
|
---|
38 | @Override
|
---|
39 | public void selectionChanged(SelectionChangeEvent event) {
|
---|
40 | /* remember new selection for actionPerformed */
|
---|
41 | this.newSelection = event.getSelection();
|
---|
42 | }
|
---|
43 |
|
---|
44 | @Override
|
---|
45 | public void actionPerformed(ActionEvent e) {
|
---|
46 | if (addressEditContainer != null) {
|
---|
47 | addressEditContainer.attachToDataSet(newSelection);
|
---|
48 | try {
|
---|
49 | //generateTagCode(addressEditContainer);
|
---|
50 | AddressEditDialog dlg = new AddressEditDialog(addressEditContainer);
|
---|
51 | dlg.setVisible(true);
|
---|
52 | } finally {
|
---|
53 | addressEditContainer.detachFromDataSet();
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
57 |
|
---|
58 | @Override
|
---|
59 | protected void updateEnabledState() {
|
---|
60 | setEnabled(getLayerManager().getEditDataSet() != null);
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | protected void updateEnabledState(
|
---|
65 | Collection<? extends OsmPrimitive> selection) {
|
---|
66 | // Enable button if there is either a selection or at least a data set
|
---|
67 | setEnabled((selection != null && !selection.isEmpty()) || getLayerManager().getEditDataSet() != null);
|
---|
68 | }
|
---|
69 |
|
---|
70 | /* This code is abused to generate tag utility code */
|
---|
71 |
|
---|
72 | @SuppressWarnings("unused")
|
---|
73 | private void generateTagCode(AddressEditContainer addrVisitor) {
|
---|
74 |
|
---|
75 | for (String tag : addrVisitor.getTags()) {
|
---|
76 | String methodName = createMethodName(tag);
|
---|
77 | // CHECKSTYLE.OFF: LineLength
|
---|
78 | System.out.println(String.format(
|
---|
79 | "/** Check if OSM primitive has a tag '%s'.\n * @param osmPrimitive The OSM entity to check.*/\npublic static boolean has%sTag(OsmPrimitive osmPrimitive) {\n return osmPrimitive != null ? osmPrimitive.hasKey(%s_TAG) : false;\n}\n",
|
---|
80 | tag, methodName, tag.toUpperCase()
|
---|
81 | .replaceAll(":", "_")));
|
---|
82 | System.out.println(String.format(
|
---|
83 | "/** Gets the value of tag '%s'.\n * @param osmPrimitive The OSM entity to check.*/\npublic static String get%sValue(OsmPrimitive osmPrimitive) {\n return osmPrimitive != null ? osmPrimitive.get(%s_TAG) : null;\n}\n",
|
---|
84 | tag, methodName, tag.toUpperCase()
|
---|
85 | .replaceAll(":", "_")));
|
---|
86 | // CHECKSTYLE.ON: LineLength
|
---|
87 | }
|
---|
88 |
|
---|
89 | for (String tag : addrVisitor.getTags()) {
|
---|
90 | System.out.println(String.format(
|
---|
91 | "public static final String %s_TAG = \"%s\";", tag
|
---|
92 | .toUpperCase().replaceAll(":", "_"), tag));
|
---|
93 | }
|
---|
94 |
|
---|
95 | for (String value : addrVisitor.getValues().keySet()) {
|
---|
96 | String tag = addrVisitor.getValues().get(value);
|
---|
97 |
|
---|
98 | String tags = tag.toUpperCase().replaceAll(":", "_");
|
---|
99 | String values = value.toUpperCase().replaceAll(":", "_");
|
---|
100 | System.out.println(String.format(
|
---|
101 | "public static final String %s_%s_VALUE = \"%s\";", tags, values, value));
|
---|
102 | }
|
---|
103 | }
|
---|
104 |
|
---|
105 | private String createMethodName(String osmName) {
|
---|
106 | StringBuffer result = new StringBuffer(osmName.length());
|
---|
107 | boolean nextUp = false;
|
---|
108 | for (int i = 0; i < osmName.length(); i++) {
|
---|
109 | char c = osmName.charAt(i);
|
---|
110 | if (i == 0) {
|
---|
111 | result.append(Character.toUpperCase(c));
|
---|
112 | continue;
|
---|
113 | }
|
---|
114 | if (c == '_' || c == ':') {
|
---|
115 | nextUp = true;
|
---|
116 | } else {
|
---|
117 | if (nextUp) {
|
---|
118 | result.append(Character.toUpperCase(c));
|
---|
119 | nextUp = false;
|
---|
120 | } else {
|
---|
121 | result.append(c);
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 |
|
---|
126 | return result.toString();
|
---|
127 | }
|
---|
128 | }
|
---|