View Javadoc

1   package com.melloware.jukes.file.image;
2   
3   import java.awt.Graphics2D;
4   import java.awt.Image;
5   import java.awt.MediaTracker;
6   import java.awt.image.BufferedImage;
7   import java.awt.image.ImageObserver;
8   import java.io.File;
9   import java.io.IOException;
10  import java.net.MalformedURLException;
11  import java.net.URL;
12  import java.util.Locale;
13  
14  import javax.imageio.ImageIO;
15  import javax.swing.ImageIcon;
16  
17  import org.apache.commons.lang.StringUtils;
18  import org.apache.commons.lang.SystemUtils;
19  import org.apache.commons.logging.Log;
20  import org.apache.commons.logging.LogFactory;
21  
22  import com.jgoodies.uif.util.ResourceUtils;
23  import com.melloware.jukes.file.FileUtil;
24  import com.melloware.jukes.file.filter.ImageFilter;
25  import com.melloware.jukes.gui.tool.Settings; 
26  //import com.melloware.jukes.gui.view.editor.DiscEditor;
27  
28  /**
29   * Class to represent and storage area for all icon references.  This will
30   * reduce overhead and memory usage throughout the application.  Also contains
31   * some static helper methods for loading and scaling images.
32   * <p>
33   * <p>Copyright: Copyright (c) 2006</p>
34   * <p>Company: Melloware, Inc.</p>
35   * @author Emil A. Lefkof III
36   * @version 4.0
37   */
38  public final class ImageFactory {
39  	
40  	private static final Log LOG = LogFactory.getLog(ImageFactory.class); //AZ
41  
42      public static final ImageIcon ICO_TRAYICON;
43      public static final ImageIcon ICO_TRAYPLAY;
44      public static final ImageIcon ICO_TRAYPAUSE;
45      public static final ImageIcon ICO_TRAYSTOP;
46      public static final ImageIcon IMAGE_NOCOVER;
47      
48      /**EAL **/public static Settings settings = null;
49      
50      /**EAL **/
51      // call this method only once to store the static settings variable from the MainModule code where its set
52      public static void setSettings(Settings aSettings) {
53         settings = aSettings;
54      } 
55  
56      static {
57         if (SystemUtils.IS_OS_WINDOWS) {
58            ICO_TRAYICON = getIcon("images/trayicon.png");
59            ICO_TRAYPLAY = getIcon("images/trayplay.png");
60            ICO_TRAYPAUSE = getIcon("images/traypause.png");
61            ICO_TRAYSTOP = getIcon("images/traystop.png");
62         } else {
63            ICO_TRAYICON = getIcon("images/tray_icon_24x24.png");
64            ICO_TRAYPLAY = getIcon("images/tray_play_24x24.png");
65            ICO_TRAYPAUSE = getIcon("images/tray_pause_24x24.png");
66            ICO_TRAYSTOP = getIcon("images/tray_stop_24x24.png");
67         }
68          
69          IMAGE_NOCOVER = getIcon("images/image_nocover.jpg");
70      }
71  
72      /**
73       * Default constructor. Private so no instantiation.
74       */
75      private ImageFactory() {
76          super();
77      }
78  
79      /**
80       * Gets the disc and returns it the correct size.
81       * <p>
82       * @param aFileName the file location of the disc
83       * @return the ImageIcon
84       */
85      public static ImageIcon getDiscImage(final String aFileName) {
86          // return getScaledImage(aFileName, 300, 300);
87      	//AZ - use settings instead of fixed size
88      	return getScaledImage(aFileName, settings.getCoverSizeLarge(), settings.getCoverSizeLarge());
89      }
90  
91      /**
92       * Method to create a new ImageIcon and return the reference
93       * <p>
94       * @param aFileName filename of icon to load
95       * @return ImageIcon reference of fileName
96       */
97      public static ImageIcon getIconFromFile(final String aFileName) {
98          return new ImageIcon(aFileName);
99      }
100 
101     /**
102      * Gets an Image from the URL specified.  Safe so we return NULL if
103      * anything goes wrong and the image can not be loaded.
104      * <p>
105      * @param aUrl the URL to look for the image
106      * @return the Image if found, NULL if nothing found
107      */
108     public static Image getImageFromUrl(final String aUrl) {
109         Image image = null;
110         if (StringUtils.isBlank(aUrl)) {
111             throw new IllegalArgumentException("aUrl must be specified");
112         }
113 
114         try {
115             final URL url = new URL(aUrl);
116             image = ImageIO.read(url);
117         } catch (MalformedURLException ex) {
118             image = null;
119         } catch (IOException ex) {
120             image = null;
121         }
122         return image;
123     }
124 
125     /**
126      * Gets an ImageIcon and scales it to the size requested.
127      * <p>
128      * @param aFileName the filename to get
129      * @param height the desired height of the image
130      * @param width the desired width of the image
131      * @return the ImageIcon
132      */
133     public static ImageIcon getScaledImage(final String aFileName, final int height, final int width) {
134         ImageIcon icon = null;
135         final String errorString = ResourceUtils.getString("messages.ErrorReadingImageFile") + ": " + aFileName;
136         if (StringUtils.isBlank(aFileName)) {
137             icon = IMAGE_NOCOVER;
138         } else {
139             // get the image
140  /** AZ **/
141         	try {
142             icon = new ImageIcon(aFileName);
143         	}
144         	catch (Exception ex) {
145         	LOG.error(errorString, ex);	
146         	}
147         }
148 
149         // if no image was found return the default
150         if (MediaTracker.ERRORED == icon.getImageLoadStatus()) {
151             icon = IMAGE_NOCOVER;
152         }
153 
154         // resizing Image Icon
155         //AZ Image.SCALE_SMOOTH
156         final Image image = icon.getImage().getScaledInstance(height, width, Image.SCALE_SMOOTH);
157 
158         return new ImageIcon(image);
159     }
160 
161     /**
162      * Saves an image file to disk in the location specified.
163      * <p>
164      * @param image the image to save
165      * @param filename the file to save it as
166      * @throws IOException if any error occurs writing the file.
167      */
168     public static File saveImage(final Image image, final String filename)
169                           throws IOException {
170         final int w = image.getHeight(null);
171         final int h = image.getHeight(null);
172         final BufferedImage bi = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
173         final Graphics2D g2 = bi.createGraphics();
174         g2.drawImage(image, 0, 0, null);
175         g2.dispose();
176         final File file = new File(filename);
177         ImageIO.write(bi, "jpg", file);
178         return file;
179     }
180 
181     /**
182      * Saves this image file based on a format from prefs.  The format is in
183      * aFormat and can have values  %y for year, %a for artist, and %d for disc.
184      * Replaces any invalid characters
185      * (\\, /, :, , *, ?, ", <, >, or |) with underscores _ to prevent any
186      * errors on file systems.
187      *
188      * Examples:
189      * %a - %d = Black Crowes - Amorica.jpg
190      * <p>
191      * @param image the image to save
192      * @param aFormat the string format like %a - %d = Black Crowes - Amorica.jpg
193      * @param aDirectory the directory to save the file in.
194      * @param aArtist the name of the artist
195      * @param aDisc the name of the disc
196      * @return true if renamed, false if failure
197      * @throws IOException if any error occurs writing the file out
198      */
199     public static String saveImageWithFileFormat(final Image image,
200                                                  final String aFormat,
201                                                  final String aDirectory,
202                                                  final String aArtist,
203                                                  final String aDisc,
204                                                  final String aYear)
205                                           throws IOException {
206         String result = null;
207         String newFileName = aFormat;
208         newFileName = StringUtils.replace(newFileName, "%y", aYear);
209         newFileName = StringUtils.replace(newFileName, "%a", aArtist);
210         newFileName = StringUtils.replace(newFileName, "%d", aDisc);
211         newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
212         newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
213         newFileName = FileUtil.correctFileName(newFileName);
214         newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
215 
216         result = newFileName;
217 
218         saveImage(image, newFileName);
219 
220         return result;
221     }
222 
223     /** AZ  **/
224     public static String saveImageToUserDefinedDirectory(final File imageFile,
225             final String aArtist,
226             final String aDisc,
227             final String aYear)
228      throws IOException {
229     	String result = null;
230     	String newFileName = settings.getFileFormatImage();
231     	String aDirectory = settings.getImagesLocation().toString();
232     	int size = settings.getCoverSizeLarge();
233 
234     	if (settings.isCopyImagesToDirectory()) {
235     		newFileName = StringUtils.replace(newFileName, "%y", aYear);
236     		newFileName = StringUtils.replace(newFileName, "%a", aArtist);
237     		newFileName = StringUtils.replace(newFileName, "%d", aDisc);
238     		newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
239     		newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
240     		newFileName = FileUtil.correctFileName(newFileName);
241     		newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
242     		ImageIcon imageIcon = getScaledImage(imageFile.toString(), size, size);
243     		Image newImage = imageIcon.getImage();
244     		saveImage(newImage, newFileName);
245     		result = imageFile.getAbsolutePath();
246     	}
247     	else {
248     		result = imageFile.getAbsolutePath();
249     	}
250     	return result;
251     }
252 
253     /**
254      * Method to create a new ImageIcon and return the reference
255      * <p>
256      * @param aFileName filename of icon to load
257      * @return ImageIcon reference of fileName
258      */
259     private static ImageIcon getIcon(final String aFileName) {
260         return new ImageIcon(ClassLoader.getSystemResource(aFileName));
261     }
262     
263     /** AZ  **/
264     public static String standardImageFileName(final String aArtist,
265     											final String aDisc,
266     											final String aYear) {
267        	String newFileName = settings.getFileFormatImage();
268     	String aDirectory = settings.getImagesLocation().toString();
269 
270     		newFileName = StringUtils.replace(newFileName, "%y", aYear);
271     		newFileName = StringUtils.replace(newFileName, "%a", aArtist);
272     		newFileName = StringUtils.replace(newFileName, "%d", aDisc);
273     		newFileName = StringUtils.replace(newFileName, "%A", aArtist.toUpperCase(Locale.US));
274     		newFileName = StringUtils.replace(newFileName, "%D", aDisc.toUpperCase(Locale.US));
275     		newFileName = FileUtil.correctFileName(newFileName);
276     		newFileName = aDirectory + SystemUtils.FILE_SEPARATOR + newFileName + "." + ImageFilter.JPG;
277     		return newFileName;
278     }
279 }    // end class ImageFactory