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


Java JEditorPane.getCaretPosition方法代码示例

本文整理汇总了Java中javax.swing.JEditorPane.getCaretPosition方法的典型用法代码示例。如果您正苦于以下问题:Java JEditorPane.getCaretPosition方法的具体用法?Java JEditorPane.getCaretPosition怎么用?Java JEditorPane.getCaretPosition使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在javax.swing.JEditorPane的用法示例。


在下文中一共展示了JEditorPane.getCaretPosition方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: resolveRoot

import javax.swing.JEditorPane; //导入方法依赖的package包/类
public static void resolveRoot(final Lookup lookup, final boolean searchFromBase, final boolean isCallerGraph, final Task<Call> rootCallback) {
        JavaSource js = null;
        RootResolver resolver = null;
        EditorCookie ec = lookup.lookup(EditorCookie.class);
        if (ec != null/*RefactoringActionsProvider.isFromEditor(ec)*/) {
            JEditorPane openedPane = NbDocument.findRecentEditorPane(ec);
            Document doc = ec.getDocument();
            js = JavaSource.forDocument(doc);
            resolver = new RootResolver(openedPane.getCaretPosition(), isCallerGraph, searchFromBase);
        }
//        else {
            // XXX resolve Node.class
//        }
        postResolveRoot(js, resolver, rootCallback);
    }
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:CallHierarchyTasks.java

