00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 package com.realtime.crossfire.jxclient.gui.gauge;
00023
00024 import com.realtime.crossfire.jxclient.gui.gui.AbstractGUIElement;
00025 import com.realtime.crossfire.jxclient.gui.gui.GUIElementListener;
00026 import com.realtime.crossfire.jxclient.gui.gui.TooltipManager;
00027 import java.awt.AlphaComposite;
00028 import java.awt.Dimension;
00029 import java.awt.Graphics;
00030 import java.awt.Graphics2D;
00031 import java.awt.Image;
00032 import java.awt.Transparency;
00033 import org.jetbrains.annotations.NotNull;
00034 import org.jetbrains.annotations.Nullable;
00035
00042 public class GUIGauge extends AbstractGUIElement implements GUIGaugeListener {
00043
00047 private static final long serialVersionUID = 1;
00048
00053 @Nullable
00054 private final String tooltipPrefix;
00055
00060 @NotNull
00061 private String tooltipText = "";
00062
00066 @Nullable
00067 private final Image emptyImage;
00068
00072 @NotNull
00073 private final Orientation orientation;
00074
00078 @NotNull
00079 private final GaugeState gaugeState;
00080
00084 private final float alpha;
00085
00089 private boolean hidden = false;
00090
00106 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 tooltipPrefix, final float alpha) {
00107 super(tooltipManager, elementListener, name, alpha < 1F ? Transparency.TRANSLUCENT : Transparency.OPAQUE);
00108 this.emptyImage = emptyImage;
00109 this.orientation = orientation;
00110 this.tooltipPrefix = tooltipPrefix;
00111 gaugeState = new GaugeState(fullImage, negativeImage, 0, 0);
00112 this.alpha = alpha;
00113 tooltipText = "-";
00114 orientation.setExtends(getWidth(), getHeight());
00115 orientation.setHasNegativeImage(negativeImage != null);
00116 orientation.setValues(0, 0, 0);
00117 gaugeState.setValues(orientation);
00118 updateTooltipText();
00119 }
00120
00124 @Override
00125 public void setBounds(final int x, final int y, final int width, final int height) {
00126 super.setBounds(x, y, width, height);
00127 if (orientation.setExtends(width, height)) {
00128 if (gaugeState.setValues(orientation)) {
00129 setChanged();
00130 }
00131 }
00132 }
00133
00137 @Override
00138 public void dispose() {
00139 super.dispose();
00140 }
00141
00145 @Override
00146 public void execute() {
00147
00148 }
00149
00153 @Override
00154 public void paintComponent(@NotNull final Graphics g) {
00155 if (hidden) {
00156 return;
00157 }
00158
00159 final Graphics paint;
00160 if (alpha < 1F) {
00161 final Graphics2D g2d = (Graphics2D)g.create();
00162 g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, alpha));
00163 paint = g2d;
00164 } else {
00165 paint = g;
00166 }
00167
00168 super.paintComponent(paint);
00169 if (emptyImage != null) {
00170 paint.drawImage(emptyImage, 0, 0, null);
00171 }
00172 gaugeState.draw(paint);
00173 if (paint != g) {
00174 paint.dispose();
00175 }
00176 }
00177
00181 @Override
00182 public void setValues(final int curValue, final int minValue, final int maxValue, @NotNull final String labelText, @NotNull final String tooltipText) {
00183 if (!orientation.setValues(curValue, minValue, maxValue) && this.tooltipText.equals(tooltipText)) {
00184 return;
00185 }
00186
00187 this.tooltipText = tooltipText;
00188
00189 if (gaugeState.setValues(orientation)) {
00190 setChanged();
00191 }
00192
00193 updateTooltipText();
00194 }
00195
00200 private void updateTooltipText() {
00201 setTooltipText(tooltipPrefix == null || tooltipText.length() == 0 ? null : tooltipPrefix+tooltipText);
00202 }
00203
00207 @Nullable
00208 @Override
00209 public Dimension getPreferredSize() {
00210 return gaugeState.getPreferredSize();
00211 }
00212
00216 @Nullable
00217 @Override
00218 public Dimension getMinimumSize() {
00219 return gaugeState.getPreferredSize();
00220 }
00221
00225 @Nullable
00226 @Override
00227 public Dimension getMaximumSize() {
00228 return gaugeState.getPreferredSize();
00229 }
00230
00234 @Override
00235 public void setHidden(final boolean hidden) {
00236 this.hidden = hidden;
00237 }
00238 }