Search in sources :

Example 1 with AbstractCommandButton

use of org.pushingpixels.flamingo.api.common.AbstractCommandButton in project freeplane by freeplane.

the class RibbonBandContributorFactory method getContributor.

public ARibbonContributor getContributor(final Properties attributes) {
    return new ARibbonContributor() {

        private JRibbonBand band;

        private boolean valid = false;

        public String getKey() {
            return attributes.getProperty("name");
        }

        public void contribute(RibbonBuildContext context, ARibbonContributor parent) {
            if (parent == null) {
                return;
            }
            band = new JRibbonBand(TextUtils.removeTranslateComment(TextUtils.getText("ribbon.band." + attributes.getProperty("name"))), null);
            // read policies and sub-contributions
            context.processChildren(context.getCurrentPath(), this);
            setResizePolicies(attributes.getProperty("resize_policies"));
            band.setFocusable(false);
            if (valid) {
                parent.addChild(band, new ChildProperties(parseOrderSettings(attributes.getProperty("orderPriority", ""))));
            }
        }

        public void addChild(Object child, ChildProperties properties) {
            if (child instanceof AbstractCommandButton) {
                RibbonElementPriority priority = properties.get(RibbonElementPriority.class);
                if (priority == null) {
                    priority = RibbonElementPriority.TOP;
                }
                band.addCommandButton((AbstractCommandButton) child, priority);
                valid = true;
            }
        }

        private void setResizePolicies(String policiesString) {
            if (policiesString != null) {
                String[] tokens = policiesString.split(",");
                List<RibbonBandResizePolicy> policyList = new ArrayList<RibbonBandResizePolicy>();
                for (String policyStr : tokens) {
                    if ("none".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.None(band.getControlPanel()));
                    } else if ("mirror".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.Mirror(band.getControlPanel()));
                    } else if ("high2low".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.High2Low(band.getControlPanel()));
                    } else if ("high2mid".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.High2Mid(band.getControlPanel()));
                    } else if ("mid2low".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.Mid2Low(band.getControlPanel()));
                    } else if ("mid2mid".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.Mid2Mid(band.getControlPanel()));
                    } else if ("low2mid".equals(policyStr.toLowerCase().trim())) {
                        policyList.add(new CoreRibbonResizePolicies.Low2Mid(band.getControlPanel()));
                    }
                }
                policyList.add(new IconRibbonBandResizePolicy(band.getControlPanel()));
                band.setResizePolicies(policyList);
                try {
                    FlamingoUtilities.checkResizePoliciesConsistency(band);
                } catch (Exception ignore) {
                    reorganizePolicies(band, true);
                }
            }
        }
    };
}
Also used : CoreRibbonResizePolicies(org.pushingpixels.flamingo.api.ribbon.resize.CoreRibbonResizePolicies) ArrayList(java.util.ArrayList) IconRibbonBandResizePolicy(org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy) RibbonBandResizePolicy(org.pushingpixels.flamingo.api.ribbon.resize.RibbonBandResizePolicy) IconRibbonBandResizePolicy(org.pushingpixels.flamingo.api.ribbon.resize.IconRibbonBandResizePolicy) RibbonElementPriority(org.pushingpixels.flamingo.api.ribbon.RibbonElementPriority) JRibbonBand(org.pushingpixels.flamingo.api.ribbon.JRibbonBand) AbstractCommandButton(org.pushingpixels.flamingo.api.common.AbstractCommandButton)

Example 2 with AbstractCommandButton

use of org.pushingpixels.flamingo.api.common.AbstractCommandButton in project freeplane by freeplane.

the class RibbonActionContributorFactory method getContributor.

/**
 *********************************************************************************
 * REQUIRED METHODS FOR INTERFACES
 *********************************************************************************
 */
