FileDocCategorySizeDatePackage
ColorELResolver.javaAPI DocGlassfish v2 API10791Fri May 04 22:34:24 BST 2007jsp2_1.examples.elresolver

ColorELResolver

public class ColorELResolver extends ELResolver
Introduces a new ${Color} implicit object and resolves properties on that object.

Features include:

  • Look up common colors by name, e.g. ${Color.MintCream}
  • Look up colors by hex, e.g. ${Color["#f0f0f0"]}
  • Specify r, g, and b values numerically, e.g. ${Color[240][255][255]}
  • Retrieve red, green, or blue individually, e.g. ${myColor.red}
  • Retrieve HTML-style hex value for a color, e.g. ${myColor.hex}
  • Retrieve java.awt.Color object for a color, e.g. ${myColor.color}
  • Retrieve brigter and darker colors, e.g. ${Color.AliceBlue.darker.hex}

author
Mark Roth

Fields Summary
Constructors Summary
Methods Summary
public java.lang.ClassgetCommonPropertyType(javax.el.ELContext context, java.lang.Object base)

        Class result = null;
        
        if(base == null) {
            // Resolving first variable (e.g. ${Color}).  
            // We only handle "Color"
            result = String.class;
        }
        else if(base instanceof ColorImplicitObject) {
            // We handle either integers or strings, so return Object
            result = Object.class;
        }
        else if(base instanceof ColorImplicitObject.ColorR) {
            // We handle only integers in this case.
            result = Long.class;
        }
        else if(base instanceof ColorImplicitObject.ColorRG) {
            // We handle only integers in this case.
            result = Long.class;
        }
        else if(base instanceof ColorImplicitObject.ColorRGB) {
            // We don't do anything with these - the BeanELResolver 
            // takes it from here.
        }
        
        return result;
    
public java.util.IteratorgetFeatureDescriptors(javax.el.ELContext context, java.lang.Object base)

        Iterator result = null;

        if(context == null) throw new NullPointerException();
        
        if(base == null) {
            result = Arrays.asList(new String[] {"Color"}).iterator();
        }
        else if(base instanceof ColorImplicitObject) {
            // Return all color names
            result = ColorImplicitObject.colorNameIterator();
            
            // XXX - There's no way to say we also accept 0-255
        }
        else if(
                (base instanceof ColorImplicitObject.ColorR)
            ||  (base instanceof ColorImplicitObject.ColorRG)
            ||  (base instanceof ColorImplicitObject.ColorRGB))
        {
            // We accept integers 0-255, but don't enumerate them.
            // The tool will call getCommonPropertyType() instead.
            result = null;
        }
        
        // BeanELResolver will add to this iterator with the bean properties.
        
        return result;
    
public java.lang.ClassgetType(javax.el.ELContext context, java.lang.Object base, java.lang.Object property)

        Class result = null;
        
        if(context == null) throw new NullPointerException();
        
        if(base == null) {
            // We don't handle setting top-level implicit objects.
        }
        else if(base instanceof ColorImplicitObject) {
            // None of the properties of the ${Color} implicit object are 
            // ever writable.
            context.setPropertyResolved(true);
        }
        else if(
                (base instanceof ColorImplicitObject.ColorR) 
            ||  (base instanceof ColorImplicitObject.ColorRG))
        {
            // Don't allow setting of
            // ${Color[100][150]} or ${Color[100][150][200]}
            if(property instanceof Long) {
                context.setPropertyResolved(true);
            }
        }
        // The rest is handled by BeanELResolver, etc.
        
        return result;
    
public java.lang.ObjectgetValue(javax.el.ELContext context, java.lang.Object base, java.lang.Object property)

        if(context == null) throw new NullPointerException();
        
        Object result = null;
        
        if(base == null) {
            // Resolving first variable (e.g. ${Color}).  
            // We only handle "Color"
            String propertyName = (String)property;
            if(propertyName.equals("Color")) {
                result = new ColorImplicitObject();
                context.setPropertyResolved(true);
            }
        }
        else if(base instanceof ColorImplicitObject) {
            // Resolving a property on ${Color}
            ColorImplicitObject color = (ColorImplicitObject)base;
            
            if(property instanceof Long) {
                // Handle ${Color[100]}
                int red = ((Long)property).intValue();
                result = new ColorImplicitObject.ColorR(red);
                context.setPropertyResolved(true);
            }
            else {
                String colorName = property.toString();
                
                if(colorName.startsWith("#")) {
                    // Handle ${Color['#f0f0f0']}
                    result = ColorImplicitObject.fromHex(colorName);
                    context.setPropertyResolved(true);
                }
                else {
                    // Handle ${Color.MintCream}
                    result = ColorImplicitObject.fromName(colorName);
                    context.setPropertyResolved(true);
                }
            }
        }
        else if(base instanceof ColorImplicitObject.ColorRGB) {
            ColorImplicitObject.ColorRGB rgb = 
                (ColorImplicitObject.ColorRGB)base;
            // The rest is handled by the BeanPropertyResolver.
        }
        else if(base instanceof ColorImplicitObject.ColorRG) {
            ColorImplicitObject.ColorRG rg = (ColorImplicitObject.ColorRG)base;
            if(property instanceof Long) {
                // Handle ${Color[100][150][200]}
                int blue = ((Long)property).intValue();
                result = new ColorImplicitObject.ColorRGB(rg.getRed(), 
                    rg.getGreen(), blue);
                context.setPropertyResolved(true);
            }
        }
        else if(base instanceof ColorImplicitObject.ColorR) {
            ColorImplicitObject.ColorR r = (ColorImplicitObject.ColorR)base;
            if(property instanceof Long) {
                // Handle ${Color[100][150]}
                int green = ((Long)property).intValue();
                result = new ColorImplicitObject.ColorRG(r.getRed(), green);
                context.setPropertyResolved(true);
            }
        }
        
        return result;
    
public booleanisReadOnly(javax.el.ELContext context, java.lang.Object base, java.lang.Object property)

        boolean result = false;
        
        if(context == null) throw new NullPointerException();
        
        if(base == null) {
            // We don't handle setting top-level implicit objects.
        }
        else if(base instanceof ColorImplicitObject) {
            // None of the properties of the ${Color} implicit object are 
            // ever writable.
            result = false;
            context.setPropertyResolved(true);
        }
        else if(
                (base instanceof ColorImplicitObject.ColorR) 
            ||  (base instanceof ColorImplicitObject.ColorRG))
        {
            // Don't allow setting of
            // ${Color[100][150]} or ${Color[100][150][200]}
            if(property instanceof Long) {
                result = false;
                context.setPropertyResolved(true);
            }
        }
        
        return result;
    
public voidsetValue(javax.el.ELContext context, java.lang.Object base, java.lang.Object property, java.lang.Object value)

        if(context == null) throw new NullPointerException();
        
        if(base == null) {
            // We don't handle setting top-level implicit objects.
        }
        else if(base instanceof ColorImplicitObject) {
            // None of the properties of the ${Color} implicit object are 
            // ever writable.
            throw new PropertyNotWritableException();
        }
        else if(
                (base instanceof ColorImplicitObject.ColorR) 
            ||  (base instanceof ColorImplicitObject.ColorRG))
        {
            // Don't allow setting of
            // ${Color[100][150]} or ${Color[100][150][200]}
            if(property instanceof Long) {
                throw new PropertyNotWritableException();
            }
        }