Coverage Report - org.jtheque.core.managers.resource.ResourceManager
 
Classes in this File Line Coverage Branch Coverage Complexity
ResourceManager
0 %
0/31
0 %
0/12
1.353
 
 1  
 package org.jtheque.core.managers.resource;
 2  
 
 3  
 /*
 4  
  * This file is part of JTheque.
 5  
  *
 6  
  * JTheque is free software: you can redistribute it and/or modify
 7  
  * it under the terms of the GNU General Public License as published by
 8  
  * the Free Software Foundation, either version 3 of the License.
 9  
  *
 10  
  * JTheque is distributed in the hope that it will be useful,
 11  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  
  * GNU General Public License for more details.
 14  
  *
 15  
  * You should have received a copy of the GNU General Public License
 16  
  * along with JTheque.  If not, see <http://www.gnu.org/licenses/>.
 17  
  */
 18  
 
 19  
 import org.jtheque.core.managers.AbstractManager;
 20  
 import org.jtheque.core.managers.Managers;
 21  
 import org.jtheque.core.managers.beans.IBeansManager;
 22  
 import org.jtheque.core.managers.beans.ioc.Ioc;
 23  
 import org.jtheque.core.managers.log.ILoggingManager;
 24  
 import org.jtheque.utils.ui.ImageUtils;
 25  
 import org.springframework.core.io.Resource;
 26  
 
 27  
 import javax.swing.Action;
 28  
 import javax.swing.ImageIcon;
 29  
 import java.awt.Color;
 30  
 import java.awt.image.BufferedImage;
 31  
 import java.io.IOException;
 32  
 import java.io.InputStream;
 33  
 import java.util.HashMap;
 34  
 import java.util.Map;
 35  
 
 36  
 /**
 37  
  * A resource manager implementation.
 38  
  * <p/>
 39  
  * WARNING : BeansManager must be inited before this manager for his good working.
 40  
  *
 41  
  * @author Baptiste Wicht
 42  
  */
 43  0
 public final class ResourceManager extends AbstractManager implements IResourceManager {
 44  
     private static final int DEFAULT_CACHE_SIZE = 50;
 45  
 
 46  0
     private final Map<String, JThequeImage> cache = new HashMap<String, JThequeImage>(DEFAULT_CACHE_SIZE);
 47  
 
 48  
     @Override
 49  
     public ImageIcon getIcon(String baseName, String id, ImageType type) {
 50  0
         return getIcon(baseName + '/' + id + '.' + type.getExtension());
 51  
     }
 52  
 
 53  
     @Override
 54  
     public ImageIcon getIcon(String id, ImageType type) {
 55  0
         return getIcon(id + '.' + type.getExtension());
 56  
     }
 57  
 
 58  
     @Override
 59  
     public ImageIcon getIcon(String path) {
 60  0
         if (isImageNotCached(path)) {
 61  0
             loadImageInCache(path);
 62  
         }
 63  
 
 64  0
         return cache.get(path) == null ? null : cache.get(path).asIcon();
 65  
     }
 66  
 
 67  
     @Override
 68  
     public BufferedImage getImage(String baseName, String id, ImageType type, int width) {
 69  0
         return getThumbnail(getImage(baseName, id, type), width);
 70  
     }
 71  
 
 72  
     @Override
 73  
     public BufferedImage getImage(String baseName, String id, ImageType type) {
 74  0
         return getImage(baseName + '/' + id + '.' + type.getExtension());
 75  
     }
 76  
 
 77  
     @Override
 78  
     public BufferedImage getImage(String id, ImageType type, int width) {
 79  0
         return getThumbnail(getImage(id, type), width);
 80  
     }
 81  
 
 82  
     @Override
 83  
     public BufferedImage getImage(String id, ImageType type) {
 84  0
         return getImage(id + '.' + type.getExtension());
 85  
     }
 86  
 
 87  
     @Override
 88  
     public BufferedImage getImage(String id, int width) {
 89  0
         return getThumbnail(getImage(id), width);
 90  
     }
 91  
 
 92  
     @Override
 93  
     public BufferedImage getImage(String path) {
 94  0
         if (isImageNotCached(path)) {
 95  0
             loadImageInCache(path);
 96  
         }
 97  
 
 98  0
         return cache.get(path) == null ? null : cache.get(path).get();
 99  
     }
 100  
 
 101  
     @Override
 102  
     public Color getColor(String name) {
 103  0
         return Managers.getManager(IBeansManager.class).getBean(name);
 104  
     }
 105  
 
 106  
     @Override
 107  
     public Action getAction(String name) {
 108  0
         return Managers.getManager(IBeansManager.class).getBean(name);
 109  
     }
 110  
 
 111  
     @Override
 112  
     public Resource getResource(String path) {
 113  0
         return Ioc.getContainer().getApplicationContext().getResource(path);
 114  
     }
 115  
 
 116  
     @Override
 117  
     public InputStream getResourceAsStream(String path) {
 118  0
         InputStream stream = null;
 119  
 
 120  
         try {
 121  0
             stream = getResource(path).getInputStream();
 122  0
         } catch (IOException e) {
 123  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error("Unable to load stream for {}", path);
 124  0
             Managers.getManager(ILoggingManager.class).getLogger(getClass()).error(e);
 125  0
         }
 126  
 
 127  0
         return stream;
 128  
     }
 129  
 
 130  
     /* Private utility methods */
 131  
 
 132  
     /**
 133  
      * Indicate if an image is not cached.
 134  
      *
 135  
      * @param index The index of the image.
 136  
      * @return true if the image is not cached else false.
 137  
      */
 138  
     private boolean isImageNotCached(String index) {
 139  0
         return cache.get(index) == null;
 140  
     }
 141  
 
 142  
     /**
 143  
      * Load the image and put it in cache.
 144  
      *
 145  
      * @param path The path to the image.
 146  
      */
 147  
     private void loadImageInCache(String path) {
 148  0
         BufferedImage image = getCompatibleImage(path);
 149  
 
 150  0
         cache.put(path, new JThequeImage(image));
 151  0
     }
 152  
 
 153  
     /**
 154  
      * Read the image and make it compatible with the environment.
 155  
      *
 156  
      * @param path The path to the image.
 157  
      * @return The compatible image.
 158  
      */
 159  
     private BufferedImage getCompatibleImage(String path) {
 160  0
         return ImageUtils.openCompatibleImage(getResourceAsStream(path));
 161  
     }
 162  
 
 163  
     /**
 164  
      * Return a thumbnail of the image.
 165  
      *
 166  
      * @param image The image to make the thumbnail from.
 167  
      * @param width The width of the thumbnail.
 168  
      * @return The thumbnail of the image, with the specified width.
 169  
      */
 170  
     private static BufferedImage getThumbnail(BufferedImage image, int width) {
 171  0
         return image == null ? null : ImageUtils.createThumbnail(image, width);
 172  
     }
 173  
 }