home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / JBuilder8.iso / Solaris / resource / jre / demo / jfc / Font2DTest / src / RangeMenu.java < prev   
Encoding:
Java Source  |  2002-09-06  |  12.5 KB  |  302 lines

  1. /*
  2.  * Copyright (c) 2002 Sun Microsystems, Inc. All  Rights Reserved.
  3.  * 
  4.  * Redistribution and use in source and binary forms, with or without
  5.  * modification, are permitted provided that the following conditions
  6.  * are met:
  7.  * 
  8.  * -Redistributions of source code must retain the above copyright
  9.  *  notice, this list of conditions and the following disclaimer.
  10.  * 
  11.  * -Redistribution in binary form must reproduct the above copyright
  12.  *  notice, this list of conditions and the following disclaimer in
  13.  *  the documentation and/or other materials provided with the distribution.
  14.  * 
  15.  * Neither the name of Sun Microsystems, Inc. or the names of contributors
  16.  * may be used to endorse or promote products derived from this software
  17.  * without specific prior written permission.
  18.  * 
  19.  * This software is provided "AS IS," without a warranty of any kind. ALL
  20.  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
  21.  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
  22.  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT
  23.  * BE LIABLE FOR ANY DAMAGES OR LIABILITIES SUFFERED BY LICENSEE AS A RESULT
  24.  * OF OR RELATING TO USE, MODIFICATION OR DISTRIBUTION OF THE SOFTWARE OR ITS
  25.  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
  26.  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
  27.  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
  28.  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE, EVEN
  29.  * IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  30.  * 
  31.  * You acknowledge that Software is not designed, licensed or intended for
  32.  * use in the design, construction, operation or maintenance of any nuclear
  33.  * facility.
  34.  */
  35.  
  36. /*
  37.  * @(#)RangeMenu.java    1.12 02/06/13
  38.  */
  39.  
  40. import java.awt.BorderLayout;
  41. import java.awt.Font;
  42. import java.awt.event.ActionEvent;
  43. import java.awt.event.ActionListener;
  44. import java.awt.event.ItemEvent;
  45. import java.awt.event.ItemListener;
  46.  
  47. import javax.swing.*;
  48.  
  49. /**
  50.  * RangeMenu.java
  51.  *
  52.  * @version @(#)RangeMenu.java    1.1 00/08/22
  53.  * @author Shinsuke Fukuda
  54.  * @author Ankit Patel [Conversion to Swing - 01/07/30]  
  55.  */
  56.  
  57. /// Custom made choice menu that holds data for unicode range
  58.  
  59. public final class RangeMenu extends JComboBox implements ActionListener {
  60.  
  61.     /// Based on Unicode 3.0
  62.  
  63.     private final int[][] UNICODE_RANGES = {
  64.         { 0x0000, 0x007f }, /// Basic Latin
  65.         { 0x0080, 0x00ff }, /// Latin-1 Supplement
  66.         { 0x0100, 0x017f }, /// Latin Extended-A
  67.         { 0x0180, 0x024f }, /// Latin Extended-B
  68.         { 0x0250, 0x02af }, /// IPA Extensions
  69.         { 0x02b0, 0x02ff }, /// Spacing Modifier Letters
  70.         { 0x0300, 0x036f }, /// Combining Diacritical Marks
  71.         { 0x0370, 0x03ff }, /// Greek
  72.         { 0x0400, 0x04ff }, /// Cyrillic
  73.         { 0x0530, 0x058f }, /// Armenian
  74.         { 0x0590, 0x05ff }, /// Hebrew
  75.         { 0x0600, 0x06ff }, /// Arabic
  76.         { 0x0700, 0x074f }, /// Syriac
  77.         { 0x0780, 0x07bf }, /// Thaana
  78.         { 0x0900, 0x097f }, /// Devanagari
  79.         { 0x0980, 0x09ff }, /// Bengali
  80.         { 0x0a00, 0x0a7f }, /// Gurmukhi
  81.         { 0x0a80, 0x0aff }, /// Gujarati
  82.         { 0x0b00, 0x0b7f }, /// Oriya
  83.         { 0x0b80, 0x0bff }, /// Tamil
  84.         { 0x0c00, 0x0c7f }, /// Telugu
  85.         { 0x0c80, 0x0cff }, /// Kannada
  86.         { 0x0d00, 0x0d7f }, /// Malayalam
  87.         { 0x0d80, 0x0dff }, /// Sinhala
  88.         { 0x0e00, 0x0e7f }, /// Thai
  89.         { 0x0e80, 0x0eff }, /// Lao
  90.         { 0x0f00, 0x0fbf }, /// Tibetan
  91.         { 0x1000, 0x109f }, /// Myanmar
  92.         { 0x10a0, 0x10ff }, /// Georgian
  93.         { 0x1100, 0x11ff }, /// Hangul Jamo
  94.         { 0x1200, 0x137f }, /// Ethiopic
  95.         { 0x13a0, 0x13ff }, /// Cherokee
  96.         { 0x1400, 0x167f }, /// Canadian Aboriginal Syllabics
  97.         { 0x1680, 0x169f }, /// Ogham
  98.         { 0x16a0, 0x16f0 }, /// Runic
  99.         { 0x1780, 0x17ff }, /// Khmer
  100.         { 0x1800, 0x18af }, /// Mongolian
  101.         { 0x1e00, 0x1eff }, /// Latin Extended Additional
  102.         { 0x1f00, 0x1fff }, /// Greek Extended
  103.         { 0x2000, 0x206f }, /// General Punctuation
  104.         { 0x2070, 0x209f }, /// Superscripts and Supscripts
  105.         { 0x20a0, 0x20cf }, /// Currency Symbols
  106.         { 0x20d0, 0x20ff }, /// Combining Marks for Symbols
  107.         { 0x2100, 0x214f }, /// Letterlike Symbols
  108.         { 0x2150, 0x218f }, /// Number Forms
  109.         { 0x2190, 0x21ff }, /// Arrows
  110.         { 0x2200, 0x22ff }, /// Mathematical Operators
  111.         { 0x2300, 0x23ff }, /// Miscellaneous Technical
  112.         { 0x2400, 0x243f }, /// Control Pictures
  113.         { 0x2440, 0x245f }, /// Optical Character Recognition
  114.         { 0x2460, 0x24ff }, /// Enclosed Alphanumerics
  115.         { 0x2500, 0x257f }, /// Box Drawing
  116.         { 0x2580, 0x259f }, /// Block Elements
  117.         { 0x25a0, 0x25ff }, /// Geometric Shapes
  118.         { 0x2600, 0x26ff }, /// Miscellaneous Symbols
  119.         { 0x2700, 0x27bf }, /// Dingbats
  120.         { 0x2800, 0x28ff }, /// Braille
  121.         { 0x2e80, 0x2fdf }, /// CJK and KangXi Radicals
  122.         { 0x2ff0, 0x2fff }, /// Ideographic Description
  123.         { 0x3000, 0x303f }, /// CJK Symbols and Punctuation
  124.         { 0x3040, 0x309f }, /// Hiragana
  125.         { 0x30a0, 0x30ff }, /// Katakana
  126.         { 0x3100, 0x312f }, /// Bopomofo
  127.         { 0x3130, 0x318f }, /// Hangul Compatibility Jamo
  128.         { 0x3190, 0x319f }, /// Kanbun
  129.         { 0x31a0, 0x31bf }, /// Bopomofo Extended
  130.         { 0x3200, 0x32ff }, /// Enclosed CJK Letters and Months
  131.         { 0x3300, 0x33ff }, /// CJK Compatibility
  132.         { 0x3400, 0x4dbf }, /// CJK Unified Ideographs Extension A
  133.         { 0x4e00, 0x9faf }, /// CJK Unified Ideographs
  134.         { 0xa000, 0xa4cf }, /// Yi
  135.         { 0xac00, 0xd7af }, /// Hangul Syllables
  136.         { 0xe000, 0xf8ff }, /// Private Use Area
  137.         { 0xf900, 0xfaff }, /// CJK Compatibility Ideographs
  138.         { 0xfb00, 0xfb4f }, /// Alphabetic Presentation Forms
  139.         { 0xfb50, 0xfdff }, /// Arabic Presentation Forms-A
  140.         { 0xfe20, 0xfe2f }, /// Combining Half-Marks
  141.         { 0xfe30, 0xfe4f }, /// CJK Compatibility Forms
  142.         { 0xfe50, 0xfe6f }, /// Small Form Variants
  143.         { 0xfe70, 0xfeff }, /// Arabic Presentation Forms-B
  144.         { 0xff00, 0xffef }, /// Halfwidth and Fullwidth Forms
  145.         { 0xfff0, 0xffff }, /// Specials
  146.     };
  147.  
  148.     private final String[] UNICODE_RANGE_NAMES = {
  149.         "Basic Latin", "Latin-1 Supplement", "Latin Extended-A", "Latin Extended-B",
  150.         "IPA Extensions", "Spacing Modifier Letters", "Combining Diacritical Marks",
  151.         "Greek", "Cyrillic", "Armenian", "Hebrew", "Arabic", "Syriac", "Thaana",
  152.         "Devanagari", "Bengali", "Gurmukhi", "Gujarati", "Oriya", "Tamil", "Telugu",
  153.         "Kannada", "Malayalam", "Sinhala", "Thai", "Lao", "Tibetan", "Myanmar",
  154.         "Georgian", "Hangul Jamo", "Ethiopic", "Cherokee",
  155.         "Canadian Aboriginal Syllabics", "Ogham", "Runic", "Khmer", "Mongolian",
  156.         "Latin Extended Additional", "Greek Extended", "General Punctuation",
  157.         "Superscripts and Supscripts", "Currency Symbols", "Combining Marks for Symbols",
  158.         "Letterlike Symbols", "Number Forms", "Arrows", "Mathematical Operators",
  159.         "Miscellaneous Technical", "Control Pictures", "Optical Character Recognition",
  160.         "Enclosed Alphanumerics", "Box Drawing", "Block Elements", "Geometric Shapes",
  161.         "Miscellaneous Symbols", "Dingbats", "Braille", "CJK and KangXi Radicals",
  162.         "Ideographic Description", "CJK Symbols and Punctuation", "Hiragana", "Katakana",
  163.         "Bopomofo", "Hangul Compatibility Jamo", "Kanbun", "Bopomofo Extended",
  164.         "Enclosed CJK Letters and Months", "CJK Compatibility",
  165.         "CJK Unified Ideographs Extension A", "CJK Unified Ideographs", "Yi",
  166.         "Hangul Syllables", "Private Use Area", "CJK Compatibility Ideographs",
  167.         "Alphabetic Presentation Forms", "Arabic Presentation Forms-A",
  168.         "CJK Compatibility Forms", "Small Form Variants", "Combining Half-Marks",
  169.         "Arabic Presentation Forms-B", "Halfwidth and Fullwidth Forms",
  170.         "Specials", "Other..."
  171.       };
  172.  
  173.     private boolean useCustomRange = false;
  174.     private int[] customRange = { 0x0000, 0x007f };
  175.  
  176.     /// Custom range dialog variables
  177.     private final JDialog customRangeDialog;
  178.     private final JTextField customRangeStart = new JTextField( "0000", 4 );
  179.     private final JTextField customRangeEnd   = new JTextField( "007F", 4 );
  180.     private final int CUSTOM_RANGE_INDEX = UNICODE_RANGE_NAMES.length - 1;
  181.  
  182.     /// Parent Font2DTest Object holder
  183.     private final Font2DTest parent;
  184.  
  185.     public RangeMenu( Font2DTest demo, JFrame f ) {
  186.         super();
  187.         parent = demo;
  188.  
  189.         for ( int i = 0; i < UNICODE_RANGE_NAMES.length; i++ )
  190.           addItem( UNICODE_RANGE_NAMES[i] );
  191.  
  192.         setSelectedIndex( 0 );
  193.         addActionListener( this );
  194.  
  195.         /// Set up custom range dialog...
  196.         customRangeDialog = new JDialog( f, "Custom Unicode Range", true );
  197.         customRangeDialog.setResizable( false );
  198.  
  199.         JPanel dialogTop = new JPanel();
  200.         JPanel dialogBottom = new JPanel();
  201.         JButton okButton = new JButton("OK");
  202.         JLabel from = new JLabel( "From" );
  203.         JLabel to = new JLabel("To:");
  204.         Font labelFont = new Font( "dialog", Font.BOLD, 12 );
  205.         from.setFont( labelFont );
  206.         to.setFont( labelFont );
  207.         okButton.setFont( labelFont );
  208.  
  209.         dialogTop.add( from );
  210.         dialogTop.add( customRangeStart );
  211.         dialogTop.add( to );
  212.         dialogTop.add( customRangeEnd );
  213.         dialogBottom.add( okButton );
  214.         okButton.addActionListener( this );
  215.  
  216.         customRangeDialog.getContentPane().setLayout( new BorderLayout() );
  217.         customRangeDialog.getContentPane().add( "North", dialogTop );
  218.         customRangeDialog.getContentPane().add( "South", dialogBottom );
  219.         customRangeDialog.pack();
  220.     }
  221.  
  222.     /// Return the range that is currently selected
  223.  
  224.     public int[] getSelectedRange() {
  225.         if ( useCustomRange ) {
  226.             int startIndex, endIndex;
  227.             String startText, endText;
  228.             String empty = "";
  229.             try {
  230.                 startText = customRangeStart.getText().trim();
  231.                 endText = customRangeEnd.getText().trim();
  232.                 if ( startText.equals(empty) && !endText.equals(empty) ) {
  233.                     endIndex = Integer.parseInt( endText, 16 );
  234.                     startIndex = endIndex - 7*25;
  235.                 }
  236.                 else if ( !startText.equals(empty) && endText.equals(empty) ) {
  237.                     startIndex = Integer.parseInt( startText, 16 );
  238.                     endIndex = startIndex + 7*25;                    
  239.                 }
  240.                 else {
  241.                     startIndex = Integer.parseInt( customRangeStart.getText(), 16 );
  242.                     endIndex = Integer.parseInt( customRangeEnd.getText(), 16 );
  243.                 }
  244.             }
  245.             catch ( Exception e ) {
  246.                 /// Error in parsing the hex number ---
  247.                 /// Reset the range to what it was before and return that
  248.                 customRangeStart.setText( Integer.toString( customRange[0], 16 ));
  249.                 customRangeEnd.setText( Integer.toString( customRange[1], 16 ));
  250.                 return customRange;
  251.             }
  252.  
  253.             if ( startIndex < 0 )
  254.               startIndex = 0;
  255.             if ( endIndex > 0xffff )
  256.               endIndex = 0xffff;
  257.             if ( startIndex > endIndex )
  258.               startIndex = endIndex;
  259.  
  260.             customRange[0] = startIndex;
  261.             customRange[1] = endIndex;
  262.             return customRange;
  263.         }
  264.         else
  265.           return UNICODE_RANGES[ getSelectedIndex() ];
  266.     }
  267.  
  268.     /// Function used by loadOptions in Font2DTest main panel
  269.     /// to reset setting and range selection
  270.     public void setSelectedRange( String name, int start, int end ) {
  271.         setSelectedItem( name );
  272.         customRange[0] = start;
  273.         customRange[1] = end;
  274.         parent.fireRangeChanged();
  275.     }
  276.  
  277.     /// ActionListener interface function
  278.     /// ABP
  279.     /// moved JComboBox event code into this fcn from
  280.     /// itemStateChanged() method. Part of change to Swing.
  281.     public void actionPerformed( ActionEvent e ) {
  282.         Object source = e.getSource();
  283.         
  284.         if ( source instanceof JComboBox ) {
  285.             String rangeName = (String)((JComboBox)source).getSelectedItem();
  286.  
  287.             if ( rangeName.equals("Other...") ) {
  288.                     useCustomRange = true;
  289.                 customRangeDialog.show();
  290.             }
  291.             else {
  292.               useCustomRange = false;
  293.             }
  294.             parent.fireRangeChanged();
  295.     }
  296.     else if ( source instanceof JButton ) {
  297.             /// Since it is only "OK" button that sends any action here...
  298.             customRangeDialog.hide();
  299.         }
  300.     }
  301. }
  302.