Crossfire JXClient, Trunk  R20561
GUICharacter.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 
32 import java.awt.Color;
33 import java.awt.Dimension;
34 import java.awt.Font;
35 import java.awt.Graphics;
36 import java.awt.Transparency;
37 import org.jetbrains.annotations.NotNull;
38 import org.jetbrains.annotations.Nullable;
39 
44 public class GUICharacter extends ActivatableGUIElement implements GUIScrollable {
45 
49  private static final long serialVersionUID = 1;
50 
54  @NotNull
55  private final FacesManager facesManager;
56 
61 
65  private int index;
66 
70  @NotNull
71  private final Font font;
72 
76  private boolean selected;
77 
81  @NotNull
83  setChanged();
84  updateTooltip();
85  };
86 
99  public GUICharacter(@NotNull final TooltipManager tooltipManager, @NotNull final FacesManager facesManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int w, final int h, @NotNull final Font font, final int defaultIndex, final CharacterModel characterModel) {
100  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
101  setSize(w, h);
102  this.facesManager = facesManager;
103  this.characterModel = characterModel;
104  this.font = font;
105  index = defaultIndex;
106  }
107 
111  @Override
112  protected void activeChanged() {
113  throw new UnsupportedOperationException("Not supported yet.");
114  }
115 
119  @Override
120  public void paintComponent(@NotNull final Graphics g) {
121  super.paintComponent(g);
122  g.setColor(new Color(0, 0, 0, 0.0f));
123  g.fillRect(0, 0, getWidth(), getHeight());
124  g.setFont(font);
125  g.setColor(isActive() || selected ? Color.RED : Color.GRAY);
126  final CharacterInformation character = characterModel.getEntry(index);
127  if (character == null) {
128  return;
129  }
130 
135  final int y = (getHeight()+font.getSize())/2;
136  int x = 0;
137  g.drawImage(facesManager.getOriginalImageIcon(character.getFaceNumber(), null).getImage(), x, (getHeight()-facesManager.getOriginalImageIcon(character.getFaceNumber(), null).getImage().getHeight(null))/2, this);
138  x += 40;
139  g.drawString(character.getName(), x, y);
140  x += 150;
141  g.drawString(character.getRace(), x, y);
142  x += 80;
143  g.drawString(character.getParty(), x, y);
144  x += 100;
145  g.drawString(character.getMap(), x, y);
146  }
147 
151  @Nullable
152  @Override
153  public Dimension getPreferredSize() {
154  return getMinimumSizeInt();
155  }
156 
160  @Nullable
161  @Override
162  public Dimension getMinimumSize() {
163  return getMinimumSizeInt();
164  }
165 
170  @NotNull
171  private Dimension getMinimumSizeInt() {
172  final CharacterInformation character = characterModel.getEntry(index);
173  return GuiUtils.getTextDimension(character == null ? "" : character.getName(), getFontMetrics(font));
174  }
175 
179  @Override
180  public boolean canScroll(final int distance) {
181  throw new UnsupportedOperationException("Not supported yet.");
182  }
183 
187  @Override
188  public void scroll(final int distance) {
189  throw new UnsupportedOperationException("Not supported yet.");
190  }
191 
195  @Override
196  public void resetScroll() {
197  throw new UnsupportedOperationException("Not supported yet.");
198  }
199 
204  public int getIndex() {
205  return index;
206  }
207 
212  public void setIndex(final int index) {
213  if (this.index == index) {
214  return;
215  }
216 
217  characterModel.removeCharacterInformationListener(index, characterInformationListener);
218  this.index = index;
219  characterModel.addCharacterInformationListener(index, characterInformationListener);
220  setChanged();
221  updateTooltip();
222  }
223 
227  private void updateTooltip() {
228  final CharacterInformation characterInformation = characterModel.getEntry(index);
229  setTooltipText(characterInformation == null ? null : characterInformation.getName());
230  }
231 
236  public void setSelected(final boolean selected) {
237  if (this.selected == selected) {
238  return;
239  }
240 
241  this.selected = selected;
242  setChanged();
243  }
244 
248  @Override
249  public void execute() {
250  // ignore
251  }
252 
253 }
ImageIcon getOriginalImageIcon(int faceNum, @Nullable boolean[] isUnknownImage)
Returns the "original" face for a face ID.
final TooltipManager tooltipManager
The TooltipManager to update.
final CharacterInformationListener characterInformationListener
All listeners to the entry itself.
void removeCharacterInformationListener(final int index, @NotNull final CharacterInformationListener listener)
Removes a character entry listener for one entry.
final GUIElementListener elementListener
The GUIElementListener to notify.
static Dimension getTextDimension(@NotNull final String text, @NotNull final FontMetrics fontMetrics)
Returns the extents of a string when rendered in a given Font on this component.
Definition: GuiUtils.java:51
final CharacterModel characterModel
Character model to display items from.
void setChanged()
Records that the contents have changed and must be repainted.
Manages image information ("faces") needed to display the map view, items, and spell icons...
final FacesManager facesManager
The FacesManager to use to display faces.
A GUIElement that can be set to active or inactive.
void setIndex(final int index)
Sets the index of this element.
boolean selected
If set, paint the element in "selected" state.
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.
void setTooltipText(@Nullable final String tooltipText)
Sets the tooltip text to show when the mouse is inside this element.the text to show ornull to disab...
static final long serialVersionUID
The serial version UID.
void setSelected(final boolean selected)
Sets the selected state.
Maintains the character list for an account.
GUICharacter(@NotNull final TooltipManager tooltipManager, @NotNull final FacesManager facesManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final int w, final int h, @NotNull final Font font, final int defaultIndex, final CharacterModel characterModel)
Creates a new instance.
boolean canScroll(final int distance)
Returns whether scrolling is possible.the distance to scroll whether scrolling is possible ...
int getFaceNumber()
Returns the character's face number.
void resetScroll()
Resets the scroll index to the default value.
int index
Index of the item to display in characterModel.
int getIndex()
Returns the index of this element.
void scroll(final int distance)
Scrolls the element.the distance to scroll
Interface for GUIElements that support scrolling.
boolean isActive()
Returns whether a GUI element is active.
Dimension getMinimumSizeInt()
Returns the minimal size needed to display this component.
Maintains a mapping of face numbers to face data.