Crossfire JXClient, Trunk  R20561
RenderState.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.Collection;
25 import org.jetbrains.annotations.NotNull;
26 
34 public class RenderState {
35 
39  @NotNull
40  private final Object sync = new Object();
41 
45  private int height = 1;
46 
51  private int topIndex;
52 
60  private int topOffset = -height;
61 
65  private int scrollPos;
66 
70  private boolean canScrollUp;
71 
75  private boolean canScrollDown;
76 
80  private boolean mustRepaint = true;
81 
87  public void setHeight(@NotNull final Buffer buffer, final int h) {
88  final int bufferHeight = buffer.getTotalHeight();
89  synchronized (sync) {
90  final int oldHeight = height;
91  height = h;
92  if (bufferHeight <= height) {
93  scrollPos = 0;
94  topIndex = 0;
95  topOffset = 0;
96  canScrollUp = false;
97  canScrollDown = false;
98  } else if (topOffset < 0 || scrollPos > bufferHeight-height || scrollPos == bufferHeight-oldHeight) {
99  scrollToBottom(buffer);
100  }
101  }
102  }
103 
108  public void linesAdded(@NotNull final Buffer buffer) {
109  synchronized (sync) {
110  if (topOffset < 0) {
111  scrollToBottom(buffer);
112  } else if (!canScrollDown) {
113  scrollToBottom(buffer);
114  mustRepaint = true;
115  } else {
116  mustRepaint = true;
117  }
118  }
119  }
120 
125  public void linesReplaced(@NotNull final Buffer buffer) {
126  synchronized (sync) {
127  if (topOffset < 0 || !canScrollDown) {
128  scrollToBottom(buffer);
129  }
130  mustRepaint = true;
131  }
132  }
133 
139  public void linesRemoved(@NotNull final Buffer buffer, @NotNull final Collection<Line> lines) {
140  final int bufferHeight = buffer.getTotalHeight();
141  synchronized (sync) {
142  if (bufferHeight <= height) {
143  scrollPos = 0;
144  topIndex = 0;
145  topOffset = 0;
146  canScrollUp = false;
147  canScrollDown = false;
148  mustRepaint = true;
149  } else {
150  for (final Line line : lines) {
151  scrollPos -= line.getHeight();
152  }
153  topIndex -= lines.size();
154  if (scrollPos < 0) {
155  scrollPos = 0;
156  topIndex = 0;
157  topOffset = 0;
158  canScrollUp = false;
159  mustRepaint = true;
160  } else {
161  assert topIndex >= 0;
162  // canScrollUp is unaffected
163  }
164  // canScrollDown is unaffected
165  }
166  }
167  }
168 
173  public int getTopIndex() {
174  synchronized (sync) {
175  return topIndex;
176  }
177  }
178 
183  public int getTopOffset() {
184  synchronized (sync) {
185  return topOffset;
186  }
187  }
188 
193  public int getScrollPos() {
194  synchronized (sync) {
195  return scrollPos;
196  }
197  }
198 
203  public boolean canScrollUp() {
204  synchronized (sync) {
205  return canScrollUp;
206  }
207  }
208 
213  public boolean canScrollDown() {
214  synchronized (sync) {
215  return canScrollDown;
216  }
217  }
218 
226  public boolean mustRepaint() {
227  synchronized (sync) {
228  final boolean result = mustRepaint;
229  mustRepaint = false;
230  return result;
231  }
232  }
233 
239  public void scrollTo(@NotNull final Buffer buffer, final int y) {
240  final int bufferHeight = buffer.getTotalHeight();
241  synchronized (sync) {
242  if (bufferHeight > height) {
243  scrollPos = Math.max(Math.min(y, bufferHeight-height), 0);
244  topIndex = 0;
245  int yPos = scrollPos;
246  while (yPos > 0) {
247  final int lineHeight = buffer.getLine(topIndex).getHeight();
248  if (yPos < lineHeight) {
249  break;
250  }
251 
252  yPos -= lineHeight;
253  topIndex++;
254  }
255  assert yPos >= 0;
256  topOffset = yPos;
257  canScrollUp = topIndex > 0 || topOffset > 0;
258  canScrollDown = y+height < bufferHeight;
259  //} else {
260  // ignore
261  }
262  }
263  }
264 
269  public void scrollToBottom(@NotNull final Buffer buffer) {
270  final int bufferHeight = buffer.getTotalHeight();
271  synchronized (sync) {
272  if (bufferHeight <= height) {
273  scrollPos = 0;
274  topIndex = 0;
275  topOffset = 0;
276  canScrollUp = false;
277  canScrollDown = false;
278  } else {
279  scrollPos = Math.max(bufferHeight-height, 0);
280  topIndex = buffer.size();
281  int y = height;
282  while (y > 0) {
283  topIndex--;
284  assert topIndex >= 0;
285  y -= buffer.getLine(topIndex).getHeight();
286  }
287  topOffset = -y;
288  canScrollUp = topIndex > 0 || topOffset > 0;
289  canScrollDown = false;
290  }
291  }
292  }
293 
294 }
Manages the contents of one text line.
Definition: Line.java:38
int height
The height of the viewable area.
boolean canScrollDown()
Returns whether scrolling down is possible.
int getTopIndex()
Returns the index of the first line to display.
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 topIndex
The first line to display in the viewable area.
boolean canScrollUp
Whether scrolling up is possible.
void linesAdded(@NotNull final Buffer buffer)
Some lines have been added to the buffer.
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.
int topOffset
The number of pixels to shift the first displayed line (topIndex.
boolean canScrollDown
Whether scrolling down is possible.
final Object sync
Synchronization object for accesses to all fields.
void setHeight(@NotNull final Buffer buffer, final int h)
Sets the the viewable height in pixel.
int getTopOffset()
Returns the number of pixels to shift the first displayed line.
boolean mustRepaint()
Returns whether the view should be repainted even if no other values have changed.
boolean canScrollUp()
Returns whether scrolling up is possible.
int scrollPos
The location of the view area relative to the buffer&#39;s total height.
Manages the contents of the contents of a log window.
Definition: Buffer.java:41
Encapsulates the state for a scroll bar.
void scrollTo(@NotNull final Buffer buffer, final int y)
Scrolls to the given pixel location.