Java Code Examples for org.openide.cookies.OpenCookie#open()

The following examples show how to use org.openide.cookies.OpenCookie#open() . 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: GenerateClassAction.java    From netbeans with Apache License 2.0 6 votes vote down vote up
@NbBundle.Messages({
    "# {0} - file name",
    "MSG_CouldNotOpenTargetFile=Could not open generated file {0}.java in editor",
    "# {0} - the reported error message",
    "ERR_GenerationError=Error during class creation: {0}. See the log for the details.",
    "TITLE_GenerationError=Class creation failed"
})
private void finishGeneration(SnippetClassGenerator task) {
    if (task.getJavaFile() != null) {
        // open the file
        OpenCookie cake = task.getJavaFile().getLookup().lookup(OpenCookie.class);
        if (cake != null) {
            cake.open();
        } else {
            StatusDisplayer.getDefault().setStatusText(
                    Bundle.MSG_CouldNotOpenTargetFile(task.getJavaFile().getName()));
        }
    }
    
    if (task.getError() != null) {
        NotifyDescriptor nd = new NotifyDescriptor.Message(Bundle.ERR_GenerationError(
            task.getError().getLocalizedMessage()),NotifyDescriptor.ERROR_MESSAGE);
        DialogDisplayer.getDefault().notify(nd);
    }
}
 
Example 2
Source File: SpringXMLConfigEditorUtils.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public static boolean openFile(FileObject fo, int offset) {
    DataObject dataObject;
    boolean opened = false;
    try {
        dataObject = DataObject.find(fo);
        if (offset > 0) {
            opened = openFileAtOffset(dataObject, offset);
        }
    } catch (IOException e) {
        Exceptions.printStackTrace(e);
        return false;
    }
    if (opened) {
        return true;
    } else {
        OpenCookie oc = dataObject.getCookie(org.openide.cookies.OpenCookie.class);
        if (oc != null) {
            oc.open();
            return true;
        }
    }
    return false;
}
 
Example 3
Source File: ViewPomAction.java    From NBANDROID-V2 with Apache License 2.0 6 votes vote down vote up
@Override
protected void performAction(Node[] activatedNodes) {
    for (Node node : activatedNodes) {
        ArtifactData data = node.getLookup().lookup(ArtifactData.class);
        if(data!=null && data.getPomPath()!=null){
            FileObject fo = FileUtil.toFileObject(new File(data.getPomPath()));
            if(fo!=null){
                try {
                    DataObject dob = DataObject.find(fo);
                    OpenCookie open = dob.getLookup().lookup(OpenCookie.class);
                    if(open!=null){
                        open.open();
                    }
                } catch (DataObjectNotFoundException ex) {
                }
            }
        }
    }
}
 
Example 4
Source File: JSFConfigHyperlinkProvider.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@NbBundle.Messages({
    "lbl.goto.source.not.found=Source file for {0} not found.",
    "lbl.managed.bean.not.found=Managed bean {0} not found."
})
@Override
public void run(CompilationController cc) throws Exception {
    cc.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED);
    Elements elements = cc.getElements();
    TypeElement element = elements.getTypeElement(fqn.trim());
    if (element != null) {
        elementFound.set(true);
        ElementHandle el = ElementHandle.create(element);
        FileObject fo = SourceUtils.getFile(el, cpi);
        if (fo == null) {
            StatusDisplayer.getDefault().setStatusText(Bundle.lbl_managed_bean_not_found(fqn));
            Toolkit.getDefaultToolkit().beep();
            return;
        }

        // Not a regular Java data object (may be a multi-view data object), open it first
        DataObject od = DataObject.find(fo);
        if (!"org.netbeans.modules.java.JavaDataObject".equals(od.getClass().getName())) { //NOI18N
            OpenCookie oc = od.getCookie(org.openide.cookies.OpenCookie.class);
            oc.open();
        }

        if (!ElementOpen.open(fo, el)) {
            StatusDisplayer.getDefault().setStatusText(Bundle.lbl_goto_source_not_found(fqn));
            Toolkit.getDefaultToolkit().beep();
        }
    } else if (!SourceUtils.isScanInProgress()) {
        StatusDisplayer.getDefault().setStatusText(Bundle.lbl_managed_bean_not_found(fqn));
        Toolkit.getDefaultToolkit().beep();
    }
}
 
