001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.gui.dialogs.relation.actions;
003
004import java.util.Comparator;
005import java.util.List;
006
007import javax.swing.JButton;
008import javax.swing.JToolBar;
009
010import org.openstreetmap.josm.actions.ExpertToggleAction;
011
012/**
013 * An action group for the relation editor, to be used in one of the tool bars.
014 *
015 * @author Michael Zangl
016 * @since 14027
017 */
018public interface IRelationEditorActionGroup {
019
020    /**
021     * Get the position at which the action group should be added.
022     *
023     * @return The order index, default is to add at the end.
024     */
025    default int order() {
026        return 100;
027    }
028
029    /**
030     * Get the actions in this action group.
031     *
032     * @param editorAccess
033     *            Methods to access the relation editor.
034     * @return The actions
035     */
036    List<AbstractRelationEditorAction> getActions(IRelationEditorActionAccess editorAccess);
037
038    /**
039     * Fills the toolbar with some action groups.
040     * <p>
041     * Groups are sorted by their ordered index and expert buttons are hidden in non-expert mode.
042     * @param toolbar The toolbar to add the buttons to.
043     * @param groups An unordered list of action groups.
044     * @param editorAccess The relation editor
045     */
046    static void fillToolbar(JToolBar toolbar, List<IRelationEditorActionGroup> groups,
047            IRelationEditorActionAccess editorAccess) {
048        groups.stream().sorted(Comparator.comparingInt(IRelationEditorActionGroup::order)).forEach(group -> {
049            if (toolbar.getComponentCount() > 0) {
050                toolbar.addSeparator();
051            }
052
053            for (AbstractRelationEditorAction action : group.getActions(editorAccess)) {
054                JButton button = toolbar.add(action);
055                if (action.isExpertOnly()) {
056                    ExpertToggleAction.addVisibilitySwitcher(button);
057                }
058            }
059        });
060    }
061}