mirror of
https://github.com/AndorsTrailRelease/ATCS.git
synced 2025-10-27 18:44:03 +01:00
extract into CustomListModel
This commit is contained in:
90
src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java
Normal file
90
src/com/gpl/rpg/atcontentstudio/ui/CustomListModel.java
Normal file
@@ -0,0 +1,90 @@
|
||||
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> {
|
||||
protected S source;
|
||||
|
||||
protected abstract List<E> getItems();
|
||||
protected abstract void setItems(List<E> items);
|
||||
|
||||
public CustomListModel(S source) {
|
||||
this.source = source;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
if (getItems() == null) return 0;
|
||||
return getItems().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public E getElementAt(int index) {
|
||||
if (getItems() == null) return null;
|
||||
return getItems().get(index);
|
||||
}
|
||||
|
||||
public void addItem(E item) {
|
||||
if (getItems() == null) {
|
||||
setItems(new ArrayList<E>());
|
||||
}
|
||||
getItems().add(item);
|
||||
int index = getItems().indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(E item) {
|
||||
int index = getItems().indexOf(item);
|
||||
getItems().remove(item);
|
||||
if (getItems().isEmpty()) {
|
||||
setItems(null);
|
||||
}
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void moveUp(E item) {
|
||||
moveUpOrDown(item, -1);
|
||||
}
|
||||
|
||||
public void moveDown(E item) {
|
||||
moveUpOrDown(item, 1);
|
||||
}
|
||||
|
||||
private void moveUpOrDown(E item, int direction) {
|
||||
int index = getItems().indexOf(item);
|
||||
E exchanged = getItems().get(index + direction);
|
||||
getItems().set(index, exchanged);
|
||||
getItems().set(index + direction, item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index + direction, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void itemChanged(E item) {
|
||||
int index = getItems().indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import java.awt.event.MouseAdapter;
|
||||
import java.awt.event.MouseEvent;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.swing.ButtonGroup;
|
||||
import javax.swing.DefaultListCellRenderer;
|
||||
@@ -28,10 +27,7 @@ import javax.swing.JScrollPane;
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.ListModel;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.event.ListDataEvent;
|
||||
import javax.swing.event.ListDataListener;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
@@ -52,6 +48,7 @@ import com.gpl.rpg.atcontentstudio.ui.BooleanBasedCheckBox;
|
||||
import com.gpl.rpg.atcontentstudio.ui.CollapsiblePanel;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
import com.gpl.rpg.atcontentstudio.ui.FieldUpdateListener;
|
||||
import com.gpl.rpg.atcontentstudio.ui.CustomListModel;
|
||||
import com.gpl.rpg.atcontentstudio.ui.OverlayIcon;
|
||||
import com.gpl.rpg.atcontentstudio.ui.gamedataeditors.dialoguetree.DialogueGraphView;
|
||||
import com.jidesoft.swing.JideBoxLayout;
|
||||
@@ -812,65 +809,19 @@ public class DialogueEditor extends JSONElementEditor {
|
||||
}
|
||||
|
||||
|
||||
public static class RewardsListModel implements ListModel<Dialogue.Reward> {
|
||||
|
||||
Dialogue source;
|
||||
|
||||
public static class RewardsListModel extends CustomListModel<Dialogue,Dialogue.Reward> {
|
||||
@Override
|
||||
protected List<Dialogue.Reward> getItems() {
|
||||
return source.rewards;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setItems(List<Dialogue.Reward> items) {
|
||||
source.rewards = items;
|
||||
}
|
||||
|
||||
public RewardsListModel(Dialogue dialogue) {
|
||||
this.source = dialogue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
if (source.rewards == null) return 0;
|
||||
return source.rewards.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialogue.Reward getElementAt(int index) {
|
||||
if (source.rewards == null) return null;
|
||||
return source.rewards.get(index);
|
||||
}
|
||||
|
||||
public void addItem(Dialogue.Reward item) {
|
||||
if (source.rewards == null) {
|
||||
source.rewards = new ArrayList<Dialogue.Reward>();
|
||||
}
|
||||
source.rewards.add(item);
|
||||
int index = source.rewards.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(Dialogue.Reward item) {
|
||||
int index = source.rewards.indexOf(item);
|
||||
source.rewards.remove(item);
|
||||
if (source.rewards.isEmpty()) {
|
||||
source.rewards = null;
|
||||
}
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void itemChanged(Dialogue.Reward item) {
|
||||
int index = source.rewards.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
super(dialogue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -981,87 +932,19 @@ public class DialogueEditor extends JSONElementEditor {
|
||||
}
|
||||
|
||||
|
||||
public static class RepliesListModel implements ListModel<Dialogue.Reply> {
|
||||
public static class RepliesListModel extends CustomListModel<Dialogue, Dialogue.Reply> {
|
||||
@Override
|
||||
protected List<Dialogue.Reply> getItems() {
|
||||
return source.replies;
|
||||
}
|
||||
|
||||
Dialogue source;
|
||||
@Override
|
||||
protected void setItems(List<Dialogue.Reply> items) {
|
||||
source.replies = items;
|
||||
}
|
||||
|
||||
public RepliesListModel(Dialogue dialogue) {
|
||||
this.source = dialogue;
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
if (source.replies == null) return 0;
|
||||
return source.replies.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Dialogue.Reply getElementAt(int index) {
|
||||
if (source.replies == null) return null;
|
||||
return source.replies.get(index);
|
||||
}
|
||||
|
||||
public void addItem(Dialogue.Reply item) {
|
||||
if (source.replies == null) {
|
||||
source.replies = new ArrayList<Dialogue.Reply>();
|
||||
}
|
||||
source.replies.add(item);
|
||||
int index = source.replies.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(Dialogue.Reply item) {
|
||||
int index = source.replies.indexOf(item);
|
||||
source.replies.remove(item);
|
||||
if (source.replies.isEmpty()) {
|
||||
source.replies = null;
|
||||
}
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void itemChanged(Dialogue.Reply item) {
|
||||
int index = source.replies.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void moveUp(Dialogue.Reply item) {
|
||||
int index = source.replies.indexOf(item);
|
||||
Dialogue.Reply exchanged = source.replies.get(index - 1);
|
||||
source.replies.set(index, exchanged);
|
||||
source.replies.set(index - 1, item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index - 1, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void moveDown(Dialogue.Reply item) {
|
||||
int index = source.replies.indexOf(item);
|
||||
Dialogue.Reply exchanged = source.replies.get(index + 1);
|
||||
source.replies.set(index, exchanged);
|
||||
source.replies.set(index + 1, item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index + 1));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
super(dialogue);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1115,69 +998,20 @@ public class DialogueEditor extends JSONElementEditor {
|
||||
}
|
||||
}
|
||||
|
||||
public static class ReplyRequirementsListModel implements ListModel<Requirement> {
|
||||
public static class ReplyRequirementsListModel extends CustomListModel<Dialogue.Reply,Requirement> {
|
||||
@Override
|
||||
protected List<Requirement> getItems() {
|
||||
return source.requirements;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void setItems(List<Requirement> items) {
|
||||
source.requirements = items;
|
||||
}
|
||||
|
||||
Dialogue.Reply reply;
|
||||
|
||||
public ReplyRequirementsListModel(Dialogue.Reply reply) {
|
||||
this.reply = reply;
|
||||
super(reply);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
if (reply.requirements == null) return 0;
|
||||
return reply.requirements.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Requirement getElementAt(int index) {
|
||||
if (reply.requirements == null) return null;
|
||||
return reply.requirements.get(index);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void addItem(Requirement item) {
|
||||
if (reply.requirements == null) {
|
||||
reply.requirements = new ArrayList<Requirement>();
|
||||
}
|
||||
reply.requirements.add(item);
|
||||
int index = reply.requirements.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(Requirement item) {
|
||||
int index = reply.requirements.indexOf(item);
|
||||
reply.requirements.remove(item);
|
||||
if (reply.requirements.isEmpty()) {
|
||||
reply.requirements = null;
|
||||
}
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void itemChanged(Requirement item) {
|
||||
int index = reply.requirements.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class ReplyRequirementsCellRenderer extends DefaultListCellRenderer {
|
||||
|
||||
@@ -5,7 +5,6 @@ import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.CopyOnWriteArrayList;
|
||||
|
||||
import javax.swing.DefaultListCellRenderer;
|
||||
import javax.swing.ImageIcon;
|
||||
@@ -17,10 +16,7 @@ import javax.swing.JPanel;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JSpinner;
|
||||
import javax.swing.JTextField;
|
||||
import javax.swing.ListModel;
|
||||
import javax.swing.ListSelectionModel;
|
||||
import javax.swing.event.ListDataEvent;
|
||||
import javax.swing.event.ListDataListener;
|
||||
import javax.swing.event.ListSelectionEvent;
|
||||
import javax.swing.event.ListSelectionListener;
|
||||
|
||||
@@ -34,6 +30,7 @@ import com.gpl.rpg.atcontentstudio.model.gamedata.Item;
|
||||
import com.gpl.rpg.atcontentstudio.ui.CollapsiblePanel;
|
||||
import com.gpl.rpg.atcontentstudio.ui.DefaultIcons;
|
||||
import com.gpl.rpg.atcontentstudio.ui.FieldUpdateListener;
|
||||
import com.gpl.rpg.atcontentstudio.ui.CustomListModel;
|
||||
import com.jidesoft.swing.JideBoxLayout;
|
||||
|
||||
public class DroplistEditor extends JSONElementEditor {
|
||||
@@ -148,65 +145,19 @@ public class DroplistEditor extends JSONElementEditor {
|
||||
pane.repaint();
|
||||
}
|
||||
|
||||
public class DroppedItemsListModel implements ListModel<Droplist.DroppedItem> {
|
||||
|
||||
Droplist source;
|
||||
|
||||
public class DroppedItemsListModel extends CustomListModel<Droplist, DroppedItem> {
|
||||
public DroppedItemsListModel(Droplist droplist) {
|
||||
this.source = droplist;
|
||||
super(droplist);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize() {
|
||||
if (source.dropped_items == null) return 0;
|
||||
return source.dropped_items.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Droplist.DroppedItem getElementAt(int index) {
|
||||
if (source.dropped_items == null) return null;
|
||||
return source.dropped_items.get(index);
|
||||
}
|
||||
|
||||
public void addItem(Droplist.DroppedItem item) {
|
||||
if (source.dropped_items == null) {
|
||||
source.dropped_items = new ArrayList<Droplist.DroppedItem>();
|
||||
}
|
||||
source.dropped_items.add(item);
|
||||
int index = source.dropped_items.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalAdded(new ListDataEvent(this, ListDataEvent.INTERVAL_ADDED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
public void removeItem(Droplist.DroppedItem item) {
|
||||
int index = source.dropped_items.indexOf(item);
|
||||
source.dropped_items.remove(item);
|
||||
if (source.dropped_items.isEmpty()) {
|
||||
source.dropped_items = null;
|
||||
}
|
||||
for (ListDataListener l : listeners) {
|
||||
l.intervalRemoved(new ListDataEvent(this, ListDataEvent.INTERVAL_REMOVED, index, index));
|
||||
}
|
||||
protected List<DroppedItem> getItems() {
|
||||
return source.dropped_items;
|
||||
}
|
||||
|
||||
public void itemChanged(Droplist.DroppedItem item) {
|
||||
int index = source.dropped_items.indexOf(item);
|
||||
for (ListDataListener l : listeners) {
|
||||
l.contentsChanged(new ListDataEvent(this, ListDataEvent.CONTENTS_CHANGED, index, index));
|
||||
}
|
||||
}
|
||||
|
||||
List<ListDataListener> listeners = new CopyOnWriteArrayList<ListDataListener>();
|
||||
|
||||
@Override
|
||||
public void addListDataListener(ListDataListener l) {
|
||||
listeners.add(l);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeListDataListener(ListDataListener l) {
|
||||
listeners.remove(l);
|
||||
protected void setItems(List<DroppedItem> items) {
|
||||
source.dropped_items = items;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user