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); } }
/** * Expand the string template and replaces the selection with the expansion of the template. The * template String may contain any of the following special tags. * <li>{@code #{selection}} replaced with the selection, if any. If there is no selection, then * the {@code #{selection}} tag will be removed. * <li>{@code #{p:AnyText}} will be replaced by {@code any text} and then set the text selection * to {@code AnyText} * * <p>This methood does NOT perform any indentation and the template should generally span one * line only */ public static void insertSimpleTemplate(JTextComponent target, String template) { String selected = target.getSelectedText(); selected = (selected == null) ? "" : selected; StringBuffer sb = new StringBuffer(template.length()); Matcher pm = PTAGS_PATTERN.matcher(template.replace(TEMPLATE_SELECTION, selected)); int selStart = -1, selEnd = -1; int lineStart = 0; while (pm.find()) { selStart = pm.start() + lineStart; pm.appendReplacement(sb, pm.group(1)); selEnd = sb.length(); } pm.appendTail(sb); // String expanded = template.replace(TEMPLATE_SELECTION, selected); if (selStart >= 0) { selStart += target.getSelectionStart(); selEnd += target.getSelectionStart(); } target.replaceSelection(sb.toString()); if (selStart >= 0) { // target.setCaretPosition(selStart); target.select(selStart, selEnd); } }
private void promptForTime() { JTextComponent field = (JTextComponent) editor; String prompt = field.getText(); prompt = prompt + "(???)"; field.setText(prompt); int len = prompt.length(); field.select(len - 4, len - 1); }
protected Transferable createTransferable(JComponent c) { String text = details.getSelectedText(); if (text == null || text.equals("")) { details.selectAll(); text = details.getSelectedText(); details.select(-1, -1); } return new StringSelection(text); }
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"); } }
protected boolean changed(JComponent comp, String str, Position.Bias bias) { JTextComponent textComp = (JTextComponent) comp; int offset = bias == Position.Bias.Forward ? textComp.getCaretPosition() : textComp.getCaret().getMark() - 1; int index = getNextMatch(textComp, str, offset, bias); if (index != -1) { textComp.select(index, index + str.length()); return true; } else { offset = bias == null || bias == Position.Bias.Forward ? 0 : textComp.getDocument().getLength(); index = getNextMatch(textComp, str, offset, bias); if (index != -1) { textComp.select(index, index + str.length()); return true; } else return false; } }
/** * Return the lines that span the selection (split as an array of Strings) if there is no * selection then current line is returned. * * <p>Note that the strings returned will not contain the terminating line feeds If the document * is empty, then an empty string array is returned. So you can always iterate over the returned * array without a null check * * <p>The text component will then have the full lines set as selection * * @return String[] of lines spanning selection / or line containing dot */ public static String[] getSelectedLines(JTextComponent target) { String[] lines = null; try { PlainDocument pDoc = (PlainDocument) target.getDocument(); int start = pDoc.getParagraphElement(target.getSelectionStart()).getStartOffset(); int end; if (target.getSelectionStart() == target.getSelectionEnd()) { end = pDoc.getParagraphElement(target.getSelectionEnd()).getEndOffset(); } else { // if more than one line is selected, we need to subtract one from the end // so that we do not select the line with the caret and no selection in it end = pDoc.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset(); } target.select(start, end); lines = pDoc.getText(start, end - start).split("\n"); target.select(start, end); } catch (BadLocationException ex) { Logger.getLogger(ActionUtils.class.getName()).log(Level.SEVERE, null, ex); lines = EMPTY_STRING_ARRAY; } return lines; }
/** * If the selection is multi lined, then the full lines are selected, otherwise, nothing is done. * * @return true if the selection is multi-line, or a whole line */ public static boolean selectLines(JTextComponent target) { if (target.getSelectionStart() == target.getSelectionEnd()) { return false; } PlainDocument pDoc = (PlainDocument) target.getDocument(); Element es = pDoc.getParagraphElement(target.getSelectionStart()); // if more than one line is selected, we need to subtract one from the end // so that we do not select the line with the caret and no selection in it Element ee = pDoc.getParagraphElement(target.getSelectionEnd() - 1); if (es.equals(ee) && ee.getEndOffset() != target.getSelectionEnd()) { return false; } int start = es.getStartOffset(); int end = ee.getEndOffset(); target.select(start, end - 1); return true; }
@Override public void actionPerformed(ActionEvent evt) { JComponent c = target; if (c == null && (KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner() instanceof JComponent)) { c = (JComponent) KeyboardFocusManager.getCurrentKeyboardFocusManager().getPermanentFocusOwner(); } if (c != null && c.isEnabled()) { if (c instanceof EditableComponent) { ((EditableComponent) c).clearSelection(); } else if (c instanceof JTextComponent) { JTextComponent tc = ((JTextComponent) c); tc.select(tc.getSelectionStart(), tc.getSelectionStart()); } else { c.getToolkit().beep(); } } }
/** * Insert the given String into the text component. If the string contains the | vertical BAr * char, then it will not be inserted, and the cursor will be set to its location. If there are * TWO vertical bars, then the text between them will be selected If the toInsert String is * multiLine, then indentation of all following lines will be the same as the first line. TAB * characters will be replaced by the number of spaces in the document's TAB property. * * @throws javax.swing.text.BadLocationException */ public static void insertMagicString(JTextComponent target, int dot, String toInsert) throws BadLocationException { Document doc = target.getDocument(); String[] lines = toInsert.split("\n"); if (lines.length > 1) { // multi line strings will need to be indented String tabToSpaces = getTab(target); String currentLine = getLineAt(target, dot); String currentIndent = getIndent(currentLine); StringBuilder sb = new StringBuilder(toInsert.length()); boolean firstLine = true; for (String l : lines) { if (!firstLine) { sb.append(currentIndent); } firstLine = false; // replace tabs with spaces. sb.append(l.replace("\t", tabToSpaces)); sb.append("\n"); } toInsert = sb.toString(); } if (toInsert.indexOf('|') >= 0) { int ofst = toInsert.indexOf('|'); int ofst2 = toInsert.indexOf('|', ofst + 1); toInsert = toInsert.replace("|", ""); doc.insertString(dot, toInsert, null); dot = target.getCaretPosition(); int strLength = toInsert.length(); if (ofst2 > 0) { // note that we already removed the first |, so end offset is now // one less than what it was. target.select(dot + ofst - strLength, dot + ofst2 - strLength - 1); } else { target.setCaretPosition(dot + ofst - strLength); } } else { doc.insertString(dot, toInsert, null); } }
/** * Expand the string template and replaces the selection with the expansion of the template. The * template String may contain any of the following special tags. * <li>{@code #{selection}} replaced with the selection, if any. If there is no selection, then * the {@code #{selection}} tag will be removed. * <li>{@code #{p:any text}} will be replaced by {@code any text} and then set selection to {@code * any text} * * <p>This method properly handles indentation as follows: The indentation of the whole block * will match the indentation of the caret line, or the line with the beginning of the * selection, if the selection is in whole line, i.e.e one or more lines of selected text. * {@see selectLines()} * * @param target JEditorCOmponent to be affected * @param templateLines template split as a String array of lines. */ public static void insertLinesTemplate(JTextComponent target, String[] templateLines) { // get some stuff we'll need: String thisIndent = getIndent(getLineAt(target, target.getSelectionStart())); String[] selLines = getSelectedLines(target); int selStart = -1, selEnd = -1; StringBuffer sb = new StringBuffer(); for (String tLine : templateLines) { int selNdx = tLine.indexOf("#{selection}"); if (selNdx >= 0) { // for each of the selected lines: for (String selLine : selLines) { sb.append(tLine.subSequence(0, selNdx)); sb.append(selLine); sb.append('\n'); } } else { sb.append(thisIndent); // now check for any ptags Matcher pm = PTAGS_PATTERN.matcher(tLine); int lineStart = sb.length(); while (pm.find()) { selStart = pm.start() + lineStart; pm.appendReplacement(sb, pm.group(1)); selEnd = sb.length(); } pm.appendTail(sb); sb.append('\n'); } } int ofst = target.getSelectionStart(); target.replaceSelection(sb.toString()); if (selStart >= 0) { // target.setCaretPosition(selStart); target.select(ofst + selStart, ofst + selEnd); } }
/** Remove selection when focus is lost. */ public void focusLost(FocusEvent e) { textComponent.select(0, 0); }