filedialog.java

来自「用Java开发的、实现类似Visio功能的应用程序源码」· Java 代码 · 共 145 行

JAVA
145
字号
/**
 *    $Id:FileDialog.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.gui.dialog;
 
import java.io.File;
import java.awt.Component;

import javax.swing.JFileChooser;
import com.jfimagine.jfgraph.transfer.JFFileFilter;

import com.jfimagine.jfdraw.gui.resource.CADResource;

import com.jfimagine.utils.log.*; 

 /**
 * FileDialog class.  A class used to get a file dialog for choosing a new file name with its absolute directory.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
public class FileDialog {


   /**an internal log utility*/
   private static JFLogger m_logger=JFLogManager.getLogger("com.jfimagine.jfdraw.gui.dialog.FileDialog");
   	
    /**
     * Get a new output file name to save.
     *
     * @param c Parent component to place the dialog.
     * @return a new file name
     */
    public static File getOutputFile(Component c) {
		JFileChooser m_fileChooser=new JFileChooser(System.getProperty("user.dir"));
		
                //show dialog
                int fd = m_fileChooser.showSaveDialog(c);
                try{
			if(fd==JFileChooser.APPROVE_OPTION){
				return m_fileChooser.getSelectedFile();
                        }
                        
                 }catch(Exception ex){
                 	m_logger.error("getOutputFile: "+ex);
                 }    	
                 
                 return null;
    }    


    /**
     * Get a new file name to open.
     *
     * @param c Parent component to place the dialog.
     * @return a new file name
     */
    public static File getInputFile(Component c) {
    	return getInputFile(c,null);
    }


    /**
     * Get a new file name to open.
     *
     * @param c Parent component to place the dialog.
     * @param filter A file filter to restrict file types.
     * @return a new file name
     */
    public static File getInputFile(Component c,JFFileFilter filter) {
		JFileChooser m_fileChooser=new JFileChooser(System.getProperty("user.dir"));

                //show dialog
                if (filter!=null)
                	m_fileChooser.setFileFilter(filter);
                int fd = m_fileChooser.showOpenDialog(c);
                try{
			if(fd==JFileChooser.APPROVE_OPTION){
				return m_fileChooser.getSelectedFile();
                        }
                        
                 }catch(Exception ex){
                 	m_logger.error("getInputFile: "+ex);
                 }    	
                 
                 return null;
    }    


    /**
     * Check if a file is an allowed image file.
     *
     * @param File A file to be check.
     * @return True if is image, false otherwise.
     */
    public static boolean isImageFile(File imageFile) {
    	if (imageFile==null)
    		return false;
    	
    	return isImageFile(imageFile.getName());
    }

    /**
     * Check if a file is an allowed image file.
     *
     * @param imageFile An image file name to be check.
     * @return True if is image, false otherwise.
     */
    public static boolean isImageFile(String imageFile) {
    	if (imageFile==null || imageFile.length()==0)
    		return false;
    	
    	if (imageFile!=null){
    		imageFile	=imageFile.toLowerCase();
    		if (imageFile.endsWith(".gif") || imageFile.endsWith(".jpg") || imageFile.endsWith(".jpeg") || imageFile.endsWith(".png"))
    			return true;
    		else
    			return false;
	}
	else 
		return false;
    }	


    /**
     * Get a image file filter.
     * @return The image file filter
     */
    public static JFFileFilter getImageFileFilter() {
 	JFFileFilter filter = new JFFileFilter();
    	filter.addExtension("gif"); 
    	filter.addExtension("jpg"); 
    	filter.addExtension("jpeg"); 
    	filter.addExtension("png"); 
    	filter.setDescription("All image files");
    	return filter;
    }



}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?