Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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

public class Main {
    /**
     * Sets the foreground of all components in a container hierachy.
     * See setForegroundOrBackgroundDeep.
     *
     * @param component
     * @param color
     */
    public static void setForegroundDeep(Component component, Color color) {
        setForegroundDeep(component, color, null, true);
    }

    /**
     * Sets the foreground of all components in a container hierachy, with exclusions.
     * See setForegroundOrBackgroundDeep.
     *
     * @param component
     * @param color
     * @param excludedClasses
     * @param processContentsOfExcludedContainers
     *
     */
    public static void setForegroundDeep(Component component, Color color, Class[] excludedClasses,
            boolean processContentsOfExcludedContainers) {
        setForegroundOrBackgroundDeep(true /* doForeground */, component, color, excludedClasses,
                processContentsOfExcludedContainers);
    }

    private static void setForegroundOrBackgroundDeep(final boolean doForeground, Component component, Color color,
            Class[] excludedClasses, boolean processContentsOfExcludedContainers) {

        // If this component one that should be excluded?
        boolean excluded = false;
        if (excludedClasses != null) {
            for (int i = 0; i < excludedClasses.length && !excluded; i++) {
                if (excludedClasses[i].isInstance(component)) {
                    excluded = true;
                }
            }
        }

        // If not exluded, set the component's foreground or background.
        if (!excluded) {
            if (doForeground) {
                component.setForeground(color);
            } else {
                component.setBackground(color);
            }
        }

        // Recursively process the contents of containers.
        if ((!excluded || processContentsOfExcludedContainers) && (component instanceof Container)) {
            Container container = (Container) component;
            Component[] children = container.getComponents();
            if (children != null) {
                for (int i = 0; i < children.length; i++) {
                    setForegroundOrBackgroundDeep(doForeground, children[i], color, excludedClasses,
                            processContentsOfExcludedContainers);
                }
            }
        }
    }
}