⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fileaction.java

📁 用Java开发的、实现类似Visio功能的应用程序源码
💻 JAVA
字号:
/**
 *    $Id:FileAction.java $
 *
 *    Copyright 2004 ~ 2005  JingFei International Cooperation LTD. All rights reserved. *
 */
package com.jfimagine.jfdraw.action;

import java.io.File;

import java.awt.Dimension;
import java.awt.Component;
import java.awt.image.BufferedImage;

import javax.swing.JFileChooser;
import javax.swing.JDesktopPane;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;

import com.jfimagine.jfdraw.gui.DrawAdapter;
import com.jfimagine.jfdraw.gui.DrawFrame;
import com.jfimagine.jfdraw.gui.DrawPane;
import com.jfimagine.jfdraw.gui.DrawCanvas;
import com.jfimagine.jfdraw.gui.resource.CADResource;
import com.jfimagine.jfdraw.gui.GlobalSettings;
import com.jfimagine.jfdraw.gui.GUIUtils;
import com.jfimagine.jfdraw.draw.Selection;

import com.jfimagine.jfgraph.transfer.*;
import com.jfimagine.jfgraph.shape.union.JFLayer;
import com.jfimagine.jfgraph.shape.union.JFPage;
import com.jfimagine.jfgraph.transfer.JFFileFilter;

