Crossfire JXClient, Trunk  R20561
GUIScrollBar.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.misc;
23 
29 import java.awt.Color;
30 import java.awt.Dimension;
31 import java.awt.Graphics;
32 import java.awt.Transparency;
33 import java.awt.event.MouseEvent;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
36 
41 public class GUIScrollBar extends ActivatableGUIElement implements ScrollableListener {
42 
46  private static final long serialVersionUID = 1;
47 
52  private final boolean proportionalSlider;
53 
57  @NotNull
58  private final GUIScrollable2 scrollable;
59 
63  @NotNull
64  private final Color colorBackground;
65 
69  @NotNull
70  private final Color colorForeground;
71 
75  private int valueMin;
76 
80  private int valueSize = 1;
81 
85  private int sliderSize = 1;
86 
90  private int sliderPos;
91 
96  private int offset;
97 
101  private boolean scrolling;
102 
114  public GUIScrollBar(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean proportionalSlider, @NotNull final GUIScrollable2 scrollable, @NotNull final Color colorBackground, @NotNull final Color colorForeground) {
115  super(tooltipManager, elementListener, name, Transparency.OPAQUE);
116  this.proportionalSlider = proportionalSlider;
117  this.scrollable = scrollable;
118  this.colorBackground = colorBackground;
119  this.colorForeground = colorForeground;
120  this.scrollable.addScrollableListener(this);
121  }
122 
126  @Override
127  public void dispose() {
128  super.dispose();
129  scrollable.removeScrollableListener(this);
130  }
131 
135  @Override
136  public void setRange(final int valueMin, final int valueMax, final int sliderPos, final int sliderSize) {
137  if (valueMax <= valueMin) {
138  throw new IllegalArgumentException("valueMax="+valueMax+" <= "+valueMin+"="+valueMin);
139  }
140  if (sliderSize <= 0) {
141  throw new IllegalArgumentException("sliderSize="+sliderSize+" <= 0");
142  }
143 
144  this.valueMin = valueMin;
145  valueSize = valueMax-valueMin;
146  this.sliderSize = Math.min(sliderSize, valueSize);
147  setPosition(sliderPos);
148  }
149 
153  @Override
154  protected void activeChanged() {
155  }
156 
160  @Override
161  public void mousePressed(@NotNull final MouseEvent e) {
162  super.mousePressed(e);
163  switch (e.getButton()) {
164  case MouseEvent.BUTTON1:
165  final int sh = getSliderHeightPixels();
166  offset = e.getY()-getSliderPosPixels(sh);
167  if (offset < 0) {
168  scrollable.scrollTo(sliderPos-sliderSize);
169  } else if (offset >= sh) {
170  scrollable.scrollTo(sliderPos+sliderSize);
171  } else {
172  scrolling = true;
173  }
174  break;
175 
176  case MouseEvent.BUTTON2:
177  break;
178 
179  case MouseEvent.BUTTON3:
180  break;
181  }
182  }
183 
187  @Override
188  public void mouseReleased(@NotNull final MouseEvent e) {
189  super.mouseReleased(e);
190  switch (e.getButton()) {
191  case MouseEvent.BUTTON1:
192  scrolling = false;
193  break;
194 
195  case MouseEvent.BUTTON2:
196  break;
197 
198  case MouseEvent.BUTTON3:
199  break;
200  }
201  }
202 
206  @Override
207  public void mouseDragged(@NotNull final MouseEvent e) {
208  super.mouseDragged(e);
209  if (scrolling) {
210  scrollable.scrollTo(getSliderPos(e.getY()-offset));
211  }
212  }
213 
217  @Override
218  public void execute() {
219  // ignore
220  }
221 
227  private int getSliderPos(final int yPixels) {
228  return (int)Math.round(yPixels*(double)(valueSize-sliderSize)/(getHeight()-getSliderHeightPixels()));
229  }
230 
236  @SuppressWarnings("IfMayBeConditional")
237  private void setPosition(final int pos) {
238  if (pos < valueMin) {
239  sliderPos = valueMin;
240  } else if (pos+sliderSize > valueMin+valueSize) {
241  sliderPos = valueMin+valueSize-sliderSize;
242  } else {
243  sliderPos = pos;
244  }
245  setChanged();
246  }
247 
252  private int getSliderHeightPixels() {
253  return proportionalSlider ? Math.max((int)Math.round(getHeight()*(double)sliderSize/valueSize), getWidth()) : getWidth();
254  }
255 
261  private int getSliderPosPixels(final int sh) {
262  return (int)Math.round(sliderPos*(double)(getHeight()-sh)/(valueSize-sliderSize));
263  }
264 
268  @Override
269  public void paintComponent(@NotNull final Graphics g) {
270  super.paintComponent(g);
271  final int sh = getSliderHeightPixels();
272  final int sy = getSliderPosPixels(sh);
273  g.setColor(colorBackground);
274  g.fillRect(0, 0, getWidth(), sy);
275  g.fillRect(0, sy+sh, getWidth(), getHeight()-sy-sh);
276  g.setColor(colorForeground);
277  g.fillRect(0, sy, getWidth(), sh);
278  }
279 
283  @Nullable
284  @Override
285  public Dimension getPreferredSize() {
286  return new Dimension(16, 64);
287  }
288 
292  @Nullable
293  @Override
294  public Dimension getMinimumSize() {
295  return new Dimension(16, 16);
296  }
297 
298 }
void setRange(final int valueMin, final int valueMax, final int sliderPos, final int sliderSize)
Sets the scroll range.the minimum scroll value the maximum scroll value the scroll location; it need ...
final TooltipManager tooltipManager
The TooltipManager to update.
final GUIElementListener elementListener
The GUIElementListener to notify.
void setChanged()
Records that the contents have changed and must be repainted.
int getSliderPosPixels(final int sh)
Returns the y-coordinate of the slider.
final boolean proportionalSlider
If set, make the slider size reflect the visible area; if unset, display the slider as a square...
void mouseDragged(@NotNull final MouseEvent e)
Will be called when the mouse moves within this component while the button is pressed.This event will be delivered after mouseMoved(MouseEvent). Note: if the mouse leaves this element&#39;s bounding box while the mouse button is still pressed, furthermouseDragged (but nomouseMoved ) events will be generated. the mouse event relative to this element
int offset
The offset between the mouse and the top border of the slider while dragging.
int getSliderHeightPixels()
Returns the height of the slider in pixels.
A GUIElement that can be set to active or inactive.
void dispose()
Releases all allocated resources.
static final long serialVersionUID
The serial version UID.
int getSliderPos(final int yPixels)
Returns the current slider position in slider-coordinates.
void setPosition(final int pos)
Sets the position of the slider.
GUIScrollBar(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, final boolean proportionalSlider, @NotNull final GUIScrollable2 scrollable, @NotNull final Color colorBackground, @NotNull final Color colorForeground)
Creates a new instance.
boolean scrolling
Set while dragging is active.
final Color colorForeground
The foreground color of the slider.
void removeScrollableListener(@NotNull ScrollableListener listener)
Removes a scrollable listener.
final GUIScrollable2 scrollable
The target element to scroll.
int sliderPos
The scroll location; it need not be within the scroll range.
A GUIScrollable that can be attached to GUIScrollBars.
void mouseReleased(@NotNull final MouseEvent e)
Will be called when the user has released the mouse.This event may be delivered even if no previous m...
final Color colorBackground
The background color of the slider.
void scrollTo(int pos)
Scrolls to the given location.
void mousePressed(@NotNull final MouseEvent e)
Will be called when the user has pressed the mouse inside this element.the mouse event relative to th...
int valueSize
The size of the scroll values.