Here you can find the source of checkLosslessImageExt(Component parent, File f)
Parameter | Description |
---|---|
parent | the parent component |
f | the file |
public static File checkLosslessImageExt(Component parent, File f)
//package com.java2s; /*/*from w ww . jav a 2 s . c o m*/ * ArikTools * Copyright (C) Arik Z.Lakritz, Peter Macko, and David K. Wittenberg * * This file is part of ArikTools. * * ArikTools 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 3 of the License, or * (at your option) any later version. * * ArikTools 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. * * You should have received a copy of the GNU General Public License * along with ArikTools. If not, see <http://www.gnu.org/licenses/>. */ import java.awt.*; import java.io.*; import javax.swing.*; public class Main { private static final String[] SUPPORTED_IMAGE_FORMATS = { "png", "jpg", "jpeg", "gif" }; private static final String[] SUPPORTED_IMAGE_LOSSLESS_FORMATS = { "png" }; /** * Optionally validate the file name to have an extension of a lossless image format. * If the initial file name does not have such extension, ask the user. * * @param parent the parent component * @param f the file * @return the optionally changed file reference */ public static File checkLosslessImageExt(Component parent, File f) { if (f == null) return null; String ext = getExtension(f); // No extension if (ext == null) { Object[] options = { "Use .png", "No Extension" }; String msg = "<html><body><b>Are you sure to not use a file extension?</b><br><br>" + "An image file without a proper extension<br>may not be opened correctly by other programs.</body></html>"; int n = JOptionPane.showOptionDialog(parent, msg, "Save Image", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if (n == 0) f = new File(f.getAbsolutePath() + ".png"); return f; } // Unsupported extension ext = ext.toLowerCase(); boolean unsupported = true; for (int i = 0; i < SUPPORTED_IMAGE_FORMATS.length; i++) { if (ext.equals(SUPPORTED_IMAGE_FORMATS[i])) { unsupported = false; break; } } if (unsupported) { Object[] options = { "Use .png", "Cancel" }; String msg = "<html><body><b>The image format \"" + ext + "\" is not supported.</b><br><br>" + "Would you like to save the image as \"png\" instead?</body></html>"; int n = JOptionPane.showOptionDialog(parent, msg, "Save Image", JOptionPane.YES_NO_OPTION, JOptionPane.ERROR_MESSAGE, null, options, options[0]); if (n == 0) { String str = f.getAbsolutePath(); return new File(str.substring(0, str.lastIndexOf('.')) + ".png"); } else { return null; } } // Not recommended extension ext = ext.toLowerCase(); boolean notRecommended = true; for (int i = 0; i < SUPPORTED_IMAGE_LOSSLESS_FORMATS.length; i++) { if (ext.equals(SUPPORTED_IMAGE_LOSSLESS_FORMATS[i])) { notRecommended = false; break; } } if (notRecommended) { Object[] options = { "Use .png", "Keep ." + ext }; String msg = "<html><body><b>Are you sure to save the file as \"" + ext + "\"?</b><br><br>" + "The file format you have selected is not recommended,<br>" + "because it does not employ lossless compression on<br>" + "RGB images with 24-bit color depth.</body></html>"; int n = JOptionPane.showOptionDialog(parent, msg, "Save Image", JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); if (n == 0) { String str = f.getAbsolutePath(); return new File(str.substring(0, str.lastIndexOf('.')) + ".png"); } return f; } // It's okay return f; } /** * Get the extension of a file. * The code is based on http://java.sun.com/docs/books/tutorial/uiswing/components/examples/Utils.java * * @param f the file * @return the file extension */ public static String getExtension(File f) { String ext = null; String s = f.getName(); int i = s.lastIndexOf('.'); if ((i > 0) && (i < s.length() - 1)) { ext = s.substring(i + 1); } return ext; } }