public ARibbonContributor getContributor(final Properties attributes) {
    final String actionKey = attributes.getProperty("action");
    if (actionKey != null) {
        String accel = attributes.getProperty("accelerator", null);
        if (accel != null) {
            if (Compat.isMacOsX()) {
                accel = accel.replaceFirst("CONTROL", "META").replaceFirst("control", "meta");
            }
            builder.getAcceleratorManager().setDefaultAccelerator(actionKey, accel);
        }
    }
    return new ARibbonContributor() {

        private List<Component> childButtons = new ArrayList<Component>();

        public String getKey() {
            String key = attributes.getProperty("action");
            if (key == null) {
                key = attributes.getProperty("name");
            }
            return key;
        }

        public void contribute(RibbonBuildContext context, ARibbonContributor parent) {
            final String actionKey = attributes.getProperty("action");
            final boolean mandatory = Boolean.parseBoolean(attributes.getProperty("mandatory", "false").toLowerCase());
            ChildProperties childProps = new ChildProperties(parseOrderSettings(attributes.getProperty("orderPriority", "")));
            childProps.set(RibbonElementPriority.class, getPriority(attributes.getProperty("priority", "medium")));
            if (actionKey != null) {
                AFreeplaneAction action = context.getBuilder().getMode().getAction(actionKey);
                if (action != null) {
                    if (mandatory) {
                        action.putValue(MANDATORY_PROPERTY, mandatory);
                    }
                    AbstractCommandButton button;
                    if (isSelectionListener(action)) {
                        button = createCommandToggleButton(action);
                        if (context.hasChildren(context.getCurrentPath())) {
                            LogUtils.severe("RibbonActionContributorFactory.getContributor(): can't add popup menu to toggle button for action: " + context.getCurrentPath().toString());
                        }
                    } else {
                        button = createCommandButton(action);
                        if (context.hasChildren(context.getCurrentPath())) {
                            StructurePath path = context.getCurrentPath();
                            ((JCommandButton) button).setPopupCallback(getPopupPanelCallBack(path, context));
                            ((JCommandButton) button).setCommandButtonKind(CommandButtonKind.ACTION_AND_POPUP_MAIN_ACTION);
                            KeyStroke ks = context.getBuilder().getAcceleratorManager().getAccelerator(actionKey);
                            updateRichTooltip(button, action, ks);
                            updateActionState(action, button);
                        }
                    }
                    button.putClientProperty(ACTION_KEY_PROPERTY, action);
                    KeyStroke ks = context.getBuilder().getAcceleratorManager().getAccelerator(actionKey);
                    if (ks != null) {
                        button.putClientProperty(ACTION_ACCELERATOR, ks);
                        updateRichTooltip(button, action, ks);
                    }
                    getAccelChangeListener().addAction(actionKey, button);
                    builder.getMapChangeAdapter().addListener(new ActionChangeListener(action, button));
                    parent.addChild(button, childProps);
                }
            } else {
                final String name = attributes.getProperty("name");
                if (name != null) {
                    AFreeplaneAction action = ActionUtils.getDummyAction(name);
                    final JCommandButton button = new JCommandButton(ActionUtils.getActionTitle(action), ActionUtils.getActionIcon(action));
                    button.putClientProperty(ACTION_NAME_PROPERTY, action);
                    updateRichTooltip(button, action, null);
                    if (context.hasChildren(context.getCurrentPath())) {
                        StructurePath path = context.getCurrentPath();
                        button.setPopupCallback(getPopupPanelCallBack(path, context));
                        button.setCommandButtonKind(CommandButtonKind.POPUP_ONLY);
                    }
                    button.setFocusable(false);
                    parent.addChild(button, childProps);
                }
            }
        }

        private PopupPanelCallback getPopupPanelCallBack(StructurePath path, final RibbonBuildContext context) {
            childButtons.clear();
            context.processChildren(path, this);
            return new PopupPanelCallback() {

                public JPopupPanel getPopupPanel(JCommandButton commandButton) {
                    JCommandPopupMenu popupmenu = new JCommandPopupMenu();
                    for (Component comp : childButtons) {
                        if (comp instanceof JSeparator) {
                            popupmenu.addMenuSeparator();
                        } else if (comp instanceof AbstractCommandButton) {
                            AbstractCommandButton button = (AbstractCommandButton) comp;
                            AbstractCommandButton menuButton = null;
                            AFreeplaneAction action = (AFreeplaneAction) button.getClientProperty(ACTION_KEY_PROPERTY);
                            if (action != null) {
                                if (isSelectionListener(action)) {
                                    menuButton = createCommandToggleMenuButton(action);
                                    popupmenu.addMenuButton((JCommandToggleMenuButton) menuButton);
                                } else {
                                    menuButton = createCommandMenuButton(action);
                                    popupmenu.addMenuButton((JCommandMenuButton) menuButton);
                                }
                                menuButton.setEnabled(button.isEnabled());
                                menuButton.putClientProperty(ACTION_KEY_PROPERTY, action);
                                KeyStroke ks = context.getBuilder().getAcceleratorManager().getAccelerator(action.getKey());
                                updateRichTooltip(menuButton, action, ks);
                                updateActionState(action, menuButton);
                            } else {
                                action = (AFreeplaneAction) button.getClientProperty(ACTION_NAME_PROPERTY);
                                menuButton = createCommandMenuButton(action);
                                if (action != null) {
                                    menuButton.putClientProperty(ACTION_NAME_PROPERTY, action);
                                    updateRichTooltip(menuButton, action, null);
                                }
                            }
                            if (button instanceof JCommandButton) {
                                if (((JCommandButton) button).getPopupCallback() != null) {
                                    ((JCommandMenuButton) menuButton).setPopupCallback(((JCommandButton) button).getPopupCallback());
                                    ((JCommandMenuButton) menuButton).setCommandButtonKind(((JCommandButton) button).getCommandButtonKind());
                                }
                            }
                            // clear all RibbonActionListeners from the menuButton
                            for (ActionListener listener : menuButton.getListeners(ActionListener.class)) {
                                if (listener instanceof RibbonActionListener) {
                                    menuButton.removeActionListener(listener);
                                }
                            }
                            // add
                            for (ActionListener listener : button.getListeners(ActionListener.class)) {
                                if (listener instanceof RibbonActionListener) {
                                    menuButton.addActionListener(listener);
                                }
                            }
                        }
                    }
                    return popupmenu;
                }
            };
        }

        public void addChild(Object child, ChildProperties properties) {
            if (child instanceof AbstractCommandButton) {
                childButtons.add((AbstractCommandButton) child);
                Object obj = ((AbstractCommandButton) child).getClientProperty(ACTION_KEY_PROPERTY);
                if (obj != null) {
                    try {
                        builder.getMapChangeAdapter().removeListener((IChangeObserver) ((AFreeplaneAction) obj).getValue(ACTION_CHANGE_LISTENER));
                        getAccelChangeListener().removeAction(((AFreeplaneAction) obj).getKey());
                    } catch (Exception e) {
                        LogUtils.info("RibbonActionContributorFactory.getContributor(...).new ARibbonContributor() {...}.addChild(): " + e.getMessage());
                    }
                }
            }
            if (child instanceof RibbonSeparator) {
                childButtons.add(new JSeparator(JSeparator.HORIZONTAL));
            }
        }
    };
}
Also used : StructurePath(org.freeplane.core.ui.ribbon.StructureTree.StructurePath) JCommandToggleMenuButton(org.pushingpixels.flamingo.api.common.JCommandToggleMenuButton) PopupPanelCallback(org.pushingpixels.flamingo.api.common.popup.PopupPanelCallback) JCommandButton(org.pushingpixels.flamingo.api.common.JCommandButton) JSeparator(javax.swing.JSeparator) JCommandMenuButton(org.pushingpixels.flamingo.api.common.JCommandMenuButton) AFreeplaneAction(org.freeplane.core.ui.AFreeplaneAction) RibbonSeparator(org.freeplane.core.ui.ribbon.RibbonSeparatorContributorFactory.RibbonSeparator) ActionListener(java.awt.event.ActionListener) AbstractCommandButton(org.pushingpixels.flamingo.api.common.AbstractCommandButton) JCommandPopupMenu(org.pushingpixels.flamingo.api.common.popup.JCommandPopupMenu) KeyStroke(javax.swing.KeyStroke) ArrayList(java.util.ArrayList) List(java.util.List) Component(java.awt.Component)

