mirror of
https://github.com/OMGeeky/ATCS.git
synced 2025-12-26 23:57:25 +01:00
extract some code into ListenerListModel interface
This commit is contained in:
@@ -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<S, E> implements ListModel<E> {
|
||||
public abstract class CustomListModel<S, E> implements ListenerListModel<E> {
|
||||
protected S source;
|
||||
|
||||
protected abstract List<E> getItems();
|
||||
@@ -17,10 +16,8 @@ public abstract class CustomListModel<S, E> implements ListModel<E> {
|
||||
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<ListDataListener> getListeners() {
|
||||
return listeners;
|
||||
}
|
||||
|
||||
|
||||
@@ -80,14 +77,4 @@ public abstract class CustomListModel<S, E> implements ListModel<E> {
|
||||
}
|
||||
|
||||
private List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
}
|
||||
}
|
||||
|
||||
27
src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java
Normal file
27
src/com/gpl/rpg/atcontentstudio/ui/ListenerListModel.java
Normal file
@@ -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<E> extends ListModel<E> {
|
||||
List<ListDataListener> 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);
|
||||
}
|
||||
}
|
||||
@@ -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<Notification>, NotificationListener {
|
||||
|
||||
@Override
|
||||
public Object getElementAt(int index) {
|
||||
public Notification getElementAt(int index) {
|
||||
return Notification.notifs.get(index);
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public List<ListDataListener> 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<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user