1 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 AdjustmentListener 29 { 30 private final Editor editor; 31 private final JScrollBar scrollBar; 32 33 public VerticalScrollBarListener(Editor editor, JScrollBar scrollBar) 34 { 35 this.editor = editor; 36 this.scrollBar = scrollBar; 37 } 38 39 public void adjustmentValueChanged(AdjustmentEvent e) 40 { 41 if (editor.inScrollBarUpdate) 42 return; 43 final Display display = editor.getDisplay(); 44 if (display.getTopLine() == null) { 45 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; } 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 } 75 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; 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 |