Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /***********************************************************************
- * Module: Utils.java
- * Author: Goran Todorovic
- * Purpose: Defines the Class for custom utils used globally (wrappers)
- ***********************************************************************/
- package geer.app.config;
- import java.awt.Color;
- import java.awt.Image;
- import java.io.IOException;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.imageio.ImageIO;
- import javax.swing.Action;
- import javax.swing.ImageIcon;
- /**
- * Utils class
- * @author Goran Todorovic
- *
- */
- public class Utils {
- /**
- * Singleton instance
- */
- private static Utils instance = null;
- /**
- * Logger object
- */
- private static final Logger log = Logger.getLogger(Utils.class.getName());
- /**
- * Instancing singleton here
- * @return
- */
- public static Utils getInstance() {
- if (instance == null) {
- instance = new Utils();
- sLog.write("Utils loaded!");
- }
- return instance;
- }
- /**
- * Method to return icon from given path
- * @param path - Path for the icon
- * @return loaded ImageIcon
- */
- public ImageIcon getIcon(String path) {
- return new ImageIcon(getClass().getResource(path));
- }
- /**
- * Method to get image file
- * @param path - Path for Image
- * @return Image
- * @throws IOException
- */
- public Image getImageFile(String path) throws IOException {
- return ImageIO.read(getClass().getResource(path));
- }
- /**
- * Method to convert
- * @param colorStr - Color string in hex format
- * @return RGB Color
- */
- public static Color hex2RGB(String colorStr) {
- log.log(Level.FINE, "Color converted: " + colorStr + " - " + Color.decode(colorStr));
- return Color.decode(colorStr);
- /*
- return new Color(
- Integer.valueOf(colorStr.substring(1, 3), 16),
- Integer.valueOf(colorStr.substring(3, 5), 16),
- Integer.valueOf(colorStr.substring(5, 7), 16));
- */
- }
- /**
- * Method to convert string values to bool values. (Used for loading user preferences properties file...)
- * Why do we even need this function when Java has Boolean.ParseBool...beacuse Java sucks.
- * @param value - input string value
- * @return - true if string matches, else false
- */
- public boolean convertToBoolean(String value) {
- boolean retVal = false;
- if (("1".equalsIgnoreCase(value)) || ("yes".equalsIgnoreCase(value)) || ("true".equalsIgnoreCase(value)) || ("on".equalsIgnoreCase(value)))
- retVal = true;
- return retVal;
- }
- /**
- * Shuffle hashtable (Map)
- * @param map - random order values
- */
- public <K, V> void shuffleMap(Map<K, V> map) {
- List<V> valueList = new ArrayList<V>(map.values());
- Collections.shuffle(valueList);
- Iterator<V> itr = valueList.iterator();
- for(Map.Entry<K, V> e : map.entrySet()) {
- e.setValue(itr.next());
- }
- }
- /**
- * Resize image icon for actions
- * @param imgPath - Image path (/img/<imgName>.<format>)
- * @param width - Image width for scaling
- * @param height - Image height for scaling
- * @param obj - AbstractAction object aka Action class
- */
- public void resizeIcon(String imgPath, int width, int height, Action obj) {
- ImageIcon img = Utils.getInstance().getIcon(imgPath);
- ImageIcon imgScaled = new ImageIcon(img.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
- obj.putValue(Action.SMALL_ICON, imgScaled);
- }
- /**
- * Resize image icon
- * @param imgPath - Image path (/img/<imgName>.<format>)
- * @param width - Image width for scaling
- * @param height - Image height for scaling
- * @return resized image icon
- */
- public ImageIcon resizeIcon(String imgPath, int width, int height) {
- ImageIcon img = Utils.getInstance().getIcon(imgPath);
- ImageIcon imgScaled = new ImageIcon(img.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH));
- return imgScaled;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement