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

📄 functiondialog.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*====================================================================*\FunctionDialog.javaFunction dialog box 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/>.\*====================================================================*/// IMPORTSimport exception.AppException;import gui.ColourSampleIcon;import gui.GuiUtilities;import java.awt.Color;import java.awt.Dialog;import java.awt.Dimension;import java.awt.Graphics;import java.awt.GridBagConstraints;import java.awt.GridBagLayout;import java.awt.GridLayout;import java.awt.Insets;import java.awt.Point;import java.awt.Window;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.FlavorEvent;import java.awt.datatransfer.FlavorListener;import java.awt.datatransfer.StringSelection;import java.awt.datatransfer.UnsupportedFlavorException;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import java.io.IOException;import javax.swing.BorderFactory;import javax.swing.JButton;import javax.swing.JColorChooser;import javax.swing.JComponent;import javax.swing.JDialog;import javax.swing.JLabel;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.KeyStroke;import textfield.ConstrainedTextField;import textfield.TextFieldConstants;import util.KeyAction;//----------------------------------------------------------------------// FUNCTION DIALOG BOX CLASSclass FunctionDialog    extends JDialog    implements ActionListener, FlavorListener{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private static final    int     COLOUR_BUTTON_ICON_WIDTH    = 40;    private static final    int     COLOUR_BUTTON_ICON_HEIGHT   = 16;    private static final    Insets  COLOUR_BUTTON_MARGINS       = new Insets( 2, 2, 2, 2 );    private static final    int EXPRESSION_FIELD_LENGTH = 512;    private static final    String  COLOUR_STR          = "Colour:";    private static final    String  EXPRESSION_STR      = "Expression:";    private static final    String  COPY_STR            = "Copy";    private static final    String  PASTE_STR           = "Paste";    private static final    String  CLEAR_STR           = "Clear";    private static final    String  COLOUR_TITLE_STR    = "Select Colour of Plotted Function";    private static final    String  COPY_TOOL_TIP_STR   = "Copy expression to clipboard";    private static final    String  PASTE_TOOL_TIP_STR  = "Paste text from clipboard";    private static final    String  CLEAR_TOOL_TIP_STR  = "Clear expression (Ctrl+Delete)";    private static final    String  CLIPBOARD_ERROR_STR = "Clipboard Error";    private static final    String  SYNTAX_ERROR_STR    = "Syntax Error";    // Commands    private interface Command    {        String  CHOOSE_COLOUR           = "chooseColour";        String  SET_TO_FUNCTION_COLOUR  = "setToFunctionColour.";        String  COPY                    = "copy";        String  PASTE                   = "paste";        String  CLEAR                   = "clear";        String  ACCEPT                  = "accept";        String  CLOSE                   = "close";    }    private static final    KeyAction.CommandMap[]  KEY_COMMANDS    =    {        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_DELETE, KeyEvent.CTRL_DOWN_MASK ),                                  Command.CLEAR ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_ESCAPE, 0 ),                                  Command.CLOSE )    };//////////////////////////////////////////////////////////////////////////  Enumeration types////////////////////////////////////////////////////////////////////////    // ERROR IDENTIFIERS    private enum ErrorId        implements AppException.Id    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        CLIPBOARD_UNAVAILABLE        ( "The clipboard is currently unavailable." ),        NO_TEXT_ON_CLIPBOARD        ( "There is no text on the clipboard." ),        FAILED_TO_GET_CLIPBOARD_DATA        ( "Failed to get data from the clipboard." );    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ErrorId( String message )        {            this.message = message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : AppException.Id interface    ////////////////////////////////////////////////////////////////////        public String getMessage( )        {            return message;        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance variables    ////////////////////////////////////////////////////////////////////        private String  message;    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Member classes : non-inner classes////////////////////////////////////////////////////////////////////////    // COLOUR BUTTON CLASS    private static class ColourButton        extends JButton    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    int WIDTH   = 16;        private static final    int HEIGHT  = 16;        private static final    Color   BORDER_COLOUR           = Color.GRAY;        private static final    Color   FOCUSED_BORDER_COLOUR   = Color.WHITE;    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private ColourButton( Color colour )        {            setBorder( BorderFactory.createEmptyBorder( ) );            setForeground( colour );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public Dimension getPreferredSize( )        {            return new Dimension( WIDTH, HEIGHT );        }        //--------------------------------------------------------------        protected void paintComponent( Graphics gr )        {            // Fill interior            gr.setColor( getForeground( ) );            gr.fillRect( 0, 0, getWidth( ), getHeight( ) );            // Draw border            gr.setColor( isFocusOwner( ) ? FOCUSED_BORDER_COLOUR : BORDER_COLOUR );            gr.drawRect( 0, 0, getWidth( ) - 1, getHeight( ) - 1 );        }        //--------------------------------------------------------------    }    //==================================================================    // EXPRESSION FIELD CLASS    private static class ExpressionField        extends ConstrainedTextField    {    ////////////////////////////////////////////////////////////////////    //  Constants    ////////////////////////////////////////////////////////////////////        private static final    int NUM_COLUMNS = 80;        private static final    String  VALID_CHARS = " .+-*/\\%^()";    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        public ExpressionField( int maxLength )        {            super( maxLength, NUM_COLUMNS );            AppFont.TEXT_FIELD.setFont( this );            GuiUtilities.setHorizontalMargins( this, AppConstants.TEXT_FIELD_MARGIN );        }        //--------------------------------------------------------------        public ExpressionField( int    maxLength,                                String text )        {            this( maxLength );            setText( text );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        protected int getColumnWidth( )        {            return ( GuiUtilities.getCharWidth( '0', getFontMetrics( getFont( ) ) ) );        }        //--------------------------------------------------------------        protected String translateInsertString( String str,                                                int    offset )        {            return str.toLowerCase( );        }        //--------------------------------------------------------------        protected char validateCharacter( char ch,                                          int  index )        {            if ( ((ch >= '0') && (ch <= '9')) || ((ch >= 'a') && (ch <= 'z')) )                return ch;            return ( (VALID_CHARS.indexOf( ch ) < 0) ? TextFieldConstants.INVALID_CHAR : ch );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public Expression getExpression( )            throws Expression.Exception        {            return new Expression( getText( ) );        }        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Member classes : inner classes////////////////////////////////////////////////////////////////////////    // WINDOW EVENT HANDLER CLASS    private class WindowEventHandler        extends WindowAdapter    {    ////////////////////////////////////////////////////////////////////    //  Constructors    ////////////////////////////////////////////////////////////////////        private WindowEventHandler( )        {        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods : overriding methods    ////////////////////////////////////////////////////////////////////        public void windowClosing( WindowEvent event )        {            doClose( );        }        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    private FunctionDialog( Window owner,                            String titleStr,                            Color  colour,                            String expression )    {        super( owner, titleStr, Dialog.ModalityType.APPLICATION_MODAL );        init( owner, colour, expression );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static FunctionDialog showDialog( Window owner,                                             String titleStr,                                             Color  colour,                                             String expression )    {        return new FunctionDialog( owner, titleStr, colour, expression );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : ActionListener interface////////////////////////////////////////////////////////////////////////    public void actionPerformed( ActionEvent event )    {        try        {            String command = event.getActionCommand( );            String methodName = AppConstants.COMMAND_METHOD_PREFIX +                                        command.substring( 0, 1 ).toUpperCase( ) + command.substring( 1 );            int index = methodName.indexOf( '.' );            if ( index < 0 )                Util.getDeclaredMethod( getClass( ), methodName, false ).invoke( this );            else                Util.getDeclaredMethod( getClass( ), methodName.substring( 0, index ), true ).                                                        invoke( this, methodName.substring( index + 1 ) );        }        catch ( Exception e )        {            e.printStackTrace( );        }    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : FlavourListener interface////////////////////////////////////////////////////////////////////////    public void flavorsChanged( FlavorEvent event )    {        try        {            pasteButton.setEnabled( getToolkit( ).getSystemClipboard( ).                                                        isDataFlavorAvailable( DataFlavor.stringFlavor ) );        }        catch ( Exception e )        {            // ignore        }    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    public boolean isAccepted( )    {        return accepted;    }    //------------------------------------------------------------------    public Color getColour( )    {        return colourButton.getForeground( );    }    //------------------------------------------------------------------    public Expression getExpression( )    {        Expression expression = null;        try        {            expression = expressionField.getExpression( );        }        catch ( Expression.Exception e )        {            e.printStackTrace( );        }        return expression;

⌨️ 快捷键说明

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