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 static void setHexadecimalInputFilter( JTextComponent textComponent, int maximumInputLength) { PlainDocument plainDocument = (PlainDocument) textComponent.getDocument(); plainDocument.setDocumentFilter(new HexadecimalInputFilter(maximumInputLength)); String text = textComponent.getText(); text = trimHexadecimal(text); if (text.length() > maximumInputLength) { textComponent.setText(text.substring(text.length() - maximumInputLength, text.length())); } else { while (text.length() < maximumInputLength) { text = "0" + text; } textComponent.setText(text); } }
String getText() { return textComponent.getText(); }