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

📄 abstractspinner.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\AbstractSpinner.javaAbstract spinner 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 gui;//----------------------------------------------------------------------// IMPORTSimport java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.FocusEvent;import java.awt.event.FocusListener;import java.awt.event.KeyEvent;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import javax.swing.JComponent;import javax.swing.JSpinner;import javax.swing.KeyStroke;import javax.swing.SpinnerModel;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import util.InputModifiers;import util.KeyAction;//----------------------------------------------------------------------// ABSTRACT SPINNER CLASSpublic abstract class AbstractSpinner    extends JSpinner    implements ActionListener, ChangeListener, FocusListener, MouseWheelListener{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    protected interface Command    {        String  DECREMENT   = "decrement.";        String  INCREMENT   = "increment.";        String  COMMIT_EDIT = "commitEdit";    }//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    protected AbstractSpinner( SpinnerModel model,                               boolean      alwaysUpdate )    {        // Call base-class constructor        super( model );        // Initialise instance variables        this.alwaysUpdate = alwaysUpdate;        // Add listeners        addChangeListener( this );        addMouseWheelListener( this );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : ActionListener interface////////////////////////////////////////////////////////////////////////    public void actionPerformed( ActionEvent event )    {        try        {            String command = event.getActionCommand( );            String methodName = Constants.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 : ChangeListener interface////////////////////////////////////////////////////////////////////////    public void stateChanged( ChangeEvent event )    {        updateEditorValue( );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : FocusListener interface////////////////////////////////////////////////////////////////////////    public void focusGained( FocusEvent event )    {        // do nothing    }    //------------------------------------------------------------------    public void focusLost( FocusEvent event )    {        updateValue( );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : MouseWheelListener interface////////////////////////////////////////////////////////////////////////    public void mouseWheelMoved( MouseWheelEvent event )    {        if ( getEditor( ).isFocusOwner( ) )        {            int factor = getModifierFactor( InputModifiers.get( event ) );            if ( factor != 0 )                incrementValue( factor * -event.getWheelRotation( ) );        }    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : abstract methods////////////////////////////////////////////////////////////////////////    public abstract void updateValue( );    //------------------------------------------------------------------    protected abstract void updateEditorValue( );    //------------------------------------------------------------------    protected abstract void incrementValue( int increment );    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    protected int getModifierFactor( InputModifiers modifiers )    {        int factor = 0;        switch ( modifiers )        {            case NONE:                factor = 1;                break;            case CTRL:                factor = 10;                break;            case SHIFT:                factor = 100;                break;            case CTRL_SHIFT:                factor = 1000;                break;        }        return factor;    }    //------------------------------------------------------------------    protected void addEditorActions( )    {        addIncDecAction( KeyEvent.CTRL_DOWN_MASK );        addIncDecAction( KeyEvent.SHIFT_DOWN_MASK );        addIncDecAction( KeyEvent.CTRL_DOWN_MASK | KeyEvent.SHIFT_DOWN_MASK );    }    //------------------------------------------------------------------    private void addAction( int    keyCode,                            int    modifiers,                            String command )    {        KeyAction.create( getEditor( ), JComponent.WHEN_FOCUSED,                          KeyStroke.getKeyStroke( keyCode, modifiers ), command, this );    }    //------------------------------------------------------------------    private void addIncDecAction( int modifiers )    {        addAction( KeyEvent.VK_UP, modifiers, Command.INCREMENT + modifiers );        addAction( KeyEvent.VK_DOWN, modifiers, Command.DECREMENT + modifiers );    }    //------------------------------------------------------------------    private void doDecrement( String str )    {        incrementValue( -getModifierFactor( InputModifiers.get( Integer.parseInt( str ) ) ) );    }    //------------------------------------------------------------------    private void doIncrement( String str )    {        incrementValue( getModifierFactor( InputModifiers.get( Integer.parseInt( str ) ) ) );    }    //------------------------------------------------------------------    private void doCommitEdit( )    {        updateValue( );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    protected   boolean alwaysUpdate;}//----------------------------------------------------------------------

⌨️ 快捷键说明

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