Crossfire JXClient, Trunk  R20561
GUICharacterList.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.gui.list;
22 
31 import java.awt.Font;
32 import org.jetbrains.annotations.NotNull;
33 import org.jetbrains.annotations.Nullable;
34 
39 public class GUICharacterList extends GUIList<GUICharacter> {
40 
44  private static final long serialVersionUID = 1;
45 
49  @NotNull
51 
55  @NotNull
57 
61  @NotNull
62  private final FacesManager facesManager;
63 
67  @NotNull
69 
73  @NotNull
74  private final String name;
75 
79  @NotNull
80  private final Font font;
81 
85  private int selectedIndex;
86 
92  @NotNull
94 
106  public GUICharacterList(@NotNull final TooltipManager tooltipManager, @NotNull final FacesManager facesManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final Font font, @NotNull final CharacterModel characterModel) {
107  super(tooltipManager, elementListener, name, cellWidth, cellHeight, new CharacterCellRenderer(new GUICharacter(tooltipManager, facesManager, elementListener, name+"_template", 50, 20, font, 0, characterModel)), null);
108  this.characterModel = characterModel;
109  this.facesManager = facesManager;
110  this.facesManager.addFacesManagerListener(face -> {
111  if (characterModel.displaysFace(face.getFaceNum())) {
112  final Gui parent = GuiUtils.getGui(this);
113  if (parent != null) {
114  parent.repaint();
115  }
116  }
117  });
118  this.characterModel.addCharacterListener(this::rebuildList);
119  this.tooltipManager = tooltipManager;
120  this.font = font;
121  this.elementListener = elementListener;
122  this.name = name;
123  }
124 
128  @Override
129  protected void selectionChanged(final int selectedIndex) {
130  this.selectedIndex = selectedIndex;
131  }
132 
136  @Override
137  protected void updateTooltip(final int index, final int x, final int y, final int w, final int h) {
138  }
139 
143  @Override
144  protected void activeChanged() {
145  }
146 
150  private void rebuildList() {
151  synchronized (getTreeLock()) {
152  final int newSize = characterModel.size();
153  final int oldSize = resizeElements(newSize);
154  if (oldSize < newSize) {
155  for (int i = oldSize; i < newSize; i++) {
156  final GUICharacter metaElement = new GUICharacter(tooltipManager, facesManager, elementListener, name+i, 1, 1, font, i, characterModel);
157  addElement(metaElement);
158  characterModel.addCharacterInformationListener(i, characterInformationListener);
159  }
160  } else {
161  for (int i = newSize; i < oldSize; i++) {
162  characterModel.removeCharacterInformationListener(i, characterInformationListener);
163  }
164  }
165  }
166  setChanged();
167  }
168 
173  @Nullable
175  return characterModel.getEntry(selectedIndex);
176  }
177 
182  public void setCharacter(@NotNull final String characterName) {
183  setSelectedIndex(characterModel.getCharacterIndex(characterName));
184  }
185 
186 }
static Gui getGui(@NotNull final Component element)
Returns the Gui an element is part of.
Definition: GuiUtils.java:91
CharacterInformation getCurrentCharacter()
Returns the currently selected character in the list.
void setSelectedIndex(final int newIndex)
Update the selected list entry.
Definition: GUIList.java:469
Combines a list of GUIElements to for a gui.
Definition: Gui.java:43
final int cellHeight
The height of a list cell in pixels.
Definition: GUIList.java:64
void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Removes a character entry listener for one entry.
final CharacterModel characterModel
The model to display characters from.
void setCharacter(@NotNull final String characterName)
Selects an entry by character name.
final Font font
The font for drawing list entries.
int selectedIndex
The currently selected list index.
Manages image information ("faces") needed to display the map view, items, and spell icons...
final FacesManager facesManager
The FacesManager to use to display faces.
int resizeElements(final int newSize)
Changes the number of list elements.
Definition: GUIList.java:205
void addElement( @NotNull final T element)
Adds an GUIElement to the list.
Definition: GUIList.java:188
final TooltipManager tooltipManager
The TooltipManager to update.
void addCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Adds a character entry listener for one entry.
Utility class for Gui related functions.
Definition: GuiUtils.java:35
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.
A GUIList display characters of an account.
final GUIElementListener elementListener
The GUIElementListener to use.
GUICharacterList(@NotNull final TooltipManager tooltipManager, @NotNull final FacesManager facesManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int cellWidth, final int cellHeight, @NotNull final Font font, @NotNull final CharacterModel characterModel)
Creates a new instance.
Maintains the character list for an account.
void updateTooltip(final int index, final int x, final int y, final int w, final int h)
int size()
Returns the number of character entries.
void addFacesManagerListener(@NotNull FacesManagerListener facesManagerListener)
Adds a FacesManagerListener to be notified about updated faces.
A GUIElement that displays a list of entries.
Definition: GUIList.java:54
final CharacterInformationListener characterInformationListener
The CharacterInformationListener attached to all tracked character models.
static final long serialVersionUID
The serial version UID.
Maintains a mapping of face numbers to face data.