Crossfire JXClient, Trunk
CharacterModel.java
Go to the documentation of this file.
1 /*
2  * This file is part of JXClient, the Fullscreen Java Crossfire Client.
3  *
4  * JXClient is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * JXClient is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with JXClient; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  *
18  * Copyright (C) 2005-2008 Yann Chachkoff
19  * Copyright (C) 2006-2017,2019-2023 Andreas Kirschbaum
20  * Copyright (C) 2010-2012,2014-2018,2020-2023 Nicolas Weeger
21  */
22 
23 package com.realtime.crossfire.jxclient.account;
24 
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.List;
31 import java.util.Map;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
40 public class CharacterModel {
41 
45  @NotNull
46  private final List<CharacterInformation> characters = new ArrayList<>();
47 
52  @NotNull
53  private final Collection<CharacterInformation> charactersPending = new ArrayList<>();
54 
58  @NotNull
59  private final Object sync = new Object();
60 
64  @NotNull
66 
71  @NotNull
72  private final Map<Integer, EventListenerList2<CharacterInformationListener>> characterInformationListeners = new HashMap<>();
73 
79  @Nullable
80  public CharacterInformation getEntry(final int index) {
81  try {
82  synchronized (sync) {
83  return characters.get(index);
84  }
85  } catch (final IndexOutOfBoundsException ignored) {
86  return null;
87  }
88  }
89 
95  public int getCharacterIndex(@NotNull final String characterName) {
96  synchronized (sync) {
97  int index = 0;
98  for (CharacterInformation characterInformation : characters) {
99  if (characterInformation.getName().equals(characterName)) {
100  return index;
101  }
102 
103  index++;
104  }
105  }
106 
107  return -1;
108  }
109 
114  public int size() {
115  synchronized (sync) {
116  return characters.size();
117  }
118  }
119 
124  public void add(@NotNull final CharacterInformation characterInformation) {
125  synchronized (sync) {
126  charactersPending.add(characterInformation);
127  }
128  }
129 
133  public void begin() {
134  charactersPending.clear();
135  }
136 
140  public void commit() {
141  final int oldMetaListSize;
142  final int newMetaListSize;
143  synchronized (sync) {
144  oldMetaListSize = characters.size();
145  characters.clear();
147  Collections.sort(characters);
148  newMetaListSize = characters.size();
149  }
150  charactersPending.clear();
151 
152  for (CharacterListener characterListener : characterListeners) {
153  characterListener.numberOfItemsChanged();
154  }
155 
156  for (int i = 0, iMax = Math.max(oldMetaListSize, newMetaListSize); i < iMax; i++) {
157  for (CharacterInformationListener characterInformationListener : getCharacterInformationListeners(i)) {
158  characterInformationListener.informationChanged();
159  }
160  }
161  }
162 
167  public void addCharacterListener(@NotNull final CharacterListener listener) {
168  characterListeners.add(listener);
169  }
170 
175  public void removeCharacterListener(@NotNull final CharacterListener listener) {
176  characterListeners.remove(listener);
177  }
178 
184  public void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
185  getCharacterInformationListeners(index).add(listener);
186  }
187 
193  public void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
194  getCharacterInformationListeners(index).remove(listener);
195  }
196 
202  @NotNull
204  synchronized (characterInformationListeners) {
206  if (existingListeners != null) {
207  return existingListeners;
208  }
209 
211  characterInformationListeners.put(index, newListeners);
212  return newListeners;
213  }
214  }
215 
221  public boolean displaysFace(final int faceNum) {
222  for (CharacterInformation character : characters) {
223  if (character.getFaceNumber() == faceNum) {
224  return true;
225  }
226  }
227 
228  return false;
229  }
230 
231 }
com.realtime.crossfire.jxclient.account.CharacterModel.displaysFace
boolean displaysFace(final int faceNum)
Definition: CharacterModel.java:221
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.account.CharacterModel.getEntry
CharacterInformation getEntry(final int index)
Definition: CharacterModel.java:80
com.realtime.crossfire.jxclient.account.CharacterModel.characterInformationListeners
final Map< Integer, EventListenerList2< CharacterInformationListener > > characterInformationListeners
Definition: CharacterModel.java:72
com.realtime.crossfire.jxclient.account.CharacterInformationListener
Definition: CharacterInformationListener.java:31
com.realtime.crossfire.jxclient.account.CharacterModel.removeCharacterInformationListener
void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Definition: CharacterModel.java:193
com.realtime.crossfire.jxclient.util.EventListenerList2
Definition: EventListenerList2.java:37
com.realtime.crossfire.jxclient.account.CharacterInformation
Definition: CharacterInformation.java:32
com.realtime.crossfire.jxclient.account.CharacterModel.getCharacterIndex
int getCharacterIndex(@NotNull final String characterName)
Definition: CharacterModel.java:95
com.realtime.crossfire.jxclient.account.CharacterModel.characterListeners
final EventListenerList2< CharacterListener > characterListeners
Definition: CharacterModel.java:65
com.realtime.crossfire.jxclient.account.CharacterModel.characters
final List< CharacterInformation > characters
Definition: CharacterModel.java:46
com.realtime.crossfire.jxclient.account.CharacterModel.addCharacterListener
void addCharacterListener(@NotNull final CharacterListener listener)
Definition: CharacterModel.java:167
com.realtime.crossfire.jxclient.account.CharacterModel.getCharacterInformationListeners
EventListenerList2< CharacterInformationListener > getCharacterInformationListeners(final int index)
Definition: CharacterModel.java:203
com.realtime.crossfire.jxclient.account.CharacterListener
Definition: CharacterListener.java:31
com.realtime.crossfire.jxclient.account.CharacterModel.size
int size()
Definition: CharacterModel.java:114
com.realtime.crossfire.jxclient.account.CharacterModel.sync
final Object sync
Definition: CharacterModel.java:59
com.realtime.crossfire.jxclient.account.CharacterModel.charactersPending
final Collection< CharacterInformation > charactersPending
Definition: CharacterModel.java:53
com.realtime.crossfire.jxclient.account.CharacterModel.add
void add(@NotNull final CharacterInformation characterInformation)
Definition: CharacterModel.java:124
com.realtime.crossfire.jxclient.util
Definition: Codec.java:23
com.realtime.crossfire
com.realtime
com
com.realtime.crossfire.jxclient.account.CharacterModel.addCharacterInformationListener
void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Definition: CharacterModel.java:184
com.realtime.crossfire.jxclient.account.CharacterModel.commit
void commit()
Definition: CharacterModel.java:140
com.realtime.crossfire.jxclient.account.CharacterModel.begin
void begin()
Definition: CharacterModel.java:133
com.realtime.crossfire.jxclient.account.CharacterModel.removeCharacterListener
void removeCharacterListener(@NotNull final CharacterListener listener)
Definition: CharacterModel.java:175
com.realtime.crossfire.jxclient.account.CharacterModel
Definition: CharacterModel.java:40