Java Code Examples for org.openide.nodes.Node.getCookie()

The following are Jave code examples for showing how to use getCookie() of the org.openide.nodes.Node class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
Example 1
Project: incubator-netbeans   File: SettingChildren.java   Source Code and License Vote up 6 votes
protected Node copyNode (Node node) {
    boolean filter = false;
    try {
        DataObject d = (DataObject) node.getCookie (DataObject.class);
        if (d != null) {
            InstanceCookie.Of inst = (InstanceCookie.Of)d.getCookie(InstanceCookie.Of.class);
            if (inst != null && (inst.instanceOf(Node.class) || inst.instanceOf(Node.Handle.class))) {
                // This is just a node, not a real setting. E.g. ModuleNode, LoaderPoolNode. As such,
                // it itself should not display any origin information, it would make no sense. However
                // its children might have a legitimate DataObject cookie from the SFS.
                d = null;
            }
        }
        DataFolder folder = (DataFolder) node.getCookie (DataFolder.class);
        FileSystem fs = d == null || folder != null ? null : d.getPrimaryFile ().getFileSystem ();
        filter = fs == null ? false : fs.isDefault();
    } catch (FileStateInvalidException e) {
        // ignore
    }

    return filter ? new SettingFilterNode (node) : 
        node.isLeaf() ? node.cloneNode() : new TrivialFilterNode(node);
}
 
Example 2
Project: incubator-netbeans   File: RenameHandlerImpl.java   Source Code and License Vote up 6 votes
@Override
public void handleRename(Node node, String newName) {
    InstanceContent ic = new InstanceContent();
    ic.add(node);
    ExplorerContext d = new ExplorerContext();
    d.setNewName(newName);
    ic.add(d);
    Lookup l = new AbstractLookup(ic);
    DataObject dob = node.getCookie(DataObject.class);
    Action a = RefactoringActionsFactory.renameAction().createContextAwareInstance(l);
    if (Boolean.TRUE.equals(a.getValue("applicable"))) {//NOI18N
        a.actionPerformed(RefactoringActionsFactory.DEFAULT_EVENT);
    } else {
        try {
            dob.rename(newName);
        } catch (IOException ioe) {
            ErrorManager.getDefault().notify(ioe);
        }
    }
}
 
Example 3
Project: incubator-netbeans   File: BlameAction.java   Source Code and License Vote up 5 votes
private File activatedFile(Node[] nodes) {
    if (nodes.length == 1) {
        Node node = nodes[0];
        DataObject dobj = (DataObject) node.getCookie(DataObject.class);
        if (dobj != null) {
            FileObject fo = dobj.getPrimaryFile();
            return FileUtil.toFile(fo);
        }
    }
    return null;
}
 
Example 4
Project: incubator-netbeans   File: I18nAction.java   Source Code and License Vote up 5 votes
/** Overrides superclass method. Adds additional test if i18n module has registered factory
    * for this data object to be able to perform i18n action. */
   protected boolean enable(Node[] activatedNodes) {    
       if (activatedNodes.length != 1) {
           return false;
       }

       final Node node = activatedNodes[0];
       DataObject dataObject = node.getCookie(DataObject.class);
       if ((dataObject == null) || (dataObject.getPrimaryFile() == null)) {
           return false;
       }

       EditorCookie editorCookie = node.getCookie(EditorCookie.class);
       if (editorCookie == null) {
           editorCookie = dataObject.getCookie(EditorCookie.class);
           if (editorCookie == null) {
               return false;
           }
       }

       if (!FactoryRegistry.hasFactory(dataObject.getClass())) {
           return false;
       }

// check that the node has project
       Future<Project[]> openProjects = OpenProjects.getDefault().openProjects();
       if(!openProjects.isDone()) {
           return true;
       }
if (FileOwnerQuery.getOwner(dataObject.getPrimaryFile()) == null) {
           return false;
       }

return true;
   }
 
