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

📄 pathnamefield.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\PathnameField.javaPathname field 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 textfield;//----------------------------------------------------------------------// IMPORTSimport java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.Transferable;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.File;import java.io.IOException;import java.util.ArrayList;import java.util.List;import javax.swing.JComponent;import javax.swing.JTextField;import javax.swing.TransferHandler;import javax.swing.text.BadLocationException;import javax.swing.text.Document;import javax.swing.text.Position;import util.DataImporter;import util.PropertyString;//----------------------------------------------------------------------// PATHNAME FIELD CLASSpublic class PathnameField    extends JTextField{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    public interface Command    {        String  TRANSFER_FILE   = "transferFile";    }//////////////////////////////////////////////////////////////////////////  Member classes : non-inner classes////////////////////////////////////////////////////////////////////////    // FILE TRANSFER HANDLER CLASS    private static class FileTransferHandler        extends TransferHandler    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    String  DATA_TRANSFER_ERROR_STR = "Data Transfer Error";    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private FileTransferHandler( )        {        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public boolean canImport( JComponent   component,                                  DataFlavor[] flavours )        {            if ( DataImporter.isFileList( flavours ) )                return true;            PathnameField field = (PathnameField)component;            return ( (field.getOldTransferHandler( ) == null)                                        ? false                                        : field.getOldTransferHandler( ).canImport( component, flavours ) );        }        //--------------------------------------------------------------        public boolean importData( JComponent   component,                                   Transferable transferable )        {            PathnameField field = (PathnameField)component;            if ( !DataImporter.isString( transferable.getTransferDataFlavors( ) ) &&                 DataImporter.isFileList( transferable.getTransferDataFlavors( ) ) )            {                try                {                    File[] files = DataImporter.getFiles( transferable );                    if ( files != null )                    {                        field.setFile( files[0] );                        field.fireActionPerformed( new ActionEvent( field, ActionEvent.ACTION_PERFORMED,                                                                    Command.TRANSFER_FILE ) );                        return true;                    }                }                catch ( Exception e )                {                    e.printStackTrace( );                }                return false;            }            return ( (field.getOldTransferHandler( ) == null)                                ? false                                : field.getOldTransferHandler( ).importData( component, transferable ) );        }        //--------------------------------------------------------------        public int getSourceActions( JComponent component )        {            return COPY_OR_MOVE;        }        //--------------------------------------------------------------        protected Transferable createTransferable( JComponent component )        {            Transferable transferable = null;            PathnameField field = (PathnameField)component;            int start = field.getSelectionStart( );            int end = field.getSelectionEnd( );            if ( start != end )            {                try                {                    Document document = field.getDocument( );                    p0 = document.createPosition( start );                    p1 = document.createPosition( end );                    transferable = new StringSelection( field.getSelectedText( ) );                }                catch ( BadLocationException e )                {                    e.printStackTrace( );                }            }            return transferable;        }        //--------------------------------------------------------------        protected void exportDone( JComponent   component,                                   Transferable data,                                   int          action )        {            if ( (action == MOVE) && (p0 != null) && (p1 != null) && (p0.getOffset( ) != p1.getOffset( )) )            {                try                {                    PathnameField field = (PathnameField)component;                    field.getDocument( ).remove( p0.getOffset( ), p1.getOffset( ) - p0.getOffset( ) );                }                catch ( BadLocationException e )                {                    e.printStackTrace( );                }            }        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private Position    p0;        private Position    p1;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public PathnameField( int numColumns )    {        super( numColumns );        oldTransferHandler = getTransferHandler( );        if ( transferHandler == null )            transferHandler = new FileTransferHandler( );        setTransferHandler( transferHandler );        actionListeners = new ArrayList<ActionListener>( );    }    //------------------------------------------------------------------    public PathnameField( String pathname,                          int    numColumns )    {        this( numColumns );        setText( pathname );    }    //------------------------------------------------------------------    public PathnameField( File file,                          int  numColumns )    {        this( numColumns );        if ( file != null )            setFile( file );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    protected static String getPathname( File file )    {        try        {            return ( (file == null) ? null : file.getCanonicalPath( ) );        }        catch ( IOException e )        {            System.err.println( e );            return file.getAbsolutePath( );        }    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    public String getPathname( )    {        return PropertyString.parsePathname( getText( ) );    }    //------------------------------------------------------------------    public File getFile( )    {        return new File( getPathname( ) );    }    //------------------------------------------------------------------    public File getCanonicalFile( )    {        File file = getFile( );        try        {            return file.getCanonicalFile( );        }        catch ( IOException e )        {            System.err.println( e );            return file.getAbsoluteFile( );        }    }    //------------------------------------------------------------------    public boolean isEmpty( )    {        return ( getText( ).length( ) == 0 );    }    //------------------------------------------------------------------    public void setFile( File file )    {        setFile( file, false );    }    //------------------------------------------------------------------    public void setFile( File    file,                         boolean unix )    {        String pathname = getPathname( file );        if ( unix && (pathname != null) )        {            try            {                String userHome = System.getProperty( "user.home" );                if ( (userHome != null) && pathname.startsWith( userHome ) )                    pathname = "~" + pathname.substring( userHome.length( ) );            }            catch ( SecurityException e )            {                // ignore            }            if ( File.separatorChar != '/' )                pathname = pathname.replace( File.separatorChar, '/' );        }        setText( pathname );    }    //------------------------------------------------------------------    public void addActionListener( ActionListener listener )    {        actionListeners.add( listener );    }    //------------------------------------------------------------------    protected void fireActionPerformed( ActionEvent event )    {        for ( int i = actionListeners.size( ) - 1; i >= 0; --i )            actionListeners.get( i ).actionPerformed( event );    }    //------------------------------------------------------------------    private TransferHandler getOldTransferHandler( )    {        return oldTransferHandler;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class variables////////////////////////////////////////////////////////////////////////    private static  TransferHandler transferHandler;//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    private TransferHandler         oldTransferHandler;    private List<ActionListener>    actionListeners;}//----------------------------------------------------------------------

⌨️ 快捷键说明

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