/**
* Return the lines that span the selection (split as an array of Strings) if there is no selection then current line is returned. 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 The text component will then have the full lines set as selection
* @param target
* @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 {
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;
}

Example 29

From project Bukkit-Plugin-Utilties, under directory /src/main/java/de/xzise/.