001    /*
002     * This file is part of muCommander, http://www.mucommander.com
003     * Copyright (C) 2002-2008 Maxence Bernard
004     *
005     * muCommander is free software; you can redistribute it and/or modify
006     * it under the terms of the GNU General Public License as published by
007     * the Free Software Foundation; either version 3 of the License, or
008     * (at your option) any later version.
009     *
010     * muCommander is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program.  If not, see <http://www.gnu.org/licenses/>.
017     */
018    
019    package com.mucommander.file.icon;
020    
021    import com.mucommander.file.AbstractFile;
022    
023    import javax.swing.*;
024    import java.awt.*;
025    
026    /**
027     * <code>CacheableFileIconProvider</code> is an interface to be implemented by file icon providers that wish to use
028     * some icon caching to improve performance. This interface is to be used in conjunction with {@link CachedFileIconProvider}
029     * to form a functional cached provider.
030     *
031     * @author Maxence Bernard
032     */
033    public interface CacheableFileIconProvider extends FileIconProvider {
034    
035        /**
036         * Returns <code>true</code> if the icon cache can be used for the specified file and preferred resolution. This
037         * method allows the icon cache to be used only for certain types of files and/or preferred resolutions.
038         *
039         * <p>This method is called by {@link CachedFileIconProvider#getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
040         * each time an icon is requested. If <code>true</code> is returned, the icon cache will be looked up with
041         * {@link #lookupCache(com.mucommander.file.AbstractFile, java.awt.Dimension)} and if the cache did not return
042         * an icon, the icon will be be added to the cache with {@link #addToCache(com.mucommander.file.AbstractFile, javax.swing.Icon, java.awt.Dimension)}.
043         * <br/>
044         * On the other hand, if <code>false</code> is returned, {@link #getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
045         * will simply be called, without querying or adding to the cache.
046         *
047         * @param file the file for which to retrieve an icon
048         * @param preferredResolution the preferred resolution for the icon
049         * @return true if the icon cache can be used with the specified file
050         */
051        public abstract boolean isCacheable(AbstractFile file, Dimension preferredResolution);
052    
053        /**
054         * This method is called by {@link CachedFileIconProvider#getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
055         * to perform a cache lookup and give implementations a chance to re-use a cached icon. If a non-null value is
056         * returned, the returned icon will be used.
057         * <br/>
058         * On the other hand, if <code>null</code> is returned, the icon will be fetched using {@link #getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
059         * followed by a call to {@link #addToCache(com.mucommander.file.AbstractFile, javax.swing.Icon,java.awt.Dimension)}
060         * to add the freshly-retrieved icon to the cache.
061         *
062         * <p>This method is called only if the prior call to {@link #isCacheable(com.mucommander.file.AbstractFile, java.awt.Dimension)}
063         * returned <code>true</code>.</p>.
064         *
065         * @param file the file for which to look for a previously cached icon
066         * @param preferredResolution the preferred resolution for the icon
067         * @return a cached icon to re-use, null if there is none
068         */
069        public abstract Icon lookupCache(AbstractFile file, Dimension preferredResolution);
070    
071        /**
072         * This method is called by {@link CachedFileIconProvider#getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
073         * to give implementations a chance to cache an icon fetched with {@link #getFileIcon(com.mucommander.file.AbstractFile, java.awt.Dimension)}
074         * and have it returned later by {@link #lookupCache(com.mucommander.file.AbstractFile, java.awt.Dimension)}.
075         * <br/>
076         * There is no obligation for this method to cache the given icon, implementations may freely choose whether to
077         * cache certain icons only.
078         *
079         * <p>This method is called only if the prior call to {@link #isCacheable(com.mucommander.file.AbstractFile, java.awt.Dimension)}
080         * returned <code>true</code>.</p>.
081         *
082         * @param file the file that corresponds to the given icon
083         * @param icon the icon to add to the cache
084         * @param preferredResolution the preferred icon resolution that was originally requested
085         */
086        public abstract void addToCache(AbstractFile file, Icon icon, Dimension preferredResolution);
087    
088    }