Example 5
Project: incubator-netbeans   File: VCSContext.java   Source Code and License Vote up 5 votes
private static void addFileObjects(Node node, Set<VCSFileProxy> rootFiles) {
    Collection<? extends NonRecursiveFolder> folders = node.getLookup().lookup(new Lookup.Template<NonRecursiveFolder>(NonRecursiveFolder.class)).allInstances();
    List<VCSFileProxy> nodeFiles = new ArrayList<VCSFileProxy>();
    if (folders.size() > 0) {
        for (Iterator j = folders.iterator(); j.hasNext();) {
            NonRecursiveFolder nonRecursiveFolder = (NonRecursiveFolder) j.next();
            VCSFileProxy proxy = Utils.createFlatFileProxy(nonRecursiveFolder.getFolder());
            if(proxy != null) {
                nodeFiles.add(proxy);
            } else {
                LOG.log(Level.WARNING, "null VCSFileProxy for non recursive folder FileObject {0}", nonRecursiveFolder.getFolder()); // NOI18N
            }
        }
    } else {
        Collection<? extends FileObject> fileObjects = node.getLookup().lookup(new Lookup.Template<FileObject>(FileObject.class)).allInstances();
        if (fileObjects.size() > 0) {
            nodeFiles.addAll(toFileCollection(fileObjects));
        } else {
            DataObject dataObject = node.getCookie(DataObject.class);
            if (dataObject instanceof DataShadow) {
                dataObject = ((DataShadow) dataObject).getOriginal();
            }
            if (dataObject != null) {
                Collection<VCSFileProxy> doFiles = toFileCollection(dataObject.files());
                nodeFiles.addAll(doFiles);
            }
        }
    }
    rootFiles.addAll(nodeFiles);
}
 
Example 6
Project: incubator-netbeans   File: AnnotateAction.java   Source Code and License Vote up 5 votes
private EditorCookie activatedEditorCookie(Node[] nodes) {
    if (nodes == null) {
        nodes = WindowManager.getDefault().getRegistry().getActivatedNodes();
    }
    if (nodes.length == 1) {
        Node node = nodes[0];
        return (EditorCookie) node.getCookie(EditorCookie.class);
    }
    return null;
}
 
Example 7
Project: incubator-netbeans   File: DefaultActionsProvider.java   Source Code and License Vote up 5 votes
@Override
public void doDelete(final Lookup lookup) {
    Collection<? extends Node> nodes = lookup.lookupAll(Node.class);
    FileObject[] fobs = new FileObject[nodes.size()];
    int i = 0;
    for (Node node:nodes) {
        DataObject dob = (DataObject) node.getCookie(DataObject.class);
        if (dob!=null) {
            fobs[i++] = dob.getPrimaryFile();
        }
    }
    UI.openRefactoringUI(new SafeDeleteUI(fobs));
}
 
Example 8
Project: incubator-netbeans   File: TreeViewDropSupport.java   Source Code and License Vote up 5 votes
private boolean canReorder(Node folder, Node[] dragNodes) {
    if ((folder == null) || (dragNodes.length == 0)) {
        return false;
    }

    // has folder a index cookie?
    Index ic = folder.getCookie(Index.class);

    if (ic == null) {
        return false;
    }

    // folder has index cookie
    // check if all dragNodes are from same folder
    for (int i = 0; i < dragNodes.length; i++) {
        // bugfix #23988, check if dragNodes[i] isn't null
        if (dragNodes[i] == null) {
            return false;
        }

        if (dragNodes[i].getParentNode() == null) {
            return false;
        }

        if (!dragNodes[i].getParentNode().equals(folder)) {
            return false;
        }
    }

    return true;
}
 
Example 9
Project: incubator-netbeans   File: Util.java   Source Code and License Vote up 5 votes
/** 
 * Create settings based on selected nodes. Finds all accepted data objects. 
 * Used by actions to populate wizard.
 * @param activatedNodes selected nodes 
 * @return map with accepted data objects as keys or empty map if no such
 * data object were found.
 */
