Java Code Examples for sun.reflect.misc.MethodUtil#getMethod()

The following examples show how to use sun.reflect.misc.MethodUtil#getMethod() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BasicComboBoxEditor.java    From jdk1.8-source-analysis with Apache License 2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 2
Source File: BasicComboBoxEditor.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 3
Source File: BasicComboBoxEditor.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 4
Source File: BasicComboBoxEditor.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class<?>[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 5
Source File: BasicComboBoxEditor.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 6
Source File: BasicComboBoxEditor.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 7
Source File: BasicComboBoxEditor.java    From JDKSourceCode1.8 with MIT License 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 8
Source File: BasicComboBoxEditor.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 9
Source File: BasicComboBoxEditor.java    From TencentKona-8 with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 10
Source File: BasicComboBoxEditor.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Object getItem() {
    Object newValue = editor.getText();

    if (oldValue != null && !(oldValue instanceof String))  {
        // The original value is not a string. Should return the value in it's
        // original type.
        if (newValue.equals(oldValue.toString()))  {
            return oldValue;
        } else {
            // Must take the value from the editor and get the value and cast it to the new type.
            Class<?> cls = oldValue.getClass();
            try {
                Method method = MethodUtil.getMethod(cls, "valueOf", new Class[]{String.class});
                newValue = MethodUtil.invoke(method, oldValue, new Object[] { editor.getText()});
            } catch (Exception ex) {
                // Fail silently and return the newValue (a String object)
            }
        }
    }
    return newValue;
}
 
Example 11
Source File: AbstractRegionPainter.java    From JDKSourceCode1.8 with MIT License 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 12
Source File: AbstractRegionPainter.java    From jdk8u60 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 13
Source File: AbstractRegionPainter.java    From openjdk-jdk8u with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 14
Source File: AbstractRegionPainter.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 15
Source File: AbstractRegionPainter.java    From TencentKona-8 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 16
Source File: AbstractRegionPainter.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @param saturationOffset additively modifies the HSB saturation component
 * of the color returned (ignored if default color is returned).
 * @param brightnessOffset additively modifies the HSB brightness component
 * of the color returned (ignored if default color is returned).
 * @param alphaOffset additively modifies the ARGB alpha component of the
 * color returned (ignored if default color is returned).
 *
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 17
Source File: AbstractRegionPainter.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @param saturationOffset additively modifies the HSB saturation component
 * of the color returned (ignored if default color is returned).
 * @param brightnessOffset additively modifies the HSB brightness component
 * of the color returned (ignored if default color is returned).
 * @param alphaOffset additively modifies the ARGB alpha component of the
 * color returned (ignored if default color is returned).
 *
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 18
Source File: AbstractRegionPainter.java    From dragonwell8_jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 19
Source File: AbstractRegionPainter.java    From jdk8u-jdk with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}
 
Example 20
Source File: AbstractRegionPainter.java    From jdk1.8-source-analysis with Apache License 2.0 4 votes vote down vote up
/**
 * Get a color property from the given JComponent. First checks for a
 * <code>getXXX()</code> method and if that fails checks for a client
 * property with key <code>property</code>. If that still fails to return
 * a Color then <code>defaultColor</code> is returned.
 *
 * @param c The component to get the color property from
 * @param property The name of a bean style property or client property
 * @param defaultColor The color to return if no color was obtained from
 *        the component.
 * @return The color that was obtained from the component or defaultColor
 */
protected final Color getComponentColor(JComponent c, String property,
                                        Color defaultColor,
                                        float saturationOffset,
                                        float brightnessOffset,
                                        int alphaOffset) {
    Color color = null;
    if (c != null) {
        // handle some special cases for performance
        if ("background".equals(property)) {
            color = c.getBackground();
        } else if ("foreground".equals(property)) {
            color = c.getForeground();
        } else if (c instanceof JList && "selectionForeground".equals(property)) {
            color = ((JList) c).getSelectionForeground();
        } else if (c instanceof JList && "selectionBackground".equals(property)) {
            color = ((JList) c).getSelectionBackground();
        } else if (c instanceof JTable && "selectionForeground".equals(property)) {
            color = ((JTable) c).getSelectionForeground();
        } else if (c instanceof JTable && "selectionBackground".equals(property)) {
            color = ((JTable) c).getSelectionBackground();
        } else {
            String s = "get" + Character.toUpperCase(property.charAt(0)) + property.substring(1);
            try {
                Method method = MethodUtil.getMethod(c.getClass(), s, null);
                color = (Color) MethodUtil.invoke(method, c, null);
            } catch (Exception e) {
                //don't do anything, it just didn't work, that's all.
                //This could be a normal occurance if you use a property
                //name referring to a key in clientProperties instead of
                //a real property
            }
            if (color == null) {
                Object value = c.getClientProperty(property);
                if (value instanceof Color) {
                    color = (Color) value;
                }
            }
        }
    }
    // we return the defaultColor if the color found is null, or if
    // it is a UIResource. This is done because the color for the
    // ENABLED state is set on the component, but you don't want to use
    // that color for the over state. So we only respect the color
    // specified for the property if it was set by the user, as opposed
    // to set by us.
    if (color == null || color instanceof UIResource) {
        return defaultColor;
    } else if (saturationOffset != 0 || brightnessOffset != 0 || alphaOffset != 0) {
        float[] tmp = Color.RGBtoHSB(color.getRed(), color.getGreen(), color.getBlue(), null);
        tmp[1] = clamp(tmp[1] + saturationOffset);
        tmp[2] = clamp(tmp[2] + brightnessOffset);
        int alpha = clamp(color.getAlpha() + alphaOffset);
        return new Color((Color.HSBtoRGB(tmp[0], tmp[1], tmp[2]) & 0xFFFFFF) | (alpha <<24));
    } else {
        return color;
    }
}