Example 3 with AbstractCommandButton

use of org.pushingpixels.flamingo.api.common.AbstractCommandButton in project freeplane by freeplane.

the class RibbonMenuSecondaryGroupContributorFactory method getContributor.

/**
 *********************************************************************************
 * CONSTRUCTORS
 *********************************************************************************
 */
/**
 *********************************************************************************
 * METHODS
 *********************************************************************************
 */
/**
 *********************************************************************************
 * REQUIRED METHODS FOR INTERFACES
 *********************************************************************************
 */
public ARibbonContributor getContributor(final Properties attributes) {
    return new ARibbonContributor() {

        SecondaryEntryGroup group;

        public String getKey() {
            return attributes.getProperty("name", null);
        }

        public void contribute(RibbonBuildContext context, ARibbonContributor parent) {
            group = new SecondaryEntryGroup(TextUtils.removeTranslateComment(TextUtils.getRawText("ribbon.menu.group." + getKey())));
            context.processChildren(context.getCurrentPath(), this);
            parent.addChild(group, new ChildProperties(parseOrderSettings(attributes.getProperty("orderPriority", ""))));
        }

        public void addChild(Object child, ChildProperties properties) {
            if (child instanceof RibbonApplicationMenuEntrySecondary) {
                group.addEntry((RibbonApplicationMenuEntrySecondary) child);
            } else if (child instanceof AbstractCommandButton) {
                group.addEntry(wrapButton((AbstractCommandButton) child));
            }
        }

        private RibbonApplicationMenuEntrySecondary wrapButton(AbstractCommandButton button) {
            ActionListener listener = null;
            PopupPanelCallback callback = null;
            CommandButtonKind kind = CommandButtonKind.ACTION_ONLY;
            if (button instanceof JCommandButton) {
                if (((JCommandButton) button).getPopupCallback() != null) {
                    kind = (((JCommandButton) button).getCommandButtonKind());
                    callback = ((JCommandButton) button).getPopupCallback();
                }
            }
            for (ActionListener l : button.getListeners(ActionListener.class)) {
                if (l instanceof RibbonActionListener) {
                    listener = l;
                    break;
                }
            }
            RibbonApplicationMenuEntrySecondary entry = new RibbonApplicationMenuEntrySecondary(button.getIcon(), button.getText(), listener, kind);
            if (callback != null) {
                entry.setPopupCallback(callback);
            }
            KeyStroke ks = (KeyStroke) button.getClientProperty(RibbonActionContributorFactory.ACTION_ACCELERATOR);
            if (ks != null) {
                AFreeplaneAction action = (AFreeplaneAction) button.getClientProperty(RibbonActionContributorFactory.ACTION_KEY_PROPERTY);
                if (action != null) {
                    RichTooltip tip = RibbonActionContributorFactory.getRichTooltip(action, ks);
                    if (tip != null) {
                        entry.setActionRichTooltip(tip);
                    }
                }
            }
            return entry;
        }
    };
}
Also used : PopupPanelCallback(org.pushingpixels.flamingo.api.common.popup.PopupPanelCallback) JCommandButton(org.pushingpixels.flamingo.api.common.JCommandButton) RichTooltip(org.pushingpixels.flamingo.api.common.RichTooltip) AFreeplaneAction(org.freeplane.core.ui.AFreeplaneAction) ActionListener(java.awt.event.ActionListener) RibbonActionListener(org.freeplane.core.ui.ribbon.RibbonActionContributorFactory.RibbonActionListener) AbstractCommandButton(org.pushingpixels.flamingo.api.common.AbstractCommandButton) KeyStroke(javax.swing.KeyStroke) SecondaryEntryGroup(org.freeplane.core.ui.ribbon.RibbonMenuPrimaryContributorFactory.SecondaryEntryGroup) CommandButtonKind(org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonKind) RibbonActionListener(org.freeplane.core.ui.ribbon.RibbonActionContributorFactory.RibbonActionListener) RibbonApplicationMenuEntrySecondary(org.pushingpixels.flamingo.api.ribbon.RibbonApplicationMenuEntrySecondary)

