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.lang.reflect.Method;

import javax.swing.JToolBar;

public class Main {
    public static void disableToolBar(JToolBar panel, Class<?>... classesToIgnore) {
        for (Component component : panel.getComponents()) {
            if (!ignoreClasses(component, classesToIgnore)) {
                Method m = discover(component, "setEnabled", boolean.class);
                invoke(m, component, new Object[] { Boolean.FALSE });
            }
        }
    }

    private static boolean ignoreClasses(Object o, Class<?>... classes) {
        if (classes != null) {
            for (Class<?> c : classes) {
                if (ignoreClass(o, c))
                    return true;
            }
        }
        return false;
    }

    public static Method discover(Object o, String method, Class<?> signature) {
        Method m = null;
        try {
            m = o.getClass().getMethod(method, signature);
        } catch (Exception ignore) {
        }
        return m;
    }

    public static void invoke(Method m, Object o, Object[] signature) {
        if (m == null)
            return;
        try {
            m.invoke(o, signature);
        } catch (Exception ignore) {
        }
    }

    private static boolean ignoreClass(Object o, Class<?> c) {
        return c.isAssignableFrom(o.getClass());
    }
}