本文整理匯總了Java中javax.swing.JEditorPane.getSelectedText方法的典型用法代碼示例。如果您正苦於以下問題:Java JEditorPane.getSelectedText方法的具體用法?Java JEditorPane.getSelectedText怎麽用?Java JEditorPane.getSelectedText使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類javax.swing.JEditorPane
的用法示例。
在下文中一共展示了JEditorPane.getSelectedText方法的10個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: getSelectedIdentifier
import javax.swing.JEditorPane; //導入方法依賴的package包/類
/**
* Returns identifier currently selected in editor or <code>null</code>.
*
* @return identifier currently selected in editor or <code>null</code>
*/
@Override
public String getSelectedIdentifier () {
JEditorPane ep = contextDispatcher.getCurrentEditor ();
if (ep == null) {
return null;
}
Caret caret = ep.getCaret();
if (caret == null) {
// No caret => no selected text
return null;
}
String s = ep.getSelectedText ();
if (s == null) {
return null;
}
if (Utilities.isJavaIdentifier (s)) {
return s;
}
return null;
}
示例2: 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);
}
示例3: openEvaluator
import javax.swing.JEditorPane; //導入方法依賴的package包/類
public static void openEvaluator() {
String selectedText = null;
JEditorPane editor = EditorContextDispatcher.getDefault().getMostRecentEditor();
if (editor != null) {
selectedText = editor.getSelectedText();
}
CodeEvaluatorUI evaluator = getInstance();
evaluator.open ();
if (selectedText != null) {
evaluator.codePane.setText(selectedText);
evaluator.codeText = selectedText;
}
evaluator.codePane.selectAll();
evaluator.requestActive ();
}
示例4: getSelectedFiles
import javax.swing.JEditorPane; //導入方法依賴的package包/類
private FileDescriptor[] getSelectedFiles() {
FileDescriptor[] result = null;
panel = new FileSearchPanel(this, findCurrentProject());
dialog = createDialog(panel);
Node[] arr = TopComponent.getRegistry ().getActivatedNodes();
if (arr.length > 0) {
EditorCookie ec = arr[0].getCookie (EditorCookie.class);
if (ec != null) {
JEditorPane recentPane = NbDocument.findRecentEditorPane(ec);
if (recentPane != null) {
String initSearchText = null;
if (org.netbeans.editor.Utilities.isSelectionShowing(recentPane.getCaret())) {
initSearchText = recentPane.getSelectedText();
}
if (initSearchText != null) {
if (initSearchText.length() > 256) {
initSearchText = initSearchText.substring(0, 256);
}
panel.setInitialText(initSearchText);
} else {
FileObject fo = arr[0].getLookup().lookup(FileObject.class);
if (fo != null) {
panel.setInitialText(fo.getNameExt());
}
}
}
}
}
dialog.setVisible(true);
result = panel.getSelectedFiles();
return result;
}
示例5: 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;
}
}
示例6: forPane
import javax.swing.JEditorPane; //導入方法依賴的package包/類
static String forPane(JEditorPane p) {
if (p == null) return null;
String selection = p.getSelectedText ();
if ( selection != null && selection.length() > 0 ) {
return selection;
} else {
// try to guess which word is underneath the caret's dot.
Document doc = p.getDocument();
Element lineRoot;
if (doc instanceof StyledDocument) {
lineRoot = NbDocument.findLineRootElement((StyledDocument)doc);
} else {
lineRoot = doc.getDefaultRootElement();
}
int dot = p.getCaret().getDot();
Element line = lineRoot.getElement(lineRoot.getElementIndex(dot));
if (line == null) return null;
String text = null;
try {
text = doc.getText(line.getStartOffset(),
line.getEndOffset() - line.getStartOffset());
} catch (BadLocationException e) {
return null;
}
if ( text == null )
return null;
int pos = dot - line.getStartOffset();
if ( pos < 0 || pos >= text.length() )
return null;
int bix, eix;
for( bix = Character.isJavaIdentifierPart( text.charAt( pos ) ) ? pos : pos - 1;
bix >= 0 && Character.isJavaIdentifierPart( text.charAt( bix ) ); bix-- );
for( eix = pos; eix < text.length() && Character.isJavaIdentifierPart( text.charAt( eix )); eix++ );
return bix == eix ? null : text.substring( bix + 1, eix );
}
}
示例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: 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();
}
}
}
}
示例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();
}
}
}
}