Example 5
Source File: CMPFieldsNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void open() {
    try {
        DataObject ddFileDO = DataObject.find(ddFile);
        OpenCookie cookie = ddFileDO.getLookup().lookup(OpenCookie.class);
        if (cookie != null) {
            cookie.open();
        }
    } catch (DataObjectNotFoundException donf) {
        Exceptions.printStackTrace(donf);
    }
}
 
Example 6
Source File: FXMLEditAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
@Override
public void actionPerformed(ActionEvent e) {
    DataObject dobj = context.lookup(DataObject.class);
    OpenCookie oc = dobj.getCookie(OpenCookie.class);
    if (oc != null) {
        oc.open();
    }
}
 
Example 7
Source File: OpenAction.java    From netbeans with Apache License 2.0 5 votes vote down vote up
protected void performAction(final Node[] activatedNodes) {
    for (int i = 0; i < activatedNodes.length; i++) {
        OpenCookie oc = activatedNodes[i].getCookie(OpenCookie.class);

        if (oc != null) {
            oc.open();
        }
    }
}
 
Example 8
Source File: CheckNodeListener.java    From netbeans with Apache License 2.0 5 votes vote down vote up
static boolean findInSource(CheckNode node) {
    Object o = node.getUserObject();
    if (o instanceof TreeElement) {
        if (o instanceof Openable) {
            ((Openable) o).open();
            return true;
        } else {
        o = ((TreeElement) o).getUserObject();
        if (o instanceof RefactoringElement) {
            APIAccessor.DEFAULT.getRefactoringElementImplementation((RefactoringElement) o).openInEditor();
            return true;
        } else if (o instanceof Openable) {
            ((Openable) o).open();
            return true;
        } else if (o instanceof FileObject) {
            try {
                OpenCookie oc = DataObject.find((FileObject) o).getCookie(OpenCookie.class);
                if (oc != null) {
                    oc.open();
                    return true;
                }
            } catch (DataObjectNotFoundException ex) {
                //ignore, unknown file, do nothing
            }
        }
        }
    }
    return false;
}
 
Example 9
Source File: DefaultDataObjectHasNodeInLookupTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testIsThereANodeInTheLookup() throws Exception {
    // make sure it has the cookie
    OpenCookie open = obj.getCookie(OpenCookie.class);
    open.open();
    open = null;

    FileObject fo = obj.getPrimaryFile();
    FileLock lock = fo.lock();
    final Object[] o = new Object[1];
    EventQueue.invokeAndWait(new Runnable() {
        @Override
        public void run() {
            try {
                final EditorCookie c = obj.getCookie(EditorCookie.class);
                assertNotNull(c);
                c.open();

                JEditorPane[] arr = c.getOpenedPanes();

                assertNotNull("Something opened", arr);
                assertEquals("One opened", 1, arr.length);

                o[0] = SwingUtilities.getAncestorOfClass(TopComponent.class, arr[0]);
                assertNotNull("Top component found", o);
            } catch (Exception x) {
                throw new RuntimeException(x);
            }
        }
    });
    TopComponent tc = (TopComponent)o[0];

    DataObject query = tc.getLookup().lookup(DataObject.class);
    assertEquals("Object is in the lookup", obj, query);

    Node node = tc.getLookup().lookup(Node.class);
    assertEquals("Node is in the lookup", obj.getNodeDelegate(), node);
}
 
Example 10
Source File: FileDescription.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void open(@NonNull final DataObject dobj) {
    final Openable openable = dobj.getLookup().lookup(Openable.class);
    if (openable != null) {
        openable.open();
    }
    //Workaround of non functional org.openide.util.Lookup class hierarchy cache.
    final OpenCookie oc = dobj.getLookup().lookup(OpenCookie.class);
    if (oc != null) {
        oc.open();
    }
}
 
Example 11
Source File: DefaultDataObjectHasOpenTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testComponentCanBeSerializedEvenItIsBinary() throws Exception {
    // make sure it has the cookie
    OpenCookie open = obj.getCookie(OpenCookie.class);
    DD.toReturn = DialogDescriptor.OK_OPTION;
    open.open();
    open = null;

    doComponentCanBeSerialized("\u0003\u0001\u0000");
}
 
