001/*
002  * Created on 14.12.2009
003  *
004*/
005// package org.jdesktop.swingx.demos.tree;
006package ca.bc.webarts.widgets.treetable;
007
008import java.awt.Dimension;
009import java.awt.image.BufferedImage;
010import java.io.IOException;
011import java.net.URL;
012import java.util.HashMap;
013import java.util.Map;
014
015import javax.imageio.ImageIO;
016import javax.swing.Icon;
017import javax.swing.ImageIcon;
018
019import org.jdesktop.swingx.icon.PainterIcon;
020import org.jdesktop.swingx.painter.ImagePainter;
021import org.jdesktop.swingx.renderer.IconValue;
022import org.jdesktop.swingx.renderer.StringValue;
023import org.jdesktop.swingx.util.GraphicsUtilities;
024
025import com.jhlabs.image.InvertFilter;
026
027/**
028 * Custom icon values used in TreeDemo.
029 */
030public class TreeDemoIconValues
031{
032
033  /**
034   * An IconValue which maps cell value to an icon. The value is mapped
035   * to a filename using a StringValue. The icons are loaded lazyly.
036   */
037  public static class LazyLoadingIconValue implements IconValue
038  {
039    private Class<?> baseClass;
040    private StringValue keyToFileName;
041    private Map<Object, Icon> iconCache;
042    private Icon fallbackIcon;
043
044    public LazyLoadingIconValue( Class<?> baseClass, StringValue sv, String fallbackName )
045    {
046      this.baseClass = baseClass;
047      iconCache = new HashMap<Object, Icon>();
048      this.keyToFileName = sv;
049      fallbackIcon = loadFromResource( fallbackName );
050    }
051
052    // <snip> JXTree rendering
053    // IconValue based on node value
054    /**
055     * {@inheritDoc} <p>
056     *
057     * Implemented to return a Icon appropriate for the given node value. The icon is
058     * loaded (and later cached) as a resource, using a lookup key created by a StringValue.
059     *
060     */
061    @Override
062    public Icon getIcon( Object value )
063    {
064      String key = keyToFileName.getString( value );
065      Icon icon = iconCache.get( key );
066      if ( icon == null )
067      {
068        icon = loadIcon( key );
069      }
070      if ( icon == null )
071      {
072        icon = fallbackIcon;
073      }
074      return icon;
075    }
076    // </snip>
077
078    private Icon loadIcon( String key )
079    {
080      Icon icon = loadFromResource( key );
081      if ( icon != null )
082      {
083        iconCache.put( key, icon );
084      }
085      return icon;
086    }
087
088    protected Icon loadFromResource( String name )
089    {
090      URL url = baseClass.getResource( "resources/images/" + name );
091      if ( url == null )
092      {
093        return null;
094      }
095      try
096      {
097        BufferedImage image = ImageIO.read( url );
098        if ( image.getHeight() > 30 )
099        {
100          image = GraphicsUtilities.createThumbnail( image, 16 );
101        }
102        return new ImageIcon( image );
103      }
104      catch ( IOException e )
105      {
106      }
107      return null;
108    }
109
110  }
111
112  /**
113   * A IconValue which delegates icon lookup to another IconValue and returns
114   * a manipulated icon.
115   */
116  public static class FilteredIconValue implements IconValue
117  {
118
119    private IconValue delegate;
120
121    private Map<Object, Icon> iconCache;
122
123    public FilteredIconValue( IconValue delegate )
124    {
125      iconCache = new HashMap<Object, Icon>();
126      this.delegate = delegate;
127    }
128
129    /**
130     * {@inheritDoc} <p>
131     *
132     * Looks up the default icon in the delegate and
133     * returns a manipulated version.
134     */
135    @Override
136    public Icon getIcon( Object value )
137    {
138      Icon icon = delegate.getIcon( value );
139      Icon xicon = iconCache.get( icon );
140      if ( xicon == null )
141      {
142        xicon = manipulatedIcon( icon );
143        iconCache.put( icon, xicon );
144      }
145      return xicon;
146    }
147
148    // <snip> JXTree rollover
149    // wraps the given icon into an ImagePainter with a filter effect
150    private Icon manipulatedIcon( Icon icon )
151    {
152      PainterIcon painterIcon = new PainterIcon( new Dimension( icon.getIconWidth(), icon.getIconHeight() ) );
153      BufferedImage image = ( BufferedImage ) ( ( ImageIcon ) icon ).getImage();
154      ImagePainter delegate = new ImagePainter( image );
155      delegate.setFilters( new InvertFilter() );
156      painterIcon.setPainter( delegate );
157      return painterIcon;
158    }
159    // </snip>
160
161  }
162
163}