dataimporter.java

来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 168 行

JAVA
168
字号
/*====================================================================*\DataImporter.javaData importer class.------------------------------------------------------------------------This file is part of FuncPlotter, a combined Java application and appletfor plotting explicit functions in one variable.Copyright 2005-2007 Andy Morgan-Richards.FuncPlotter is free software: you can redistribute it and/or modify itunder the terms of the GNU General Public License as published by theFree Software Foundation, either version 3 of the License, or (at youroption) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public License alongwith this program.  If not, see <http://www.gnu.org/licenses/>.\*====================================================================*/// PACKAGEpackage util;//----------------------------------------------------------------------// IMPORTSimport java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.datatransfer.UnsupportedFlavorException;import java.io.File;import java.io.IOException;import java.net.URI;import java.net.URISyntaxException;import java.util.ArrayList;import java.util.List;//----------------------------------------------------------------------// DATA IMPORTER CLASSpublic class DataImporter{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private static final    String      FILE_SCHEME_STR     = "file";    private static final    DataFlavor  URI_LIST_FLAVOUR    = getUriListFlavour( );//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    private DataImporter( )    {    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static boolean isString( DataFlavor[] flavours )    {        for ( DataFlavor flavour : flavours )        {            if ( flavour.equals( DataFlavor.stringFlavor ) )                return true;        }        return false;    }    //------------------------------------------------------------------    public static boolean isFileList( DataFlavor[] flavours )    {        for ( DataFlavor flavour : flavours )        {            if ( flavour.isFlavorJavaFileListType( ) || flavour.equals( URI_LIST_FLAVOUR ) )                return true;        }        return false;    }    //------------------------------------------------------------------    public static File[] getFiles( Transferable transferable )        throws IOException, UnsupportedFlavorException    {        List<File> files = new ArrayList<File>( );        if ( isFileList( transferable.getTransferDataFlavors( ) ) )        {            if ( transferable.isDataFlavorSupported( DataFlavor.javaFileListFlavor ) )            {                Object transferData = transferable.getTransferData( DataFlavor.javaFileListFlavor );                if ( transferData instanceof List )                {                    for ( Object obj : (List)transferData )                    {                        if ( obj instanceof File )                            files.add( (File)obj );                    }                }            }            else            {                String[] strs =                            ((String)transferable.getTransferData( URI_LIST_FLAVOUR )).split( "[\\r\\n]+" );                for ( String str : strs )                {                    try                    {                        URI uri = new URI( str );                        if ( FILE_SCHEME_STR.equals( uri.getScheme( ) ) )                            files.add( new File( uri ) );                    }                    catch ( URISyntaxException e )                    {                        System.err.println( e );                    }                }            }        }        return ( files.isEmpty( ) ? null : files.toArray( new File[files.size( )] ) );    }    //------------------------------------------------------------------    private static DataFlavor getUriListFlavour( )    {        DataFlavor flavour = null;        try        {            flavour = new DataFlavor( "text/uri-list;class=java.lang.String" );        }        catch ( ClassNotFoundException e )        {            e.printStackTrace( );        }        return flavour;    }    //------------------------------------------------------------------}//----------------------------------------------------------------------

⌨️ 快捷键说明

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