diff --git a/MekHQ/src/mekhq/MHQStaticDirectoryManager.java b/MekHQ/src/mekhq/MHQStaticDirectoryManager.java index fb3d13845f..04d1e8ce07 100644 --- a/MekHQ/src/mekhq/MHQStaticDirectoryManager.java +++ b/MekHQ/src/mekhq/MHQStaticDirectoryManager.java @@ -23,9 +23,9 @@ import megamek.common.annotations.Nullable; import megamek.common.icons.AbstractIcon; import megamek.common.util.fileUtils.DirectoryItems; +import megamek.common.util.fileUtils.ImageFileFactory; import mekhq.campaign.force.Force; import mekhq.gui.enums.LayeredForceIcon; -import mekhq.gui.utilities.PortraitFileFactory; import java.awt.*; import java.awt.image.BufferedImage; @@ -50,7 +50,6 @@ protected MHQStaticDirectoryManager() { } //endregion Constructors - //region Initialization /** * This initialized all of the directories under this manager @@ -73,7 +72,7 @@ private static void initializeForceIcons() { parseForceIconDirectory = false; try { forceIconDirectory = new DirectoryItems(new File("data/images/force"), - "", PortraitFileFactory.getInstance()); + "", new ImageFileFactory()); } catch (Exception e) { MegaMek.getLogger().error("Could not parse the force icon directory!", e); } @@ -92,7 +91,7 @@ private static void initializeAwardIcons() { parseAwardIconDirectory = false; try { awardIconDirectory = new DirectoryItems(new File("data/images/awards"), - "", PortraitFileFactory.getInstance()); + "", new ImageFileFactory()); } catch (Exception e) { MegaMek.getLogger().error("Could not parse the award icon directory!", e); } diff --git a/MekHQ/src/mekhq/gui/utilities/PortraitFileFactory.java b/MekHQ/src/mekhq/gui/utilities/PortraitFileFactory.java deleted file mode 100644 index df100dcedb..0000000000 --- a/MekHQ/src/mekhq/gui/utilities/PortraitFileFactory.java +++ /dev/null @@ -1,215 +0,0 @@ -/* - * MegaMek - Copyright (C) 2004 Ben Mazur (bmazur@sev.org) - * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the Free - * Software Foundation; either version 2 of the License, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY - * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License - * for more details. - */ -package mekhq.gui.utilities; - -import java.awt.Image; -import java.awt.Toolkit; -import java.awt.image.BufferedImage; -import java.io.BufferedInputStream; -import java.io.File; -import java.io.IOException; -import java.io.InputStream; -import java.util.zip.ZipEntry; -import java.util.zip.ZipFile; - -import javax.imageio.ImageIO; - -import megamek.common.util.ItemFile; -import megamek.common.util.ItemFileFactory; - -/** - * This class will produce Image objects from files. If an image - * file is inside of JAR and ZIP file, then it must save the contents to a - * temporary file.

