Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
/*
 * Copyright (C) 2015 Miquel Sas
 * 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

import java.awt.Component;
import java.awt.Container;

import java.util.ArrayList;

import java.util.List;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import javax.swing.event.MenuKeyListener;

public class Main {
    /**
     * Installs the key listener in the tree of components where the argument component is included, starting in the
     * first parent <i>JFrame</i> or <i>JDialog</i> parent.
     * 
     * @param cmp The starting components in the tree.
     * @param keyListener The key listener to install.
     */
    public static void installMenuKeyListener(Component cmp, MenuKeyListener keyListener) {
        List<Component> components = getAllComponents(cmp);
        for (Component component : components) {
            if ((component instanceof JPopupMenu)) {
                JPopupMenu popupMenu = (JPopupMenu) component;
                popupMenu.addMenuKeyListener(keyListener);
            }
            if ((component instanceof JMenuItem)) {
                JMenuItem menuItem = (JMenuItem) component;
                menuItem.addMenuKeyListener(keyListener);
            }
        }
    }

    /**
     * Returns the list of all components contained in a component and its subcomponents.
     * 
     * @return The list of components.
     * @param parent The parent component.
     */
    public static List<Component> getAllComponents(Component parent) {
        List<Component> list = new ArrayList<>();
        fillComponentList(parent, list);
        return list;
    }

    /**
     * Returns the array of all components contained in a component and its subcomponents.
     * 
     * @param parent The parent component.
     * @param clazz The class to filter components.
     * @return The array of components.
     */
    public static List<Component> getAllComponents(Component parent, Class<?> clazz) {
        return getAllComponents(parent, new Class[] { clazz });
    }

    /**
     * Returns the list of all components contained in a component and its subcomponents.
     * 
     * @param parent The parent component.
     * @param classes an array of possible classes.
     * @return The list of components.
     */
    public static List<Component> getAllComponents(Component parent, Class<?>[] classes) {
        List<Component> list = new ArrayList<>();
        List<Component> components = getAllComponents(parent);
        for (Component component : components) {
            for (int j = 0; j < classes.length; j++) {
                if (classes[j].isInstance(component)) {
                    list.add(component);
                }
            }
        }
        return list;
    }

    /**
     * Fills the array list with the all the components contained in the parent component and its sub-components.
     * 
     * @param list An <code>ArrayList</code>.
     * @param cmp The parent component.
     */
    public static void fillComponentList(Component cmp, List<Component> list) {
        list.add(cmp);
        if (cmp instanceof Container) {
            Container cnt = (Container) cmp;
            for (int i = 0; i < cnt.getComponentCount(); i++) {
                fillComponentList(cnt.getComponent(i), list);
            }
        }
    }

    /**
     * Returns the component with the given name contained in the top component, or null if it does not contain a
     * component with that name.
     * 
     * @param topComponent The top component.
     * @param name The name of the component to search.
     * @return The component with the name or null.
     */
    public static Component getComponent(Component topComponent, String name) {
        List<Component> components = getAllComponents(topComponent);
        for (Component component : components) {
            if (component.getName() != null && component.getName().equals(name)) {
                return component;
            }
        }
        return null;
    }
}