示例2: invokeInstantRename

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private void invokeInstantRename(final Lookup lookup, final EditorCookie ec) {
    try {
        JEditorPane target = ec.getOpenedPanes()[0];
        final int caret = target.getCaretPosition();
        String ident = Utilities.getIdentifier(Utilities.getDocument(target), caret);
        
        if (ident == null) {
            Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
            return;
        }
        
        DataObject od = (DataObject) target.getDocument().getProperty(Document.StreamDescriptionProperty);
        JavaSource js = od != null ? JavaSource.forFileObject(od.getPrimaryFile()) : null;

        if (js == null) {
            Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
            return;
        }
        InstantRefactoringUI ui = InstantRefactoringUIImpl.create(js, caret);
        
        if (ui != null) {
            if (ui.getRegions().isEmpty() || ui.getKeyStroke() == null) {
                doFullRename(lookup);
            } else {
                doInstantRename(target, js, caret, ui);
            }
        } else {
            Utilities.setStatusBoldText(target, WARN_CannotPerformHere());
        }
    } catch (BadLocationException e) {
        Exceptions.printStackTrace(e);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:34,代码来源:RefactoringActionsProvider.java

示例3: getCurrentClassDeclaration

import javax.swing.JEditorPane; //导入方法依赖的package包/类
/** @return declared class name
 */
public String getCurrentClassDeclaration() {
    FileObject fo = contextDispatcher.getCurrentFile();
    if (fo == null) {
        return null;
    }
    JEditorPane ep = contextDispatcher.getCurrentEditor();
    final int currentOffset = (ep == null) ? 0 : ep.getCaretPosition();
    //final int currentOffset = org.netbeans.editor.Registry.getMostActiveComponent().getCaretPosition();
    
    return EditorContextSupport.getClassDeclaredAt(fo, currentOffset);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:14,代码来源:EditorContextImpl.java

示例4: getCurrentMethodDeclaration

import javax.swing.JEditorPane; //导入方法依赖的package包/类
/** @return { "method name", "method signature", "enclosing class name" }
 */
@Override
public String[] getCurrentMethodDeclaration() {
    FileObject fo = contextDispatcher.getCurrentFile();
    if (fo == null) {
        return null;
    }
    JEditorPane ep = contextDispatcher.getCurrentEditor();
    final int currentOffset = (ep == null) ? 0 : ep.getCaretPosition();
    return EditorContextSupport.getMethodDeclaredAt(fo, currentOffset);
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:13,代码来源:EditorContextImpl.java

示例5: 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

示例6: debugCaret

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private static StringBuilder debugCaret(StringBuilder sb, JEditorPane pane) throws Exception {
    int caretOffset = pane.getCaretPosition();
    Document doc = pane.getDocument();
    sb.append("caret[").append(caretOffset).append("]sel<");
    sb.append(pane.getSelectionStart()).append(',');
    sb.append(pane.getSelectionEnd()).append('>');
    int startTextOffset = Math.max(0, caretOffset - 2);
    int endTextOffset = Math.min(caretOffset + 2, doc.getLength() + 1);
    sb.append(" \"");
    CharSequenceUtilities.debugText(sb, doc.getText(startTextOffset, caretOffset - startTextOffset));
    sb.append('|');
    CharSequenceUtilities.debugText(sb, doc.getText(caretOffset, endTextOffset - caretOffset));
    sb.append("\"");
    return sb;
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:16,代码来源:EditorPaneTesting.java

示例7: runInEDT

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private void runInEDT(Snapshot snapshot, Model model, final FileObject file, String mimeType, int caretOffset) {
    LOG.log(Level.FINER, "runInEDT(), file: {0}, caret: {1}", new Object[]{file, caretOffset});

    if (cancelled) {
        LOG.log(Level.FINER, "cancelled");
        return;
    }

    if (caretOffset == -1) {
        try {
            //dirty workaround
            DataObject dobj = DataObject.find(file);
            EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
            if (ec != null) {
                JEditorPane pane = NbDocument.findRecentEditorPane(ec);
                if (pane != null) {
                    caretOffset = pane.getCaretPosition();
                }
            }

        } catch (DataObjectNotFoundException ex) {
            //possibly deleted file, give up
            return;

        }
        
        LOG.log(Level.INFO, "workarounded caret offset: {0}", caretOffset);
    }


    //find rule corresponding to the offset
    Rule rule = findRuleAtOffset(snapshot, model, caretOffset);
    //>>hack, remove once the css.lib css grammar gets finalized
    if(rule != null && rule.getSelectorsGroup() == null) {
        rule = null;
    }
    //<<hack
    
    
    if(rule != null) {
        //check whether the rule is virtual
        if(snapshot.getOriginalOffset(rule.getSelectorsGroup().getStartOffset()) == -1) {
            //virtual selector created for html source element with class or id attribute
            LOG.log(Level.FINER, "the found rule is virtual, exiting w/o change of the RuleEditor", caretOffset);
            return ;
        }
    }
    
    if (!mimeType.equals("text/css")) {
        //if not a css file, 
        //update the rule editor only if there's a rule in an embedded css code
        if (rule == null) {
            LOG.log(Level.FINER, "not a css file and rule not found at {0} offset, exiting w/o change of the RuleEditor", caretOffset);
            return;
        }
    }

    final CssStylesTC cssStylesTC = (CssStylesTC) WindowManager.getDefault().findTopComponent(CssStylesTC.ID);
    if (cssStylesTC == null) {
        return;
    }
    
    //update the RuleEditor TC name
    RuleEditorController controller = cssStylesTC.getRuleEditorController();
    LOG.log(Level.FINER, "SourceTask: calling controller.setModel({0})", model);
    controller.setModel(model);
    
    if (rule == null) {
        controller.setNoRuleState();
    } else {
        controller.setRule(rule);
    }
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:74,代码来源:CssCaretAwareSourceTask.java

示例8: updateTextInAWT

import javax.swing.JEditorPane; //导入方法依赖的package包/类
private void updateTextInAWT(EditorCookie es, final String in) throws IOException, BadLocationException {
    StyledDocument tmpdoc = es.getDocument();
    if (tmpdoc == null)
        tmpdoc = es.openDocument();

    //sample editor position

    JEditorPane[] eps = es.getOpenedPanes();
    JEditorPane pane = null;
    JViewport port = null;
    int caretPosition = 0;
    Point viewPosition = null;
    if (eps != null) {
        pane = eps[0];
        caretPosition = pane.getCaretPosition();
        port = getParentViewport (pane);
        if (port != null)
            viewPosition = port.getViewPosition();
    }

    // prepare modification task

    final Exception[] taskEx = new Exception[] {null};
    final StyledDocument sdoc = tmpdoc;

    Runnable task = new Runnable() {
        public void run() {
            try {
                sdoc.remove (0, sdoc.getLength());  // right alternative

                // we are at Unicode level
                sdoc.insertString (0, in, null);
            } catch (Exception iex) {
                taskEx[0] = iex;
            }
        }
    };

    // perform document modification

    org.openide.text.NbDocument.runAtomicAsUser(sdoc, task);

    //??? setModified (true);  

    //restore editor position

    if (eps != null) {
        try {
            pane.setCaretPosition (caretPosition);
        } catch (IllegalArgumentException e) {
        }
        port.setViewPosition (viewPosition);
    }

    if (taskEx[0]!=null) {
        if (taskEx[0] instanceof IOException) {
            throw (IOException)taskEx[0];
        }
        throw new IOException(taskEx[0]);
    }
    
}
 
开发者ID:apache,项目名称:incubator-netbeans,代码行数:63,代码来源:XMLTextRepresentation.java

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


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