@Override public Dimension getPreferredSize() { if (myPreferredSize == null) { int maxLineLength = 0; int linesCount = 0; for (LineTokenizer lt = new LineTokenizer(myRawText); !lt.atEnd(); lt.advance()) { maxLineLength = Math.max(maxLineLength, lt.getLength()); linesCount++; } FontMetrics fontMetrics = ((EditorImpl) getEditor()) .getFontMetrics( myTextAttributes != null ? myTextAttributes.getFontType() : Font.PLAIN); int preferredHeight = getEditor().getLineHeight() * Math.max(1, linesCount); int preferredWidth = fontMetrics.charWidth('m') * maxLineLength; Insets insets = getInsets(); if (insets != null) { preferredHeight += insets.top + insets.bottom; preferredWidth += insets.left + insets.right; } myPreferredSize = new Dimension(preferredWidth, preferredHeight); } return myPreferredSize; }
private int yPositionToOffset(int y, boolean beginLine) { if (myEditorScrollbarTop == -1 || myEditorTargetHeight == -1) { recalcEditorDimensions(); } final int safeY = Math.max(0, y - myEditorScrollbarTop); VisualPosition visual; if (myEditorSourceHeight < myEditorTargetHeight) { visual = myEditor.xyToVisualPosition(new Point(0, safeY)); } else { float fraction = Math.max(0, Math.min(1, safeY / (float) myEditorTargetHeight)); final int lineCount = myEditorSourceHeight / myEditor.getLineHeight(); visual = new VisualPosition((int) (fraction * lineCount), 0); } int line = myEditor.visualToLogicalPosition(visual).line; Document document = myEditor.getDocument(); if (line < 0) return 0; if (line >= document.getLineCount()) return document.getTextLength(); return beginLine ? document.getLineStartOffset(line) : document.getLineEndOffset(line); }
private LogicalPosition fitInsideEditor(LogicalPosition pos) { int lineCount = myDocumentWindow.getLineCount(); if (pos.line >= lineCount) { pos = new LogicalPosition(lineCount - 1, pos.column); } int lineLength = myDocumentWindow.getLineEndOffset(pos.line) - myDocumentWindow.getLineStartOffset(pos.line); if (pos.column >= lineLength) { pos = new LogicalPosition(pos.line, Math.max(0, lineLength - 1)); } return pos; }