Java Code Examples for org.openide.cookies.EditorCookie#getOpenedPanes()

The following examples show how to use org.openide.cookies.EditorCookie#getOpenedPanes() . 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: ParsingFoldSupport.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private static int getCaretPos(FoldHierarchy h) {
    int caretPos = -1;
    JTextComponent c = h.getComponent();
    if (c == null) {
        return -1;
    }
    Document doc = getDocument(h);
    Object od = doc.getProperty(Document.StreamDescriptionProperty);
    if (od instanceof DataObject) {
        DataObject d = (DataObject)od;
        EditorCookie cake = d.getCookie(EditorCookie.class);
        JEditorPane[] panes = cake.getOpenedPanes();
        int idx = panes == null ? -1 : Arrays.asList(panes).indexOf(c);
        if (idx != -1) {
            caretPos = c.getCaret().getDot();
        }
    }
    return caretPos;
}
 
Example 2
Source File: AnnotateAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performContextAction (Node[] nodes) {
    if (visible(nodes)) {
        JEditorPane pane = activatedEditorPane(nodes);
        AnnotationBarManager.hideAnnotationBar(pane);
    } else {
        EditorCookie ec = activatedEditorCookie(nodes);
        if (ec == null) return;
        final File file = activatedFile(nodes);
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes == null) {
            ec.open();
            panes = ec.getOpenedPanes();
        }

        if (panes == null) {
            return;
        }
        final JEditorPane currentPane = panes[0];
        showAnnotations(currentPane, file, null);
    }
}
 
Example 3
Source File: Utils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
/**
 * Saves the given document to its underlying {@link FileObject} if the document
 * is not opened in the nb editor, more formally if EditorCookie.getOpenedPanes() == null.
 * 
 * @param document
 * @throws IOException 
 */
public static void saveDocumentIfNotOpened(Document document) throws IOException {

    Object o = document.getProperty(Document.StreamDescriptionProperty);
    if (o == null || !(o instanceof DataObject)) {
        return;
    }
    DataObject dobj = (DataObject) o;
    EditorCookie ec = dobj.getLookup().lookup(EditorCookie.class);
    if (ec != null && ec.getOpenedPanes() == null) {
        //file not open in any editor
        SaveCookie save = dobj.getLookup().lookup(SaveCookie.class);
        if (save != null) {
            save.save();
        }
    }
}
 
Example 4
Source File: TextDetail.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void highlightDetail(EditorCookie edCookie) {
    if (markLength > 0) {
        final JEditorPane[] panes = edCookie.getOpenedPanes();
        if (panes != null && panes.length > 0) {
            // Necessary since above lineObj.show leads to invoke
            // later as well.
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    try {
                        Caret caret = panes[0].getCaret(); // #23626
                        caret.moveDot(caret.getDot() + markLength);
                    } catch (Exception e) { // #217038
                        StatusDisplayer.getDefault().setStatusText(
                                Bundle.MSG_CannotShowTextDetai());
                        LOG.log(Level.FINE,
                                Bundle.MSG_CannotShowTextDetai(), e);
                    }
                }
            });
        }
    }
}
 
Example 5
Source File: VcsAnnotateAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void actionPerformed(ActionEvent e) {
    if (visible()) {
        JEditorPane pane = activatedEditorPane();
        AnnotationBarManager.hideAnnotationBar(pane);
    } else {
        EditorCookie ec = activatedEditorCookie();
        if (ec == null) return;
        
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes == null) {
            ec.open();
        }
        panes = ec.getOpenedPanes();
        if (panes == null) {
            return;
        }
        final JEditorPane currentPane = panes[0];
        
        AnnotationBar ab = AnnotationBarManager.showAnnotationBar(currentPane);
        ab.setAnnotationMessage(NbBundle.getMessage(VcsAnnotateAction.class, "CTL_AnnotationSubstitute")); // NOI18N;
        
        computeAnnotationsAsync(ab);
    }
}
 
Example 6
Source File: XMLMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
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);
            }
        }
    }
}
 
