Crossfire JXClient, Trunk  R20561
GUILog.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.log;
23 
30 import java.awt.Color;
31 import java.awt.Dimension;
32 import java.awt.Graphics;
33 import java.awt.Graphics2D;
34 import java.awt.GraphicsEnvironment;
35 import java.awt.Image;
36 import java.awt.Transparency;
37 import java.awt.font.FontRenderContext;
38 import java.awt.image.BufferedImage;
39 import java.util.Iterator;
40 import org.jetbrains.annotations.NotNull;
41 import org.jetbrains.annotations.Nullable;
42 
48 public abstract class GUILog extends AbstractGUIElement implements GUIScrollable2 {
49 
53  private static final long serialVersionUID = 1;
54 
58  private static final int SCROLL_PIXEL = 12;
59 
63  @NotNull
65 
69  @NotNull
70  private final Buffer buffer;
71 
76  @Nullable
77  private final Image backgroundImage;
78 
82  @NotNull
83  private final Fonts fonts;
84 
88  @NotNull
90 
94  @NotNull
95  @SuppressWarnings("FieldCanBeLocal")
97 
98  @Override
99  public void stateChanged() {
100  setChanged();
101  for (final ScrollableListener listener : listeners) {
102  listener.setRange(0, buffer.getTotalHeight(), renderStateManager.getScrollPos(), getHeight());
103  }
104  }
105 
106  @Override
107  public int getHeight() {
108  return Math.max(1, GUILog.this.getHeight());
109  }
110 
111  };
112 
122  protected GUILog(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image backgroundImage, @NotNull final Fonts fonts) {
123  super(tooltipManager, elementListener, name, Transparency.TRANSLUCENT);
124  this.backgroundImage = backgroundImage;
125  this.fonts = fonts;
126  final FontRenderContext context;
127  final GraphicsEnvironment graphicsEnvironment = GraphicsEnvironment.getLocalGraphicsEnvironment();
128  final Graphics2D g = graphicsEnvironment.createGraphics(new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB)); // XXX
129  try {
130  context = g.getFontRenderContext();
131  } finally {
132  g.dispose();
133  }
134  buffer = new Buffer(fonts, context, getWidth());
135  renderStateManager = new RenderStateManager(renderStateListener, buffer);
136  renderStateManager.setHeight(getHeight());
137  }
138 
142  @Override
143  public void dispose() {
144  super.dispose();
145  renderStateManager.dispose();
146  }
147 
151  @Override
152  public void paintComponent(@NotNull final Graphics g) {
153  super.paintComponent(g);
154 
155  g.setColor(new Color(0, 0, 0, 0.0f));
156  g.fillRect(0, 0, getWidth(), getHeight());
157  if (backgroundImage != null) {
158  g.drawImage(backgroundImage, 0, 0, null);
159  }
160 
161  int y = -renderStateManager.getTopOffset();
162  final int topIndex = renderStateManager.getTopIndex();
163  synchronized (buffer.getSyncObject()) {
164  final Iterator<Line> it = buffer.listIterator(topIndex);
165  while (y < getHeight() && it.hasNext()) {
166  final Line line = it.next();
167  drawLine(g, y, line);
168  y += line.getHeight();
169  }
170  }
171  }
172 
179  private void drawLine(@NotNull final Graphics g, final int y, @NotNull final Iterable<Segment> line) {
180  for (final Segment segment : line) {
181  segment.draw(g, y, fonts);
182  }
183  }
184 
188  @Override
189  public boolean canScroll(final int distance) { // XXX: implement |distance|>1
190  if (distance < 0) {
191  return renderStateManager.canScrollUp();
192  }
193  //noinspection SimplifiableIfStatement
194  if (distance > 0) {
195  return renderStateManager.canScrollDown();
196  }
197  return false;
198  }
199 
203  @Override
204  public void scroll(final int distance) {
205  if (distance < 0) {
206  renderStateManager.scrollUp(-distance*SCROLL_PIXEL);
207  } else if (distance > 0) {
208  renderStateManager.scrollDown(distance*SCROLL_PIXEL);
209  } else {
210  assert false;
211  }
212  }
213 
217  @Override
218  public void resetScroll() {
219  renderStateManager.resetScroll();
220  }
221 
225  @Override
226  public void scrollTo(final int pos) {
227  renderStateManager.scrollTo(pos);
228  }
229 
233  @Override
234  public void addScrollableListener(@NotNull final ScrollableListener listener) {
235  listeners.add(listener);
236  }
237 
241  @Override
242  public void removeScrollableListener(@NotNull final ScrollableListener listener) {
243  listeners.remove(listener);
244  }
245 
250  @NotNull
251  public Buffer getBuffer() {
252  return buffer;
253  }
254 
258  @Override
259  public void setBounds(final int x, final int y, final int width, final int height) {
260  super.setBounds(x, y, width, height);
261  buffer.setRenderWidth(width);
262  renderStateManager.setHeight(height);
263  }
264 
268  @Nullable
269  @Override
270  public Dimension getPreferredSize() {
271  return new Dimension(600, 150); // XXX
272  }
273 
277  @Nullable
278  @Override
279  public Dimension getMinimumSize() {
280  return new Dimension(100, 10); // XXX
281  }
282 
283 }
static final long serialVersionUID
The serial version UID.
Definition: GUILog.java:53
void scrollTo(final int pos)
Scrolls to the given location.The possible range is given by a previous notification through a listen...
Definition: GUILog.java:226
boolean canScrollUp()
Returns whether scrolling up is possible.
Manages the contents of one text line.
Definition: Line.java:38
int getTotalHeight()
Returns the total height of all lines.
Definition: Buffer.java:237
void addScrollableListener(@NotNull final ScrollableListener listener)
Adds a scrollable listener to be informed about changes.the listener to add
Definition: GUILog.java:234
final TooltipManager tooltipManager
The TooltipManager to update.
void removeScrollableListener(@NotNull final ScrollableListener listener)
Removes a scrollable listener.the listener to remove
Definition: GUILog.java:242
void setChanged()
Records that the contents have changed and must be repainted.
int getHeight()
Returns the height of this line.
Definition: Line.java:95
boolean canScrollDown()
Returns whether scrolling down is possible.
void scroll(final int distance)
Scrolls the element.the distance to scroll
Definition: GUILog.java:204
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
final RenderStateManager renderStateManager
The rendering state.
Definition: GUILog.java:89
final GUIElementListener elementListener
The GUIElementListener to notify.
Object getSyncObject()
Returns the object to synchronize on when calling iterator() or listIterator(int).
Definition: Buffer.java:339
final Image backgroundImage
The background image drawn below the text contents.
Definition: GUILog.java:77
Interface for listeners interested in changes of a RenderStateManager instance.
final Fonts fonts
The Fonts instance for looking up fonts.
Definition: GUILog.java:83
GUILog(@NotNull final TooltipManager tooltipManager, @NotNull final GUIElementListener elementListener, @NotNull final String name, @Nullable final Image backgroundImage, @NotNull final Fonts fonts)
Creates a new instance.
Definition: GUILog.java:122
void dispose()
Releases all allocated resources.
Definition: GUILog.java:143
void setHeight(final int height)
Sets the viewable height in pixel.
final EventListenerList2< ScrollableListener > listeners
All listeners.
Definition: GUILog.java:64
One segment of a Line which should be displayed without changing text attributes. ...
Definition: Segment.java:34
int getScrollPos()
Returns the location of the view area in pixels.
Buffer getBuffer()
Returns the Buffer instance containing the text messages.
Definition: GUILog.java:251
void resetScroll()
Resets the scrolling range to default values.
void paintComponent(@NotNull final Graphics g)
Definition: GUILog.java:152
void add(@NotNull final T listener)
Adds a listener.
Encapsulates the state for rendering a Buffer instance.
void setBounds(final int x, final int y, final int width, final int height)
Definition: GUILog.java:259
boolean canScroll(final int distance)
Returns whether scrolling is possible.the distance to scroll whether scrolling is possible ...
Definition: GUILog.java:189
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
Iterator< Line > listIterator(final int line)
Returns an Iterator for the lines in this buffer.
Definition: Buffer.java:260
final Buffer buffer
The Buffer containing all received text messages.
Definition: GUILog.java:70
Abstract base class for GUI elements to be shown in Guis.
A GUIScrollable that can be attached to GUIScrollBars.
void setRenderWidth(final int renderWidth)
Updates the width to render.
Definition: Buffer.java:127
void remove(@NotNull final T listener)
Removes a listener.
Abstract base class for gui elements implementing text fields.
Definition: GUILog.java:48
final RenderStateListener renderStateListener
The RenderStateListener attached to renderStateManager.
Definition: GUILog.java:96
void resetScroll()
Resets the scroll index to the default value.
Definition: GUILog.java:218
static final int SCROLL_PIXEL
The number of pixels to scroll.
Definition: GUILog.java:58
void drawLine(@NotNull final Graphics g, final int y, @NotNull final Iterable< Segment > line)
Draws one Line to a Graphics2D instance.
Definition: GUILog.java:179
void scrollDown(final int dy)
Scrolls down by pixels.