public static Map<DataObject,SourceData> createWizardSourceMap(Node[] activatedNodes) {
    Map<DataObject,SourceData> sourceMap = createWizardSourceMap();
    
    if (activatedNodes != null && activatedNodes.length > 0) {
        final VisibilityQuery visQuery = VisibilityQuery.getDefault();
        for (Node node : activatedNodes) {
            DataObject dobj = node.getCookie(DataObject.class);
            if (dobj != null && !visQuery.isVisible(dobj.getPrimaryFile())) {
                continue;
            }

            DataObject.Container container = node.getCookie(DataObject.Container.class);
            if (container != null) {
                for (DataObject dataObj : I18nUtil.getAcceptedDataObjects(container)) {
                    addSource(sourceMap, dataObj);
                }
            }

            if (dobj == null) {
                continue;
            }

            if (FactoryRegistry.hasFactory(dobj.getClass())) {
                addSource(sourceMap, dobj);
            }
        }
    }
    
    return sourceMap;
}
 
Example 10
Project: incubator-netbeans   File: ReloadActionPerformer.java   Source Code and License Vote up 5 votes
public void perform(Node node) {
    if (node == null) return;
    EditorCookie editor = (EditorCookie) node.getCookie(EditorCookie.class);
    if (editor == null) return;
    Document doc = editor.getDocument();
    if (doc == null) return;
    GrammarManager cache = (GrammarManager)
        doc.getProperty(XMLCompletionQuery.DOCUMENT_GRAMMAR_BINDING_PROP);        
    if (cache == null) return;
    
    cache.invalidateGrammar();        
}
 
Example 11
Project: incubator-netbeans   File: CatalogNode.java   Source Code and License Vote up 5 votes
protected boolean enable(Node[] activatedNodes) {
    if (activatedNodes.length > 0) {
        for (int i = 0; i<activatedNodes.length; i++) {
            Node me = activatedNodes[i];
            Object self = me.getCookie(CatalogNode.class);
            if (self instanceof CatalogNode) {                        
                return ((CatalogNode)self).isRemovable();
            }
        }
    }
    return false;
}
 
Example 12
Project: incubator-netbeans   File: TextImporter.java   Source Code and License Vote up 5 votes
private FileObject findFolder( Lookup category ) {
    Node n = category.lookup( Node.class );
    if( null != n ) {
        DataFolder df = n.getCookie( DataFolder.class );
        if( null != df ) {
            return df.getPrimaryFile();
        }
    }
    return null;
}
 
Example 13
Project: incubator-netbeans   File: HibernateMappingRefactoringActionsProvider.java   Source Code and License Vote up 5 votes
private FileObject getTarget(Lookup look) {
    ExplorerContext drop = look.lookup(ExplorerContext.class);
    if (drop == null) {
        return null;
    }
    Node n = (Node) drop.getTargetNode();
    if (n == null) {
        return null;
    }
    DataObject dob = n.getCookie(DataObject.class);
    if (dob != null) {
        return dob.getPrimaryFile();
    }
    return null;
}
 
Example 14
Project: MaxSim   File: RemoveAction.java   Source Code and License Vote up 5 votes
@Override
protected void performAction(Node[] activatedNodes) {
    for (Node n : activatedNodes) {
        RemoveCookie removeCookie = n.getCookie(RemoveCookie.class);
        if (removeCookie != null) {
            removeCookie.remove();
        }
    }
}
 
Example 15
Project: incubator-netbeans   File: TemplateWizard1.java   Source Code and License Vote up 5 votes
/** Creates nodes for nodes.
 */
@Override
protected Node[] createNodes(Node key) {
    Node n = key;
    String nodeName = n.getDisplayName();
    
    DataObject obj = null;
    DataShadow shadow = n.getCookie(DataShadow.class);
    if (shadow != null) {
        // I need DataNode here to get localized name of the
        // shadow, but without the ugly "(->)" at the end
        DataNode dn = new DataNode(shadow, Children.LEAF);
        nodeName = dn.getDisplayName();
        obj = shadow.getOriginal();
        n = obj.getNodeDelegate();
    }
    
    if (obj == null)
        obj = n.getCookie(DataObject.class);
    
    if (obj != null) {
        if (obj.isTemplate ()) {
            // on normal nodes stop recursion
            return new Node[] { new DataShadowFilterNode (n, Children.LEAF, nodeName) };
        }
    
        if (acceptDataObject (obj)) {
            // on folders use normal filtering
            return new Node[] { new DataShadowFilterNode (n, new DataShadowFilterChildren (n), nodeName) };
        }
    }
    return new Node[] {};
}
 