import com.jfimagine.utils.commonutil.CommonUtil;
 
 /**
 * File Action class. Used to process new file, open file,save file, save as file and other file related operations.
 *
 * @author     CookieMaker    
 *
 * @version $Revision: 1.00 $
 */  
 public class FileAction {

 	/** A DrawAdapter panel(CADPane or DrawPane) to dispatch events */
    	private DrawAdapter m_pane;

    	
    	/** Constructor */
    	public FileAction(DrawAdapter pane){
    		this.m_pane	=pane;
    	}

    	/** new file operation.
    	 *  This operation should generate a new drawing page.
    	 */
    	public void newFile(){ 
    		m_pane.newFile();
    	}
    		

    	/** Save file operation.
    	 *  This operation should save current selected document.
    	 */
    	public boolean saveFile(){
    		//get a last opened file name
    		String fileName	=m_pane.getFileName();
    		
    		if (fileName!=null && !fileName.equals("")){
			if (CommonUtil.isURLFile(fileName))
				fileName	="";
    		}
    		
    		if (fileName==null || fileName.equals("")){
    		    fileName	=getOutputFileName(m_pane,JFFileFilter.FILEEXT_DRAW,JFFileFilter.FILEDESC_DRAW);
    		}
		
		return m_pane.saveFile(fileName);    
	}


    	/** Save as a new binary file operation.
    	 *  This operation should save current selected document to a new file.
    	 */
    	public boolean saveasFile(){
    		//get a new output file name
    		String fileName	=getOutputFileName(m_pane,JFFileFilter.FILEEXT_DRAW,JFFileFilter.FILEDESC_DRAW);
		return m_pane.saveFile(fileName);      		
	}


    	/** Save as a new xml file operation.
    	 *  This operation should save current selected document to a new file.
    	 */
    	public boolean saveasXMLFile(){
    		//get a new output file name
    		String fileName	=getOutputFileName(m_pane,JFFileFilter.FILEEXT_DRAW_XML,JFFileFilter.FILEDESC_DRAW_XML);
		return m_pane.saveFile(fileName);      		
	}


	/** get a new output file name from a new FileChooser dialog*/
	public static String getOutputFileName(Component c,String ext,String desc){
    		GlobalSettings m_settings	=GlobalSettings.getInstance();

		JFileChooser fc=new JFileChooser(m_settings.getGlobalPath());
 		JFFileFilter filter = new JFFileFilter();
    		filter.addExtension(ext);
    		filter.setDescription(desc);
    		fc.setFileFilter(filter);
		
                //show dialog
                int fd = fc.showSaveDialog(c);
		if(fd==JFileChooser.APPROVE_OPTION){
            		String fileName	=fc.getSelectedFile().getAbsolutePath();
            		String fileExt	="."+ext;		 
			
			if (!fileName.endsWith(fileExt)){
            			fileName	+=fileExt;
            		}
            		boolean exists = (new File(fileName)).exists();
			if (exists) {
                		int n = JOptionPane.showConfirmDialog(
                                		null,
                                		CADResource.getString("savefile.fileexists"),
                                		CADResource.getString("sys.warn"),
                                		JOptionPane.YES_NO_OPTION);
                                   
               			if (n == JOptionPane.NO_OPTION) return null;
			}
			return fileName;
            	}else{
            		return null;
            	}
	}


    	/** Open file operation.
    	 *  This operation will open a new file in a new internal frame.
    	 */
    	public boolean openFile(){

		//consider the path of a currently opened file.
		String fileName=m_pane.getFileName();
                
                GlobalSettings m_settings	=GlobalSettings.getInstance();
		String currDir=m_settings.getGlobalPath();
		if (fileName!=null && fileName.length()>0){
			currDir =fileName;
		}
		
		JFileChooser fc=new JFileChooser(currDir);
 		JFFileFilter filter = new JFFileFilter();
    		filter.addExtension(JFFileFilter.FILEEXT_DRAW); 
    		//* trial version
    		filter.addExtension(JFFileFilter.FILEEXT_DRAW_XML);
    		//*/
    		filter.setDescription(JFFileFilter.FILEDESC_DRAW);
    		fc.setFileFilter(filter);
		
                //show dialog
                int fd = fc.showOpenDialog(m_pane);
		if(fd==JFileChooser.APPROVE_OPTION){
            		fileName	=fc.getSelectedFile().getAbsolutePath();
            	}else{
            		return false;
        	}

        	if (m_pane.openFile(fileName)){
        		m_settings.setGlobalPath(fileName);
        		return true;
        	}else{
        		return false;
        	}
        }
        

    	/** Open URL file operation.
    	 *  This operation will open an URL file from remote machine.
    	 */
    	public boolean openURLFile(){
		String fileName =JOptionPane.showInputDialog(CADResource.getString("label.filetype.web.jfdraw"), "http://");

	
		return m_pane.openFile(fileName);		
    	}


    	/** Exporting current page to another file format.
    	 *  @param cmd An exporting command.
    	 */
    	public boolean exportingFile(String cmd){

    		boolean isSelection	=false;
		ExportBase export	=null;
		
    		//exporting full pages..
		if (ActionConst.CMD_EXPORTING_JPG.equals(cmd)){//exporting full page to jpg file
			export		=new ExportJPG();
		}else if (ActionConst.CMD_EXPORTING_JPG_SELECTION.equals(cmd)){  //exporting selection to jpg file
			export		=new ExportJPG();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_GIF.equals(cmd)){//exporting full page to gif file
			export		=new ExportGIF();
		}else if (ActionConst.CMD_EXPORTING_GIF_SELECTION.equals(cmd)){  //exporting selection to gif file
			export		=new ExportGIF();
	        	isSelection	=true;
	        
		}else if (ActionConst.CMD_EXPORTING_PNG.equals(cmd)){//exporting full page to gpng file
			export		=new ExportPNG();
		}else if (ActionConst.CMD_EXPORTING_PNG_SELECTION.equals(cmd)){  //exporting selection to png file
			export		=new ExportPNG();
			isSelection	=true;
    		

		}else if (ActionConst.CMD_EXPORTING_EPS.equals(cmd)){//exporting full page to eps file
			export		=new ExportEPS();
		}else if (ActionConst.CMD_EXPORTING_EPS_SELECTION.equals(cmd)){  //exporting selection to eps file
			export		=new ExportEPS();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_SVG.equals(cmd)){//exporting full page to svg file
			export		=new ExportSVG();
		}else if (ActionConst.CMD_EXPORTING_SVG_SELECTION.equals(cmd)){  //exporting selection to svg file
			export		=new ExportSVG();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_SWF.equals(cmd)){//exporting full page to swf file
			export		=new ExportSWF();
		}else if (ActionConst.CMD_EXPORTING_SWF_SELECTION.equals(cmd)){  //exporting selection to swf file
			export		=new ExportSWF();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_CGM.equals(cmd)){//exporting full page to cgm file
			export		=new ExportCGM();
		}else if (ActionConst.CMD_EXPORTING_CGM_SELECTION.equals(cmd)){  //exporting selection to cgm file
			export		=new ExportCGM();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_EMF.equals(cmd)){//exporting full page to emf file
			export		=new ExportEMF();
		}else if (ActionConst.CMD_EXPORTING_EMF_SELECTION.equals(cmd)){  //exporting selection to emf file
			export		=new ExportEMF();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_PDF.equals(cmd)){//exporting full page to pdf file
			export		=new ExportPDF();
		}else if (ActionConst.CMD_EXPORTING_PDF_SELECTION.equals(cmd)){  //exporting selection to pdf file
			export		=new ExportPDF();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_PPM.equals(cmd)){//exporting full page to ppm file
			export		=new ExportPPM();
		}else if (ActionConst.CMD_EXPORTING_PPM_SELECTION.equals(cmd)){  //exporting selection to ppm file
			export		=new ExportPPM();
			isSelection	=true;

		}else if (ActionConst.CMD_EXPORTING_TIFF.equals(cmd)){//exporting full page to tiff file
			export		=new ExportTIFF();
		}else if (ActionConst.CMD_EXPORTING_TIFF_SELECTION.equals(cmd)){  //exporting selection to tiff file
			export		=new ExportTIFF();
			isSelection	=true;
		}else
			return false;
			
		
		return exportingFile(export,isSelection,null);
	}		

    	/** Exporting current page to another file format.
    	 *  @param export An exporting object
    	 *  @param isSelection If export a selection
    	 *  @param fileName Exporting file name, null if need to be specified later
    	 */
    	public boolean exportingFile(ExportBase export,boolean isSelection,String fileName){
					
		
		//get drawing page or selection
                DrawCanvas canvas	=m_pane.getDrawCanvas();
                if (canvas==null)
                	return false;
                	
                JFPage page		=canvas.getDrawPage();
                Selection selection	=canvas.getSelection();
                if (isSelection && selection.size()==0){
			JOptionPane.showMessageDialog(null, CADResource.getString("invalid.shapeObject"), CADResource.getString("sys.warn"), JOptionPane.ERROR_MESSAGE); 
                	return false;
                }

		//get file name		
		if (fileName==null || fileName.equals(""))
			fileName =getOutputFileName(m_pane,export.getExportFileExt(),export.getExportFileDesc()); 
		if (fileName==null || fileName.equals(""))
			return false;
		
		//get a generated image from current drawing page
                BufferedImage image	=null;
		
		if (!isSelection){
			GlobalSettings m_settings	=GlobalSettings.getInstance();
                        export.setPage( page,true,1.0,
                        		m_settings.isExportWithGrid(),
                        		m_settings.isExportWithRuler(),
                        		canvas.isMetric(),
                        		canvas.getGridFormat());
			export.export(fileName);				                          	       
                }else{
                        export.setList( selection.getList(),true,1.0);
			export.export(fileName);				                          	       
                }
                
		return true;    		
	}

	
	 	
 }

⌨️ 快捷键说明

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