Search in sources :

Example 1 with SimplePanel

use of org.cytoscape.work.internal.tunables.utils.SimplePanel in project cytoscape-impl by cytoscape.

the class JPanelTunableMutator method buildConfiguration.

/**
 * A special case of buildConfiguration that allows a parent window to be specified.
 * This special case is when a task only has a single tunable of type File, in
 * which case we don't want to show the normal tunable dialog, just the file dialog.
 */
JPanel buildConfiguration(final Object objectWithTunables, Window possibleParent) {
    // # of descendents of TaskFactory...
    int factoryCount = 0;
    // ...everything else.  (Presumeably descendents of Task.)
    int otherCount = 0;
    if (objectWithTunables instanceof TaskFactory)
        ++factoryCount;
    else
        ++otherCount;
    synchronized (lock) {
        handlers = getApplicableHandlers(objectWithTunables, "gui");
        // Sanity check:
        if (factoryCount > 0) {
            if (factoryCount != 1) {
                logger.error("More than one annotated TaskFactory found.");
                return null;
            } else if (otherCount != 0) {
                logger.error("Found annotated Task objects in addition to an annotated TaskFactory.");
                return null;
            }
        }
        if (handlers.isEmpty()) {
            if (tunablePanel != null) {
                tunablePanel.removeAll();
                return tunablePanel;
            }
            return null;
        }
        // and all of the extra clicks, instead we just want to show the special dialog.
        if (handlers.size() == 1 && handlers.get(0) instanceof DirectlyPresentableTunableHandler) {
            DirectlyPresentableTunableHandler fh = (DirectlyPresentableTunableHandler) handlers.get(0);
            if (fh.isForcedToSetDirectly()) {
                boolean fileFound = fh.setTunableDirectly(possibleParent);
                return fileFound ? null : HANDLER_CANCEL_PANEL;
            }
        }
        if (!panelMap.containsKey(handlers)) {
            Map<String, JPanel> panels = new HashMap<>();
            final JPanel topLevel = new SimplePanel(true);
            panels.put(TOP_GROUP, topLevel);
            // construct the GUI
            for (GUITunableHandler gh : handlers) {
                // hook up dependency listeners
                String dep = gh.getDependency();
                if (dep != null && !dep.equals("")) {
                    for (GUITunableHandler gh2 : handlers) {
                        if (gh2.getName().equals(dep)) {
                            gh2.addDependent(gh);
                            break;
                        }
                    }
                }
                // hook up change listeners
                for (String cs : gh.getChangeSources()) {
                    if (cs != null && !cs.equals("")) {
                        for (GUITunableHandler gh2 : handlers) {
                            if (gh2.getName().equals(cs)) {
                                gh2.addChangeListener(gh);
                                break;
                            }
                        }
                    }
                }
                // Get information about the Groups and alignment from Tunables Annotations
                // in order to create the proper GUI
                final Map<String, Boolean> groupToVerticalMap = processGroupParams(gh, "alignments", "vertical");
                final Map<String, Boolean> groupToDisplayedMap = processGroupParams(gh, "groupTitles", "displayed");
                // find the proper group to put the handler panel in given the Alignment/Group parameters
                String lastGroup = TOP_GROUP;
                String groupNames = "";
                for (String g : gh.getGroups()) {
                    if (g == null || g.equals(""))
                        throw new IllegalArgumentException("A group's name must not be \"\".");
                    groupNames = groupNames + g;
                    if (!panels.containsKey(groupNames)) {
                        boolean displayed = groupToDisplayedMap.get(g);
                        boolean vertical = groupToVerticalMap.get(g);
                        // #2935
                        if (g.startsWith("_"))
                            displayed = false;
                        panels.put(groupNames, createJPanel(g, gh, vertical, displayed));
                        final JPanel pnl = panels.get(groupNames);
                        panels.get(lastGroup).add(pnl, gh.getChildKey());
                    }
                    lastGroup = groupNames;
                }
                panels.get(lastGroup).add(gh.getJPanel());
            }
            panelMap.put(handlers, panels.get(TOP_GROUP));
        }
        updateTunableFieldPanelMargins();
        // Get the GUI into the proper state
        for (GUITunableHandler gh : handlers) gh.notifyDependents();
        // if no tunablePane is defined, then create a new JDialog to display the Tunables' panels
        if (tunablePanel == null) {
            return panelMap.get(handlers);
        } else {
            // add them to the "tunablePanel" JPanel
            tunablePanel.removeAll();
            tunablePanel.add(panelMap.get(handlers));
            final JPanel retVal = tunablePanel;
            tunablePanel = null;
            return retVal;
        }
    }
}
Also used : JPanel(javax.swing.JPanel) HashMap(java.util.HashMap) WeakHashMap(java.util.WeakHashMap) AbstractGUITunableHandler(org.cytoscape.work.swing.AbstractGUITunableHandler) GUITunableHandler(org.cytoscape.work.swing.GUITunableHandler) SimplePanel(org.cytoscape.work.internal.tunables.utils.SimplePanel) DirectlyPresentableTunableHandler(org.cytoscape.work.swing.DirectlyPresentableTunableHandler) TaskFactory(org.cytoscape.work.TaskFactory)

Aggregations

HashMap (java.util.HashMap)1 WeakHashMap (java.util.WeakHashMap)1 JPanel (javax.swing.JPanel)1 TaskFactory (org.cytoscape.work.TaskFactory)1 SimplePanel (org.cytoscape.work.internal.tunables.utils.SimplePanel)1 AbstractGUITunableHandler (org.cytoscape.work.swing.AbstractGUITunableHandler)1 DirectlyPresentableTunableHandler (org.cytoscape.work.swing.DirectlyPresentableTunableHandler)1 GUITunableHandler (org.cytoscape.work.swing.GUITunableHandler)1