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

📄 singleselectionlist.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
📖 第 1 页 / 共 3 页
字号:
/*====================================================================*\SingleSelectionList.javaSingle-selection list 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.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.FontMetrics;import java.awt.Graphics;import java.awt.Point;import java.awt.Rectangle;import 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.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.MouseWheelEvent;import java.awt.event.MouseWheelListener;import java.util.ArrayList;import java.util.Collections;import java.util.List;import javax.swing.JComponent;import javax.swing.JViewport;import javax.swing.KeyStroke;import javax.swing.Scrollable;import javax.swing.SwingConstants;import javax.swing.SwingUtilities;import javax.swing.ToolTipManager;import javax.swing.event.ListSelectionEvent;import javax.swing.event.ListSelectionListener;import util.KeyAction;//----------------------------------------------------------------------// SINGLE-SELECTION LIST CLASSpublic class SingleSelectionList<E>    extends JComponent    implements ActionListener, FocusListener, MouseListener, MouseMotionListener, MouseWheelListener,               Scrollable{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    public static final     int DEFAULT_HORIZONTAL_MARGIN   = 4;    public static final     int DEFAULT_VERTICAL_MARGIN     = 1;    private static final    int UNIT_INCREMENT_ROWS     = 1;    private static final    int BLOCK_INCREMENT_ROWS    = 8;    private static final    int DRAG_BAR_HEIGHT = 6;    private static final    int DRAG_DELAY      = 250;    private static final    Color   FOREGROUND_COLOUR           = Color.BLACK;    private static final    Color   BACKGROUND_COLOUR           = new Color( 240, 240, 232 );    private static final    Color   SELECTED_BACKGROUND_COLOUR  = new Color( 220, 220, 212 );    private static final    Color   FOCUSED_BACKGROUND_COLOUR   = new Color( 184, 212, 192 );    private static final    Color   DRAG_BAR_COLOUR             = new Color( 192, 0, 0 );    // Commands    public interface Command    {        String  EDIT_ELEMENT        = "editElement";        String  DELETE_ELEMENT      = "deleteElement";        String  DELETE_EX_ELEMENT   = "deleteExElement";        String  DRAG_ELEMENT        = "dragElement";    }    private interface ListCommand    {        String  SELECT_UP_UNIT      = "selectUpUnit";        String  SELECT_DOWN_UNIT    = "selectDownUnit";        String  SELECT_UP_BLOCK     = "selectUpBlock";        String  SELECT_DOWN_BLOCK   = "selectDownBlock";        String  SELECT_UP_MAX       = "selectUpMax";        String  SELECT_DOWN_MAX     = "selectDownMax";        String  SCROLL_UP_UNIT      = "scrollUpUnit";        String  SCROLL_DOWN_UNIT    = "scrollDownUnit";    }    private static final    KeyAction.CommandMap[]  KEY_COMMANDS    =    {        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_UP, 0 ),                                  ListCommand.SELECT_UP_UNIT ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_DOWN, 0 ),                                  ListCommand.SELECT_DOWN_UNIT ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_UP, 0 ),                                  ListCommand.SELECT_UP_BLOCK ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_PAGE_DOWN, 0 ),                                  ListCommand.SELECT_DOWN_BLOCK ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_HOME, 0 ),                                  ListCommand.SELECT_UP_MAX ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_END, 0 ),                                  ListCommand.SELECT_DOWN_MAX ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_ENTER, 0 ),                                  Command.EDIT_ELEMENT ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_DELETE, 0 ),                                  Command.DELETE_ELEMENT ),        new KeyAction.CommandMap( KeyStroke.getKeyStroke( KeyEvent.VK_DELETE, KeyEvent.SHIFT_DOWN_MASK ),                                  Command.DELETE_EX_ELEMENT ),    };//////////////////////////////////////////////////////////////////////////  Member interfaces////////////////////////////////////////////////////////////////////////    // LIST MODEL INTERFACE    public interface Model<E>    {    ////////////////////////////////////////////////////////////////////    //  Methods    ////////////////////////////////////////////////////////////////////        int getNumElements( );        //--------------------------------------------------------------        E getElement( int index );        //--------------------------------------------------------------        void setElement( int index,                         E   element );        //--------------------------------------------------------------        void addElement( int index,                         E   element );        //--------------------------------------------------------------        E removeElement( int index );        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Member classes : non-inner classes////////////////////////////////////////////////////////////////////////    // DEFAULT LIST MODEL CLASS    public static class DefaultModel<E>        extends ArrayList<E>        implements Model<E>    {    ////////////////////////////////////////////////////////////////////    //  Instance methods : Model interface    ////////////////////////////////////////////////////////////////////        public int getNumElements( )        {            return size( );        }        //--------------------------------------------------------------        public E getElement( int index )        {            return get( index );        }        //--------------------------------------------------------------        public void setElement( int index,                                E   element )        {            set( index, element );        }        //--------------------------------------------------------------        public void addElement( int index,                                E   element )        {            add( index, element );        }        //--------------------------------------------------------------        public E removeElement( int index )        {            return remove( index );        }        //--------------------------------------------------------------    ////////////////////////////////////////////////////////////////////    //  Instance methods    ////////////////////////////////////////////////////////////////////        public void setElements( E[] elements )        {            clear( );            if ( elements != null )                Collections.addAll( this, elements );        }        //--------------------------------------------------------------        public void setElements( List<E> elements )        {            clear( );            if ( elements != null )                addAll( elements );        }        //--------------------------------------------------------------    }    //==================================================================//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public SingleSelectionList( int  columns,                                int  viewableRows,                                Font font )    {        this( columns, viewableRows, 0, 0, font );    }    //------------------------------------------------------------------    public SingleSelectionList( int  columns,                                int  viewableRows,                                Font font,                                E[]  elements )    {        this( columns, viewableRows, 0, 0, font );        setElements( elements );    }    //------------------------------------------------------------------    public SingleSelectionList( int     columns,                                int     viewableRows,                                Font    font,                                List<E> elements )    {        this( columns, viewableRows, 0, 0, font );        setElements( elements );    }    //------------------------------------------------------------------    public SingleSelectionList( int      columns,                                int      viewableRows,                                Font     font,                                Model<E> model )    {        this( columns, viewableRows, 0, 0, font );        setModel( model );    }    //------------------------------------------------------------------    public SingleSelectionList( int  columns,                                int  viewableRows,                                int  columnWidth,                                int  rowHeight,                                Font font )    {        // Initialise instance variables        this.columns = columns;        this.viewableRows = viewableRows;        FontMetrics fontMetrics = getFontMetrics( font );        this.columnWidth = (columnWidth == 0) ? GuiUtilities.getCharWidth( '0', fontMetrics )                                              : columnWidth;        this.rowHeight = (rowHeight == 0) ? 2 * DEFAULT_VERTICAL_MARGIN + fontMetrics.getHeight( )                                          : rowHeight;        horizontalMargin = DEFAULT_HORIZONTAL_MARGIN;        selectedIndex = -1;        dragIndex = -1;        dragStartIndex = -1;        dragEndIndex = -1;        model = new DefaultModel<E>( );        actionListeners = new ArrayList<ActionListener>( );        selectionListeners = new ArrayList<ListSelectionListener>( );        // Set component attributes        setFont( font );        setForeground( FOREGROUND_COLOUR );        setBackground( BACKGROUND_COLOUR );        setOpaque( true );        setFocusable( true );        setAutoscrolls( true );        // Add commands to action map        KeyAction.create( this, JComponent.WHEN_FOCUSED, KEY_COMMANDS, this );        // Add listeners        addFocusListener( this );        addMouseListener( this );        addMouseMotionListener( this );        addMouseWheelListener( this );    }    //------------------------------------------------------------------    public SingleSelectionList( int  columns,                                int  viewableRows,                                int  columnWidth,                                int  rowHeight,                                Font font,                                E[]  elements )    {        this( columns, viewableRows, columnWidth, rowHeight, font );        setElements( elements );    }    //------------------------------------------------------------------    public SingleSelectionList( int     columns,                                int     viewableRows,                                int     columnWidth,                                int     rowHeight,                                Font    font,                                List<E> elements )    {        this( columns, viewableRows, columnWidth, rowHeight, font );        setElements( elements );    }    //------------------------------------------------------------------    public SingleSelectionList( int      columns,                                int      viewableRows,                                int      columnWidth,                                int      rowHeight,                                Font     font,                                Model<E> model )    {        this( columns, viewableRows, columnWidth, rowHeight, font );        setModel( model );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  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 );            Util.getDeclaredMethod( getClass( ), methodName, false ).invoke( this );        }        catch ( Exception e )        {            e.printStackTrace( );        }    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : FocusListener interface////////////////////////////////////////////////////////////////////////    public void focusGained( FocusEvent event )    {

⌨️ 快捷键说明

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