Example usage for javax.swing JComponent getClass

List of usage examples for javax.swing JComponent getClass

Introduction

In this page you can find the example usage for javax.swing JComponent getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

public static ComponentUI getUI(JComponent component) {
    try {/*from  w w  w .  j  av  a 2 s. c o m*/
        Method method = component.getClass().getMethod("getUI", new Class[0]);
        Object o = method.invoke(component, new Object[0]);
        if (o instanceof ComponentUI)
            return (ComponentUI) o;
        else
            return null;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Object getValue(JComponent com) {
    if (com == null) {
        return null;
    }//from  w ww  . j ava2 s.  c o  m
    Class<?> comType = com.getClass();
    Object val = null;
    if (JTextComponent.class.isAssignableFrom(comType)) {
        val = ((JTextComponent) com).getText();
    } else {
        throw new RuntimeException("Unsupported getValue component-" + comType);
    }
    return val;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties of a JComponent. Array properties are not included.
 * <P>/*w  w  w.j a  va 2s  .  c  o  m*/
 * 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) {
    Map<Object, Object> retVal = new HashMap<>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    Object value = null;
    for (Method method : methods) {
        if (method.getName().matches("^(is|get).*") && //$NON-NLS-1$
                method.getParameterTypes().length == 0) {
            try {
                Class<?> returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[") && //$NON-NLS-1$
                        !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 | IllegalArgumentException | InvocationTargetException ex) {
                //no catch
            }
        }
    }
    return retVal;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>//from  w  ww  .j  a  v  a2s .  c  om
 * 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) {
    Map<Object, Object> retVal = new HashMap<Object, Object>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    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;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent.  Array properties are not included.
 * <P>/*from   w  ww  .j  a v a 2  s. c om*/
 * 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) {
    Map<Object, Object> retVal = new HashMap<>();
    Class<?> clazz = component.getClass();
    Method[] methods = clazz.getMethods();
    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 | IllegalArgumentException | InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:Main.java

/**
 * Convenience method for obtaining most non-null human readable properties
 * of a JComponent. Array properties are not included.
 * <P>//from  w  w w.j  av a  2  s  . co m
 * 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(final JComponent component) {
    final Map<Object, Object> retVal = new HashMap<Object, Object>();
    final Class<?> clazz = component.getClass();
    final Method[] methods = clazz.getMethods();
    Object value = null;
    for (final Method method : methods) {
        if (method.getName().matches("^(is|get).*") && method.getParameterTypes().length == 0) {
            try {
                final Class returnType = method.getReturnType();
                if (returnType != void.class && !returnType.getName().startsWith("[")
                        && !setExclude.contains(method.getName())) {
                    final 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 (final IllegalAccessException ex) {
            } catch (final IllegalArgumentException ex) {
            } catch (final InvocationTargetException ex) {
            }
        }
    }
    return retVal;
}

From source file:Main.java

/**
 * A helper for creating and updating key bindings for components with
 * mnemonics. The {@code pressed} action will be invoked when the mnemonic
 * is activated and the {@code released} action will be invoked when the
 * mnemonic is deactivated.//from   w  w  w  . j  a va2  s.co  m
 * <p>
 * TODO establish an interface for the mnemonic properties, such as {@code
 * MnemonicEnabled} and change signature to {@code public static <T extends
 * JComponent & MnemonicEnabled> void updateMnemonicBinding(T c, String
 * pressed, String released)}
 * 
 * @param c
 *            the component bindings to update
 * @param pressed
 *            the name of the action in the action map to invoke when the
 *            mnemonic is pressed
 * @param released
 *            the name of the action in the action map to invoke when the
 *            mnemonic is released (if the action is a toggle style, then
 *            this parameter should be {@code null})
 * @throws NullPointerException
 *             if the component is {@code null}
 */
public static void updateMnemonicBinding(JComponent c, String pressed, String released) {
    Class<?> clazz = c.getClass();
    int m = -1;

    try {
        Method mtd = clazz.getMethod("getMnemonic");
        m = (Integer) mtd.invoke(c);
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalArgumentException("unable to access mnemonic", e);
    }

    InputMap map = SwingUtilities.getUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW);

    if (m != 0) {
        if (map == null) {
            map = new ComponentInputMapUIResource(c);
            SwingUtilities.replaceUIInputMap(c, JComponent.WHEN_IN_FOCUSED_WINDOW, map);
        }

        map.clear();

        //TODO is ALT_MASK right for all platforms?
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, false), pressed);
        map.put(KeyStroke.getKeyStroke(m, InputEvent.ALT_MASK, true), released);
        map.put(KeyStroke.getKeyStroke(m, 0, true), released);
    } else {
        if (map != null) {
            map.clear();
        }
    }
}

From source file:Main.java

public static void printTree(JComponent c, int tabs) {
    if (c.getName() != null && !c.getName().contains("null") || tabs == 0) {
        for (int i = 0; i < tabs; i++)
            System.out.print(" ");
        System.out.println(c.getName() + " [" + c.getClass().getSimpleName() + "]");
    } else//  w  w w . j av a  2s. co  m
        tabs--;

    if (c.getComponents() == null)
        return;
    for (Component ci : c.getComponents()) {
        if (ci instanceof JComponent)
            printTree((JComponent) ci, tabs + 1);
    }
}

From source file:net.sf.nmedit.jtheme.JTContext.java

public static boolean hasUIClass(JComponent component, UIDefaults defaults) {
    String uiClassID = component.getUIClassID();
    if (uiClassID == null)
        return false;

    Object cl = defaults.get(UIDefaultsClassLoaderKey);
    ClassLoader uiClassLoader = (cl != null) ? (ClassLoader) cl : component.getClass().getClassLoader();

    Class uiClass = defaults.getUIClass(uiClassID, uiClassLoader);
    return uiClass != null;
}

From source file:com.kstenschke.copypastestack.ToolWindow.java

/**
 * @param   project     Idea Project//from w  w w .j a  v a 2 s  .  c o m
 * @return  Instance of AhnToolWindow
 */
private static ToolWindow getInstance(Project project) {
    com.intellij.openapi.wm.ToolWindow toolWindow = ToolWindowManager.getInstance(project)
            .getToolWindow("Copy/Paste Stack");

    if (toolWindow != null) {
        try {
            Content content = toolWindow.getContentManager().getContent(0);
            if (content != null) {
                JComponent toolWindowComponent = content.getComponent();
                String canonicalName = toolWindowComponent.getClass().getCanonicalName();
                if (canonicalName.endsWith("com.kstenschke.copypastestack.ToolWindow")) {
                    return (ToolWindow) toolWindowComponent;
                }
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
    }

    return null;
}