Crossfire JXClient, Trunk  R20561
GUIGauge.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.AlphaComposite;
29 import java.awt.Dimension;
30 import java.awt.Graphics;
31 import java.awt.Graphics2D;
32 import java.awt.Image;
33 import java.awt.Transparency;
34 import java.awt.event.MouseEvent;
35 import org.jetbrains.annotations.NotNull;
36 import org.jetbrains.annotations.Nullable;
37 
44 public class GUIGauge extends AbstractGUIElement implements GUIGaugeListener {
45 
49  private static final long serialVersionUID = 1;
50 
55  @Nullable
56  private final String tooltipFormat;
57 
61  @Nullable
62  private final CommandList commandList;
63 
67  @NotNull
68  private String tooltipText = "";
69 
73  @Nullable
74  private final Image emptyImage;
75 
79  @NotNull
80  private final Orientation orientation;
81 
85  @NotNull
86  private final GaugeState gaugeState;
87 
91  private final float alpha;
92 
96  private boolean hidden;
97 
114  public GUIGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image fullImage, @Nullable final Image negativeImage, @Nullable final Image emptyImage, @NotNull final Orientation orientation, @Nullable final String tooltipFormat, final float alpha, @Nullable final CommandList commandList) {
115  super(tooltipManager, elementListener, name, alpha < 1.0F ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
116  this.emptyImage = emptyImage;
117  this.orientation = orientation;
118  this.tooltipFormat = tooltipFormat;
119  this.commandList = commandList;
120  gaugeState = new GaugeState(fullImage, negativeImage, 0, 0);
121  this.alpha = alpha;
122  tooltipText = "-"; // make sure the following setValues() does not short-cut
123  orientation.setExtends(getWidth(), getHeight());
124  orientation.setHasNegativeImage(negativeImage != null);
125  orientation.setValues(0, 0, 0);
126  gaugeState.setValues(orientation);
128  }
129 
133  @Override
134  public void setBounds(final int x, final int y, final int width, final int height) {
135  super.setBounds(x, y, width, height);
136  if (orientation.setExtends(width, height)) {
137  if (gaugeState.setValues(orientation)) {
138  setChanged();
139  }
140  }
141  }
142 
146  @Override
147  public void paintComponent(@NotNull final Graphics g) {
148  if (hidden) {
149  return;
150  }
151 
152  final Graphics paint;
153  if (alpha < 1.0F) {
154  final Graphics2D g2d = (Graphics2D)g.create();
155  g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
156  paint = g2d;
157  } else {
158  paint = g;
159  }
160 
161  super.paintComponent(paint);
162  if (emptyImage != null) {
163  paint.drawImage(emptyImage, 0, 0, null);
164  }
165  gaugeState.draw(paint);
166  if (paint != g) {
167  paint.dispose();
168  }
169  }
170 
174  @Override
175  public void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText) {
176  if (!orientation.setValues(curValue, minValue, maxValue) && this.tooltipText.equals(tooltipText)) {
177  return;
178  }
179 
180  this.tooltipText = tooltipText;
181 
182  if (gaugeState.setValues(orientation)) {
183  setChanged();
184  }
185 
187  }
188 
193  private void updateTooltipText() {
195  }
196 
200  @Nullable
201  @Override
202  public Dimension getPreferredSize() {
203  return gaugeState.getPreferredSize();
204  }
205 
209  @Nullable
210  @Override
211  public Dimension getMinimumSize() {
212  return gaugeState.getPreferredSize();
213  }
214 
218  @Nullable
219  @Override
220  public Dimension getMaximumSize() {
221  return gaugeState.getPreferredSize();
222  }
223 
227  @Override
228  public void setHidden(final boolean hidden) {
229  this.hidden = hidden;
230  }
231 
235  @Override
236  public void mouseClicked(@NotNull final MouseEvent e) {
237  super.mouseClicked(e);
238  switch (e.getButton()) {
239  case MouseEvent.BUTTON1:
240  break;
241 
242  case MouseEvent.BUTTON2:
243  if (commandList != null) {
244  commandList.execute();
245  }
246  break;
247 
248  case MouseEvent.BUTTON3:
249  break;
250  }
251  }
252 
263  @Nullable
264  private String formatTooltip() {
265  if (tooltipFormat == null) {
266  return null;
267  }
268  final StringBuilder sb = new StringBuilder();
269  final char[] formatChars = tooltipFormat.toCharArray();
270  int i = 0;
271  while (i < formatChars.length) {
272  final char ch = formatChars[i++];
273  if (ch != '%' || i >= formatChars.length) {
274  sb.append(ch);
275  } else {
276  switch (formatChars[i++]) {
277  case '%':
278  sb.append('%');
279  break;
280 
281  case 'T':
282  sb.append(tooltipText);
283  break;
284 
285  default:
286  sb.append('%');
287  sb.append(formatChars[i-1]);
288  break;
289  }
290  }
291  }
292  return sb.toString();
293  }
294 
295 }
Interface which is implemented by all listener classes.
final TooltipManager tooltipManager
The TooltipManager to update.
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.
Dimension getPreferredSize()
Returns the preferred size.
void mouseClicked(@NotNull final MouseEvent e)
Will be called when the user has clicked (pressed+released) this element.This event will be delivered...
Definition: GUIGauge.java:236
static final long serialVersionUID
The serial version UID.
Definition: GUIGauge.java:49
void paintComponent(@NotNull final Graphics g)
Definition: GUIGauge.java:147
final GUIElementListener elementListener
The GUIElementListener to notify.
boolean setValues(int cur, int min, int max)
Sets the gauge&#39;s values.
final float alpha
The gauge alpha value, 1 is opaque and 0 full transparent.
Definition: GUIGauge.java:91
final GaugeState gaugeState
The gauge state.
Definition: GUIGauge.java:86
final Orientation orientation
The gauge&#39;s orientation.
Definition: GUIGauge.java:80
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...
Definition: GUIGauge.java:175
boolean hidden
If true, the gauge will not paint itself, whatever its visibility.
Definition: GUIGauge.java:96
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 String tooltipFormat
The tooltip format.
Definition: GUIGauge.java:56
void setBounds(final int x, final int y, final int width, final int height)
Definition: GUIGauge.java:134
final CommandList commandList
The CommandList that is executed on button 2.
Definition: GUIGauge.java:62
boolean setExtends(int width, int height)
Sets the extends of the image.
void updateTooltipText()
Updates the tooltip&#39;s text from tooltipFormat ad tooltipText.
Definition: GUIGauge.java:193
final Image emptyImage
The image representing an empty gauge.
Definition: GUIGauge.java:74
boolean setValues(@NotNull final Orientation orientation)
Updates the values from a Orientation state.
Abstract base class for GUI elements to be shown in Guis.
String tooltipText
The default tooltip text.
Definition: GUIGauge.java:68
Displays a value as a graphical gauge that&#39;s filling state depends on the value.
Definition: GUIGauge.java:44
GUIGauge(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image fullImage, @Nullable final Image negativeImage, @Nullable final Image emptyImage, @NotNull final Orientation orientation, @Nullable final String tooltipFormat, final float alpha, @Nullable final CommandList commandList)
Creates a new instance.
Definition: GUIGauge.java:114
void execute()
Execute the command list by calling GUICommand#execute() for each command in order.
String formatTooltip()
Returns a formatted string using the given format.
Definition: GUIGauge.java:264
void setHidden(final boolean hidden)
Specify whether this item should hide itself, independent of the "visibility" state.whether the gauge is hidden
Definition: GUIGauge.java:228