00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 package com.realtime.crossfire.jxclient.account;
00022
00023 import com.realtime.crossfire.jxclient.util.EventListenerList2;
00024 import java.util.ArrayList;
00025 import java.util.Collection;
00026 import java.util.Collections;
00027 import java.util.HashMap;
00028 import java.util.List;
00029 import java.util.Map;
00030 import org.jetbrains.annotations.NotNull;
00031 import org.jetbrains.annotations.Nullable;
00032
00038 public class CharacterModel {
00039
00043 @NotNull
00044 private final List<CharacterInformation> characters = new ArrayList<CharacterInformation>();
00045
00050 @NotNull
00051 private final Collection<CharacterInformation> charactersPending = new ArrayList<CharacterInformation>();
00052
00056 @NotNull
00057 private final Object sync = new Object();
00058
00062 @NotNull
00063 private final EventListenerList2<CharacterListener> characterListeners = new EventListenerList2<CharacterListener>(CharacterListener.class);
00064
00069 @NotNull
00070 private final Map<Integer, EventListenerList2<CharacterInformationListener>> characterInformationListeners = new HashMap<Integer, EventListenerList2<CharacterInformationListener>>();
00071
00078 @Nullable
00079 public CharacterInformation getEntry(final int index) {
00080 try {
00081 synchronized (sync) {
00082 return characters.get(index);
00083 }
00084 } catch (final IndexOutOfBoundsException ignored) {
00085 return null;
00086 }
00087 }
00088
00094 public int getCharacterIndex(@NotNull final String characterName) {
00095 synchronized (sync) {
00096 int index = 0;
00097 for (final CharacterInformation characterInformation : characters) {
00098 if (characterInformation.getName().equals(characterName)) {
00099 return index;
00100 }
00101
00102 index++;
00103 }
00104 }
00105
00106 return -1;
00107 }
00108
00113 public int size() {
00114 synchronized (sync) {
00115 return characters.size();
00116 }
00117 }
00118
00123 public void add(@NotNull final CharacterInformation characterInformation) {
00124 synchronized (sync) {
00125 charactersPending.add(characterInformation);
00126 }
00127 }
00128
00132 public void begin() {
00133 charactersPending.clear();
00134 }
00135
00139 public void commit() {
00140 final int oldMetaListSize;
00141 final int newMetaListSize;
00142 synchronized (sync) {
00143 oldMetaListSize = characters.size();
00144 characters.clear();
00145 characters.addAll(charactersPending);
00146 Collections.sort(characters);
00147 newMetaListSize = characters.size();
00148 }
00149 charactersPending.clear();
00150
00151 for (final CharacterListener characterListener : characterListeners.getListeners()) {
00152 characterListener.numberOfItemsChanged();
00153 }
00154
00155 for (int i = 0, iMax = Math.max(oldMetaListSize, newMetaListSize); i < iMax; i++) {
00156 for (final CharacterInformationListener characterInformationListener : getCharacterInformationListeners(i).getListeners()) {
00157 characterInformationListener.informationChanged();
00158 }
00159 }
00160 }
00161
00166 public void addCharacterListener(@NotNull final CharacterListener listener) {
00167 characterListeners.add(listener);
00168 }
00169
00174 public void removeCharacterListener(@NotNull final CharacterListener listener) {
00175 characterListeners.remove(listener);
00176 }
00177
00183 public void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
00184 getCharacterInformationListeners(index).add(listener);
00185 }
00186
00192 public void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
00193 getCharacterInformationListeners(index).remove(listener);
00194 }
00195
00201 @NotNull
00202 private EventListenerList2<CharacterInformationListener> getCharacterInformationListeners(final int index) {
00203 synchronized (characterInformationListeners) {
00204 final EventListenerList2<CharacterInformationListener> existingListeners = characterInformationListeners.get(index);
00205 if (existingListeners != null) {
00206 return existingListeners;
00207 }
00208
00209 final EventListenerList2<CharacterInformationListener> newListeners = new EventListenerList2<CharacterInformationListener>(CharacterInformationListener.class);
00210 characterInformationListeners.put(index, newListeners);
00211 return newListeners;
00212 }
00213 }
00214
00220 public boolean displaysFace(final int faceNum) {
00221 for (final CharacterInformation character : characters) {
00222 if (character.getFaceNumber() == faceNum) {
00223 return true;
00224 }
00225 }
00226
00227 return false;
00228 }
00229
00230 }