Example 12
Source File: DefaultDataObjectHasOpenTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testBinaryFilesQuestionContainsOpenActions() throws Exception {
    EditorCookie c = tryToOpen("\u0003\u0001\u0000");
    assertNull(c);

    DD.toReturn = DialogDescriptor.CLOSED_OPTION;
    OpenCookie open = obj.getCookie(OpenCookie.class);
    open.open();

    assertNotNull("There was a query", DD.options);
    assertEquals("Two options", 2, DD.options.length);
    assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]);
    assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]);
    DD.options = null;

    DD.toReturn = DialogDescriptor.OK_OPTION;
    open.open();

    assertNotNull("There was a query", DD.options);
    assertEquals("Still 2 options", 2, DD.options.length);
    assertEquals(DialogDescriptor.OK_OPTION, DD.options[0]);
    assertEquals(DialogDescriptor.CANCEL_OPTION, DD.options[1]);

    c = obj.getCookie(EditorCookie.class);
    assertNotNull("Editor cookie created", c);

    doRegularCheck(c);
}
 
Example 13
Source File: ExportPatch.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void openFile(File file) {
    FileObject fo = FileUtil.toFileObject(file);
    if (fo != null) {
        try {
            DataObject dao = DataObject.find(fo);
            OpenCookie oc = dao.getCookie(OpenCookie.class);
            if (oc != null) {
                oc.open();
            }
        } catch (DataObjectNotFoundException e) {
            // nonexistent DO, do nothing
        }
    }
}
 
Example 14
Source File: CMPFieldNode.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void open() {
    try {
        DataObject ddFileDO = DataObject.find(ddFile);
        OpenCookie cookie = ddFileDO.getLookup().lookup(OpenCookie.class);
        if (cookie != null) {
            cookie.open();
        }
    } catch (DataObjectNotFoundException donf) {
        Exceptions.printStackTrace(donf);
    }
}
 
Example 15
Source File: UiUtils.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private static void doOpen(final OpenCookie oc) {
    if (SwingUtilities.isEventDispatchThread()) {
        oc.open();
    } else {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                oc.open();
            }
        });
    }
}
 
Example 16
Source File: DefaultDataObjectHasOpenTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doRegularCheck(final EditorCookie c) throws Exception {
    assertEquals(
            "Next questions results in the same cookie",
            c,
            obj.getCookie(EditorCookie.class)
            );
    assertEquals(
            "Next questions results in the same cookie",
            c,
            obj.getLookup().lookup(EditorCookie.class)
            );
    assertEquals(
            "Print cookie is provided",
            c,
            obj.getCookie(PrintCookie.class)
            );
    assertEquals(
            "CloseCookie as well",
            c,
            obj.getCookie(CloseCookie.class)
            );

    OpenCookie open = obj.getCookie(OpenCookie.class);
    open.open();


    Document d = null;
    for (int i = 0; i < 10; i++) {
        d = c.getDocument();
        if (d != null) {
            break;
        }
        Thread.sleep(1000);
    }
    assertNotNull(d);

    d.insertString(0, "Kuk", null);

    assertNotNull(
            "Now there is a save cookie",
            obj.getCookie(SaveCookie.class)
            );
}
 
Example 17
Source File: SimpleDESTest.java    From netbeans with Apache License 2.0 4 votes vote down vote up
private void doCookieCheck (boolean hasEditCookie) throws Exception {
    EditorCookie c = tryToOpen (
        "Ahoj Jardo," +
        "how are you" +
        "\t\n\rBye"
    );
    assertNotNull (c);
    
    assertEquals (
        "Next questions results in the same cookie", 
        c, 
        obj.getCookie(EditorCookie.class)
    );
    assertEquals (
        "Print cookie is provided",
        c,
        obj.getCookie(org.openide.cookies.PrintCookie.class)
    );
    assertEquals (
        "CloseCookie as well",
        c,
        obj.getCookie(org.openide.cookies.CloseCookie.class)
    );
    
    if (hasEditCookie) {
        assertEquals (
            "EditCookie as well",
            c,
            obj.getCookie(org.openide.cookies.EditCookie.class)
        );
    } else {
        assertNull (
            "No EditCookie",
            obj.getCookie(org.openide.cookies.EditCookie.class)
        );
        
    }
    
    OpenCookie open = obj.getCookie(OpenCookie.class);
    open.open ();
    javax.swing.text.Document d = null;
    for (int i = 0; i < 10; i++) {
        d = c.getDocument();
        LOG.log(Level.INFO, "Round {0} document {1}", new Object[]{i, d});
        if (d != null) {
            break;
        }
        Thread.sleep(100);
        waitAWT();
    }
    assertNotNull ("Document is now openned", d);
    
    d.insertString(0, "Kuk", null);
    
    assertNotNull (
        "Now there is a save cookie", 
        obj.getCookie (org.openide.cookies.SaveCookie.class)
    );
}
 
