KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > armedbear > j > VerticalScrollBarListener


1 /*
2  * VerticalScrollBarListener.java
3  *
4  * Copyright (C) 1998-2003 Peter Graves
5  * $Id: VerticalScrollBarListener.java,v 1.2 2003/08/01 17:34:17 piso Exp $
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * 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 JavaDoc;
25 import java.awt.event.AdjustmentListener JavaDoc;
26 import javax.swing.JScrollBar JavaDoc;
27
28 public final class VerticalScrollBarListener implements AdjustmentListener JavaDoc
29 {
30     private final Editor editor;
31     private final JScrollBar JavaDoc scrollBar;
32
33     public VerticalScrollBarListener(Editor editor, JScrollBar JavaDoc scrollBar)
34     {
35         this.editor = editor;
36         this.scrollBar = scrollBar;
37     }
38
39     public void adjustmentValueChanged(AdjustmentEvent JavaDoc e)
40     {
41         if (editor.inScrollBarUpdate)
42             return;
43         final Display display = editor.getDisplay();
44         if (display.getTopLine() == null) {
45             // Image buffer.
46
int value = e.getValue();
47             if (value != display.getPixelsAboveTopLine()) {
48                 display.setPixelsAboveTopLine(value);
49                 display.repaint();
50             }
51             return;
52         }
53         Line topLine = display.getTopLine();
54         int oldValue = editor.getBuffer().getY(topLine) + display.getPixelsAboveTopLine();
55         int newValue = e.getValue();
56         int change = newValue - oldValue;
57         final int charHeight = Display.getCharHeight();
58         if (Math.abs(change) < charHeight) {
59             if (change > 0 && newValue + scrollBar.getVisibleAmount() == scrollBar.getMaximum())
60                 change = charHeight;
61             else
62                 return; // No visible change.
63
}
64         if (topLine instanceof ImageLine) {
65             int value = e.getValue();
66             int y = editor.getBuffer().getY(topLine);
67             if (value > y && value < y + topLine.getHeight()) {
68                 display.setPixelsAboveTopLine(value - y);
69                 display.repaint();
70                 editor.updateScrollBars();
71                 return;
72             }
73             // Fall through...
74
}
75         // Find the right top line.
76
Line line = editor.getBuffer().getFirstLine();
77         int y = 0;
78         while (line != null && y + line.getHeight() <= newValue) {
79             y += line.getHeight();
80             line = line.nextVisible();
81         }
82         if (line == display.getTopLine())
83             return; // No change.
84
if (line != null) {
85             display.setTopLine(line);
86             if (line instanceof ImageLine)
87                 display.setPixelsAboveTopLine(newValue - y);
88             else if (newValue > y) {
89                 line = line.nextVisible();
90                 if (line != null)
91                     display.setTopLine(line);
92             }
93             editor.maybeScrollCaret();
94             display.repaint();
95         }
96     }
97 }
98
Popular Tags