public boolean isValidLineIndex(int index) { return getContent().getLineCount() > index; }
static int computeEnd(StyledTextContent content, int lastLine) { int endIndex; if (content.getLineCount() > lastLine + 1) { endIndex = content.getOffsetAtLine(lastLine + 1); } else { endIndex = content.getOffsetAtLine(lastLine) + content.getLine(lastLine).length(); } return endIndex; }
/** * navigates to specified line * * @param lineIndex */ public void navigateToLine(int lineIndex) { if (lineIndex >= 0 && lineIndex <= getContent().getLineCount()) { int offset = getContent().getOffsetAtLine(lineIndex); setCaretOffset(offset); } }
private void bindContentListener() { this.content.addListener((x, o, n)->{ if (o != null) { o.removeTextChangeListener(this.textChangeListener); } if (n != null) { n.addTextChangeListener(this.textChangeListener); this.numberOfLines.set(n.getLineCount()); } }); StyledTextContent current = this.content.get(); if (current != null) { current.addTextChangeListener(this.textChangeListener); // set inital values this.numberOfLines.set(current.getLineCount()); } }
/** * recalculate the line items */ public void recalculateItems() { if (this.lineList.size() != getSkinnable().getContent().getLineCount()) { if (this.lineList.size() > getSkinnable().getContent().getLineCount()) { this.lineList.remove(getSkinnable().getContent().getLineCount(), this.lineList.size()); } else { List<Line> tmp = new ArrayList<>(getSkinnable().getContent().getLineCount() - this.lineList.size()); for (int i = this.lineList.size(); i < getSkinnable().getContent().getLineCount(); i++) { tmp.add(new Line()); } // System.err.println("DOING AN ADD!!!!!"); this.lineList.addAll(tmp); // System.err.println("ADD IS DONE!"); } } redraw(); }
private @NonNull LineRegion getLineRegion(TextSelection selection) { int firstLine = getControl().getLineAtOffset(selection.offset); int lastLine = getControl().getLineAtOffset(selection.offset + selection.length); int lastLineBegin = getControl().getOffsetAtLine(lastLine); // dont count the last line if the caret is at index 0 if (lastLineBegin == selection.offset + selection.length) { lastLine -= 1; } // limit lastLine = Math.min(getControl().getContent().getLineCount() - 1, lastLine); lastLine = Math.max(firstLine, lastLine); return new LineRegion(firstLine, lastLine); }
@Override public void textSet(TextChangedEvent event) { if (!this.toUpdate.isEmpty()) { updateNodesNow(this.toUpdate); this.toUpdate.clear(); } // update number of lines ContentView.this.numberOfLines.set(getContent().getLineCount()); getLineLayer().requestLayout(); }
private int calculatePageDownOffset() { final int linesPerPage = ((StyledTextSkin)getControl().getSkin()).getVisibleLineCount(); final int globalOffset = getControl().getCaretOffset(); final int lineIndex = getControl().getContent().getLineAtOffset(globalOffset); final int lineLocalOffset = globalOffset - getControl().getContent().getOffsetAtLine(lineIndex); final int newLineIndex = Math.min(getControl().getContent().getLineCount()-1, lineIndex + linesPerPage); final int newLineLocalOffset = Math.min(getControl().getContent().getLine(newLineIndex).length(), lineLocalOffset); return getControl().getOffsetAtLine(newLineIndex) + newLineLocalOffset; }
public void scrollOffsetIntoView(int offset, int verticalOffset, int horizontalOffset) { if( offset >= 0 ) { int lineIdx = getSkinnable().getContent().getLineAtOffset(offset); Range<Integer> visibleLines = this.content.getVisibleLines(); if( ! visibleLines.contains(Integer.valueOf(lineIdx)) ) { int linesVisible = visibleLines.upperEndpoint().intValue(); int delta = linesVisible - verticalOffset; int scrollLine = Math.min(lineIdx+delta, getSkinnable().getContent().getLineCount()-1); scrollLineIntoView(scrollLine); } int colIdx = offset - getSkinnable().getContent().getOffsetAtLine(lineIdx); String line = getSkinnable().getContent().getLine(lineIdx).substring(0, colIdx); int tabCount = (int)line.chars().filter(c -> c == '\t').count(); scrollColumnIntoView(colIdx + tabCount * (getSkinnable().tabAvanceProperty().get() - 1), horizontalOffset); } else { scrollLineIntoView(0); scrollColumnIntoView(0, 0); } }
/** * default implementation for {@link DefaultTextEditActions#MOVE_LINES_DOWN} */ protected void defaultMoveLinesDown() { LineRegion moveTarget = getLineRegion(getControl().getSelection()); if (moveTarget.lastLine + 1 < getControl().getContent().getLineCount()) { LineRegion below = new LineRegion(moveTarget.lastLine + 1); LineRegion all = new LineRegion(moveTarget.firstLine, below.lastLine); String belowText = below.read(); String moveTargetText = moveTarget.read(); // we reach the last line if (below.lastLine + 1 == getControl().getContent().getLineCount()) { belowText += getControl().getLineSeparator().getValue(); moveTargetText = moveTargetText.replaceFirst("\r?\n$", ""); //$NON-NLS-1$ //$NON-NLS-2$ } all.replace(belowText + moveTargetText); new LineRegion(moveTarget.firstLine + 1, moveTarget.lastLine + 1).selectWithCaretAtStart(); } }
/** * default implementation for {@link DefaultTextEditActions#MOVE_DOWN} and * {@link DefaultTextEditActions#SELECT_DOWN} * * @param select * whether to change the selection */ protected void defaultDown(boolean select) { int currentRowIndex = getControl().getContent().getLineAtOffset(getControl().getCaretOffset()); final int offset = getControl().getCaretOffset(); int rowIndex = currentRowIndex; if (rowIndex + 1 == getControl().getContent().getLineCount()) { return; } int colIdx = offset - getControl().getContent().getOffsetAtLine(rowIndex); rowIndex += 1; int lineOffset = getControl().getContent().getOffsetAtLine(rowIndex); int newCaretPosition = lineOffset + colIdx; int maxPosition = lineOffset + getControl().getContent().getLine(rowIndex).length(); moveCaretAbsolute(Math.min(newCaretPosition, maxPosition), select); }
/** * default implementation for {@link DefaultTextEditActions#MOVE_LINES_UP} */ protected void defaultMoveLinesUp() { LineRegion moveTarget = getLineRegion(getControl().getSelection()); if (moveTarget.firstLine > 0) { LineRegion above = new LineRegion(moveTarget.firstLine - 1); LineRegion all = new LineRegion(above.firstLine, moveTarget.lastLine); String aboveText = above.read(); String moveTargetText = moveTarget.read(); // we reach the last line if (moveTarget.lastLine + 1 == getControl().getContent().getLineCount()) { moveTargetText += getControl().getLineSeparator().getValue(); aboveText = aboveText.replaceFirst("\r?\n$", ""); //$NON-NLS-1$ //$NON-NLS-2$ } all.replace(moveTargetText + aboveText); new LineRegion(moveTarget.firstLine - 1, moveTarget.lastLine - 1).selectWithCaretAtStart(); } }
@Override public void textChanged(TextChangedEvent event) { if (!this.toRelease.isEmpty()) { releaseNodesNow(this.toRelease); this.toRelease.clear(); } // execute permutation if (this.mapping != null) { getLineLayer().permutateNodes(this.mapping); this.mapping = null; } // execute updates if (!this.toUpdate.isEmpty()) { updateNodesNow(this.toUpdate); this.toUpdate.clear(); } // update number of lines ContentView.this.numberOfLines.set(getContent().getLineCount()); getLineLayer().requestLayout(); } };
if (rowIndex + 1 == getControl().getContent().getLineCount()) { break;