FileDocCategorySizeDatePackage
RangeMenu.javaAPI DocSun JDK 1.4.2 Example12838Thu May 12 00:35:27 BST 2005None

RangeMenu

public final class RangeMenu extends JComboBox implements ActionListener
RangeMenu.java
version
@(#)RangeMenu.java 1.1 00/08/22
author
Shinsuke Fukuda
author
Ankit Patel [Conversion to Swing - 01/07/30]

Fields Summary
private final int[]
UNICODE_RANGES
private final String[]
UNICODE_RANGE_NAMES
private boolean
useCustomRange
private int[]
customRange
private final JDialog
customRangeDialog
private final JTextField
customRangeStart
private final JTextField
customRangeEnd
private final int
CUSTOM_RANGE_INDEX
private final Font2DTest
parent
Constructors Summary
public RangeMenu(Font2DTest demo, JFrame f)


           
        super();
        parent = demo;

        for ( int i = 0; i < UNICODE_RANGE_NAMES.length; i++ )
          addItem( UNICODE_RANGE_NAMES[i] );

        setSelectedIndex( 0 );
        addActionListener( this );

        /// Set up custom range dialog...
        customRangeDialog = new JDialog( f, "Custom Unicode Range", true );
        customRangeDialog.setResizable( false );

        JPanel dialogTop = new JPanel();
        JPanel dialogBottom = new JPanel();
        JButton okButton = new JButton("OK");
        JLabel from = new JLabel( "From" );
        JLabel to = new JLabel("To:");
        Font labelFont = new Font( "dialog", Font.BOLD, 12 );
        from.setFont( labelFont );
        to.setFont( labelFont );
        okButton.setFont( labelFont );

        dialogTop.add( from );
        dialogTop.add( customRangeStart );
        dialogTop.add( to );
        dialogTop.add( customRangeEnd );
        dialogBottom.add( okButton );
        okButton.addActionListener( this );

        customRangeDialog.getContentPane().setLayout( new BorderLayout() );
        customRangeDialog.getContentPane().add( "North", dialogTop );
        customRangeDialog.getContentPane().add( "South", dialogBottom );
        customRangeDialog.pack();
    
Methods Summary
public voidactionPerformed(java.awt.event.ActionEvent e)

        Object source = e.getSource();
        
        if ( source instanceof JComboBox ) {
	        String rangeName = (String)((JComboBox)source).getSelectedItem();

	        if ( rangeName.equals("Other...") ) {
            	    useCustomRange = true;
        	    customRangeDialog.show();
	        }
        	else {
	          useCustomRange = false;
        	}
	        parent.fireRangeChanged();
	}
	else if ( source instanceof JButton ) {
	        /// Since it is only "OK" button that sends any action here...
        	customRangeDialog.hide();
        }
    
public int[]getSelectedRange()

        if ( useCustomRange ) {
            int startIndex, endIndex;
            String startText, endText;
            String empty = "";
            try {
                startText = customRangeStart.getText().trim();
                endText = customRangeEnd.getText().trim();
                if ( startText.equals(empty) && !endText.equals(empty) ) {
                    endIndex = Integer.parseInt( endText, 16 );
                    startIndex = endIndex - 7*25;
                }
                else if ( !startText.equals(empty) && endText.equals(empty) ) {
                    startIndex = Integer.parseInt( startText, 16 );
                    endIndex = startIndex + 7*25;                    
                }
                else {
                    startIndex = Integer.parseInt( customRangeStart.getText(), 16 );
                    endIndex = Integer.parseInt( customRangeEnd.getText(), 16 );
                }
            }
            catch ( Exception e ) {
                /// Error in parsing the hex number ---
                /// Reset the range to what it was before and return that
                customRangeStart.setText( Integer.toString( customRange[0], 16 ));
                customRangeEnd.setText( Integer.toString( customRange[1], 16 ));
                return customRange;
            }

            if ( startIndex < 0 )
              startIndex = 0;
            if ( endIndex > 0xffff )
              endIndex = 0xffff;
            if ( startIndex > endIndex )
              startIndex = endIndex;

            customRange[0] = startIndex;
            customRange[1] = endIndex;
            return customRange;
        }
        else
          return UNICODE_RANGES[ getSelectedIndex() ];
    
public voidsetSelectedRange(java.lang.String name, int start, int end)

        setSelectedItem( name );
        customRange[0] = start;
        customRange[1] = end;
        parent.fireRangeChanged();