List of usage examples for javax.swing.text JTextComponent getSelectionEnd
@Transient public int getSelectionEnd()
From source file:org.pmedv.core.components.RelativeImageView.java
/** * Determines whether the image is selected, and if it's the only thing * selected.// www .j a v a 2 s. c om * * @return 0 if not selected, 1 if selected, 2 if exclusively selected. * "Exclusive" selection is only returned when editable. */ protected int getSelectionState() { int p0 = fElement.getStartOffset(); int p1 = fElement.getEndOffset(); if (fContainer instanceof JTextComponent) { JTextComponent textComp = (JTextComponent) fContainer; int start = textComp.getSelectionStart(); int end = textComp.getSelectionEnd(); if ((start <= p0) && (end >= p1)) { if ((start == p0) && (end == p1) && isEditable()) { return 2; } else { return 1; } } } return 0; }
From source file:ro.nextreports.designer.ui.sqleditor.syntax.SyntaxUtil.java
/** * Return the lines that span the selection (split as an array of Strings) * if there is no selection then current line is returned. * //w ww . j ava 2s .co m * Note that the strings returned will not contain the terminating line feeds. * * The text component will then have the full lines set as selection. * * @param target * @return String[] of lines spanning selection / or Dot */ public static String[] getSelectedLines(JTextComponent target) { String[] lines = null; try { PlainDocument document = (PlainDocument) target.getDocument(); int start = document.getParagraphElement(target.getSelectionStart()).getStartOffset(); int end; if (target.getSelectionStart() == target.getSelectionEnd()) { end = document.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 = document.getParagraphElement(target.getSelectionEnd() - 1).getEndOffset(); } target.select(start, end); lines = document.getText(start, end - start).split("\n"); target.select(start, end); } catch (BadLocationException e) { LOG.error(e.getMessage(), e); lines = EMPTY_STRING_ARRAY; } return lines; }