Example 18
Source File: MapActionUtility.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public static void openPageFlowSceneElement(PageFlowSceneElement element) {
    OpenCookie openCookie = (element).getNode().getCookie(OpenCookie.class);
    if (openCookie != null) {
        openCookie.open();
    }
}
 
Example 19
Source File: NbFileOpenHandler.java    From netbeans with Apache License 2.0 4 votes vote down vote up
@Override
    public void open(final FileObject file, final int lineno) {
        // FIXME this should not be needed
        if (!SwingUtilities.isEventDispatchThread()) {
            SwingUtilities.invokeLater(new Runnable() {
                    public void run() {
                        open(file, lineno);
                    }
                });

            return; // not exactly accurate, but....
        }

        try {
            DataObject od = DataObject.find(file);
            EditorCookie ec = od.getCookie(EditorCookie.class);
            LineCookie lc = od.getCookie(LineCookie.class);

            if ((ec != null) && (lc != null)) {
                Document doc = ec.openDocument();

                if (doc != null) {
                    int line = lineno;

                    if (line < 1) {
                        line = 1;
                    }

                    // XXX .size() call is super-slow for large files, see issue
                    // #126531. So we fallback to catching IOOBE
//                    int nOfLines = lines.getLines().size();
//                    if (line > nOfLines) {
//                        line = nOfLines;
//                    }
                    try {
                        Line.Set lines = lc.getLineSet();
                        Line l = lines.getCurrent(line - 1);
                        if (l != null) {
                            l.show(ShowOpenType.OPEN, ShowVisibilityType.FOCUS);
                            return;
                        }
                    } catch (IndexOutOfBoundsException ioobe) {
                        // OK, since .size() cannot be used, see above
                    }
                }
            }

            OpenCookie oc = od.getCookie(OpenCookie.class);

            if (oc != null) {
                oc.open();
                return;
            }
        } catch (IOException e) {
            LOGGER.log(Level.INFO, null, e);
        }
    }
 
Example 20
Source File: OpenTaskAction.java    From netbeans with Apache License 2.0 4 votes vote down vote up
public void actionPerformed( ActionEvent e ) {
    if( !canOpenTask() )
        return;

    ActionListener al = Accessor.getDefaultAction( task );
    if( null != al ) {
        al.actionPerformed( e );
        return;
    }

    URL url = Accessor.getURL( task );
    if( null != url ) {
        URLDisplayer.getDefault().showURL(url);
        return;
    }
    int line = Accessor.getLine( task )-1;
    FileObject fileObject = Accessor.getFile(task);
    if( null == fileObject )
        return;
    /* Find a DataObject for the FileObject: */
    final DataObject dataObject;
    try {
        dataObject = DataObject.find(fileObject);
    } catch( DataObjectNotFoundException donfE ) {
        return;
    }

    LineCookie lineCookie = (LineCookie)dataObject.getCookie( LineCookie.class );
    if( null != lineCookie && openAt( lineCookie, line ) ) {
        return;
    }

    EditCookie editCookie = (EditCookie)dataObject.getCookie( EditCookie.class );
    if( null != editCookie ) {
        editCookie.edit();
        return;
    }

    OpenCookie openCookie = (OpenCookie)dataObject.getCookie( OpenCookie.class );
    if( null != openCookie ) {
        openCookie.open();
        return;
    }

    ViewCookie viewCookie = (ViewCookie)dataObject.getCookie( ViewCookie.class );
    if( null != viewCookie ) {
        viewCookie.view();
        return;
    }
}