下面列出了javax.swing.JEditorPane#getSelectionEnd ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* 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);
}
protected final void jsonMinify(final Node[] activatedNodes) {
final EditorCookie editorCookie
= Utilities.actionsGlobalContext().lookup(EditorCookie.class);
for (final JEditorPane pane : editorCookie.getOpenedPanes()) {
if (pane.isShowing()
&& pane.getSelectionEnd() > pane.getSelectionStart()) {
try {
StringSelection content = new StringSelection(selectedSourceAsMinify(pane));
Toolkit.getDefaultToolkit().getSystemClipboard().
setContents(content, content);
return;
} catch (final HeadlessException e) {
org.openide.ErrorManager.getDefault().notify(e);
}
}
}
}
/**
* 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);
}
protected final void xmlMinify(final Node[] activatedNodes) {
final EditorCookie editorCookie
= Utilities.actionsGlobalContext().lookup(EditorCookie.class);
for (final JEditorPane pane : editorCookie.getOpenedPanes()) {
if (pane.isShowing()
&& pane.getSelectionEnd() > pane.getSelectionStart()) {
try {
StringSelection content = new StringSelection(selectedSourceAsMinify(pane));
Toolkit.getDefaultToolkit().getSystemClipboard().
setContents(content, content);
return;
} catch (final HeadlessException e) {
org.openide.ErrorManager.getDefault().notify(e);
}
}
}
}
/**
* 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);
}
private String selectedSourceAsMinify(final JEditorPane pane) {
MinifyProperty minifyProperty = MinifyProperty.getInstance();
String minifedString = "";
String oldContent = "";
FileObject fileObject = NbEditorUtilities.getFileObject(pane.getDocument());
try {
final TokenSequence ts = TokenHierarchy.get(pane.getDocument()).tokenSequence();
final StringBuilder sb = new StringBuilder();
ts.move(pane.getSelectionStart());
while (ts.moveNext() && ts.offset() < pane.getSelectionEnd()) {
sb.append(ts.token().text().toString());
}
MinifyUtil minifyUtil = new MinifyUtil();
oldContent = sb.toString();
minifedString = minifyUtil.compressSelectedJavaScript(fileObject.getNameExt(), sb.toString(), minifyProperty);
NotificationDisplayer.getDefault().notify("Successful copied", NotificationDisplayer.Priority.NORMAL.getIcon(), "Copied as minified JS source.", null);
} catch (IOException ex) {
Exceptions.printStackTrace(ex);
}
if (minifedString.equals("")) {
minifedString = oldContent;
}
return minifedString;
}
private String getSelectedText(JEditorPane pane, int offset) {
if (pane != null
&& pane.getCaret() != null
&& pane.getSelectionStart() <= offset
&& offset <= pane.getSelectionEnd()) {
return pane.getSelectedText();
}
return null;
}
/**
* 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;
}
/**
*
*/
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();
}
}
}
}
private static String getIdentifier (
JPDADebugger debugger,
StyledDocument doc,
JEditorPane ep,
int offset,
boolean[] isMethodPtr,
String[] fieldOfPtr
) {
// 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;
}
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);
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;
}
String ident = t.substring (identStart, identEnd);
if (JAVA_KEYWORDS.contains(ident)) {
// Java keyword => Do not show anything
return null;
}
int newOffset = NbDocument.findLineOffset(doc, line) + identStart + 1;
final boolean[] isFieldStatic = new boolean[] { false };
if (!isValidTooltipLocation(debugger, doc, newOffset, ident, fieldOfPtr, isFieldStatic)) {
return null;
}
while (identEnd < lineLen &&
Character.isWhitespace(t.charAt(identEnd))
) {
identEnd++;
}
if (identEnd < lineLen && t.charAt(identEnd) == '(') {
// We're at a method call
isMethodPtr[0] = true;
}
return ident;
} catch (BadLocationException e) {
return null;
}
}
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;
}
}
private static String getIdentifier (
JPDADebugger debugger,
StyledDocument doc,
JEditorPane ep,
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;
}
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);
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;
}
String ident = t.substring (identStart, identEnd);
//if (JS_KEYWORDS.contains(ident)) {
// JS 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;
} catch (BadLocationException e) {
return null;
}
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}
/**
* 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);
}