textutilities.java

来自「FuncPlotter is a combined Java applicati」· Java 代码 · 共 216 行

JAVA
216
字号
/*====================================================================*\TextUtilities.javaTime utility methods 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 util;//----------------------------------------------------------------------// IMPORTSimport java.awt.FontMetrics;import java.io.File;import java.util.ArrayList;import java.util.List;//----------------------------------------------------------------------// TEXT UTILITY METHODS CLASSpublic class TextUtilities{//////////////////////////////////////////////////////////////////////////  Constants////////////////////////////////////////////////////////////////////////    private static final    String  ELLIPSIS_STR    = "...";//////////////////////////////////////////////////////////////////////////  Constructors////////////////////////////////////////////////////////////////////////    private TextUtilities( )    {    }    //------------------------------------------------------------------//////////////////////////////////////////////////////////////////////////  Class methods////////////////////////////////////////////////////////////////////////    public static String getWidthLimitedString( String      str,                                                FontMetrics fontMetrics,                                                int         maxWidth,                                                boolean     removeFromStart )    {        // Test for null string        if ( str == null )            return null;        // Test whether entire string is less than or equal to maximum width        if ( fontMetrics.stringWidth( str ) <= maxWidth )            return str;        // Remove characters from string until it is less than or equal to maximum width        maxWidth -= fontMetrics.stringWidth( ELLIPSIS_STR );        char[] chars = str.toCharArray( );        int length = chars.length;        // Remove characters from start of string        if ( removeFromStart )        {            int offset = 0;            while ( ++offset < length )            {                if ( fontMetrics.charsWidth( chars, offset, length - offset ) <= maxWidth )                    break;            }            return ( (length - offset > 0) ? ELLIPSIS_STR + new String( chars, offset, length - offset )                                           : ELLIPSIS_STR );        }        // Remove characters from end of string        else        {            while ( --length > 0 )            {                if ( fontMetrics.charsWidth( chars, 0, length ) <= maxWidth )                    break;            }            return ( (length > 0) ? new String( chars, 0, length ) + ELLIPSIS_STR : ELLIPSIS_STR );        }    }    //------------------------------------------------------------------    public static String getWidthLimitedPathname( String      pathname,                                                  FontMetrics fontMetrics,                                                  int         maxWidth )    {        return getWidthLimitedPathname( pathname, fontMetrics, maxWidth, File.separatorChar );    }    //------------------------------------------------------------------    public static String getWidthLimitedPathname( String      pathname,                                                  FontMetrics fontMetrics,                                                  int         maxWidth,                                                  char        separatorChar )    {        // Test for null pathname        if ( pathname == null )            return null;        // Split the pathname into its components        List<String> strs = new ArrayList<String>( );        int index = 0;        while ( index < pathname.length( ) )        {            int startIndex = index;            index = pathname.indexOf( separatorChar, index );            if ( index < 0 )                index = pathname.length( );            if ( index > startIndex )                strs.add( pathname.substring( startIndex, index ) );            ++index;        }        if ( strs.isEmpty( ) )            return pathname;        // Get the maximum number of components        StringBuilder buffer = new StringBuilder( ELLIPSIS_STR );        int numComponents = 0;        for ( int i = strs.size( ) - 1; i >= 0; --i )        {            buffer.append( separatorChar );            buffer.append( strs.get( i ) );            if ( fontMetrics.stringWidth( buffer.toString( ) ) > maxWidth )                break;            ++numComponents;        }        // If last component is too wide, remove leading characters until it fits        if ( numComponents == 0 )        {            char[] chars = strs.get( strs.size( ) - 1 ).toCharArray( );            int offset = 0;            while ( offset < chars.length )            {                if ( fontMetrics.charsWidth( chars, offset, chars.length - offset ) <= maxWidth )                    break;                ++offset;            }            return new String( chars, offset, chars.length - offset );        }        // If the entire pathname fits, return it        if ( numComponents == strs.size( ) )            return pathname;        // Construct a reduced pathname        buffer = new StringBuilder( ELLIPSIS_STR );        for ( int i = strs.size( ) - numComponents; i < strs.size( ); ++i )        {            buffer.append( separatorChar );            buffer.append( strs.get( i ) );        }        return buffer.toString( );    }    //------------------------------------------------------------------}//----------------------------------------------------------------------

⌨️ 快捷键说明

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