Java Code Examples for javax.swing.JEditorPane#getCaretPosition()
The following examples show how to use
javax.swing.JEditorPane#getCaretPosition() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CallHierarchyTasks.java From netbeans with Apache License 2.0 | 5 votes |
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); }
Example 2
Source File: RefactoringActionsProvider.java From netbeans with Apache License 2.0 | 5 votes |
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); } }
Example 3
Source File: EditorContextImpl.java From netbeans with Apache License 2.0 | 5 votes |
/** @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); }
Example 4
Source File: EditorContextImpl.java From netbeans with Apache License 2.0 | 5 votes |
/** @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); }
Example 5
Source File: EditorContextImpl.java From netbeans with Apache License 2.0 | 5 votes |
/** 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); }
Example 6
Source File: JaxWsCodeGenerator.java From netbeans with Apache License 2.0 | 5 votes |
public static void insertMethodCall(InvokeOperationCookie.TargetSourceType targetSourceType, DataObject dataObj, Lookup sourceNodeLookup) { EditorCookie cookie = dataObj.getCookie(EditorCookie.class); OperationNode opNode = sourceNodeLookup.lookup(OperationNode.class); boolean inJsp = InvokeOperationCookie.TargetSourceType.JSP == targetSourceType; Node portNode = opNode.getParentNode(); Node serviceNode = portNode.getParentNode(); addProjectReference(serviceNode, dataObj); final Document document; int position = -1; if (inJsp) { //TODO: //this should be handled differently, see issue 60609 document = cookie.getDocument(); try { String content = document.getText(0, document.getLength()); position = content.lastIndexOf("</body>"); //NOI18N if (position < 0) { position = content.lastIndexOf("</html>"); //NOI18N } if (position >= 0) { //find where line begins while (position > 0 && content.charAt(position - 1) != '\n' && content.charAt(position - 1) != '\r') { position--; } } else { position = document.getLength(); } } catch (BadLocationException ble) { Exceptions.printStackTrace(ble); } } else { EditorCookie ec = dataObj.getCookie(EditorCookie.class); JEditorPane pane = ec.getOpenedPanes()[0]; document = pane.getDocument(); position = pane.getCaretPosition(); } final int pos = position; insertMethod(document, pos, opNode); }
Example 7
Source File: EditorPaneTesting.java From netbeans with Apache License 2.0 | 5 votes |
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; }
Example 8
Source File: StatusProvider.java From netbeans with Apache License 2.0 | 4 votes |
@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 }
Example 9
Source File: CssCaretAwareSourceTask.java From netbeans with Apache License 2.0 | 4 votes |
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); } }
Example 10
Source File: XMLTextRepresentation.java From netbeans with Apache License 2.0 | 4 votes |
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]); } }