Created on January 18, 2004 - * - * @author James Damour - * @version 1 - */ -public class PortraitFileFactory implements ItemFileFactory { - - /** - * Accepted image file extensions - */ - private final static String JPG = "JPG", JPEG = "JPEG", GIF = "GIF", PNG = "PNG"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - - /** - * Implement the Singleton pattern. - */ - private PortraitFileFactory() { - } - - private static PortraitFileFactory singleton = null; - - /** - * Get the Singleton ImageFileFactory. - * - * @return the Singleton ImageFileFactory. - */ - public static PortraitFileFactory getInstance() { - if (null == singleton) - singleton = new PortraitFileFactory(); - return singleton; - } - - /** - * Get the ItemFile for the given File. - * - * @param file - the input File object that will be read to - * produce the item. This value must not be null. - * @return an ItemFile for the given file. - * @throws IllegalArgumentException if the file is null. - */ - public ItemFile getItemFile(final File file) throws IllegalArgumentException { - - // Validate the input. - if (null == file) { - throw new IllegalArgumentException("A null image file was passed."); //$NON-NLS-1$ - } - - // Construct an anonymous class that gets an Image for the file. - return new ItemFile() { - - private File itemFile = file; // copy the file entry - private BufferedImage image = null; // cache the Image - - public Object getItem() throws Exception { - // Cache the image on first use. - if (null == image) { - String name = itemFile.getAbsolutePath(); - image = ImageIO.read(new File(name)); - } - // Return a copy of the image. - //return image.getScaledInstance(150, -1, Image.SCALE_DEFAULT); - return image; - } // End getItem() - }; - - } - - /** - * Get the ItemFile for the given ZipEntry in - * the ZipFile. - * - * @param zipEntry - the ZipEntry that will be read to - * produce the item. This value must not be null. - * @param zipFile - the ZipFile object that contains the - * ZipEntry that will produce the item. This value - * must not be null. - * @return an ItemFile for the given zip file entry. - * @throws IllegalArgumentException if either the - * zipEntry or the zipFile is - * null. - */ - public ItemFile getItemFile(final ZipEntry zipEntry, final ZipFile zipFile) - throws IllegalArgumentException { - - // Validate the input. - if (null == zipEntry) { - throw new IllegalArgumentException("A null ZIP entry was passed."); //$NON-NLS-1$ - } - if (null == zipFile) { - throw new IllegalArgumentException("A null ZIP file was passed."); //$NON-NLS-1$ - } - - // Construct an anonymous class that gets an Image for the file. - return new ItemFile() { - - private ZipEntry itemEntry = zipEntry; // copy the ZipEntry - private Image image = null; // cache the Image - - public Object getItem() throws Exception { - - // Cache the image on first use. - if (null == image) { - - // Get ready to read from the item. - InputStream is = new BufferedInputStream(zipFile.getInputStream(itemEntry), - (int) itemEntry.getSize()); - - // Make a buffer big enough to hold the item, - // read from the ZIP file, and write it to temp. - byte[] buffer = new byte[(int) itemEntry.getSize()]; - is.read(buffer); - - //close the input stream - is.close(); - - // Check the last 10 bytes. I've been having - // some problems with incomplete image files, - // and I want to detect it early and give advice - // to players for dealing with the problem. - int index = (int) itemEntry.getSize() - 10; - while (itemEntry.getSize() > index) { - if (0 != buffer[index]) { - break; - } - index++; - } - if (itemEntry.getSize() <= index) { - throw new IOException( - "Error reading " + itemEntry.getName() + //$NON-NLS-1$ - "\nYou may want to unzip " + //$NON-NLS-1$ - zipFile.getName()); - } - - // Create the image from the buffer. - image = Toolkit.getDefaultToolkit().createImage(buffer); - } // End get-image - - // Return a copy of the image. - return image.getScaledInstance(150, 150, Image.SCALE_FAST); - - } // End getItem() - }; - - } - - /** - * The method that must be implemented by any object that filters file names - * (i.e., selects a subset of file names from a list of file names).

This - * definition is copied from java.io.FilenameFilter for - * completeness. - * - * @param dir - the File object of the directory containing - * the named file. - * @param name - the String name of the file. - */ - public boolean accept(File dir, String name) { - - // Convert the file name to upper case, and compare it to image - // file extensions. Yeah, it's a bit of a hack, but whatever. - String ucName = name.toUpperCase(); - return (ucName.endsWith(JPG) || ucName.endsWith(JPEG) - || ucName.endsWith(GIF) || ucName.endsWith(PNG)); - } - - /** - * The method that must be implemented by any object that filters file names - * within a ZipFile (i.e., selects a subset of file names from - * a list of file names in a ZIP archive). - * - * @param zipFile - the ZipFile object that contains the - * named file's entry. - * @param name - the String name of the file. - */ - public boolean accept(ZipFile zipFile, String name) { - - // Convert the file name to upper case, and compare it to image - // file extensions. Yeah, it's a bit of a hack, but whatever. - String ucName = name.toUpperCase(); - return (ucName.endsWith(JPG) || ucName.endsWith(JPEG) - || ucName.endsWith(GIF) || ucName.endsWith(PNG)); - } - -}