Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.awt.Component;

import java.util.Arrays;

import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.Action;
import javax.swing.ActionMap;

import javax.swing.JButton;
import javax.swing.JCheckBoxMenuItem;

import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;

import javax.swing.JToggleButton;
import javax.swing.JToolBar;

public class Main {
    /**
     * Populates a {@link JMenuBar} from the given list and {@link ActionMap}.
     * 
     * @param menubar
     * @param actionMap
     * @param list
     * @return
     */
    public static void populate(JMenuBar menubar, ActionMap actionMap, List<?> list) {
        JMenu menu = null;

        for (Object element : list) {
            if (element == null) {
                if (menu != null) {
                    menu.addSeparator();
                }
            } else if (element instanceof List<?>) {
                menu = createMenu(actionMap, (List<?>) element);
                if (menu != null) {
                    menubar.add(menu);
                }
            } else if (element.getClass().isArray()) {
                menu = createMenu(actionMap, (Object[]) element);
                if (menu != null) {
                    menubar.add(menu);
                }
            } else {
                if (menu != null) {
                    menu.add(actionMap.get(element));
                }
            }
        }
    }

    /**
     * Populates a {@link JToolBar} with the items and {@link ActionMap}.
     * 
     * @param toolBar
     * @param actionMap
     * @param items
     */
    public static void populate(JToolBar toolBar, ActionMap actionMap, Object... items) {
        populate(toolBar, actionMap, Arrays.asList(items));
    }

    /**
     * Populates a {@link JToolBar} with the list items and {@link ActionMap}.
     * 
     * @param toolBar
     * @param actionMap
     * @param items
     */
    public static void populate(JToolBar toolBar, ActionMap actionMap, List<?> items) {
        toolBar.setOpaque(false);
        for (Object item : items) {
            if (item instanceof Component) {
                toolBar.add((Component) item);
            } else if (item == null) {
                toolBar.addSeparator();
            } else {
                final Action action = item instanceof Action ? (Action) item : actionMap.get(item);
                final AbstractButton button = createToolbarItem(action);
                toolBar.add(button);
            }
        }
    }

    /**
     * Creates a {@link JMenu} from the given list and action map.
     * 
     * @param actionMap
     * @param list
     * @return
     */
    public static JMenu createMenu(ActionMap actionMap, List<?> list) {
        // The first item will be the action for the JMenu
        final Action titleAction = actionMap.get(list.get(0));
        if (titleAction == null) {
            return null;
        }
        final JMenu menu = new JMenu();
        menu.setAction(titleAction);

        // The rest of the items represent the menu items.
        for (Object element : list.subList(1, list.size())) {
            if (element == null) {
                menu.addSeparator();
            } else if (element instanceof List<?>) {
                JMenu newMenu = createMenu(actionMap, (List<?>) element);
                if (newMenu != null) {
                    menu.add(newMenu);
                }
            } else if (element.getClass().isArray()) {
                JMenu newMenu = createMenu(actionMap, (Object[]) element);
                if (newMenu != null) {
                    menu.add(newMenu);
                }
            } else {
                final Action action = actionMap.get(element);
                if (action == null) {
                    continue;
                } else {
                    menu.add(createMenuItem(action));
                }
            }
        }
        return menu;
    }

    /**
     * Creates a {@link JMenu} from the given array and action map.
     * 
     * @param actionMap
     * @param keys
     * @return
     * @see #createMenu(ActionMap, List)
     */
    public static JMenu createMenu(ActionMap actionMap, Object... array) {
        return createMenu(actionMap, Arrays.asList(array));
    }

    /**
     * 
     * TODO
     * @param action
     * @return
     */
    public static AbstractButton createToolbarItem(Action action) {
        final AbstractButton button;
        if (action == null) {
            throw new NullPointerException("Action cannot be null!");
        } else if (action.getValue(Action.SELECTED_KEY) != null) {
            button = new JToggleButton(action);
        } else {
            button = new JButton(action);
        }

        button.setOpaque(false);
        // hide text if icon is available
        if (action != null
                && (action.getValue(Action.SMALL_ICON) != null || action.getValue(Action.LARGE_ICON_KEY) != null)) {
            button.setHideActionText(true);
        }
        button.setHorizontalTextPosition(JButton.CENTER);
        button.setVerticalTextPosition(JButton.BOTTOM);
        return button;
    }

    /**
     * Creates a suitable menu item.
     * 
     * @param action
     * @return
     */
    public static JMenuItem createMenuItem(Action action) {
        final JMenuItem mi;
        final Boolean selected = (Boolean) action.getValue(Action.SELECTED_KEY);
        if (selected != null) {
            mi = new JCheckBoxMenuItem(action);
        } else {
            mi = new JMenuItem(action);
        }
        mi.setHorizontalTextPosition(JButton.TRAILING);
        mi.setVerticalTextPosition(JButton.CENTER);
        return mi;
    }
}