/** * Converts a y co-ordinate to a line index. * @param y The y co-ordinate */ public int yToLine(int y) { FontMetrics fm = painter.getFontMetrics(); int height = fm.getHeight(); return Math.max(0,Math.min(getLineCount() - 1, y / height + firstLine)); }
@Override public void mouseWheelMoved(MouseWheelEvent e) { JEditTextArea ta = SyntaxUtils.as(e.getSource(), JEditTextArea.class); if (ta == null) { return; } int line = ta.getFirstLine(); if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { int u = e.getUnitsToScroll(); line += u > 0 ? 1 : -1; if (line < 0) { line = 0; } else if (line >= ta.getLineCount()) { line = ta.getLineCount() - 1; } ta.setFirstLine(line); } } };
@Override public void mouseWheelMoved(MouseWheelEvent e) { if (!(e.getSource() instanceof JEditTextArea)) return; JEditTextArea ta = (JEditTextArea)e.getSource(); int line = ta.getFirstLine(); if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { int u = e.getUnitsToScroll(); line += u > 0 ? 1 : -1; if (line < 0) line = 0; else if (line >= ta.getLineCount()) line = ta.getLineCount() - 1; ta.setFirstLine(line); } }
@Override public void mouseWheelMoved(MouseWheelEvent e) { JEditTextArea ta = SyntaxUtils.as(e.getSource(), JEditTextArea.class); if (ta == null) { return; } int line = ta.getFirstLine(); if (e.getScrollType() == MouseWheelEvent.WHEEL_UNIT_SCROLL) { int u = e.getUnitsToScroll(); line += u > 0 ? 1 : -1; if (line < 0) { line = 0; } else if (line >= ta.getLineCount()) { line = ta.getLineCount() - 1; } ta.setFirstLine(line); } } };
if(newFirstLine + visibleLines >= getLineCount()) newFirstLine = getLineCount() - visibleLines; if(newFirstLine < 0) newFirstLine = 0;
public void actionPerformed(ActionEvent evt) { JEditTextArea textArea = getTextArea(evt); int lineCount = textArea.getLineCount(); int firstLine = textArea.getFirstLine(); int visibleLines = textArea.getVisibleLines(); int line = textArea.getCaretLine(); firstLine += visibleLines; if(firstLine + visibleLines >= lineCount - 1) firstLine = lineCount - visibleLines; textArea.setFirstLine(firstLine); int caret = textArea.getLineStartOffset( Math.min(textArea.getLineCount() - 1, line + visibleLines)); if(select) textArea.select(textArea.getMarkPosition(),caret); else textArea.setCaretPosition(caret); } }
protected void paintLine(Graphics gfx, TokenMarker tokenMarker, int line, int x) { Font defaultFont = getFont(); Color defaultColor = getForeground(); currentLineIndex = line; int y = textArea.lineToY(line); if(line < 0 || line >= textArea.getLineCount()) { if(paintInvalid) { paintHighlight(gfx,line,y); styles[Token.INVALID].setGraphicsFlags(gfx,defaultFont); gfx.drawString("~",0,y + fm.getHeight()); } } else if(tokenMarker == null) { paintPlainLine(gfx,line,defaultFont,defaultColor,x,y); } else { paintSyntaxLine(gfx,tokenMarker,line,defaultFont, defaultColor,x,y); } }
int lastVisibleLine = textArea.getFirstLine() + textArea.getVisibleLines(); if(lastVisibleLine >= textArea.getLineCount()) lastVisibleLine = Math.min(textArea.getLineCount() - 1, lastVisibleLine);
/** * Updates the state of the scroll bars. This should be called * if the number of lines in the document changes, or when the * size of the text are changes. */ public void updateScrollBars() { if(vertical != null && visibleLines != 0) { vertical.setValues(firstLine,visibleLines,0,getLineCount()); vertical.setUnitIncrement(2); vertical.setBlockIncrement(visibleLines); } int width = painter.getWidth(); if(horizontal != null && width != 0) { horizontal.setValues(-horizontalOffset,width,0,width * 5); horizontal.setUnitIncrement(painter.getFontMetrics() .charWidth('w')); horizontal.setBlockIncrement(width / 2); } }
public void actionPerformed(ActionEvent evt) { JEditTextArea textArea = getTextArea(evt); int caret = textArea.getCaretPosition(); int line = textArea.getCaretLine(); if(line == textArea.getLineCount() - 1) { textArea.getToolkit().beep(); return; } int magic = textArea.getMagicCaretPosition(); if(magic == -1) { magic = textArea.offsetToX(line, caret - textArea.getLineStartOffset(line)); } caret = textArea.getLineStartOffset(line + 1) + textArea.xToOffset(line + 1,magic); if(select) textArea.select(textArea.getMarkPosition(),caret); else textArea.setCaretPosition(caret); textArea.setMagicCaretPosition(magic); } }