All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.marid.swing.actions.MaridActions Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2014 Dmitry Ovchinnikov
 * Marid, the free data acquisition and visualization software
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see .
 */

package org.marid.swing.actions;

import org.marid.l10n.L10nSupport;
import org.marid.swing.menu.*;
import org.marid.swing.menu.MenuContainer;

import javax.swing.*;
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.stream.Collectors;

import static java.util.stream.IntStream.range;

/**
 * @author Dmitry Ovchinnikov
 */
public class MaridActions implements L10nSupport {

    public static final String TOOLBAR_ENABLED = "toolbarEnabled";
    public static final String MENUBAR_DISABLED = "menubarDisabled";

    private static List> actions(ActionMap actionMap) {
        final Object[] keys = actionMap.allKeys();
        return Arrays.stream(keys == null ? new Object[0] : keys)
                .filter(k -> k instanceof ActionKey)
                .map(ActionKey.class::cast)
                .filter(k -> k.size() >= 4)
                .sorted()
                .map(k -> new AbstractMap.SimpleImmutableEntry<>(k, actionMap.get(k)))
                .filter(e -> e.getValue() != null && !Boolean.TRUE.equals(e.getValue().getValue(MENUBAR_DISABLED)))
                .collect(Collectors.toList());
    }

    public static void fillMenu(ActionMap actionMap, JMenuBar menuBar) {
        final List> actions = actions(actionMap);
        for (final ListIterator> it = actions.listIterator(); it.hasNext(); ) {
            if (it.hasPrevious() && it.hasNext()) {
                final Map.Entry ne = actions.get(it.nextIndex());
                final Map.Entry pe = actions.get(it.previousIndex());
                final ActionKey nk = ne.getKey(), pk = pe.getKey();
                final JMenu menu = getOrCreateMenu(new SwingMenuBarWrapper(menuBar), nk.getPath());
                if (Arrays.equals(pk.getPath(), nk.getPath()) && !pk.getGroup().equals(nk.getGroup())) {
                    menu.addSeparator();
                }
            }
            final Map.Entry e = it.next();
            final Action a = e.getValue();
            final ActionKey k = e.getKey();
            final String[] path = k.getPath();
            final JMenu menu = getOrCreateMenu(new SwingMenuBarWrapper(menuBar), path);
            if (a.getValue(Action.SELECTED_KEY) != null) {
                final JCheckBoxMenuItem menuItem = new JCheckBoxMenuItem(a);
                menuItem.setName(k.getLastName());
                menu.add(menuItem);
            } else {
                final JMenuItem menuItem = menu.add(a);
                menuItem.setName(k.getLastName());
            }
        }
    }

    public static void fillMenu(ActionMap actionMap, PopupMenu popupMenu) {
        final List> actions = actions(actionMap);
        for (final ListIterator> it = actions.listIterator(); it.hasNext(); ) {
            if (it.hasPrevious() && it.hasNext()) {
                final Map.Entry ne = actions.get(it.nextIndex());
                final Map.Entry pe = actions.get(it.previousIndex());
                final ActionKey nk = ne.getKey(), pk = pe.getKey();
                final Menu menu = getOrCreateMenu(new MenuContainer(popupMenu), nk.getPath());
                if (Arrays.equals(pk.getPath(), nk.getPath()) && !pk.getGroup().equals(nk.getGroup())) {
                    menu.addSeparator();
                }
            }
            final Map.Entry e = it.next();
            final Action a = e.getValue();
            final ActionKey k = e.getKey();
            final String[] path = k.getPath();
            final Menu menu = getOrCreateMenu(new MenuContainer(popupMenu), path);
            if (a.getValue(Action.SELECTED_KEY) != null) {
                final CheckboxMenuItem menuItem = new CheckboxMenuItem((String) a.getValue(Action.NAME));
                menuItem.setName(k.getLastName());
                menu.add(menuItem);
            } else {
                final MenuItem menuItem = new MenuItem((String) a.getValue(Action.NAME));
                menuItem.setName(k.getLastName());
                menu.add(menuItem);
            }
        }
    }

    public static void fillToolbar(ActionMap actionMap, JToolBar toolBar) {
        final List> actions = Arrays.stream(actionMap.allKeys())
                .filter(k -> k instanceof ActionKey)
                .map(ActionKey.class::cast)
                .sorted()
                .map(k -> new AbstractMap.SimpleImmutableEntry<>(k, actionMap.get(k)))
                .filter(e -> e.getValue() != null && Boolean.TRUE.equals(e.getValue().getValue(TOOLBAR_ENABLED)))
                .collect(Collectors.toList());
        for (final ListIterator> it = actions.listIterator(); it.hasNext(); ) {
            if (it.hasPrevious() && it.hasNext()) {
                final Map.Entry ne = actions.get(it.nextIndex());
                final Map.Entry pe = actions.get(it.previousIndex());
                if (!pe.getKey().getGroup().equals(ne.getKey().getGroup())) {
                    toolBar.addSeparator();
                }
            }
            final Map.Entry e = it.next();
            final Action a = e.getValue();
            final ActionKey k = e.getKey();
            if (a.getValue(Action.SELECTED_KEY) != null) {
                final JToggleButton toggleButton = new JToggleButton(a);
                toggleButton.setName(k.getLastName());
                toggleButton.setFocusable(false);
                toggleButton.setHideActionText(true);
                toolBar.add(toggleButton);
            } else {
                final JButton button = toolBar.add(a);
                button.setName(k.getLastName());
                button.setFocusable(false);
            }
        }
    }

    public static void fillInputMap(InputMap inputMap, ActionMap actionMap) {
        for (final Object key : actionMap.allKeys()) {
            final Action a = actionMap.get(key);
            final Object keyStrokeObject = a.getValue(Action.ACCELERATOR_KEY);
            if (keyStrokeObject instanceof KeyStroke) {
                final KeyStroke keyStroke = (KeyStroke) keyStrokeObject;
                inputMap.put(keyStroke, key);
            }
        }
    }

    private static JMenu getOrCreateMenu(SwingMenuContainer wrapper, String[] path) {
        final JMenu topMenu = range(0, wrapper.getMenuCount()).mapToObj(wrapper::getMenu)
                .filter(i -> i != null && path[0].equals(i.getName())).findFirst().orElseGet(() -> {
                    final JMenu m = new JMenu(LS.s(path[0]));
                    m.setName(path[0]);
                    wrapper.add(m);
                    return m;
                });
        final int n = path.length;
        return n == 1 ? topMenu : getOrCreateMenu(new SwingMenuWrapper(topMenu), Arrays.copyOf(path, n - 1));
    }

    private static Menu getOrCreateMenu(AwtMenuContainer wrapper, String[] path) {
        final Menu topMenu = range(0, wrapper.getMenuCount()).mapToObj(wrapper::getMenu)
                .filter(i -> i != null && path[0].equals(i.getName())).findFirst().orElseGet(() -> {
                    final Menu m = new Menu(LS.s(path[0]));
                    m.setName(path[0]);
                    wrapper.add(m);
                    return m;
                });
        final int n = path.length;
        return n == 1 ? topMenu : getOrCreateMenu(new MenuContainer(topMenu), Arrays.copyOf(path, n - 1));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy