Crossfire JXClient, Trunk  R20561
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) 2010 Nicolas Weeger.
19  */
20 
21 package com.realtime.crossfire.jxclient.account;
22 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.HashMap;
28 import java.util.List;
29 import java.util.Map;
30 import org.jetbrains.annotations.NotNull;
31 import org.jetbrains.annotations.Nullable;
32 
38 public class CharacterModel {
39 
43  @NotNull
44  private final List<CharacterInformation> characters = new ArrayList<>();
45 
50  @NotNull
51  private final Collection<CharacterInformation> charactersPending = new ArrayList<>();
52 
56  @NotNull
57  private final Object sync = new Object();
58 
62  @NotNull
64 
69  @NotNull
70  private final Map<Integer, EventListenerList2<CharacterInformationListener>> characterInformationListeners = new HashMap<>();
71 
77  @Nullable
78  public CharacterInformation getEntry(final int index) {
79  try {
80  synchronized (sync) {
81  return characters.get(index);
82  }
83  } catch (final IndexOutOfBoundsException ignored) {
84  return null;
85  }
86  }
87 
93  public int getCharacterIndex(@NotNull final String characterName) {
94  synchronized (sync) {
95  int index = 0;
96  for (final CharacterInformation characterInformation : characters) {
97  if (characterInformation.getName().equals(characterName)) {
98  return index;
99  }
100 
101  index++;
102  }
103  }
104 
105  return -1;
106  }
107 
112  public int size() {
113  synchronized (sync) {
114  return characters.size();
115  }
116  }
117 
122  public void add(@NotNull final CharacterInformation characterInformation) {
123  synchronized (sync) {
124  charactersPending.add(characterInformation);
125  }
126  }
127 
131  public void begin() {
132  charactersPending.clear();
133  }
134 
138  public void commit() {
139  final int oldMetaListSize;
140  final int newMetaListSize;
141  synchronized (sync) {
142  oldMetaListSize = characters.size();
143  characters.clear();
144  characters.addAll(charactersPending);
145  Collections.sort(characters);
146  newMetaListSize = characters.size();
147  }
148  charactersPending.clear();
149 
150  for (final CharacterListener characterListener : characterListeners) {
151  characterListener.numberOfItemsChanged();
152  }
153 
154  for (int i = 0, iMax = Math.max(oldMetaListSize, newMetaListSize); i < iMax; i++) {
155  for (final CharacterInformationListener characterInformationListener : getCharacterInformationListeners(i)) {
156  characterInformationListener.informationChanged();
157  }
158  }
159  }
160 
165  public void addCharacterListener(@NotNull final CharacterListener listener) {
166  characterListeners.add(listener);
167  }
168 
173  public void removeCharacterListener(@NotNull final CharacterListener listener) {
174  characterListeners.remove(listener);
175  }
176 
182  public void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
183  getCharacterInformationListeners(index).add(listener);
184  }
185 
191  public void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener) {
192  getCharacterInformationListeners(index).remove(listener);
193  }
194 
200  @NotNull
202  synchronized (characterInformationListeners) {
203  final EventListenerList2<CharacterInformationListener> existingListeners = characterInformationListeners.get(index);
204  if (existingListeners != null) {
205  return existingListeners;
206  }
207 
209  characterInformationListeners.put(index, newListeners);
210  return newListeners;
211  }
212  }
213 
219  public boolean displaysFace(final int faceNum) {
220  for (final CharacterInformation character : characters) {
221  if (character.getFaceNumber() == faceNum) {
222  return true;
223  }
224  }
225 
226  return false;
227  }
228 
229 }
void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Removes a character entry listener for one entry.
EventListenerList2< CharacterInformationListener > getCharacterInformationListeners(final int index)
Returns the character entry listeners for one entry index.
boolean displaysFace(final int faceNum)
Returns whether any character has the given face.
final List< CharacterInformation > characters
The current entries.
final Collection< CharacterInformation > charactersPending
The pending entries.
void removeCharacterListener(@NotNull final CharacterListener listener)
Removes a character listener.
final Map< Integer, EventListenerList2< CharacterInformationListener > > characterInformationListeners
All registered character entry listeners.
void commit()
Finishes an update transaction.
void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Adds a character entry listener for one entry.
Callback for a change in a character list.
CharacterInformation getEntry(final int index)
Returns a character entry by index.
int getCharacterIndex(@NotNull final String characterName)
Returns the index of an entry by character name.
void add(@NotNull final T listener)
Adds a listener.
Maintains the character list for an account.
void addCharacterListener(@NotNull final CharacterListener listener)
Adds a character listener.
final Object sync
Object used for synchronization.
final EventListenerList2< CharacterListener > characterListeners
All registered character listeners.
int size()
Returns the number of character entries.
void remove(@NotNull final T listener)
Removes a listener.
void add(@NotNull final CharacterInformation characterInformation)
Adds an entry.