diff --git a/src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java b/src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java index 381e8ca..db403c7 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java @@ -1,13 +1,12 @@ package com.gpl.rpg.atcontentstudio.ui; -import javax.swing.*; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; import java.util.ArrayList; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; -public abstract class CustomListModel implements ListModel { +public abstract class CustomListModel implements ListenerListModel { protected S source; protected abstract List getItems(); @@ -17,10 +16,8 @@ public abstract class CustomListModel implements ListModel { this.source = source; } - protected void notifyListeners(int event, int index0, int index1) { - for (ListDataListener l : listeners) { - l.intervalRemoved(new ListDataEvent(this, event, index0, index1)); - } + public List getListeners() { + return listeners; } @@ -80,14 +77,4 @@ public abstract class CustomListModel implements ListModel { } private List listeners = new CopyOnWriteArrayList(); - - @Override - public void addListDataListener(ListDataListener l) { - listeners.add(l); - } - - @Override - public void removeListDataListener(ListDataListener l) { - listeners.remove(l); - } } diff --git a/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java b/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java new file mode 100644 index 0000000..7a6f5f6 --- /dev/null +++ b/src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java @@ -0,0 +1,27 @@ +package com.gpl.rpg.atcontentstudio.ui; + +import javax.swing.*; +import javax.swing.event.ListDataEvent; +import javax.swing.event.ListDataListener; +import java.util.List; + +public interface ListenerListModel extends ListModel { + List getListeners(); + + default void notifyListeners(int event, int index0, int index1) { + notifyListeners(this, event, index0, index1); + } + default void notifyListeners(Object source, int event, int index0, int index1) { + for (ListDataListener l : getListeners()) { + l.intervalRemoved(new ListDataEvent(source, event, index0, index1)); + } + } + + default void addListDataListener(ListDataListener l) { + getListeners().add(l); + } + + default void removeListDataListener(ListDataListener l) { + getListeners().remove(l); + } +} diff --git a/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java b/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java index 80436a5..c4194e3 100644 --- a/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java +++ b/src/com/gpl/rpg/atcontentstudio/ui/NotificationsPane.java @@ -14,7 +14,6 @@ import javax.swing.ImageIcon; import javax.swing.JLabel; import javax.swing.JList; import javax.swing.ListCellRenderer; -import javax.swing.ListModel; import javax.swing.event.ListDataEvent; import javax.swing.event.ListDataListener; @@ -62,15 +61,20 @@ public class NotificationsPane extends JList { }); Notification.addNotificationListener(model); } - - - private class MyListModel implements ListModel, NotificationListener { + + + private class MyListModel implements ListenerListModel, NotificationListener { @Override - public Object getElementAt(int index) { + public Notification getElementAt(int index) { return Notification.notifs.get(index); } - + + @Override + public List getListeners() { + return listeners; + } + @Override public int getSize() { return Notification.notifs.size(); @@ -78,28 +82,15 @@ public class NotificationsPane extends JList { @Override public void onNewNotification(Notification n) { - for (ListDataListener l : listeners) { - l.intervalAdded(new ListDataEvent(NotificationsPane.this, ListDataEvent.INTERVAL_ADDED, Notification.notifs.size() - 1 , Notification.notifs.size() - 1)); - } + notifyListeners(NotificationsPane.this, ListDataEvent.INTERVAL_ADDED, Notification.notifs.size() - 1 , Notification.notifs.size() - 1); NotificationsPane.this.ensureIndexIsVisible(Notification.notifs.indexOf(n)); } @Override public void onListCleared(int i) { - for (ListDataListener l : listeners) { - l.intervalRemoved(new ListDataEvent(NotificationsPane.this, ListDataEvent.INTERVAL_REMOVED, 0 , i)); - } + notifyListeners(NotificationsPane.this, ListDataEvent.INTERVAL_REMOVED, 0 , i); } private List listeners = new CopyOnWriteArrayList(); - @Override - public void addListDataListener(ListDataListener l) { - listeners.add(l); - } - @Override - public void removeListDataListener(ListDataListener l) { - listeners.remove(l); - } - } } \ No newline at end of file