Aggregations

AbstractCommandButton (org.pushingpixels.flamingo.api.common.AbstractCommandButton)3 ActionListener (java.awt.event.ActionListener)2 ArrayList (java.util.ArrayList)2 KeyStroke (javax.swing.KeyStroke)2 AFreeplaneAction (org.freeplane.core.ui.AFreeplaneAction)2 JCommandButton (org.pushingpixels.flamingo.api.common.JCommandButton)2 PopupPanelCallback (org.pushingpixels.flamingo.api.common.popup.PopupPanelCallback)2 Component (java.awt.Component)1 List (java.util.List)1 JSeparator (javax.swing.JSeparator)1 RibbonActionListener (org.freeplane.core.ui.ribbon.RibbonActionContributorFactory.RibbonActionListener)1 SecondaryEntryGroup (org.freeplane.core.ui.ribbon.RibbonMenuPrimaryContributorFactory.SecondaryEntryGroup)1 RibbonSeparator (org.freeplane.core.ui.ribbon.RibbonSeparatorContributorFactory.RibbonSeparator)1 StructurePath (org.freeplane.core.ui.ribbon.StructureTree.StructurePath)1 CommandButtonKind (org.pushingpixels.flamingo.api.common.JCommandButton.CommandButtonKind)1 JCommandMenuButton (org.pushingpixels.flamingo.api.common.JCommandMenuButton)1 JCommandToggleMenuButton (org.pushingpixels.flamingo.api.common.JCommandToggleMenuButton)1 RichTooltip (org.pushingpixels.flamingo.api.common.RichTooltip)1 JCommandPopupMenu (org.pushingpixels.flamingo.api.common.popup.JCommandPopupMenu)1 JRibbonBand (org.pushingpixels.flamingo.api.ribbon.JRibbonBand)1