Crossfire JXClient, Trunk  R20561
GUIDupGauge.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-2011 Andreas Kirschbaum.
20  */
21 
22 package com.realtime.crossfire.jxclient.gui.gauge;
23 
28 import java.awt.Color;
29 import java.awt.Dimension;
30 import java.awt.Graphics;
31 import java.awt.Image;
32 import java.awt.Transparency;
33 import java.awt.event.MouseEvent;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
42 public class GUIDupGauge extends AbstractGUIElement implements GUIGaugeListener {
43 
47  private static final long serialVersionUID = 1;
48 
52  @NotNull
53  private String labelText = "";
54 
59  @Nullable
60  private final String tooltipPrefix;
61 
65  @Nullable
66  private final CommandList commandList;
67 
72  @NotNull
73  private String tooltipText = "";
74 
78  @Nullable
79  private final Image emptyImage;
80 
84  @NotNull
85  private final Orientation orientationDiv;
86 
90  @NotNull
91  private final Orientation orientationMod;
92 
96  @NotNull
97  private final GaugeState gaugeStateDiv;
98 
102  @NotNull
103  private final GaugeState gaugeStateMod;
104 
120  public GUIDupGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Image fullImageDiv, @NotNull final Image fullImageMod, @Nullable final Image emptyImage, @NotNull final Orientation orientationDiv, @NotNull final Orientation orientationMod, @Nullable final String tooltipPrefix, @Nullable final CommandList commandList) {
121  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
122  this.emptyImage = emptyImage;
123  this.orientationDiv = orientationDiv;
124  this.orientationMod = orientationMod;
125  this.tooltipPrefix = tooltipPrefix;
126  this.commandList = commandList;
127  gaugeStateDiv = new GaugeState(fullImageDiv, null, 0, 0);
128  final int w = getWidth();
129  final int h = getHeight();
130  gaugeStateMod = new GaugeState(fullImageMod, null, 0, h/2);
131  orientationDiv.setExtends(w, h);
132  orientationMod.setExtends(w, h);
133  orientationDiv.setValues(0, 0, 9);
134  orientationMod.setValues(0, 0, 9);
135  gaugeStateDiv.setValues(orientationDiv);
136  gaugeStateMod.setValues(orientationMod);
138  }
139 
143  @Override
144  public void paintComponent(@NotNull final Graphics g) {
145  super.paintComponent(g);
146  g.setColor(new Color(0, 0, 0, 0.0f));
147  g.fillRect(0, 0, getWidth(), getHeight());
148  if (emptyImage != null) {
149  g.drawImage(emptyImage, 0, 0, null);
150  }
151  gaugeStateDiv.draw(g);
152  gaugeStateMod.draw(g);
153  }
154 
158  @NotNull
159  @Override
160  public Dimension getPreferredSize() {
161  return getGaugeStateSize();
162  }
163 
167  @NotNull
168  @Override
169  public Dimension getMinimumSize() {
170  return getGaugeStateSize();
171  }
172 
176  @NotNull
177  @Override
178  public Dimension getMaximumSize() {
179  return getGaugeStateSize();
180  }
181 
187  @NotNull
188  private Dimension getGaugeStateSize() {
189  final Dimension div = gaugeStateDiv.getPreferredSize();
190  final Dimension mod = gaugeStateMod.getPreferredSize();
191  return new Dimension(Math.max(div.width, mod.width), div.height+mod.height);
192  }
193 
197  @Override
198  public void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText) {
199  if (minValue != 0) {
200  throw new IllegalArgumentException("minValue="+minValue);
201  }
202  if (maxValue != 99) {
203  throw new IllegalArgumentException("maxValue="+maxValue);
204  }
205  if (!orientationDiv.setValues(curValue/10, 0, 9) && !orientationMod.setValues(curValue%10, 0, 9) && this.labelText.equals(labelText) && this.tooltipText.equals(tooltipText)) {
206  return;
207  }
208 
209  this.labelText = labelText;
210  this.tooltipText = tooltipText;
211 
212  if (gaugeStateDiv.setValues(orientationDiv)) {
213  setChanged();
214  }
215  if (gaugeStateMod.setValues(orientationMod)) {
216  setChanged();
217  }
218 
220  }
221 
226  private void updateTooltipText() {
227  if (tooltipPrefix == null || tooltipPrefix.isEmpty()) {
228  setTooltipText(tooltipText);
229  } else if (tooltipText.isEmpty()) {
230  setTooltipText(tooltipPrefix);
231  } else {
232  setTooltipText(tooltipPrefix+tooltipText);
233  }
234  }
235 
239  @Override
240  public void setBounds(final int x, final int y, final int width, final int height) {
241  super.setBounds(x, y, width, height);
242  gaugeStateMod.setDy(height/2);
243  orientationDiv.setExtends(width, height);
244  orientationMod.setExtends(width, height);
245  }
246 
250  @Override
251  public void setHidden(final boolean hidden) {
252  // nothing
253  }
254 
258  @Override
259  public void mouseClicked(@NotNull final MouseEvent e) {
260  super.mouseClicked(e);
261  switch (e.getButton()) {
262  case MouseEvent.BUTTON1:
263  break;
264 
265  case MouseEvent.BUTTON2:
266  if (commandList != null) {
267  commandList.execute();
268  }
269  break;
270 
271  case MouseEvent.BUTTON3:
272  break;
273  }
274  }
275 
276 }
static final long serialVersionUID
The serial version UID.
Interface which is implemented by all listener classes.
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
final TooltipManager tooltipManager
The TooltipManager to update.
void setHidden(final boolean hidden)
Specify whether this item should hide itself, independent of the "visibility" state.whether the gauge is hidden
void draw(@NotNull final Graphics g)
Draws the gauge image into the given graphics context.
void setChanged()
Records that the contents have changed and must be repainted.
final CommandList commandList
The CommandList that is executed on button 2.
Dimension getPreferredSize()
Returns the preferred size.
Dimension getGaugeStateSize()
Returns the maximum size of gaugeStateDiv and gaugeStateMod.
final GUIElementListener elementListener
The GUIElementListener to notify.
void updateTooltipText()
Updates the tooltip's text from tooltipPrefix ad tooltipText.
GUIDupGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @NotNull final Image fullImageDiv, @NotNull final Image fullImageMod, @Nullable final Image emptyImage, @NotNull final Orientation orientationDiv, @NotNull final Orientation orientationMod, @Nullable final String tooltipPrefix, @Nullable final CommandList commandList)
Creates a new instance.
boolean setValues(int cur, int min, int max)
Sets the gauge's values.
void setDy(final int dy)
Sets the y-offset for drawing.
final GaugeState gaugeStateDiv
The gauge state.
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...
final Orientation orientationMod
The gauge's orientation.
boolean setExtends(int width, int height)
Sets the extends of the image.
Displays a value as a graphical gauge that's filling state depends on the value.
final GaugeState gaugeStateMod
The gauge state.
void setBounds(final int x, final int y, final int width, final int height)
void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText)
Sets the values to display.the values to display the minimum possible value the maximum possible valu...
final Image emptyImage
The image representing an empty gauge.
boolean setValues(@NotNull final Orientation orientation)
Updates the values from a Orientation state.
Abstract base class for GUI elements to be shown in Guis.
final String tooltipPrefix
The tooltip prefix.
final Orientation orientationDiv
The gauge's orientation.
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.