本文整理匯總了Java中javax.swing.JEditorPane.getSelectionStart方法的典型用法代碼示例。如果您正苦於以下問題:Java JEditorPane.getSelectionStart方法的具體用法?Java JEditorPane.getSelectionStart怎麽用?Java JEditorPane.getSelectionStart使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JEditorPane
的用法示例。
在下文中一共展示了JEditorPane.getSelectionStart方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getPlaintextFromEditor
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
* Returns plain text from the editor.
*
* @param editor
* the editor from which to take the text.
* @param onlySelected
* if {@code true} will only return the selected text
* @return the text of the editor converted to plain text
* @throws BadLocationException
* @throws IOException
*/
public static String getPlaintextFromEditor(final JEditorPane editor, final boolean onlySelected) throws IOException,
BadLocationException {
if (editor == null) {
throw new IllegalArgumentException("editor must not be null!");
}
HTMLDocument document = (HTMLDocument) editor.getDocument();
StringWriter writer = new StringWriter();
int start = 0;
int length = document.getLength();
if (onlySelected) {
start = editor.getSelectionStart();
length = editor.getSelectionEnd() - start;
}
editor.getEditorKit().write(writer, document, start, length);
String text = writer.toString();
text = AnnotationDrawUtils.removeStyleFromComment(text);
// switch <br> and <br/> to actual newline (current system)
text = text.replaceAll("<br.*?>", System.lineSeparator());
// kill all other html tags
text = text.replaceAll("\\<.*?>", "");
text = StringEscapeUtils.unescapeHtml(text);
return text;
}
示例2: setCharacterAttributes
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
* Applies the given attributes to character
* content. If there is a selection, the attributes
* are applied to the selection range. If there
* is no selection, the attributes are applied to
* the input attribute set which defines the attributes
* for any new text that gets inserted.
*
* @param editor the editor
* @param attr the attributes
* @param replace if true, then replace the existing attributes first
*/
protected final void setCharacterAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
if (p0 != p1) {
StyledDocument doc = getStyledDocument(editor);
doc.setCharacterAttributes(p0, p1 - p0, attr, replace);
}
StyledEditorKit k = getStyledEditorKit(editor);
MutableAttributeSet inputAttributes = k.getInputAttributes();
if (replace) {
inputAttributes.removeAttributes(inputAttributes);
}
inputAttributes.addAttributes(attr);
}
示例3: isEnabled
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
* Determine if the action should be enabled. By default, this method
* checks for the presence of an open editor; if <code>requiresSelection</code>
* was passed to the constructor, it also depends on a text selection in that
* editor being present.
* @param context A Lookup containing either an EditorCookie or one or more
* Nodes whose lookup contains an EditorCookie
* @return true if the action should be enabled, false otherwise
*/
protected boolean isEnabled(Lookup context) {
EditorCookie ck = JavaRefactoringGlobalAction.getEditorCookie(context);
boolean result = false;
if(ck != null) {
JEditorPane pane = NbDocument.findRecentEditorPane(ck);
result = pane != null;
if (requiresSelection) {
result = result && (pane.getSelectionStart() != pane.getSelectionEnd());
}
}
return result;
}
示例4: getCurrentElement
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/** throws IllegalComponentStateException when can not return the data in AWT. */
private String getCurrentElement(FileObject fo, JEditorPane ep,
final ElementKind kind, final String[] elementSignaturePtr)
throws java.awt.IllegalComponentStateException {
if (fo == null) {
return null;
}
final int currentOffset;
final String selectedIdentifier;
if (ep != null) {
String s;
Caret caret = ep.getCaret();
if (caret == null) {
s = null;
currentOffset = 0;
} else {
s = ep.getSelectedText ();
currentOffset = ep.getCaretPosition();
if (ep.getSelectionStart() > currentOffset || ep.getSelectionEnd() < currentOffset) {
s = null; // caret outside of the selection
}
}
if (s != null && Utilities.isJavaIdentifier (s)) {
selectedIdentifier = s;
} else {
selectedIdentifier = null;
}
} else {
selectedIdentifier = null;
currentOffset = 0;
}
return EditorContextSupport.getCurrentElement(fo, currentOffset, selectedIdentifier, kind, elementSignaturePtr);
}
示例5: create
import javax.swing.JEditorPane; //導入方法依賴的package包/類
@Override
public RefactoringUI create(CompilationInfo info, TreePathHandle[] handles, FileObject[] files, NonRecursiveFolder[] packages) {
EditorCookie ec = lookup.lookup(EditorCookie.class);
if (ec == null) {
return create(info, -1, handles);
}
JEditorPane textC = NbDocument.findRecentEditorPane(ec);
if (textC == null) {
return create(info, -1, handles);
}
int startOffset = textC.getSelectionStart();
int endOffset = textC.getSelectionEnd();
if (startOffset == endOffset) {
//cursor position
return create(info, startOffset, handles[0]);
}
//editor selection
TreePath path = info.getTreeUtilities().pathFor(startOffset);
if (path == null) {
return null;
}
TreePath enclosingClass = JavaRefactoringUtils.findEnclosingClass(info, path, true, true, true, true, true);
if (enclosingClass == null) {
return null;
}
Element el = info.getTrees().getElement(enclosingClass);
if (el == null) {
return null;
}
if (!(el.getKind().isClass() || el.getKind().isInterface())) {
el = info.getElementUtilities().enclosingTypeElement(el);
}
Collection<TreePathHandle> h = new ArrayList<TreePathHandle>();
for (Element e : ElementFilter.fieldsIn(el.getEnclosedElements())) {
// SourcePositions sourcePositions = info.getTrees().getSourcePositions();
Tree leaf = info.getTrees().getPath(e).getLeaf();
int[] namespan = info.getTreeUtilities().findNameSpan((VariableTree) leaf);
if (namespan != null) {
long start = namespan[0]; //sourcePositions.getStartPosition(info.getCompilationUnit(), leaf);
long end = namespan[1]; //sourcePositions.getEndPosition(info.getCompilationUnit(), leaf);
if ((start <= endOffset && start >= startOffset)
|| (end <= endOffset && end >= startOffset)) {
h.add(TreePathHandle.create(e, info));
}
}
}
if (h.isEmpty()) {
return create(info, startOffset, handles[0]);
}
return create(info, -1, h.toArray(new TreePathHandle[h.size()]));
}
示例6: getIdentifier
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private static String getIdentifier (
StyledDocument doc,
JEditorPane ep,
int offset
) {
String t = null;
if ( (ep.getSelectionStart () <= offset) &&
(offset <= ep.getSelectionEnd ())
) t = ep.getSelectedText ();
if (t != null) return t;
int line = NbDocument.findLineNumber (
doc,
offset
);
int col = NbDocument.findLineColumn (
doc,
offset
);
try {
Element lineElem =
NbDocument.findLineRootElement (doc).
getElement (line);
if (lineElem == null) return null;
int lineStartOffset = lineElem.getStartOffset ();
int lineLen = lineElem.getEndOffset() - lineStartOffset;
t = doc.getText (lineStartOffset, lineLen);
lineLen = t.length ();
int identStart = col;
while ( (identStart > 0) &&
(t.charAt (identStart - 1) != '"')
) {
identStart--;
}
int identEnd = Math.max (col, 1);
while ( (identEnd < lineLen) &&
(t.charAt (identEnd - 1) != '"')
) {
identEnd++;
}
if (identStart == identEnd) return null;
return t.substring (identStart, identEnd - 1);
} catch (BadLocationException e) {
return null;
}
}
示例7: getIdentifier
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private static String getIdentifier (
StyledDocument doc,
JEditorPane ep,
int offset
) {
String t = null;
if ( (ep.getSelectionStart () <= offset) &&
(offset <= ep.getSelectionEnd ())
) t = ep.getSelectedText ();
if (t != null) return t;
int line = NbDocument.findLineNumber (
doc,
offset
);
int col = NbDocument.findLineColumn (
doc,
offset
);
try {
javax.swing.text.Element lineElem =
org.openide.text.NbDocument.findLineRootElement (doc).
getElement (line);
if (lineElem == null) return null;
int lineStartOffset = lineElem.getStartOffset ();
int lineLen = lineElem.getEndOffset() - lineStartOffset;
t = doc.getText (lineStartOffset, lineLen);
int identStart = col;
while (identStart > 0 &&
(Character.isJavaIdentifierPart (
t.charAt (identStart - 1)
) ||
(t.charAt (identStart - 1) == '.'))) {
identStart--;
}
int identEnd = col;
while (identEnd < lineLen &&
Character.isJavaIdentifierPart(t.charAt(identEnd))
) {
identEnd++;
}
if (identStart == identEnd) return null;
return t.substring (identStart, identEnd);
} catch (javax.swing.text.BadLocationException e) {
return null;
}
}
示例8: getIdentifier
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private static String getIdentifier (
StyledDocument doc,
JEditorPane ep,
int line, int column, int offset,
boolean[] isFunctionPtr
) {
// do always evaluation if the tooltip is invoked on a text selection
String t = null;
if ( (ep.getSelectionStart () <= offset) &&
(offset <= ep.getSelectionEnd ())
) {
t = ep.getSelectedText ();
}
if (t != null) {
return t;
}
Element lineElem =
NbDocument.findLineRootElement (doc).
getElement (line);
if (lineElem == null) {
return null;
}
int lineStartOffset = lineElem.getStartOffset ();
int lineLen = lineElem.getEndOffset() - lineStartOffset;
try {
t = doc.getText (lineStartOffset, lineLen);
} catch (BadLocationException ble) {
return null;
}
column = Math.min(column, t.length());
int identStart = column;
boolean wasDot = false;
while (identStart > 0) {
char c = t.charAt (identStart - 1);
if (Character.isJavaIdentifierPart(c) ||
(c == '.' && (wasDot = true)) || // Please note that '=' is intentional here.
(wasDot && (c == ']' || c == '['))) {
identStart--;
} else {
break;
}
}
int identEnd = column;
while (identEnd < lineLen &&
Character.isJavaIdentifierPart(t.charAt(identEnd))) {
identEnd++;
}
if (identStart == identEnd) {
return null;
}
String ident = t.substring (identStart, identEnd).trim();
if (JS_KEYWORDS.contains(ident)) {
// Java keyword => Do not show anything
return null;
}
while (identEnd < lineLen &&
Character.isWhitespace(t.charAt(identEnd))
) {
identEnd++;
}
if (identEnd < lineLen && t.charAt(identEnd) == '(') {
// We're at a function call
isFunctionPtr[0] = true;
}
return ident;
}
示例9: getUpToDate
import javax.swing.JEditorPane; //導入方法依賴的package包/類
@Override
public UpToDateStatus getUpToDate() {
if (model == null) {
return UpToDateStatus.UP_TO_DATE_OK;
}
FileObject fo = NbEditorUtilities.getFileObject(document);
boolean ok = false;
try {
if (fo.isValid()) {
DataObject dobj = DataObject.find(fo);
EditorCookie ed = dobj.getLookup().lookup(EditorCookie.class);
if (ed != null) {
JEditorPane[] panes = ed.getOpenedPanes();
if (panes != null && panes.length > 0) {
//#214527
JEditorPane pane = panes[0];
if (panes.length > 1) {
for (JEditorPane p : panes) {
if (p.isFocusOwner()) {
pane = p;
break;
}
}
}
//TODO this code is called very often apparently.
//we should only run the checks if something changed..
//something means file + selection start + selection end.
final int selectionStart = pane.getSelectionStart();
final int selectionEnd = pane.getSelectionEnd();
final int caretPosition = pane.getCaretPosition();
RP.post(new Runnable() {
@Override
public void run() {
refreshLinkAnnotations(document, model, selectionStart, selectionEnd);
}
});
if (selectionStart != selectionEnd) { //maybe we want to remove the condition?
RP.post(new Runnable() {
@Override public void run() {
//this condition is important in order not to break any running hints
//the model sync+refresh renders any existing POMComponents people
// might be holding useless
if (!model.isIntransaction()) {
HintsController.setErrors(document, LAYER_POM_SELECTION, findHints(model, project, selectionStart, selectionEnd, caretPosition));
} else {
HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
}
}
});
ok = true;
return UpToDateStatus.UP_TO_DATE_PROCESSING;
}
}
}
}
} catch (DataObjectNotFoundException ex) {
//#166011 just a minor issue, just log, but don't show to user directly
LOG.log(Level.INFO, "Touched somehow invalidated FileObject", ex);
} finally {
if (!ok) {
HintsController.setErrors(document, LAYER_POM_SELECTION, Collections.<ErrorDescription>emptyList());
}
}
return UpToDateStatus.UP_TO_DATE_OK; // XXX should use UP_TO_DATE_PROCESSING if checkHints task is currently running
}
示例10: actionPerformed
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
*
*/
public void actionPerformed(ActionEvent e)
{
if (e.getSource() instanceof mxGraphComponent)
{
mxGraphComponent graphComponent = (mxGraphComponent) e
.getSource();
Component editorComponent = null;
if (graphComponent.getCellEditor() instanceof mxCellEditor)
{
editorComponent = ((mxCellEditor) graphComponent
.getCellEditor()).getEditor();
}
if (editorComponent instanceof JEditorPane)
{
JEditorPane editorPane = (JEditorPane) editorComponent;
int start = editorPane.getSelectionStart();
int ende = editorPane.getSelectionEnd();
String text = editorPane.getSelectedText();
if (text == null)
{
text = "";
}
try
{
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument document = (HTMLDocument) editorPane
.getDocument();
document.remove(start, (ende - start));
editorKit.insertHTML(document, start, ((bold) ? "<b>"
: "<i>") + text + ((bold) ? "</b>" : "</i>"),
0, 0, (bold) ? HTML.Tag.B : HTML.Tag.I);
}
catch (Exception ex)
{
ex.printStackTrace();
}
editorPane.requestFocus();
editorPane.select(start, ende);
}
else
{
mxIGraphModel model = graphComponent.getGraph().getModel();
model.beginUpdate();
try
{
graphComponent.stopEditing(false);
graphComponent.getGraph().toggleCellStyleFlags(
mxConstants.STYLE_FONTSTYLE,
(bold) ? mxConstants.FONT_BOLD
: mxConstants.FONT_ITALIC);
}
finally
{
model.endUpdate();
}
}
}
}
示例11: actionPerformed
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
*
*/
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof mxGraphComponent) {
mxGraphComponent graphComponent = (mxGraphComponent) e.getSource();
Component editorComponent = null;
if (graphComponent.getCellEditor() instanceof mxCellEditor) {
editorComponent = ((mxCellEditor) graphComponent.getCellEditor()).getEditor();
}
if (editorComponent instanceof JEditorPane) {
JEditorPane editorPane = (JEditorPane) editorComponent;
int start = editorPane.getSelectionStart();
int ende = editorPane.getSelectionEnd();
String text = editorPane.getSelectedText();
if (text == null) {
text = "";
}
try {
HTMLEditorKit editorKit = new HTMLEditorKit();
HTMLDocument document = (HTMLDocument) editorPane.getDocument();
document.remove(start, (ende - start));
editorKit.insertHTML(document, start,
((bold) ? "<b>" : "<i>") + text + ((bold) ? "</b>" : "</i>"), 0, 0,
(bold) ? HTML.Tag.B : HTML.Tag.I);
} catch (Exception ex) {
ex.printStackTrace();
}
editorPane.requestFocus();
editorPane.select(start, ende);
} else {
mxIGraphModel model = graphComponent.getGraph().getModel();
model.beginUpdate();
try {
graphComponent.stopEditing(false);
graphComponent.getGraph().toggleCellStyleFlags(mxConstants.STYLE_FONTSTYLE,
(bold) ? mxConstants.FONT_BOLD : mxConstants.FONT_ITALIC);
} finally {
model.endUpdate();
}
}
}
}
示例12: setParagraphAttributes
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
* Applies the given attributes to paragraphs. If
* there is a selection, the attributes are applied
* to the paragraphs that intersect the selection.
* if there is no selection, the attributes are applied
* to the paragraph at the current caret position.
*
* @param editor the editor
* @param attr the attributes
* @param replace if true, replace the existing attributes first
*/
protected final void setParagraphAttributes(JEditorPane editor,
AttributeSet attr, boolean replace) {
int p0 = editor.getSelectionStart();
int p1 = editor.getSelectionEnd();
StyledDocument doc = getStyledDocument(editor);
doc.setParagraphAttributes(p0, p1 - p0, attr, replace);
}