/** Import the given stream data into the text component. */ protected void handleReaderImport(Reader in, JTextComponent c) throws BadLocationException, IOException { char[] buff = new char[1024]; int nch; boolean lastWasCR = false; int last; StringBuffer sbuff = null; // Read in a block at a time, mapping \r\n to \n, as well as single // \r to \n. while ((nch = in.read(buff, 0, buff.length)) != -1) { if (sbuff == null) { sbuff = new StringBuffer(nch); } last = 0; for (int counter = 0; counter < nch; counter++) { switch (buff[counter]) { case '\r': if (lastWasCR) { if (counter == 0) sbuff.append('\n'); else buff[counter - 1] = '\n'; } else lastWasCR = true; break; case '\n': if (lastWasCR) { if (counter > (last + 1)) sbuff.append(buff, last, counter - last - 1); // else nothing to do, can skip \r, next write will // write \n lastWasCR = false; last = counter; } break; default: if (lastWasCR) { if (counter == 0) sbuff.append('\n'); else buff[counter - 1] = '\n'; lastWasCR = false; } break; } // End fo switch (buff[counter]). } // End of for (int counter = 0; counter < nch; counter++). if (last < nch) { if (lastWasCR) { if (last < (nch - 1)) sbuff.append(buff, last, nch - last - 1); } else sbuff.append(buff, last, nch - last); } } // End of while ((nch = in.read(buff, 0, buff.length)) != -1). if (withinSameComponent) { ((RTextArea) c).beginAtomicEdit(); } if (lastWasCR) sbuff.append('\n'); c.replaceSelection(sbuff != null ? sbuff.toString() : ""); }
public void insertBreak(JTextComponent text) { indentationLogic = ((EditorPane) text).getIndentationLogic(); boolean noSelection = text.getSelectionStart() == text.getSelectionEnd(); if (noSelection) { insertNewlineWithAutoIndent(text); } else { text.replaceSelection("\n"); // TODO insertNewlineWithAutoIndent } }
private void outdentText(JTextComponent textComponent) throws BadLocationException { int tabSize = ((Integer) textComponent.getDocument().getProperty(PlainDocument.tabSizeAttribute)) .intValue(); String selectedText = textComponent.getSelectedText(); int newLineIndex = selectedText != null ? selectedText.indexOf('\n') : -1; if (newLineIndex >= 0) { int originalSelectionStart = textComponent.getSelectionStart(); int selectionStart = originalSelectionStart; int selectionEnd = textComponent.getSelectionEnd(); int lastNewLineBeforeSelection = textComponent.getText(0, selectionStart).lastIndexOf('\n'); int begin = lastNewLineBeforeSelection >= 0 ? lastNewLineBeforeSelection : 0; int end = selectionEnd; String text = textComponent.getText(begin, end - begin); if (lastNewLineBeforeSelection < 0) { text = "\n" + text; } int len = text.length(); StringBuffer out = new StringBuffer(len); for (int i = 0; i < len; i++) { char ch = text.charAt(i); out.append(ch); if (ch == '\n' && i < len - 1) { char next = text.charAt(i + 1); int stripCount = 0; if (next == '\t') { stripCount = 1; } else { for (; stripCount < tabSize && i + 1 + stripCount < len; stripCount++) { next = text.charAt(i + 1 + stripCount); if (next != ' ' && next != '\t') { break; } } } selectionEnd -= stripCount; if (i + begin < originalSelectionStart - 1) { selectionStart -= stripCount; } i += stripCount; } } textComponent.select(begin, end); textComponent.replaceSelection( lastNewLineBeforeSelection < 0 ? out.toString().substring(1) : out.toString()); textComponent.select(selectionStart, selectionEnd); } }
private void indentText(JTextComponent textComponent) throws BadLocationException { String selectedText = textComponent.getSelectedText(); int newLineIndex = selectedText != null ? selectedText.indexOf('\n') : -1; if (newLineIndex >= 0) { int originalSelectionStart = textComponent.getSelectionStart(); int selectionStart = originalSelectionStart; int selectionEnd = textComponent.getSelectionEnd(); int lastNewLineBeforeSelection = textComponent.getText(0, selectionStart).lastIndexOf('\n'); int begin = lastNewLineBeforeSelection >= 0 ? lastNewLineBeforeSelection : 0; int end = selectionEnd; String text = textComponent.getText(begin, end - begin); int len = text.length(); StringBuffer out = new StringBuffer(len); if (lastNewLineBeforeSelection < 0) { out.insert(0, '\t'); selectionStart++; selectionEnd++; } for (int i = 0; i < len; i++) { char ch = text.charAt(i); out.append(ch); if (ch == '\n' && i < len - 1) { out.append("\t"); selectionEnd++; if (begin + i < originalSelectionStart) { selectionStart++; } } } textComponent.select(begin, end); textComponent.replaceSelection(out.toString()); textComponent.select(selectionStart, selectionEnd); } else { textComponent.replaceSelection("\t"); } }
public void actionPerformed(JTextComponent text) { indentationLogic = ((EditorPane) text).getIndentationLogic(); boolean indentError = false; Document doc = text.getDocument(); Element map = doc.getDefaultRootElement(); String tabWhitespace = PreferencesUser.getInstance().getTabWhitespace(); Caret c = text.getCaret(); int dot = c.getDot(); int mark = c.getMark(); int dotLine = map.getElementIndex(dot); int markLine = map.getElementIndex(mark); if (dotLine != markLine) { int first = Math.min(dotLine, markLine); int last = Math.max(dotLine, markLine); Element elem; int start; try { for (int i = first; i < last; i++) { elem = map.getElement(i); start = elem.getStartOffset(); doc.insertString(start, tabWhitespace, null); } elem = map.getElement(last); start = elem.getStartOffset(); if (Math.max(c.getDot(), c.getMark()) != start) { doc.insertString(start, tabWhitespace, null); } } catch (BadLocationException ble) { Debug.error(me + "Problem while indenting line\n%s", ble.getMessage()); UIManager.getLookAndFeel().provideErrorFeedback(text); } } else { text.replaceSelection(tabWhitespace); } }
private void insertNewlineWithAutoIndent(JTextComponent text) { try { int caretPos = text.getCaretPosition(); StyledDocument doc = (StyledDocument) text.getDocument(); Element map = doc.getDefaultRootElement(); int lineNum = map.getElementIndex(caretPos); Element line = map.getElement(lineNum); int start = line.getStartOffset(); int end = line.getEndOffset() - 1; int len = end - start; String s = doc.getText(start, len); String leadingWS = PythonIndentation.getLeadingWhitespace(doc, start, caretPos - start); StringBuffer sb = new StringBuffer("\n"); sb.append(leadingWS); // TODO better control over automatic indentation indentationLogic.checkIndent(leadingWS, lineNum + 1); // If there is only whitespace between the caret and // the EOL, pressing Enter auto-indents the new line to // the same place as the previous line. int nonWhitespacePos = PythonIndentation.atEndOfLine(doc, caretPos, start, s, len); if (nonWhitespacePos == -1) { if (leadingWS.length() == len) { // If the line was nothing but whitespace, select it // so its contents get removed. text.setSelectionStart(start); } else { // Select the whitespace between the caret and the EOL // to remove it text.setSelectionStart(caretPos); } text.setSelectionEnd(end); text.replaceSelection(sb.toString()); // auto-indentation for python statements like if, while, for, try, // except, def, class and auto-deindentation for break, continue, // pass, return analyseDocument(doc, lineNum, indentationLogic); // auto-completion: add colon if it is obvious if (indentationLogic.shouldAddColon()) { doc.insertString(caretPos, ":", null); indentationLogic.setLastLineEndsWithColon(); } int lastLineChange = indentationLogic.shouldChangeLastLineIndentation(); int nextLineChange = indentationLogic.shouldChangeNextLineIndentation(); if (lastLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 1, lastLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum, lastLineChange); // nextLineChange was determined based on indentation of last line before // the change nextLineChange += lastLineChange; } if (nextLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 2, nextLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum + 1, nextLineChange); } } // If there is non-whitespace between the caret and the // EOL, pressing Enter takes that text to the next line // and auto-indents it to the same place as the last // line. Additional auto-indentation or dedentation for // specific python statements is only done for the next line. else { text.setCaretPosition(nonWhitespacePos); doc.insertString(nonWhitespacePos, sb.toString(), null); analyseDocument(doc, lineNum, indentationLogic); int nextLineChange = indentationLogic.shouldChangeNextLineIndentation(); if (nextLineChange != 0) { Debug.log(5, "change line %d indentation by %d columns", lineNum + 2, nextLineChange); changeIndentation((DefaultStyledDocument) doc, lineNum + 1, nextLineChange); } } } catch (BadLocationException ble) { text.replaceSelection("\n"); Debug.error(me + "Problem while inserting new line with auto-indent\n%s", ble.getMessage()); } }