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

📄 selectionlist.java

📁 FuncPlotter is a combined Java application and applet for displaying two-dimensional plots of explic
💻 JAVA
字号:
/*====================================================================*\SelectionList.javaSelection 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.util.ArrayList;import java.util.List;import javax.swing.JComponent;import util.TextUtilities;//----------------------------------------------------------------------// SELECTION LIST CLASSpublic class SelectionList<E>    extends JComponent{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private static final    int BORDER_TOP      = 1;    private static final    int BORDER_BOTTOM   = BORDER_TOP;    private static final    int BORDER_LEFT     = 1;    private static final    int BORDER_RIGHT    = BORDER_LEFT;    private static final    int ICON_LEADING_MARGIN = 6;    private static final    int ICON_WIDTH          = 5;    private static final    int ICON_HEIGHT         = ICON_WIDTH;    private static final    int TEXT_TOP_MARGIN         = 1;    private static final    int TEXT_BOTTOM_MARGIN      = TEXT_TOP_MARGIN;    private static final    int TEXT_LEADING_MARGIN     = 6;    private static final    int TEXT_TRAILING_MARGIN    = TEXT_LEADING_MARGIN;    private static final    Color   BORDER_COLOUR   = Color.DARK_GRAY;//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    public SelectionList( List<E> items,                          E       currentItem )    {        this( items, currentItem, 0, 0 );    }    //------------------------------------------------------------------    public SelectionList( List<E> items,                          E       currentItem,                          int     maxWidth,                          int     maxHeight )        throws IllegalArgumentException    {        this( items, (currentItem == null) ? -1 : items.indexOf( currentItem ), maxWidth, maxHeight );    }    //------------------------------------------------------------------    public SelectionList( List<E> items,                          int     currentIndex )    {        this( items, currentIndex, 0, 0 );    }    //------------------------------------------------------------------    public SelectionList( List<E> items,                          int     currentIndex,                          int     maxWidth,                          int     maxHeight )        throws IllegalArgumentException    {        // Validate arguments        if ( (items == null) || (maxWidth < 0) || (maxHeight < 0) )            throw new IllegalArgumentException( );        // Substitute screen dimension for zero maximum dimension        if ( (maxWidth == 0) || (maxHeight == 0) )        {            Rectangle screenRect = GuiUtilities.getScreenBounds( );            if ( maxWidth == 0 )                maxWidth = screenRect.width;            if ( maxHeight == 0 )                maxHeight = screenRect.height;        }        // Initialise instance variables        this.items = new ArrayList<E>( );        selectedIndex = -1;        GuiUtilities.setFont( Constants.FontName.MAIN, this );        FontMetrics fontMetrics = getFontMetrics( getFont( ) );        rowHeight = TEXT_TOP_MARGIN + fontMetrics.getHeight( ) + TEXT_BOTTOM_MARGIN;        int numRows = Math.min( (maxHeight - BORDER_TOP - BORDER_BOTTOM) / rowHeight, items.size( ) );        this.currentIndex = ((currentIndex < 0) || (currentIndex >= numRows)) ? -1 : currentIndex;        maxTextWidth = maxWidth - BORDER_LEFT - TEXT_LEADING_MARGIN - TEXT_TRAILING_MARGIN - BORDER_RIGHT;        if ( this.currentIndex >= 0 )            maxTextWidth -= ICON_LEADING_MARGIN + ICON_WIDTH;        for ( int i = 0; i < numRows; ++i )        {            E item = items.get( i );            this.items.add( item );            String str = TextUtilities.getWidthLimitedString( item.toString( ), fontMetrics, maxTextWidth,                                                              false );            int strWidth = fontMetrics.stringWidth( str );            if ( columnWidth < strWidth )                columnWidth = strWidth;        }        columnWidth += TEXT_LEADING_MARGIN + TEXT_TRAILING_MARGIN;        if ( this.currentIndex >= 0 )            columnWidth += ICON_LEADING_MARGIN + ICON_WIDTH;        // Set attributes        setOpaque( true );        setFocusable( false );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods : overriding methods////////////////////////////////////////////////////////////////////////    public Dimension getPreferredSize( )    {        return new Dimension( BORDER_LEFT + columnWidth + BORDER_RIGHT,                              BORDER_TOP + items.size( ) * rowHeight + BORDER_BOTTOM );    }    //------------------------------------------------------------------    protected void paintComponent( Graphics gr )    {        // Fill background        Rectangle rect = gr.getClipBounds( );        gr.setColor( ListColour.BACKGROUND.getColour( ) );        gr.fillRect( rect.x, rect.y, rect.width, rect.height );        // Get start and end indexes of rows        int startIndex = Math.max( 0, rect.y / rowHeight );        int endIndex = Math.min( Math.max( 0, (rect.y + rect.height + rowHeight - 1) / rowHeight ),                                 items.size( ) - 1 );        // Draw selection background        if ( (selectedIndex >= startIndex) && (selectedIndex <= endIndex) )        {            gr.setColor( ListColour.SELECTION_BACKGROUND.getColour( ) );            gr.fillRect( rect.x, BORDER_TOP + selectedIndex * rowHeight, rect.width, rowHeight );        }        // Draw icon for current item        boolean leftToRight = getComponentOrientation( ).isLeftToRight( );        int width = getWidth( );        if ( currentIndex >= 0 )        {            int x = leftToRight ? BORDER_LEFT + ICON_LEADING_MARGIN                                : width - BORDER_RIGHT - ICON_LEADING_MARGIN - ICON_WIDTH;            int y = BORDER_TOP + currentIndex * rowHeight + (rowHeight - ICON_HEIGHT + 1) / 2;            gr.setColor( (currentIndex == selectedIndex) ? ListColour.SELECTION_FOREGROUND.getColour( )                                                         : ListColour.FOREGROUND.getColour( ) );            for ( int i = 0; i < ICON_HEIGHT; ++i )            {                int x1 = x;                int x2 = x + ICON_WIDTH - 1;                if ( (i == 0) || (i == ICON_HEIGHT - 1) )                {                    ++x1;                    --x2;                }                gr.drawLine( x1, y, x2, y );                ++y;            }        }        // Draw text        FontMetrics fontMetrics = gr.getFontMetrics( );        int y = BORDER_TOP + startIndex * rowHeight + TEXT_TOP_MARGIN + fontMetrics.getAscent( );        for ( int i = startIndex; i <= endIndex; ++i )        {            String str = TextUtilities.getWidthLimitedString( items.get( i ).toString( ), fontMetrics,                                                              maxTextWidth, false );            int x = leftToRight                            ? BORDER_LEFT + TEXT_LEADING_MARGIN                            : width - BORDER_RIGHT - TEXT_LEADING_MARGIN - fontMetrics.stringWidth( str );            if ( currentIndex >= 0 )            {                if ( leftToRight )                    x += ICON_LEADING_MARGIN + ICON_WIDTH;                else                    x -= ICON_LEADING_MARGIN + ICON_WIDTH;            }            gr.setColor( (i == selectedIndex) ? ListColour.SELECTION_FOREGROUND.getColour( )                                              : ListColour.FOREGROUND.getColour( ) );            gr.drawString( str, x, y );            y += rowHeight;        }        // Draw border        gr.setColor( BORDER_COLOUR );        gr.drawRect( 0, 0, width - 1, getHeight( ) - 1 );    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance methods////////////////////////////////////////////////////////////////////////    public int getNumItems( )    {        return items.size( );    }    //------------------------------------------------------------------    public int getSelectedIndex( )    {        return selectedIndex;    }    //------------------------------------------------------------------    public E getSelectedItem( )    {        return ( (selectedIndex < 0) ? null : items.get( selectedIndex ) );    }    //------------------------------------------------------------------    public void setSelectedIndex( int index )    {        if ( selectedIndex != index )        {            selectedIndex = index;            repaint( );        }    }    //------------------------------------------------------------------    public void setSelectedItem( E item )    {        setSelectedIndex( items.indexOf( item ) );    }    //------------------------------------------------------------------    public int pointToIndex( Point point )    {        int index = -1;        if ( ((point.x >= BORDER_LEFT) && (point.x < getWidth( ) - BORDER_RIGHT) &&              (point.y >= BORDER_TOP) && (point.y < getHeight( ) - BORDER_BOTTOM)) )        {            index = (point.y - BORDER_TOP) / rowHeight;            if ( index >= items.size( ) )                index = -1;        }        return index;    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Instance variables////////////////////////////////////////////////////////////////////////    private int     columnWidth;    private int     rowHeight;    private int     maxTextWidth;    private List<E> items;    private int     currentIndex;    private int     selectedIndex;}//----------------------------------------------------------------------

⌨️ 快捷键说明

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