Search in sources :

Example 1 with CyNodeViewContextMenuFactory

use of org.cytoscape.application.swing.CyNodeViewContextMenuFactory in project cytoscape-impl by cytoscape.

the class PopupMenuHelper method createNodeViewMenu.

/**
 * Creates a menu based on the NodeView.
 *
 * @param action Acceptable values are "NEW", "OPEN", or "EDGE". Case does not matter.
 */
void createNodeViewMenu(NodeView nview, int x, int y, String action) {
    if (nview != null) {
        Collection<NodeViewTaskFactory> usableTFs = getPreferredActions(graphView.nodeViewTFs, action);
        Collection<CyNodeViewContextMenuFactory> usableCMFs = getPreferredActions(graphView.cyNodeViewContextMenuFactory, action);
        View<CyNode> nv = (DNodeView) nview;
        // If the action is NEW, we should also include the Edge Actions
        if (action.equalsIgnoreCase("NEW")) {
            usableTFs.addAll(getPreferredActions(graphView.nodeViewTFs, "Edge"));
            usableCMFs.addAll(getPreferredActions(graphView.cyNodeViewContextMenuFactory, "Edge"));
        } else // remove TaskFactories that can't be executed from double-click menu
        if (action.equalsIgnoreCase("OPEN")) {
            Iterator<NodeViewTaskFactory> i = usableTFs.iterator();
            while (i.hasNext()) {
                if (!i.next().isReady(nv, graphView))
                    i.remove();
            }
        }
        int menuItemCount = usableTFs.size() + usableCMFs.size();
        int tfCount = usableTFs.size();
        if ((action.equalsIgnoreCase("OPEN") || action.equalsIgnoreCase("Edge")) && menuItemCount == 1 && tfCount == 1) {
            NodeViewTaskFactory tf = usableTFs.iterator().next();
            serviceRegistrar.getService(DialogTaskManager.class).execute(tf.createTaskIterator(nv, graphView));
        } else {
            String nodeLabel = graphView.getModel().getRow(nv.getModel()).get(CyNetwork.NAME, String.class);
            JPopupMenu menu = createMenu(nodeLabel);
            JMenuTracker tracker = new JMenuTracker(menu);
            if (!action.equalsIgnoreCase("OPEN")) {
                initializeNodeTracker(tracker);
                tracker.getGravityTracker(".").addMenuSeparator(-0.1);
                tracker.getGravityTracker(".").addMenuSeparator(999.99);
            }
            for (NodeViewTaskFactory nvtf : usableTFs) {
                Object context = null;
                NamedTaskFactory provisioner = factoryProvisioner.createFor(nvtf, nv, graphView);
                addMenuItem(nv, menu, provisioner, context, tracker, graphView.nodeViewTFs.get(nvtf));
            }
            for (CyNodeViewContextMenuFactory nodeCMF : usableCMFs) {
                // menu.add(nodeVMF.createMenuItem(m_view,  nv).getMenuItem());
                try {
                    CyMenuItem menuItem = nodeCMF.createMenuItem(graphView, nv);
                    addCyMenuItem(nv, menu, menuItem, tracker, graphView.cyNodeViewContextMenuFactory.get(nodeCMF));
                } catch (Throwable t) {
                    logger.error("Could not display context menu.", t);
                }
            }
            menu.show(invoker, x, y);
        }
    }
}
Also used : CyMenuItem(org.cytoscape.application.swing.CyMenuItem) JMenuTracker(org.cytoscape.util.swing.JMenuTracker) DialogTaskManager(org.cytoscape.work.swing.DialogTaskManager) Point(java.awt.Point) JPopupMenu(javax.swing.JPopupMenu) Iterator(java.util.Iterator) CyNode(org.cytoscape.model.CyNode) NodeViewTaskFactory(org.cytoscape.task.NodeViewTaskFactory) CyNodeViewContextMenuFactory(org.cytoscape.application.swing.CyNodeViewContextMenuFactory)

Aggregations

Point (java.awt.Point)1 Iterator (java.util.Iterator)1 JPopupMenu (javax.swing.JPopupMenu)1 CyMenuItem (org.cytoscape.application.swing.CyMenuItem)1 CyNodeViewContextMenuFactory (org.cytoscape.application.swing.CyNodeViewContextMenuFactory)1 CyNode (org.cytoscape.model.CyNode)1 NodeViewTaskFactory (org.cytoscape.task.NodeViewTaskFactory)1 JMenuTracker (org.cytoscape.util.swing.JMenuTracker)1 DialogTaskManager (org.cytoscape.work.swing.DialogTaskManager)1