Crossfire JXClient, Trunk
GUINewcharLabel.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.gui.label;
24 
31 import java.awt.Color;
32 import java.awt.Font;
33 import java.util.regex.Pattern;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
42 public class GUINewcharLabel extends GUIOneLineLabel {
43 
47  private static final long serialVersionUID = 1;
48 
52  @NotNull
53  private static final Pattern PATTERN_DIGITS = Pattern.compile("^([0-9]+ )?");
54 
58  @NotNull
59  private final NewcharStat stat;
60 
64  @NotNull
65  private final NewcharType type;
66 
70  @NotNull
71  private final NewCharModel newCharModel;
72 
76  @NotNull
77  private final Font fontError;
78 
82  @NotNull
83  private final Color colorError;
84 
88  @NotNull
90 
94  @NotNull
95  private final NewCharModelListener listener = this::updateContents;
96 
101  @Nullable
103 
107  private boolean validValue = true;
108 
124  public GUINewcharLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Font font, @NotNull final Font fontError, @NotNull final NewcharStat stat, @NotNull final NewcharType type, @NotNull final NewCharModel newCharModel, @NotNull final Color color, @NotNull final Color colorError, @NotNull final GuiFactory guiFactory) {
125  super(tooltipManager, elementListener, name, null, font, color, color, null, type == NewcharType.ERROR || type == NewcharType.CHAR_OPTION ? Alignment.LEFT : Alignment.RIGHT, "", guiFactory);
126  this.stat = stat;
127  this.type = type;
128  this.newCharModel = newCharModel;
129  this.fontError = fontError;
130  this.colorError = colorError;
131 
133  updateContents();
134 
135  this.crossfireServerConnection = crossfireServerConnection;
137 
138  @Override
139  public void failure(@NotNull final String command, @NotNull final String arguments) {
141  }
142 
143  @Override
144  public void clearFailure() {
146  }
147 
148  } : null;
149  if (crossfireFailureListener != null) {
150  this.crossfireServerConnection.addCrossfireFailureListener(crossfireFailureListener);
151  }
152  }
153 
154  @Override
155  public void dispose() {
156  super.dispose();
158  if (crossfireFailureListener != null) {
160  }
161  }
162 
163  @Override
164  public void notifyOpen() {
165  super.notifyOpen();
166  if (type == NewcharType.ERROR) {
168  }
169  updateContents();
170  }
171 
172  @NotNull
173  @Override
174  protected Color getTextColor() {
175  return validValue ? super.getTextColor() : colorError;
176  }
177 
178  @NotNull
179  @Override
180  protected Font getTextFont() {
181  return validValue ? super.getTextFont() : fontError;
182  }
183 
187  private void updateContents() {
188  switch (type) {
189  case RACE:
190  setText(Integer.toString(newCharModel.getRaceStatAdjustment(stat)));
191  break;
192 
193  case CLASS:
194  setText(Integer.toString(newCharModel.getClassStatAdjustment(stat)));
195  break;
196 
197  case TOTAL:
198  final int total = newCharModel.getTotal(stat);
199  validValue = total >= 1;
200  setText(Integer.toString(total));
201  newCharModel.setErrorText(stat.getPriority(), validValue ? null : "The "+stat.getName()+" is invalid: it must be at least 1.");
202  break;
203 
204  case ERROR:
206  break;
207 
208  case UNUSED:
209  final int unusedPoints = newCharModel.getUnusedPoints();
210  validValue = unusedPoints == 0;
211  setText(Integer.toString(unusedPoints));
212  newCharModel.setErrorText(NewCharModel.PRIORITY_UNUSED_POINTS, unusedPoints < 0 ? "You have used more than your allotted total attribute points." : unusedPoints > 0 ? "You have not used all your allotted attribute points." : null);
213  break;
214 
215  case CHAR_OPTION:
216  final Choice option = newCharModel.getOption();
217  setText(option == null ? "" : option.getChoiceDescription());
218  break;
219  }
220  }
221 
222 }
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.name
final String name
Definition: AbstractGUIElement.java:77
com.realtime.crossfire.jxclient
com.realtime.crossfire.jxclient.skin.skin
Definition: DefaultJXCSkin.java:23
com.realtime.crossfire.jxclient.gui.label.NewcharStat
Definition: NewcharStat.java:32
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.notifyOpen
void notifyOpen()
Definition: GUINewcharLabel.java:164
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.updateContents
void updateContents()
Definition: GUINewcharLabel.java:187
com.realtime.crossfire.jxclient.server
com.realtime.crossfire.jxclient.gui.label.NewCharModelListener
Definition: NewCharModelListener.java:30
com.realtime.crossfire.jxclient.skin
com.realtime.crossfire.jxclient.character.Choice
Definition: Choice.java:34
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getErrorText
String getErrorText()
Definition: NewCharModel.java:396
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.serialVersionUID
static final long serialVersionUID
Definition: GUINewcharLabel.java:47
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_UNUSED_POINTS
static final int PRIORITY_UNUSED_POINTS
Definition: NewCharModel.java:89
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.type
final NewcharType type
Definition: GUINewcharLabel.java:65
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.crossfireFailureListener
final CrossfireFailureListener crossfireFailureListener
Definition: GUINewcharLabel.java:102
com.realtime.crossfire.jxclient.gui.label.NewCharModel.addListener
void addListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:208
com.realtime.crossfire.jxclient.skin.skin.GuiFactory
Definition: GuiFactory.java:41
com.realtime.crossfire.jxclient.gui.label.Alignment
Definition: Alignment.java:29
com.realtime.crossfire.jxclient.gui.label.NewcharType.ERROR
ERROR
Definition: NewcharType.java:49
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.listener
final NewCharModelListener listener
Definition: GUINewcharLabel.java:95
com.realtime.crossfire.jxclient.server.crossfire.CrossfireFailureListener
Definition: CrossfireFailureListener.java:33
com.realtime.crossfire.jxclient.gui.label.NewcharStat.getPriority
int getPriority()
Definition: NewcharStat.java:135
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.newCharModel
final NewCharModel newCharModel
Definition: GUINewcharLabel.java:71
com.realtime.crossfire.jxclient.gui.label.NewcharType.CHAR_OPTION
CHAR_OPTION
Definition: NewcharType.java:59
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.GUINewcharLabel
GUINewcharLabel(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final CrossfireServerConnection crossfireServerConnection, @NotNull final Font font, @NotNull final Font fontError, @NotNull final NewcharStat stat, @NotNull final NewcharType type, @NotNull final NewCharModel newCharModel, @NotNull final Color color, @NotNull final Color colorError, @NotNull final GuiFactory guiFactory)
Definition: GUINewcharLabel.java:124
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection
Definition: CrossfireServerConnection.java:37
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.colorError
final Color colorError
Definition: GUINewcharLabel.java:83
com.realtime.crossfire.jxclient.gui.label.NewcharType
Definition: NewcharType.java:29
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.fontError
final Font fontError
Definition: GUINewcharLabel.java:77
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.PATTERN_DIGITS
static final Pattern PATTERN_DIGITS
Definition: GUINewcharLabel.java:53
com.realtime.crossfire.jxclient.gui
com.realtime.crossfire.jxclient.gui.label.Alignment.LEFT
LEFT
Definition: Alignment.java:34
com.realtime.crossfire.jxclient.server.crossfire.CrossfireServerConnection.removeCrossfireFailureListener
void removeCrossfireFailureListener(@NotNull CrossfireFailureListener listener)
com.realtime.crossfire.jxclient.gui.label.NewCharModel.PRIORITY_SERVER_FAILURE
static final int PRIORITY_SERVER_FAILURE
Definition: NewCharModel.java:99
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.elementListener
final GUIElementListener elementListener
Definition: AbstractGUIElement.java:89
com.realtime.crossfire.jxclient.server.crossfire
Definition: AbstractCrossfireServerConnection.java:23
com.realtime.crossfire.jxclient.gui.gui.TooltipManager
Definition: TooltipManager.java:33
com.realtime.crossfire.jxclient.gui.label.NewCharModel
Definition: NewCharModel.java:43
com.realtime.crossfire.jxclient.gui.gui
Definition: AbstractGUIElement.java:23
com.realtime.crossfire.jxclient.gui.label.AbstractLabel.setText
void setText(@NotNull final String text)
Definition: AbstractLabel.java:120
com.realtime.crossfire
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.stat
final NewcharStat stat
Definition: GUINewcharLabel.java:59
com.realtime
com
com.realtime.crossfire.jxclient.gui.label.Alignment.RIGHT
RIGHT
Definition: Alignment.java:44
com.realtime.crossfire.jxclient.gui.label.NewcharStat.getName
String getName()
Definition: NewcharStat.java:118
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getTotal
int getTotal(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:331
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.validValue
boolean validValue
Definition: GUINewcharLabel.java:107
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getRaceStatAdjustment
int getRaceStatAdjustment(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:363
com.realtime.crossfire.jxclient.character
Definition: Choice.java:23
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel
Definition: GUINewcharLabel.java:42
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.guiFactory
final GuiFactory guiFactory
Definition: AbstractGUIElement.java:48
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.dispose
void dispose()
Definition: GUINewcharLabel.java:155
com.realtime.crossfire.jxclient.character.Choice.getChoiceDescription
String getChoiceDescription()
Definition: Choice.java:80
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getOption
Choice getOption()
Definition: NewCharModel.java:438
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.getTextFont
Font getTextFont()
Definition: GUINewcharLabel.java:180
com.realtime.crossfire.jxclient.gui.label.NewCharModel.removeListener
void removeListener(@NotNull final NewCharModelListener listener)
Definition: NewCharModel.java:216
com.realtime.crossfire.jxclient.gui.label.GUIOneLineLabel
Definition: GUIOneLineLabel.java:43
com.realtime.crossfire.jxclient.gui.gui.GUIElementListener
Definition: GUIElementListener.java:32
com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement.tooltipManager
final TooltipManager tooltipManager
Definition: AbstractGUIElement.java:83
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getUnusedPoints
int getUnusedPoints()
Definition: NewCharModel.java:425
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.crossfireServerConnection
final CrossfireServerConnection crossfireServerConnection
Definition: GUINewcharLabel.java:89
com.realtime.crossfire.jxclient.gui.label.NewCharModel.getClassStatAdjustment
int getClassStatAdjustment(@NotNull final NewcharStat stat)
Definition: NewCharModel.java:373
com.realtime.crossfire.jxclient.gui.label.GUINewcharLabel.getTextColor
Color getTextColor()
Definition: GUINewcharLabel.java:174
com.realtime.crossfire.jxclient.gui.label.NewCharModel.setErrorText
void setErrorText(final int priority, @Nullable final String text)
Definition: NewCharModel.java:383