Ticket #6535: patch_hook.txt

File patch_hook.txt, 6.1 KB (added by Don-vip, 14 years ago)

A much nicer patch !

Line 
1Index: src/org/openstreetmap/josm/gui/DefaultNameFormatter.java
2===================================================================
3--- src/org/openstreetmap/josm/gui/DefaultNameFormatter.java (revision 4200)
4+++ src/org/openstreetmap/josm/gui/DefaultNameFormatter.java (working copy)
5@@ -11,6 +11,7 @@
6 import java.util.Collections;
7 import java.util.Comparator;
8 import java.util.HashSet;
9+import java.util.LinkedList;
10 import java.util.List;
11 import java.util.Set;
12
13@@ -42,6 +43,8 @@
14
15 static private DefaultNameFormatter instance;
16
17+ private static final LinkedList<NameFormatterHook> formatHooks = new LinkedList<NameFormatterHook>();
18+
19 /**
20 * Replies the unique instance of this formatter
21 *
22@@ -53,6 +56,30 @@
23 }
24 return instance;
25 }
26+
27+ /**
28+ * Registers a format hook. Adds the hook at the first position of the format hooks.
29+ *
30+ * @param hook the format hook. Ignored if null.
31+ */
32+ public static void registerFormatHook(NameFormatterHook hook) {
33+ if (hook == null) return;
34+ if (!formatHooks.contains(hook)) {
35+ formatHooks.add(0,hook);
36+ }
37+ }
38+
39+ /**
40+ * Unregisters a format hook. Removes the hook from the list of format hooks.
41+ *
42+ * @param hook the format hook. Ignored if null.
43+ */
44+ public static void unregisterFormatHook(NameFormatterHook hook) {
45+ if (hook == null) return;
46+ if (formatHooks.contains(hook)) {
47+ formatHooks.remove(hook);
48+ }
49+ }
50
51 /** the default list of tags which are used as naming tags in relations */
52 static public final String[] DEFAULT_NAMING_TAGS_FOR_RELATIONS = {"name", "ref", "restriction", "landuse", "natural",
53@@ -140,6 +167,14 @@
54 name += " (" + node.getCoor().latToString(CoordinateFormat.getDefaultFormat()) + ", " + node.getCoor().lonToString(CoordinateFormat.getDefaultFormat()) + ")";
55 }
56 name = decorateNameWithId(name, node);
57+
58+ for (NameFormatterHook hook: formatHooks) {
59+ String hookResult = hook.checkFormat(node, name);
60+ if (hookResult != null && !hookResult.isEmpty()) {
61+ return hookResult;
62+ }
63+ }
64+
65 return name;
66 }
67
68@@ -216,6 +251,14 @@
69 name += (name.length() > 0) ? " ("+nodes+")" : nodes;
70 }
71 name = decorateNameWithId(name, way);
72+
73+ for (NameFormatterHook hook: formatHooks) {
74+ String hookResult = hook.checkFormat(way, name);
75+ if (hookResult != null && !hookResult.isEmpty()) {
76+ return hookResult;
77+ }
78+ }
79+
80 return name;
81 }
82
83@@ -263,6 +306,14 @@
84 name += ")";
85 }
86 name = decorateNameWithId(name, relation);
87+
88+ for (NameFormatterHook hook: formatHooks) {
89+ String hookResult = hook.checkFormat(relation, name);
90+ if (hookResult != null && !hookResult.isEmpty()) {
91+ return hookResult;
92+ }
93+ }
94+
95 return name;
96 }
97
98@@ -355,6 +406,13 @@
99 if (admin_level != null) {
100 name += "["+admin_level+"]";
101 }
102+
103+ for (NameFormatterHook hook: formatHooks) {
104+ String hookResult = hook.checkRelationTypeName(relation, name);
105+ if (hookResult != null && !hookResult.isEmpty()) {
106+ return hookResult;
107+ }
108+ }
109
110 return name;
111 }
112Index: src/org/openstreetmap/josm/gui/NameFormatterHook.java
113===================================================================
114--- src/org/openstreetmap/josm/gui/NameFormatterHook.java (revision 0)
115+++ src/org/openstreetmap/josm/gui/NameFormatterHook.java (revision 0)
116@@ -0,0 +1,41 @@
117+// License: GPL. For details, see LICENSE file.
118+package org.openstreetmap.josm.gui;
119+
120+import org.openstreetmap.josm.data.osm.INode;
121+import org.openstreetmap.josm.data.osm.IRelation;
122+import org.openstreetmap.josm.data.osm.IWay;
123+
124+public interface NameFormatterHook {
125+
126+ /**
127+ * Check the relation type name. Return the corrected type name if needed, null otherwise.
128+ * @param relation The relation.
129+ * @param defaultName The default name generated by core.
130+ * @return The corrected type name if needed, null otherwise.
131+ */
132+ public String checkRelationTypeName(IRelation relation, String defaultName);
133+
134+ /**
135+ * Check the node format. Return the corrected format if needed, null otherwise.
136+ * @param node The node.
137+ * @param defaultName The default name generated by core.
138+ * @return The corrected format if needed, null otherwise.
139+ */
140+ public String checkFormat(INode node, String defaultName);
141+
142+ /**
143+ * Check the way format. Return the corrected format if needed, null otherwise.
144+ * @param way The way.
145+ * @param defaultName The default name generated by core.
146+ * @return The corrected format if needed, null otherwise.
147+ */
148+ public String checkFormat(IWay node, String defaultName);
149+
150+ /**
151+ * Check the relation format. Return the corrected format if needed, null otherwise.
152+ * @param relation The relation.
153+ * @param defaultName The default name generated by core.
154+ * @return The corrected format if needed, null otherwise.
155+ */
156+ public String checkFormat(IRelation node, String defaultName);
157+}
158Index: src/org/openstreetmap/josm/gui/OsmPrimitivRenderer.java
159===================================================================
160--- src/org/openstreetmap/josm/gui/OsmPrimitivRenderer.java (revision 4200)
161+++ src/org/openstreetmap/josm/gui/OsmPrimitivRenderer.java (working copy)
162@@ -24,7 +24,7 @@
163 * @author Frederik Ramm <frederik@remote.org>
164 */
165 public class OsmPrimitivRenderer implements ListCellRenderer, TableCellRenderer {
166- private DefaultNameFormatter formatter = new DefaultNameFormatter();
167+ private DefaultNameFormatter formatter = DefaultNameFormatter.getInstance();
168
169 /**
170 * Default list cell renderer - delegate for ListCellRenderer operation