01: /*
02: * VerticalScrollBarListener.java
03: *
04: * Copyright (C) 1998-2003 Peter Graves
05: * $Id: VerticalScrollBarListener.java,v 1.2 2003/08/01 17:34:17 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.j;
23:
24: import java.awt.event.AdjustmentEvent;
25: import java.awt.event.AdjustmentListener;
26: import javax.swing.JScrollBar;
27:
28: public final class VerticalScrollBarListener implements
29: AdjustmentListener {
30: private final Editor editor;
31: private final JScrollBar scrollBar;
32:
33: public VerticalScrollBarListener(Editor editor, JScrollBar scrollBar) {
34: this .editor = editor;
35: this .scrollBar = scrollBar;
36: }
37:
38: public void adjustmentValueChanged(AdjustmentEvent e) {
39: if (editor.inScrollBarUpdate)
40: return;
41: final Display display = editor.getDisplay();
42: if (display.getTopLine() == null) {
43: // Image buffer.
44: int value = e.getValue();
45: if (value != display.getPixelsAboveTopLine()) {
46: display.setPixelsAboveTopLine(value);
47: display.repaint();
48: }
49: return;
50: }
51: Line topLine = display.getTopLine();
52: int oldValue = editor.getBuffer().getY(topLine)
53: + display.getPixelsAboveTopLine();
54: int newValue = e.getValue();
55: int change = newValue - oldValue;
56: final int charHeight = Display.getCharHeight();
57: if (Math.abs(change) < charHeight) {
58: if (change > 0
59: && newValue + scrollBar.getVisibleAmount() == scrollBar
60: .getMaximum())
61: change = charHeight;
62: else
63: return; // No visible change.
64: }
65: if (topLine instanceof ImageLine) {
66: int value = e.getValue();
67: int y = editor.getBuffer().getY(topLine);
68: if (value > y && value < y + topLine.getHeight()) {
69: display.setPixelsAboveTopLine(value - y);
70: display.repaint();
71: editor.updateScrollBars();
72: return;
73: }
74: // Fall through...
75: }
76: // Find the right top line.
77: Line line = editor.getBuffer().getFirstLine();
78: int y = 0;
79: while (line != null && y + line.getHeight() <= newValue) {
80: y += line.getHeight();
81: line = line.nextVisible();
82: }
83: if (line == display.getTopLine())
84: return; // No change.
85: if (line != null) {
86: display.setTopLine(line);
87: if (line instanceof ImageLine)
88: display.setPixelsAboveTopLine(newValue - y);
89: else if (newValue > y) {
90: line = line.nextVisible();
91: if (line != null)
92: display.setTopLine(line);
93: }
94: editor.maybeScrollCaret();
95: display.repaint();
96: }
97: }
98: }
|