From f710db6fa49c6d83c09b6bd7cb4a9169a41b711a Mon Sep 17 00:00:00 2001 From: Taylor Smock Date: Wed, 6 Nov 2024 07:30:59 -0700 Subject: [PATCH] Add potential issues to TODO list, if the TODO plugin is installed Signed-off-by: Taylor Smock --- .../highwaynamemodification/ModifyWays.java | 30 +++++++--- .../highwaynamemodification/TodoHelper.java | 59 +++++++++++++++++++ 2 files changed, 80 insertions(+), 9 deletions(-) create mode 100644 src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/TodoHelper.java diff --git a/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/ModifyWays.java b/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/ModifyWays.java index 573fdc8..1d98819 100644 --- a/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/ModifyWays.java +++ b/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/ModifyWays.java @@ -22,11 +22,15 @@ import org.openstreetmap.josm.data.osm.OsmPrimitive; import org.openstreetmap.josm.data.osm.Way; import org.openstreetmap.josm.data.osm.WaySegment; +import org.openstreetmap.josm.data.osm.search.SearchCompiler; +import org.openstreetmap.josm.data.osm.search.SearchParseError; import org.openstreetmap.josm.gui.ConditionalOptionPaneUtil; import org.openstreetmap.josm.gui.MainApplication; import org.openstreetmap.josm.gui.util.GuiHelper; import org.openstreetmap.josm.tools.Geometry; +import org.openstreetmap.josm.tools.JosmRuntimeException; import org.openstreetmap.josm.tools.Logging; +import org.openstreetmap.josm.tools.SubclassFilteredCollection; /** * The actual class for modifying ways @@ -127,7 +131,7 @@ public void run() { newWays = DownloadAdditionalWays.getAdditionalWays(wayChangingName, originalName); } } - newWays = newWays.thenApplyAsync(ignored -> { + newWays.thenApplyAsync(ignored -> { for (OsmPrimitive osm : wayChangingName) { if (originalName != null) { doRealRun(osm, originalName); @@ -140,17 +144,25 @@ public void run() { } } return ignored; - }); - if (Boolean.TRUE.equals(this.recursive)) { - newWays.thenApplyAsync(primitives -> { - List toChange = primitives.stream().filter(p -> p.hasTag("name", this.originalName)) - .collect(Collectors.toList()); + }).thenApplyAsync(primitives -> { + List toChange = primitives.stream().filter(p -> p.hasTag("name", this.originalName)) + .collect(Collectors.toList()); + if (Boolean.TRUE.equals(this.recursive) && !toChange.isEmpty()) { final ChangePropertyCommand changePropertyCommand = new ChangePropertyCommand(toChange, "name", wayChangingName.iterator().next().get("name")); GuiHelper.runInEDT(() -> UndoRedoHandler.getInstance().add(changePropertyCommand)); - return primitives; - }); - } + } else if (toChange.isEmpty() || !Boolean.TRUE.equals(this.recursive)) { + try { + final DataSet ds = this.wayChangingName.iterator().next().getDataSet(); + ds.setSelected(SubclassFilteredCollection.filter(ds.allPrimitives(), + SearchCompiler.compile(this.originalName))); + TodoHelper.addTodoItems(); + } catch (SearchParseError searchParseError) { + throw new JosmRuntimeException(searchParseError); + } + } + return primitives; + }); } catch (InterruptedException e) { Thread.currentThread().interrupt(); Logging.error(e); diff --git a/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/TodoHelper.java b/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/TodoHelper.java new file mode 100644 index 0000000..8a12ed1 --- /dev/null +++ b/src/main/java/org/openstreetmap/josm/plugins/highwaynamemodification/TodoHelper.java @@ -0,0 +1,59 @@ +// License: GPL. For details, see LICENSE file. +package org.openstreetmap.josm.plugins.highwaynamemodification; + +import java.lang.reflect.Field; + +import javax.swing.AbstractListModel; + +import org.openstreetmap.josm.actions.JosmAction; +import org.openstreetmap.josm.gui.MainApplication; +import org.openstreetmap.josm.gui.dialogs.ToggleDialog; +import org.openstreetmap.josm.plugins.PluginException; +import org.openstreetmap.josm.plugins.PluginInformation; +import org.openstreetmap.josm.tools.Logging; +import org.openstreetmap.josm.tools.ReflectionUtils; + +/** + * Helper for the TODO plugin (so we don't have to depend upon it) + */ +public final class TodoHelper { + private static final Class CLASS; + private static final Field ACT_ADD; + private static final Field MODEL; + static { + Class clazz = null; + Field actAdd = null; + Field model = null; + try { + final PluginInformation info = PluginInformation.findPlugin("todo"); + if (info != null) { + clazz = (Class) Class.forName("org.openstreetmap.josm.plugins.todo.TodoDialog", + false, info.getClass().getClassLoader()); + actAdd = clazz.getDeclaredField("actAdd"); + model = clazz.getDeclaredField("model"); + ReflectionUtils.setObjectsAccessible(actAdd, model); + } + } catch (ClassNotFoundException | PluginException | NoSuchFieldException classNotFoundException) { + Logging.trace(classNotFoundException); + } + ACT_ADD = actAdd; + MODEL = model; + CLASS = clazz; + } + + public static void addTodoItems() { + if (CLASS == null || MODEL == null || ACT_ADD == null) { + return; + } + final ToggleDialog todoDialog = MainApplication.getMap().getToggleDialog(CLASS); + try { + AbstractListModel m = (AbstractListModel) MODEL.get(todoDialog); + if (m.getSize() == 0) { + JosmAction a = (JosmAction) ACT_ADD.get(todoDialog); + a.actionPerformed(null); + } + } catch (ReflectiveOperationException e) { + Logging.error(e); + } + } +}