Example 16
Project: incubator-netbeans   File: AnnotateAction.java   Source Code and License Vote up 5 votes
private EditorCookie activatedEditorCookie(Node[] nodes) {
    if (nodes == null) {
        nodes = WindowManager.getDefault().getRegistry().getActivatedNodes();
    }
    if (nodes.length == 1) {
        Node node = nodes[0];
        return (EditorCookie) node.getCookie(EditorCookie.class);
    }
    return null;
}
 
Example 17
Project: incubator-netbeans   File: OptionsAction.java   Source Code and License Vote up 5 votes
private HelpCtx getHelpId (Node node) {
    // it's template, return specific help id
    DataObject dataObj = (DataObject)node.getCookie (DataObject.class);
    if (dataObj != null) {
        Object o = dataObj.getPrimaryFile ().getAttribute ("helpID"); // NOI18N
        if (o != null) {
            return new HelpCtx (o.toString ());
        }
    }
    return null;
}
 
Example 18
Project: openjdk-jdk10   File: RemoveAction.java   Source Code and License Vote up 5 votes
@Override
protected void performAction(Node[] activatedNodes) {
    for (Node n : activatedNodes) {
        RemoveCookie removeCookie = n.getCookie(RemoveCookie.class);
        if (removeCookie != null) {
            removeCookie.remove();
        }
    }
}
 
Example 19
Project: incubator-netbeans   File: OutlineViewDropSupport.java   Source Code and License Vote up 4 votes
private void performReorder(final Node folder, Node[] dragNodes, int lNode, int uNode) {
    try {
        Index indexCookie = folder.getCookie (Index.class);
        log("performReorder indexCookie == " + indexCookie);

        if (indexCookie != null) {
            int[] perm = new int[indexCookie.getNodesCount()];
            int[] indexes = new int[dragNodes.length];
            int indexesLength = 0;

            for (int i = 0; i < dragNodes.length; i++) {
                int idx = indexCookie.indexOf(dragNodes[i]);

                if ((idx >= 0) && (idx < perm.length)) {
                    indexes[indexesLength++] = idx;
                }
            }

            // XXX: normally indexes of dragged nodes should be in ascending order, but
            // it seems that Tree.getSelectionPaths doesn't keep this order
            Arrays.sort(indexes);

            if ((lNode < 0) || (uNode >= perm.length) || (indexesLength == 0)) {
                return;
            }

            int k = 0;

            for (int i = 0; i < perm.length; i++) {
                if (i <= uNode) {
                    if (!containsNumber(indexes, indexesLength, i)) {
                        perm[i] = k++;
                    }

                    if (i == uNode) {
                        for (int j = 0; j < indexesLength; j++) {
                            if (indexes[j] <= uNode) {
                                perm[indexes[j]] = k++;
                            }
                        }
                    }
                } else {
                    if (i == lNode) {
                        for (int j = 0; j < indexesLength; j++) {
                            if (indexes[j] >= lNode) {
                                perm[indexes[j]] = k++;
                            }
                        }
                    }

                    if (!containsNumber(indexes, indexesLength, i)) {
                        perm[i] = k++;
                    }
                }
            }

            // check for identity permutation
            for (int i = 0; i < perm.length; i++) {
                if (perm[i] != i) {
                    indexCookie.reorder(perm);

                    break;
                }
            }
        }
    } catch (Exception e) {
        // Pending: add annotation or remove try/catch block
        ErrorManager.getDefault().notify(ErrorManager.INFORMATIONAL, e);
    }
}
 
Example 20
Project: incubator-netbeans   File: TemplateWizard1.java   Source Code and License Vote up 4 votes
public int getChildCount (Object o) {
    Node n = Visualizer.findNode(o);
    DataObject obj = n.getCookie(DataObject.class);

    return obj == null || obj.isTemplate () ? 0 : super.getChildCount (o);
}