Example 7
Source File: CssBracketCompleterTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
private void setupEditor() throws IOException {
    // this doesn't work since the JTextPane doesn't like our Kits since they aren't StyleEditorKits.
    //            Document doc = createDocument();
    //            JTextPane pane = new JTextPane((StyledDocument)doc);
    //            EditorKit kit = CloneableEditorSupport.getEditorKit("text/css");
    //            pane.setEditorKit(kit);

    File tmpFile = new File(getWorkDir(), "bracketCompleterTest.css");
    tmpFile.createNewFile();
    FileObject fo = FileUtil.createData(tmpFile);
    DataObject dobj = DataObject.find(fo);
    EditorCookie ec = dobj.getCookie(EditorCookie.class);
    this.doc = ec.openDocument();
    ec.open();
    this.pane = ec.getOpenedPanes()[0];

    this.defaultKeyTypedAction = (BaseAction) pane.getActionMap().get(NbEditorKit.defaultKeyTypedAction);
    this.backspaceAction = (BaseAction) pane.getActionMap().get(NbEditorKit.deletePrevCharAction);
    this.deleteAction = (BaseAction) pane.getActionMap().get(NbEditorKit.deleteNextCharAction);
}
 
Example 8
Source File: BlameAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@Override
protected void performContextAction(Node[] nodes) {        
    if(!Subversion.getInstance().checkClientAvailable()) {            
        return;
    }        
    if (visible(nodes)) {
        JEditorPane pane = activatedEditorPane(nodes);
        AnnotationBarManager.hideAnnotationBar(pane);
    } else {
        EditorCookie ec = activatedEditorCookie(nodes);
        if (ec == null) return;
        
        final File file = activatedFile(nodes);

        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes == null) {
            ec.open();
            panes = ec.getOpenedPanes();
        }
        if (panes == null) {
            return;
        }
        final JEditorPane currentPane = panes[0];
        showAnnotations(currentPane, file, null);
    }
}
 
Example 9
Source File: CSSMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
protected final void cssMinify(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) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }
}
 
Example 10
Source File: JSMinifyClipboard.java    From minifierbeans with Apache License 2.0 6 votes vote down vote up
protected final void jsMinify(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) {
                ErrorManager.getDefault().notify(e);
            }
        }
    }
}
 
Example 11
Source File: GoToActionOrViewAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private int getOffset(EditorCookie editorCookie) {
    if (editorCookie == null) {
        return DEFAULT_OFFSET;
    }
    JEditorPane[] openedPanes = editorCookie.getOpenedPanes();
    if (openedPanes == null || openedPanes.length == 0) {
        return DEFAULT_OFFSET;
    }
    return openedPanes[0].getCaretPosition();
}
 
Example 12
Source File: TokensBrowserTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private AbstractDocument getCurrentDocument () {
    Node[] ns = TopComponent.getRegistry ().getActivatedNodes ();
    if (ns.length != 1) return null;
    EditorCookie editorCookie = ns [0].getLookup ().
        lookup (EditorCookie.class);
    if (editorCookie == null) return null;
    if (editorCookie.getOpenedPanes () == null) return null;
    if (editorCookie.getOpenedPanes ().length < 1) return null;
    JEditorPane pane = editorCookie.getOpenedPanes () [0];
    
    if (caretListener == null)
        caretListener = new CListener ();
    if (lastPane != null && lastPane != pane) {
        lastPane.removeCaretListener (caretListener);
        lastPane = null;
    }
    if (lastPane == null) {
        pane.addCaretListener (caretListener);
        lastPane = pane;
    }

    AbstractDocument doc = (AbstractDocument) editorCookie.getDocument ();
    if (documentListener == null)
        documentListener = new CDocumentListener ();
    if (lastDocument != null && lastDocument != doc) {
        lastDocument.removeDocumentListener (documentListener);
        lastDocument = null;
    }
    if (lastDocument == null) {
        doc.addDocumentListener (documentListener);
        lastDocument = doc;
    }
    return doc;
}
 
Example 13
Source File: CPActionsImplementationProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static boolean isFromEditor(EditorCookie ec) {
    if (ec != null && ec.getOpenedPanes() != null) {
        TopComponent activetc = TopComponent.getRegistry().getActivated();
        if (activetc instanceof CloneableEditorSupport.Pane) {
            return true;
        }
    }
    return false;
}
 
Example 14
Source File: TokensBrowserTopComponent.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JEditorPane getCurrentEditor () {
    Node[] ns = TopComponent.getRegistry ().getActivatedNodes ();
    if (ns.length != 1) return null;
    EditorCookie editorCookie = ns [0].getLookup ().
        lookup (EditorCookie.class);
    if (editorCookie == null) return null;
    if (editorCookie.getOpenedPanes () == null) return null;
    if (editorCookie.getOpenedPanes ().length < 1) return null;
    return editorCookie.getOpenedPanes () [0];
}
 
