Advertisement
Lauda

Utils

Jan 10th, 2015
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.96 KB | None | 0 0
  1. /***********************************************************************
  2.  * Module:  Utils.java
  3.  * Author:  Goran Todorovic
  4.  * Purpose: Defines the Class for custom utils used globally (wrappers)
  5.  ***********************************************************************/
  6.  
  7. package geer.app.config;
  8.  
  9. import java.awt.Color;
  10. import java.awt.Image;
  11. import java.io.IOException;
  12. import java.util.ArrayList;
  13. import java.util.Collections;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.Map;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. import javax.imageio.ImageIO;
  21. import javax.swing.Action;
  22. import javax.swing.ImageIcon;
  23.  
  24. /**
  25.  * Utils class
  26.  * @author Goran Todorovic
  27.  *
  28.  */
  29. public class Utils {
  30.     /**
  31.      * Singleton instance
  32.      */
  33.     private static Utils instance = null;
  34.  
  35.     /**
  36.      * Logger object
  37.      */
  38.     private static final Logger log = Logger.getLogger(Utils.class.getName());
  39.  
  40.  
  41.     /**
  42.      * Instancing singleton here
  43.      * @return
  44.      */
  45.     public static Utils getInstance() {
  46.         if (instance == null) {
  47.             instance = new Utils();
  48.             sLog.write("Utils loaded!");
  49.         }
  50.         return instance;
  51.     }
  52.  
  53.     /**
  54.      * Method to return icon from given path
  55.      * @param path - Path for the icon
  56.      * @return loaded ImageIcon
  57.      */
  58.     public ImageIcon getIcon(String path) {
  59.         return new ImageIcon(getClass().getResource(path));
  60.     }
  61.  
  62.     /**
  63.      * Method to get image file
  64.      * @param path - Path for Image
  65.      * @return Image
  66.      * @throws IOException
  67.      */
  68.     public Image getImageFile(String path) throws IOException {
  69.         return ImageIO.read(getClass().getResource(path));
  70.     }
  71.  
  72.     /**
  73.      * Method to convert
  74.      * @param colorStr - Color string in hex format
  75.      * @return RGB Color
  76.      */
  77.     public static Color hex2RGB(String colorStr) {
  78.         log.log(Level.FINE, "Color converted: " + colorStr + " - " + Color.decode(colorStr));
  79.         return Color.decode(colorStr);
  80.         /*
  81.         return new Color(
  82.             Integer.valueOf(colorStr.substring(1, 3), 16),
  83.             Integer.valueOf(colorStr.substring(3, 5), 16),
  84.             Integer.valueOf(colorStr.substring(5, 7), 16));
  85.          */
  86.     }
  87.  
  88.     /**
  89.      * Method to convert string values to bool values. (Used for loading user preferences properties file...)
  90.      * Why do we even need this function when Java has Boolean.ParseBool...beacuse Java sucks.
  91.      * @param value - input string value
  92.      * @return - true if string matches, else false
  93.      */
  94.     public boolean convertToBoolean(String value) {
  95.         boolean retVal = false;
  96.         if (("1".equalsIgnoreCase(value)) || ("yes".equalsIgnoreCase(value)) || ("true".equalsIgnoreCase(value)) || ("on".equalsIgnoreCase(value)))
  97.             retVal = true;
  98.  
  99.         return retVal;
  100.     }
  101.    
  102.     /**
  103.      * Shuffle hashtable (Map)
  104.      * @param map - random order values
  105.      */
  106.     public <K, V> void shuffleMap(Map<K, V> map) {
  107.         List<V> valueList = new ArrayList<V>(map.values());
  108.         Collections.shuffle(valueList);
  109.        
  110.         Iterator<V> itr = valueList.iterator();
  111.         for(Map.Entry<K, V> e : map.entrySet()) {
  112.             e.setValue(itr.next());
  113.         }
  114.     }
  115.    
  116.     /**
  117.      * Resize image icon for actions
  118.      * @param imgPath - Image path (/img/<imgName>.<format>)
  119.      * @param width - Image width for scaling
  120.      * @param height - Image height for scaling
  121.      * @param obj - AbstractAction object aka Action class
  122.      */
  123.     public void resizeIcon(String imgPath, int width, int height, Action obj) {
  124.         ImageIcon img = Utils.getInstance().getIcon(imgPath);
  125.         ImageIcon imgScaled = new ImageIcon(img.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
  126.         obj.putValue(Action.SMALL_ICON, imgScaled);
  127.     }
  128.    
  129.     /**
  130.      * Resize image icon
  131.      * @param imgPath - Image path (/img/<imgName>.<format>)
  132.      * @param width - Image width for scaling
  133.      * @param height - Image height for scaling
  134.      * @return resized image icon
  135.      */
  136.     public ImageIcon resizeIcon(String imgPath, int width, int height) {
  137.         ImageIcon img = Utils.getInstance().getIcon(imgPath);
  138.         ImageIcon imgScaled = new ImageIcon(img.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
  139.        
  140.         return imgScaled;
  141.     }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement