Search in sources :

Example 1 with PolicyStrategy

use of org.knime.base.util.coordinate.PolicyStrategy in project knime-core by knime.

the class Axis method createPopupMenu.

private JPopupMenu createPopupMenu() {
    if (getCoordinate() == null) {
        return null;
    }
    m_currFormat = NORMAL;
    JPopupMenu popupMenu = new JPopupMenu();
    createNotationMenu(popupMenu);
    // policies
    if (m_coordinate != null) {
        List<PolicyStrategy> strategies = new LinkedList<PolicyStrategy>();
        Set<PolicyStrategy> policies = m_coordinate.getCompatiblePolicies();
        if (policies != null) {
            strategies.addAll(policies);
            Collections.sort(strategies, new Comparator<PolicyStrategy>() {

                /**
                 * {@inheritDoc}
                 */
                @Override
                public int compare(final PolicyStrategy o1, final PolicyStrategy o2) {
                    return o1.getDisplayName().compareTo(o2.getDisplayName());
                }
            });
        }
        JMenu tickPolicyMenu = new JMenu("Tick policies");
        ButtonGroup tickPolicyButtons = new ButtonGroup();
        if (strategies.size() > 0) {
            popupMenu.add(tickPolicyMenu);
            for (PolicyStrategy strategy : strategies) {
                final PolicyStrategy tempStrategy = strategy;
                JRadioButtonMenuItem tickPolicy = new JRadioButtonMenuItem(strategy.getDisplayName());
                tickPolicyButtons.add(tickPolicy);
                tickPolicyMenu.add(tickPolicy);
                if (strategy.equals(m_coordinate.getCurrentPolicy())) {
                    tickPolicy.setSelected(true);
                }
                tickPolicy.addItemListener(new ItemListener() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void itemStateChanged(final ItemEvent e) {
                        if (e.getStateChange() == ItemEvent.SELECTED) {
                            m_coordinate.setPolicy(tempStrategy);
                            // recreate popup menu
                            setComponentPopupMenu(createPopupMenu());
                            if (tempStrategy.isMappingAllowed()) {
                                m_mappingMethodMenu.setEnabled(true);
                                m_notationsMenu.setEnabled(true);
                            } else {
                                // hide mapping methods
                                m_mappingMethodMenu.setEnabled(false);
                                m_notationsMenu.setEnabled(false);
                                if (getCoordinate() != null) {
                                    getCoordinate().setActiveMappingMethod(null);
                                }
                            }
                            notifyChangeListeners();
                        }
                    }
                });
            }
        }
        // add strategies
        Set<MappingMethod> mappingMethods = m_coordinate.getCompatibleMappingMethods();
        m_mappingMethodMenu = new JMenu("Mapping Methods");
        if (mappingMethods != null && mappingMethods.size() > 0) {
            popupMenu.add(m_mappingMethodMenu);
            ButtonGroup buttons = new ButtonGroup();
            final Map<MappingMethod, JRadioButtonMenuItem> checkboxes = new HashMap<MappingMethod, JRadioButtonMenuItem>();
            JRadioButtonMenuItem none = new JRadioButtonMenuItem("none", true);
            buttons.add(none);
            none.addItemListener(new ItemListener() {

                @Override
                public void itemStateChanged(final ItemEvent e) {
                    if (e.getStateChange() == ItemEvent.SELECTED) {
                        m_coordinate.setActiveMappingMethod(null);
                        notifyChangeListeners();
                    }
                }
            });
            m_mappingMethodMenu.add(none);
            for (MappingMethod method : mappingMethods) {
                // final needed
                final MappingMethod tempMethod = method;
                JRadioButtonMenuItem checkbox = new JRadioButtonMenuItem(method.getDisplayName(), false);
                checkbox.setEnabled(method.isCompatibleWithDomain(getCoordinate().getDomain()));
                checkboxes.put(method, checkbox);
                buttons.add(checkbox);
                if (method.equals(m_coordinate.getActiveMappingMethod())) {
                    checkbox.setSelected(true);
                }
                checkbox.addItemListener(new ItemListener() {

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void itemStateChanged(final ItemEvent e) {
                        if (e.getStateChange() == ItemEvent.SELECTED) {
                            m_coordinate.setActiveMappingMethod(tempMethod);
                        }
                        notifyChangeListeners();
                    }
                });
                m_mappingMethodMenu.add(checkbox);
            }
            for (Map.Entry<MappingMethod, JRadioButtonMenuItem> entry : checkboxes.entrySet()) {
                if (entry.getKey().isCompatibleWithDomain(getCoordinate().getDomain())) {
                    entry.getValue().setEnabled(true);
                }
            }
        }
    }
    if (popupMenu.getComponentCount() < 1) {
        return null;
    }
    return popupMenu;
}
Also used : PolicyStrategy(org.knime.base.util.coordinate.PolicyStrategy) ItemEvent(java.awt.event.ItemEvent) HashMap(java.util.HashMap) MappingMethod(org.knime.base.util.coordinate.MappingMethod) JRadioButtonMenuItem(javax.swing.JRadioButtonMenuItem) JPopupMenu(javax.swing.JPopupMenu) LinkedList(java.util.LinkedList) ButtonGroup(javax.swing.ButtonGroup) ItemListener(java.awt.event.ItemListener) HashMap(java.util.HashMap) Map(java.util.Map) JMenu(javax.swing.JMenu)

Aggregations

ItemEvent (java.awt.event.ItemEvent)1 ItemListener (java.awt.event.ItemListener)1 HashMap (java.util.HashMap)1 LinkedList (java.util.LinkedList)1 Map (java.util.Map)1 ButtonGroup (javax.swing.ButtonGroup)1 JMenu (javax.swing.JMenu)1 JPopupMenu (javax.swing.JPopupMenu)1 JRadioButtonMenuItem (javax.swing.JRadioButtonMenuItem)1 MappingMethod (org.knime.base.util.coordinate.MappingMethod)1 PolicyStrategy (org.knime.base.util.coordinate.PolicyStrategy)1