public void appendToPane(String msg, Color c) { if (inputText.isEnabled()) { StyleContext sc = StyleContext.getDefaultStyleContext(); AttributeSet aset = sc.addAttribute(SimpleAttributeSet.EMPTY, StyleConstants.Foreground, c); textArea.setCharacterAttributes(aset, false); textArea.replaceSelection(msg); textArea.setCaretPosition(textArea.getDocument().getLength()); } }
public void replaceSelection(String content) { super.replaceSelection(content); // TODO delete this commented-out block once i'm sure i don't need it any more. // if (isEditable() == false || isEnabled() == false) // { // return; // } // // Document doc = getDocument(); // // int charsToLookBack = 10; // if (doc != null && lookAhead != null) // { // try // { // // go back to previous whitespace // int docLength = doc.getLength(); // if (docLength < charsToLookBack) // charsToLookBack = docLength - 1; // String recentDocText = null; // String oldContent = null; // // recentDocText = doc.getText(0, getCaretPosition()); // // // pass the look-ahead algorithm all of the doc except the // // partial word you're currently working on // // this may be a bad approach; need to look for whitespace at beginning // // and // // end of words, periods, etc. // if (doc.getLength() > charsToLookBack) // { // lookAhead.setText(doc.getText(0, doc.getLength() - charsToLookBack)); // } // // // find last whitespace character, and make sure I keep looking for that // // same character throughout the rest of the code // // REFACTOR THIS SECTION // int lastBlank = recentDocText.lastIndexOf(" "); // int lastTab = recentDocText.lastIndexOf("\t"); // int lastNewline = recentDocText.lastIndexOf("\n"); // int lastFF = recentDocText.lastIndexOf("\f"); // int lastR = recentDocText.lastIndexOf("\r"); // // int lastWhitespaceLoc = 0; // String lastWhitespaceString = ""; // if (lastBlank > lastTab && lastBlank > lastNewline) // { // lastWhitespaceLoc = lastBlank; // lastWhitespaceString = " "; // } // else if (lastTab > lastBlank && lastTab > lastNewline) // { // lastWhitespaceLoc = lastTab; // lastWhitespaceString = "\t"; // } // else if (lastNewline > lastBlank && lastNewline > lastTab) // { // lastWhitespaceLoc = lastNewline; // lastWhitespaceString = "\n"; // } // // if (lastWhitespaceLoc > 0 && doc.getLength() > (charsToLookBack - 1)) // { // // get caret position // int caretPosition = getCaretPosition(); // // look at last 10 characters // int scanBackPosition = caretPosition - charsToLookBack; // if (scanBackPosition <= 0) // return; // String recentChars = doc.getText(scanBackPosition, charsToLookBack); // // if any characters are blanks, get the characters since the last // // blank // int lastWhitespacePosition = recentChars.lastIndexOf(lastWhitespaceString); // if (lastWhitespacePosition <= 0) // return; // String charsSinceLastBlank = recentChars.substring(lastWhitespacePosition + 1, // charsToLookBack); // // String newContent = lookAhead.doLookAhead(charsSinceLastBlank); // if (newContent != null) // { // int lengthOfAddedContent = newContent.length() - charsSinceLastBlank.length(); // String newContentSubstring = newContent.substring(charsSinceLastBlank.length(), // newContent.length()); // doc.insertString(getCaretPosition(), newContentSubstring, null); // // // Highlight the added text // setCaretPosition(caretPosition + lengthOfAddedContent); // moveCaretPosition(caretPosition); // } // } // else // { // oldContent = recentDocText; // String newContent = lookAhead.doLookAhead(oldContent); // if (newContent != null) // { // int lengthOld = oldContent.length(); // String newContentSubstring = newContent.substring(lengthOld); // doc.insertString(getCaretPosition(), newContentSubstring, null); // // // Highlight the added text // setCaretPosition(newContent.length()); // moveCaretPosition(oldContent.length()); // } // } // // } // catch (BadLocationException e) // { // System.err.println(e.getMessage()); // } // } }