New Jide License
[indepmod/experimental.git] / IndependentModeler / src / cz / cvut / promod / resources / AbstractResources.java
blobd7ba1e0a90e0b1acf1121ed5acf7f8ddb043738b
1 package cz.cvut.promod.resources;
3 import org.apache.log4j.Logger;
5 import javax.swing.*;
6 import java.io.File;
7 import java.net.URL;
8 import java.util.HashMap;
9 import java.util.Map;
11 public abstract class AbstractResources {
13 private static final Logger LOG = Logger.getLogger(Resources.class);
15 private final static Map<String, ImageIcon> icons = new HashMap<String, ImageIcon>();
16 private final static Map<String, File> configFiles = new HashMap<String, File>();
18 /**
19 * Returns the icon that has been already loaded or tries to load this icon.
21 * @param resourceName is the full name of required resource (= icon)
22 * @return the required icon if no error(s) occurred, null otherwise
24 public static ImageIcon getIcon(final String resourceName) {
25 ImageIcon imageIcon;
27 if (icons.containsKey(resourceName)) {
28 imageIcon = icons.get(resourceName);
29 } else {
30 imageIcon = loadIcon(resourceName);
32 if(imageIcon != null){
33 icons.put(resourceName, imageIcon);
37 return imageIcon;
40 /**
41 * Loads the icon.
43 * @param resourceName is the full name of the icon
44 * @return the required icon or null if the there is no resource with given resourceName
46 private static ImageIcon loadIcon(final String resourceName) {
47 final URL systemResource = ClassLoader.getSystemResource(resourceName);
49 if (systemResource == null) {
50 LOG.error("Resource " + resourceName + " couldn't be found.");
51 return null;
54 return new ImageIcon(systemResource);
57 /**
58 * Returns the resource that has been already loaded or tries to load this icon.
60 * @param resourceName is the full name of required resource
61 * @return the required icon if no error(s) occurred, null otherwise
63 public static File getConfigFile(final String resourceName){
64 File configFile;
66 if(icons.containsKey(resourceName)){
67 configFile = configFiles.get(resourceName);
69 } else {
70 configFile = loadConfigFile(resourceName);
72 if(configFile != null){
73 configFiles.put(resourceName, configFile);
77 return configFile;
80 /**
81 * Loads the config resources.
83 * @param resourceName is the full name of the resource
84 * @return the required resource or null if the there is no resource with given resourceName
86 private static File loadConfigFile(final String resourceName) {
87 final URL systemResource = ClassLoader.getSystemResource(resourceName);
89 if(systemResource == null){
90 LOG.error("Resource " + resourceName + " couldn't be found.");
91 return null;
94 return new File(systemResource.getFile());