001    /*
002     * Copyright 2002-2004 the original author or authors.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License"); you may not
005     * use this file except in compliance with the License. You may obtain a copy of
006     * the License at
007     * 
008     * http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
012     * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
013     * License for the specific language governing permissions and limitations under
014     * the License.
015     */
016    package org.springframework.richclient.image;
017    
018    import java.awt.Image;
019    import java.util.Iterator;
020    
021    import javax.swing.ImageIcon;
022    
023    import org.springframework.util.Assert;
024    
025    /**
026     * Default implementation of a reloadable sized icon registry. Icons by default
027     * are loaded by appending registry's current iconSize name to the iconKey as
028     * follows:
029     * <p>
030     * 
031     * <pre>
032     * 
033     *  
034     *    &lt;iconKey&gt;.&lt;iconSize&gt;
035     *   
036     *  
037     * </pre>
038     * 
039     * <p>
040     * For example:
041     * <p>
042     * 
043     * <pre>
044     * 
045     *  
046     *    action.edit.copy.small=/images/edit/copy16.gif
047     *   
048     *  
049     * </pre>
050     * 
051     * @author Keith Donald
052     */
053    public class ReloadableSizedIconSource extends DefaultIconSource implements SizedIconSource {
054        private IconSize iconSize;
055    
056        /**
057         * Create a sized icon registry with icons of a specified size and icon
058         * resources to be loaded from the specified image source.
059         * 
060         * @param iconSize
061         *            the size of icons in this registry
062         * @param iconResources
063         *            The image icon source
064         */
065        public ReloadableSizedIconSource(IconSize iconSize, ImageSource iconResources) {
066            super(iconResources);
067            Assert.notNull(iconSize);
068            this.iconSize = iconSize;
069        }
070    
071        public void reload(IconSize size) {
072            Assert.notNull(size);
073            this.iconSize = size;
074            Iterator keys = cache().keySet().iterator();
075            if (!keys.hasNext()) {
076                logger.warn("No icons currently in the registry--nothing to reload.");
077                return;
078            }
079            while (keys.hasNext()) {
080                reloadIconImage((String)keys.next());
081            }
082        }
083    
084        // reloads the specified image resource key and update the cached icon
085        private void reloadIconImage(String key) {
086            ImageIcon icon = (ImageIcon)cache().get(key);
087            if (icon != null) {
088                Image image = cache().images().getImage(appendIconSizeSuffix(key));
089                icon.setImage(image);
090            }
091        }
092    
093        private String appendIconSizeSuffix(String key) {
094            if (iconSize == null) 
095                return key;
096    
097            logger.debug("Appending icon suffix '." + iconSize.getName() + "'");
098            return key + "." + iconSize.getName();
099        }
100    
101        protected String doProcessImageKeyBeforeLookup(String key) {
102            return appendIconSizeSuffix(key);
103        }
104    
105    }