Example 15
Source File: VcsAnnotateAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private JEditorPane activatedEditorPane() {
    EditorCookie ec = activatedEditorCookie();        
    if (ec != null && SwingUtilities.isEventDispatchThread()) {              
        JEditorPane[] panes = ec.getOpenedPanes();
        if (panes != null && panes.length > 0) {
            return panes[0];
        }
    }
    return null;
}
 
Example 16
Source File: RefactoringActionsProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
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 17
Source File: SimpleFactoryTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void testOurNodeSubclassCreated() throws Exception {
    final EditorCookie ec = obj.getLookup().lookup(EditorCookie.class);
    assertNotNull("Editor", ec);
    Openable open = obj.getLookup().lookup(Openable.class);
    assertNotNull("Open", open);
    open.open();
    
    class GEP implements Runnable {
        JEditorPane[] arr;
        
        @Override
        public void run() {
            arr = ec.getOpenedPanes();
        }
    }
    GEP gep = new GEP();
    EventQueue.invokeAndWait(gep);
    
    assertEquals("One", 1, gep.arr.length);
    
    MyCE mice = null;
    Container c = gep.arr[0];
    for (;;) {
        if (c instanceof MyCE) {
            // OK
            mice = (MyCE)c;
            break;
        }
        if (c instanceof CloneableEditor) {
            fail("Wrong CloneableEditor: " + c);
        }
        if (c == null) {
            fail("No good parent!");
        }
        c = c.getParent();
    }
    
    assertNotNull("MyCE found", mice);
    assertNull("No integers", mice.getLookup().lookup(Integer.class));
    ((SO)obj).addInteger();
    assertEquals("One integer in object", Integer.valueOf(10), obj.getLookup().lookup(Integer.class));
    assertEquals("One integer", Integer.valueOf(10), mice.getLookup().lookup(Integer.class));
    
    Savable sav = obj.getLookup().lookup(Savable.class);
    assertNull("No savable yet", sav);
    
    ec.getDocument().insertString(0, "Ahoj!", null);
    
    sav = obj.getLookup().lookup(Savable.class);
    assertNotNull("Now modified", sav);
    
    assertEquals(obj.getPrimaryFile().getNameExt(), sav.toString());
    obj.setModified(false);
    assertNull("Changes discarded", obj.getLookup().lookup(Savable.class));
}
 
Example 18
Source File: RefactoringActionsProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private boolean isFromEditor(Lookup lookup) {
    //TODO: is from editor? review, improve
    EditorCookie ec = lookup.lookup(EditorCookie.class);
    return ec != null && ec.getOpenedPanes() != null;
}
 
Example 19
Source File: InsertI18nStringAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
/**
 * Open I18nPanel and grab user response then update Document.
 * @param activatedNodes currently activated nodes
 */
protected void performAction (final Node[] activatedNodes) {
    try {
        final EditorCookie editorCookie = (activatedNodes[0]).getCookie(EditorCookie.class);
        if (editorCookie == null) {
            Util.debug(new IllegalArgumentException("Missing editor cookie!")); // NOI18N
            return;
        }

        // Set data object.
        dataObject = activatedNodes[0].getCookie(DataObject.class);
        if (dataObject == null) {
            Util.debug(new IllegalArgumentException("Missing DataObject!"));    // NOI18N
            return;
        }

        JEditorPane[] panes = editorCookie.getOpenedPanes();

        if (panes == null || panes.length == 0) {
            //??? should it be tools action at all, once launched
            // from node it may raise this exception or it inserts
            // string in latest caret position in possibly hidden source
            Util.debug(new IllegalArgumentException("Missing editor pane!"));   // NOI18N
            return;
        }

        // Set insert position.
        position = NbDocument.createPosition(panes[0].getDocument(), panes[0].getCaret().getDot(), Position.Bias.Backward);

        // If there is a i18n action in run on the same editor, cancel it.
        I18nManager.getDefault().cancel();

        try {
            showModalPanel();
        } catch(IOException ex) {
            String msg = "Document loading failure " + dataObject.getName();    // NOI18N
            Util.debug(msg, ex);
            return;
        }

        // Ensure caret is visible.
        panes[0].getCaret().setVisible(true);
    } catch (BadLocationException blex) {
        ErrorManager.getDefault().notify(blex);
    } finally {
        dataObject = null;
        support = null;
        i18nPanel = null;
        position = null;
    }
}
 
Example 20
Source File: StatusProvider.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@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
}