当前位置: 首页>>代码示例>>Java>>正文


Java JEditorPane.getSelectedText方法代码示例

本文整理汇总了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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:26,代码来源:EditorContextImpl.java

示例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);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:35,代码来源:EditorContextImpl.java

示例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 ();
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:CodeEvaluatorUI.java

示例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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:35,代码来源:FileSearchAction.java

示例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;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:49,代码来源:ToolTipAnnotation.java

示例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  );
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:49,代码来源:GetJavaWord.java

示例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;
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:50,代码来源:Utils.java

示例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;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:72,代码来源:AbstractJSToolTipAnnotation.java

示例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();
			}
		}
	}
}
 
开发者ID:ModelWriter,项目名称:Tarski,代码行数:67,代码来源:EditorActions.java

示例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();
      }
    }
  }
}
 
开发者ID:ModelWriter,项目名称:Tarski,代码行数:49,代码来源:EditorActions.java


注:本文中的javax.swing.JEditorPane.getSelectedText方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。