/** * Gets the line number that the caret is on. * * @return The zero-based line number that the caret is on. */ public final int getCaretLineNumber() { try { return getLineOfOffset(getCaretPosition()); } catch (BadLocationException ble) { return 0; // Never happens } }
@Override public void addNotify() { super.addNotify(); // If the caret is not on the first line, we must recalculate the line // highlight y-offset after the text area is sized. This seems to be // the best way to do it. if (getCaretPosition() != 0) { SwingUtilities.invokeLater(() -> possiblyUpdateCurrentLineHighlightLocation()); } }
/** * Gets the position from the beginning of the current line that the caret * is on. * * @return The zero-based position from the beginning of the current line * that the caret is on. */ public final int getCaretOffsetFromLineStart() { try { int pos = getCaretPosition(); return pos - getLineStartOffset(getLineOfOffset(pos)); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Converts all instances of a tab into a number of spaces equivalent * to a tab in this text area. * * @see #convertSpacesToTabs * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertTabsToSpaces() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuilder tabInSpaces = new StringBuilder(); for (int i=0; i<tabSize; i++) { tabInSpaces.append(' '); } String text = getText(); setText(text.replaceAll("\t", tabInSpaces.toString())); // Put caret back at same place in document. setCaretPosition(caretPosition); }
/** * Converts all instances of a number of spaces equal to a tab size * into a tab in this text area. * * @see #convertTabsToSpaces * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertSpacesToTabs() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuilder stringBuilder = new StringBuilder(); for (int i=0; i<tabSize; i++) { stringBuilder.append(" "); } String text = getText(); setText(text.replaceAll(stringBuilder.toString(), "\t")); int newDocumentLength = getDocument().getLength(); // Place the caret back in its proper position. if (caretPosition<newDocumentLength) { setCaretPosition(caretPosition); } else { setCaretPosition(newDocumentLength-1); } }
int dot = getCaretPosition();
/** * Gets the line number that the caret is on. * * @return The zero-based line number that the caret is on. */ public final int getCaretLineNumber() { try { return getLineOfOffset(getCaretPosition()); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Gets the line number that the caret is on. * * @return The zero-based line number that the caret is on. */ public final int getCaretLineNumber() { try { return getLineOfOffset(getCaretPosition()); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Gets the line number that the caret is on. * * @return The zero-based line number that the caret is on. */ public final int getCaretLineNumber() { try { return getLineOfOffset(getCaretPosition()); } catch (BadLocationException ble) { return 0; // Never happens } }
@Override public void addNotify() { super.addNotify(); // If the caret is not on the first line, we must recalculate the line // highlight y-offset after the text area is sized. This seems to be // the best way to do it. if (getCaretPosition() != 0) { SwingUtilities.invokeLater(() -> possiblyUpdateCurrentLineHighlightLocation()); } }
/** * Gets the position from the beginning of the current line that the caret * is on. * * @return The zero-based position from the beginning of the current line * that the caret is on. */ public final int getCaretOffsetFromLineStart() { try { int pos = getCaretPosition(); return pos - getLineStartOffset(getLineOfOffset(pos)); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Gets the position from the beginning of the current line that the caret * is on. * * @return The zero-based position from the beginning of the current line * that the caret is on. */ public final int getCaretOffsetFromLineStart() { try { int pos = getCaretPosition(); return pos - getLineStartOffset(getLineOfOffset(pos)); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Gets the position from the beginning of the current line that the caret * is on. * * @return The zero-based position from the beginning of the current line * that the caret is on. */ public final int getCaretOffsetFromLineStart() { try { int pos = getCaretPosition(); return pos - getLineStartOffset(getLineOfOffset(pos)); } catch (BadLocationException ble) { return 0; // Never happens } }
/** * Converts all instances of a tab into a number of spaces equivalent * to a tab in this text area. * * @see #convertSpacesToTabs * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertTabsToSpaces() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuffer tabInSpaces = new StringBuffer(); for (int i=0; i<tabSize; i++) tabInSpaces.append(' '); String text = getText(); setText(text.replaceAll("\t", tabInSpaces.toString())); // Put caret back at same place in document. setCaretPosition(caretPosition); }
/** * Converts all instances of a tab into a number of spaces equivalent * to a tab in this text area. * * @see #convertSpacesToTabs * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertTabsToSpaces() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuffer tabInSpaces = new StringBuffer(); for (int i=0; i<tabSize; i++) tabInSpaces.append(' '); String text = getText(); setText(text.replaceAll("\t", tabInSpaces.toString())); // Put caret back at same place in document. setCaretPosition(caretPosition); }
/** * Converts all instances of a tab into a number of spaces equivalent * to a tab in this text area. * * @see #convertSpacesToTabs * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertTabsToSpaces() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuilder tabInSpaces = new StringBuilder(); for (int i=0; i<tabSize; i++) { tabInSpaces.append(' '); } String text = getText(); setText(text.replaceAll("\t", tabInSpaces.toString())); // Put caret back at same place in document. setCaretPosition(caretPosition); }
/** * Converts all instances of a number of spaces equal to a tab size * into a tab in this text area. * * @see #convertTabsToSpaces * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertSpacesToTabs() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); StringBuilder stringBuilder = new StringBuilder(); for (int i=0; i<tabSize; i++) { stringBuilder.append(" "); } String text = getText(); setText(text.replaceAll(stringBuilder.toString(), "\t")); int newDocumentLength = getDocument().getLength(); // Place the caret back in its proper position. if (caretPosition<newDocumentLength) { setCaretPosition(caretPosition); } else { setCaretPosition(newDocumentLength-1); } }
/** * Converts all instances of a number of spaces equal to a tab size * into a tab in this text area. * * @see #convertTabsToSpaces * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertSpacesToTabs() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); String tabInSpaces = ""; for (int i=0; i<tabSize; i++) tabInSpaces += " "; String text = getText(); setText(text.replaceAll(tabInSpaces, "\t")); int newDocumentLength = getDocument().getLength(); // Place the caret back in its proper position. if (caretPosition<newDocumentLength) setCaretPosition(caretPosition); else setCaretPosition(newDocumentLength-1); }
/** * Converts all instances of a number of spaces equal to a tab size * into a tab in this text area. * * @see #convertTabsToSpaces * @see #getTabsEmulated * @see #setTabsEmulated */ public void convertSpacesToTabs() { // FIXME: This is inefficient and will yield an OutOfMemoryError if // done on a large document. We should scan 1 line at a time and // replace; it'll be slower but safer. int caretPosition = getCaretPosition(); int tabSize = getTabSize(); String tabInSpaces = ""; for (int i=0; i<tabSize; i++) tabInSpaces += " "; String text = getText(); setText(text.replaceAll(tabInSpaces, "\t")); int newDocumentLength = getDocument().getLength(); // Place the caret back in its proper position. if (caretPosition<newDocumentLength) setCaretPosition(caretPosition); else setCaretPosition(newDocumentLength-1); }