From e40fe058dd0b56cee73d1944cde5baf9c06d696c Mon Sep 17 00:00:00 2001 From: OMGeeky Date: Sun, 22 Jun 2025 18:57:37 +0200 Subject: [PATCH] fix oversight, where changes would call the wrong event --- .../atcontentstudio/ui/ListenerListModel.java | 25 ++++++++++++++++--- .../atcontentstudio/ui/NotificationsPane.java | 4 +-- .../ui/OrderedListenerListModel.java | 10 ++++---- .../atcontentstudio/ui/map/TMXMapEditor.java | 6 ++--- 4 files changed, 31 insertions(+), 14 deletions(-) diff --git a/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java b/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java index 7baf9b6..a90ad3a 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java @@ -8,13 +8,24 @@ import java.util.List; public interface ListenerListModel extends ListModel { List getListeners(); - default void notifyListeners(int event, int index0, int index1) { + default void notifyListeners(ChangeType event, int index0, int index1) { notifyListeners(this, event, index0, index1); } - default void notifyListeners(Object source, int event, int index0, int index1) { + default void notifyListeners(Object source, ChangeType event, int index0, int index1) { + int eventCode = switch (event) { + case CHANGED -> ListDataEvent.CONTENTS_CHANGED; + case ADDED -> ListDataEvent.INTERVAL_ADDED; + case REMOVED -> ListDataEvent.INTERVAL_REMOVED; + }; + for (ListDataListener l : getListeners()) { - l.intervalRemoved(new ListDataEvent(source, event, index0, index1)); + ListDataEvent e = new ListDataEvent(source, eventCode, index0, index1); + switch (event) { + case CHANGED -> l.contentsChanged(e); + case ADDED -> l.intervalAdded(e); + case REMOVED -> l.intervalRemoved(e); + } } } @@ -27,6 +38,12 @@ public interface ListenerListModel extends ListModel { } default void fireListChanged() { - notifyListeners(ListDataEvent.CONTENTS_CHANGED, 0, getSize() - 1); + notifyListeners(this, ChangeType.CHANGED, 0, getSize() - 1); + } + + enum ChangeType { + CHANGED, + ADDED, + REMOVED, } } diff --git a/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java b/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java index e995d3c..3606b76 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java @@ -74,13 +74,13 @@ public class NotificationsPane extends JList { @Override public void onNewNotification(Notification n) { - notifyListeners(NotificationsPane.this, ListDataEvent.INTERVAL_ADDED, Notification.notifs.size() - 1, Notification.notifs.size() - 1); + notifyListeners(NotificationsPane.this, ChangeType.ADDED, Notification.notifs.size() - 1, Notification.notifs.size() - 1); NotificationsPane.this.ensureIndexIsVisible(Notification.notifs.indexOf(n)); } @Override public void onListCleared(int i) { - notifyListeners(NotificationsPane.this, ListDataEvent.INTERVAL_REMOVED, 0, i); + notifyListeners(NotificationsPane.this, ChangeType.REMOVED, 0, i); } private List listeners = new CopyOnWriteArrayList(); diff --git a/src/com/gpl/rpg/atcontentstudio/ui/OrderedListenerListModel.java b/src/com/gpl/rpg/atcontentstudio/ui/OrderedListenerListModel.java index 5e7a0d2..65c8e3e 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/OrderedListenerListModel.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/OrderedListenerListModel.java @@ -44,7 +44,7 @@ public abstract class OrderedListenerListModel implements ListenerCollecti } getItems().add(item); int index = getItems().indexOf(item); - notifyListeners(ListDataEvent.INTERVAL_ADDED, index, index); + notifyListeners(ChangeType.ADDED, index, index); } public void removeObject(E item) { @@ -57,7 +57,7 @@ public abstract class OrderedListenerListModel implements ListenerCollecti if (getSize() == 0) { setItems(null); } - notifyListeners(ListDataEvent.INTERVAL_REMOVED, index, index); + notifyListeners(this, ChangeType.REMOVED, index, index); } @@ -74,7 +74,7 @@ public abstract class OrderedListenerListModel implements ListenerCollecti E exchanged = getElementAt(index + direction); setElementAt(index, exchanged); setElementAt(index + direction, item); - notifyListeners(ListDataEvent.CONTENTS_CHANGED, index + direction, index); + notifyListeners(this, ChangeType.CHANGED, index + direction, index); } public void objectChanged(E item) { @@ -83,10 +83,10 @@ public abstract class OrderedListenerListModel implements ListenerCollecti public void itemChanged(E item) { int index = getItems().indexOf(item); - notifyListeners(ListDataEvent.CONTENTS_CHANGED, index, index); + notifyListeners(this, ChangeType.CHANGED, index, index); } - private List listeners = new CopyOnWriteArrayList(); + private final List listeners = new CopyOnWriteArrayList(); public List getListeners() { return listeners; diff --git a/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java b/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java index a774e5b..825aaf6 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/map/TMXMapEditor.java @@ -1020,20 +1020,20 @@ public class TMXMapEditor extends Editor implements TMXMap.MapChangedOnDiskListe public void objectChanged(tiled.core.MapLayer layer) { int index = map.tmxMap.getLayerIndex(layer); - notifyListeners(ListDataEvent.CONTENTS_CHANGED, index, index); + notifyListeners(ChangeType.CHANGED, index, index); } public void addObject(tiled.core.MapLayer layer) { map.addLayer(layer); int index = map.tmxMap.getLayerIndex(layer); - notifyListeners(ListDataEvent.INTERVAL_ADDED, index, index); + notifyListeners(ChangeType.ADDED, index, index); } public void removeObject(tiled.core.MapLayer layer) { int index = map.tmxMap.getLayerIndex(layer); map.removeLayer(layer); - notifyListeners(ListDataEvent.INTERVAL_REMOVED, index, index); + notifyListeners(ChangeType.REMOVED, index, index); } List listeners = new CopyOnWriteArrayList();