Java JComponent Properties getProperties(JComponent component)

Here you can find the source of getProperties(JComponent component)

Description

Convenience method for obtaining most non-null human readable properties of a JComponent.

License

Open Source License

Parameter

Parameter Description
component the component whose proerties are to be determined

Return

the class and value of the properties

Declaration

public static Map<Object, Object> getProperties(JComponent component) 

Method Source Code

//package com.java2s;
/*/*  w  w  w.j  av a2s .c o m*/
 * This file is part of McIDAS-V
 *
 * Copyright 2007-2014
 * Space Science and Engineering Center (SSEC)
 * University of Wisconsin - Madison
 * 1225 W. Dayton Street, Madison, WI 53706, USA
 * http://www.ssec.wisc.edu/mcidas
 * 
 * All Rights Reserved
 * 
 * McIDAS-V is built on Unidata's IDV and SSEC's VisAD libraries, and
 * some McIDAS-V source code is based on IDV and VisAD source code.  
 * 
 * McIDAS-V is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 * 
 * McIDAS-V 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 Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser Public License
 * along with this program.  If not, see http://www.gnu.org/licenses.
 */

import java.awt.Component;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import java.util.HashMap;
import java.util.HashSet;

import java.util.Map;

import java.util.Set;

import javax.swing.JComponent;

public class Main {
    /**
     * Exclude methods that return values that are meaningless to the user
     */
    static Set<String> setExclude = new HashSet<>(10);

    /**
     * Convenience method for obtaining most non-null human readable properties
     * of a JComponent.  Array properties are not included.
     * <P>
     * Implementation note:  The returned value is a HashMap.  This is subject
     * to change, so callers should code against the interface Map.
     *
     * @param component the component whose proerties are to be determined
     * @return the class and value of the properties
     */
    public static Map<Object, Object> getProperties(JComponent component) {
        Class<?> clazz = component.getClass();
        Method[] methods = clazz.getMethods();
        Map<Object, Object> retVal = new HashMap<>(methods.length);
        Object value = null;
        for (Method method : methods) {
            if (method.getName().matches("^(is|get).*")
                    && (method.getParameterTypes().length == 0)) {
                try {
                    Class<?> returnType = method.getReturnType();
                    if ((returnType != void.class)
                            && !returnType.getName().startsWith("[")
                            && !setExclude.contains(method.getName())) {
                        String key = method.getName();
                        value = method.invoke(component);
                        if ((value != null)
                                && !(value instanceof Component)) {
                            retVal.put(key, value);
                        }
                    }
                    // ignore exceptions that arise if the property could not be accessed
                } catch (IllegalAccessException ex) {
                } catch (IllegalArgumentException ex) {
                } catch (InvocationTargetException ex) {
                }
            }
        }
        return retVal;
    }
}

Related

  1. disableAll(JComponent component)
  2. disposeParentWindow(JComponent component)
  3. doDisable(JComponent component)
  4. getProperties(JComponent component)
  5. getProperties(JComponent component)
  6. getVisibleBoundsOnScreen(JComponent component)
  7. invert(JComponent c)
  8. isActive(JComponent c)
  9. isComponentShowing(final JComponent component)