Crossfire JXClient, Trunk  R20561
RenderStateManager.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 
24 import java.util.List;
25 import org.jetbrains.annotations.NotNull;
26 
31 public class RenderStateManager {
32 
36  @NotNull
38 
42  @NotNull
43  private final Buffer buffer;
44 
48  private int lastTopIndex = -1;
49 
53  private int lastTopOffset = -1;
54 
59  private int lastScrollPos = -1;
60 
65  private boolean lastCanScrollDown;
66 
71  private boolean lastCanScrollUp;
72 
76  @NotNull
77  private final RenderState renderState = new RenderState();
78 
82  @NotNull
84 
85  @Override
86  public void lineAdded() {
87  renderState.linesAdded(buffer);
88  fireChanges();
89  }
90 
91  @Override
92  public void lineReplaced() {
93  renderState.linesReplaced(buffer);
94  fireChanges();
95  }
96 
97  @Override
98  public void linesRemoved(@NotNull final List<Line> lines) {
99  renderState.linesRemoved(buffer, lines);
100  fireChanges();
101  }
102 
103  };
104 
110  public RenderStateManager(@NotNull final RenderStateListener renderStateListener, @NotNull final Buffer buffer) {
111  this.renderStateListener = renderStateListener;
112  this.buffer = buffer;
113  this.buffer.addBufferListener(bufferListener);
114  fireChanges();
115  }
116 
121  public void setHeight(final int height) {
122  renderState.setHeight(buffer, height);
123  }
124 
129  public void dispose() {
130  buffer.removeBufferListener(bufferListener);
131  }
132 
137  public int getTopIndex() {
138  return renderState.getTopIndex();
139  }
140 
145  public int getTopOffset() {
146  return renderState.getTopOffset();
147  }
148 
153  public int getScrollPos() {
154  return renderState.getScrollPos();
155  }
156 
160  public void resetScroll() {
161  renderState.scrollToBottom(buffer);
162  fireChanges();
163  }
164 
169  public void scrollUp(final int dy) {
170  assert dy > 0;
171  renderState.scrollTo(buffer, renderState.getScrollPos()-dy);
172  fireChanges();
173  }
174 
179  public void scrollDown(final int dy) {
180  assert dy > 0;
181  renderState.scrollTo(buffer, renderState.getScrollPos()+dy);
182  fireChanges();
183  }
184 
189  public void scrollTo(final int y) {
190  renderState.scrollTo(buffer, y);
191  fireChanges();
192  }
193 
198  public boolean canScrollUp() {
199  return renderState.canScrollUp();
200  }
201 
206  public boolean canScrollDown() {
207  return renderState.canScrollDown();
208  }
209 
213  private void fireChanges() {
214  boolean fireChanges = false;
215 
216  if (lastTopIndex != renderState.getTopIndex()) {
217  lastTopIndex = renderState.getTopIndex();
218  fireChanges = true;
219  }
220 
221  if (lastTopOffset != renderState.getTopOffset()) {
222  lastTopOffset = renderState.getTopOffset();
223  fireChanges = true;
224  }
225 
226  if (lastScrollPos != renderState.getScrollPos()) {
227  lastScrollPos = renderState.getScrollPos();
228  fireChanges = true;
229  }
230 
231  if (lastCanScrollDown != renderState.canScrollDown()) {
232  lastCanScrollDown = renderState.canScrollDown();
233  fireChanges = true;
234  }
235 
236  if (lastCanScrollUp != renderState.canScrollUp()) {
237  lastCanScrollUp = renderState.canScrollUp();
238  fireChanges = true;
239  }
240 
241  if (renderState.mustRepaint() || fireChanges) {
242  renderStateListener.stateChanged();
243  }
244  }
245 
246 }
boolean canScrollUp()
Returns whether scrolling up is possible.
int getTopIndex()
Returns the index of the first line to display.
boolean mustRepaint
Whether the view has to be repainted even if no other value has changed.
void scrollToBottom(@NotNull final Buffer buffer)
Sets the view area to the bottom-most value.
void linesReplaced(@NotNull final Buffer buffer)
Some lines have been replaced at the end of the buffer.
int lastTopIndex
The first line of buffer to display.
final RenderStateListener renderStateListener
The listener to notify about state changes.
boolean canScrollUp
Whether scrolling up is possible.
int lastTopOffset
The number of pixels to shift the first displayed line.
int lastScrollPos
The position in pixels of the viewable window.
void linesAdded(@NotNull final Buffer buffer)
Some lines have been added to the buffer.
boolean canScrollDown()
Returns whether scrolling down is possible.
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
int getScrollPos()
Returns the location of the view area in pixels.
void linesRemoved(@NotNull final Buffer buffer, @NotNull final Collection< Line > lines)
Some lines have been removed from the buffer.
void addBufferListener(@NotNull final BufferListener listener)
Adds a listener to notify of changes.
Definition: Buffer.java:321
final RenderState renderState
The underlying RenderState instance.
Interface for listeners interested in changes of a RenderStateManager instance.
void setHeight(final int height)
Sets the viewable height in pixel.
boolean lastCanScrollDown
The last known result of RenderState#canScrollDown for renderState.
boolean canScrollDown
Whether scrolling down is possible.
boolean lastCanScrollUp
The last known result of RenderState#canScrollUp for renderState.
void setHeight(@NotNull final Buffer buffer, final int h)
Sets the the viewable height in pixel.
int getScrollPos()
Returns the location of the view area in pixels.
void resetScroll()
Resets the scrolling range to default values.
void stateChanged()
Notifies the listener about a state change in the tracked RenderStateManager object.
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
Encapsulates the state for rendering a Buffer instance.
RenderStateManager(@NotNull final RenderStateListener renderStateListener, @NotNull final Buffer buffer)
Creates a new instance.
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
final BufferListener bufferListener
The listener to re-render the window contents after changes.
Interface for listeners for changes of Buffer contents.
Encapsulates the state for a scroll bar.
void scrollTo(@NotNull final Buffer buffer, final int y)
Scrolls to the given pixel location.
void removeBufferListener(@NotNull final BufferListener listener)
Removes a listener to be notified of changes.
Definition: Buffer.java:329
void scrollDown(final int dy)
Scrolls down by pixels.