KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > RangeMenu


1 /*
2  * @(#)RangeMenu.java 1.18 05/11/17
3  *
4  * Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * -Redistribution of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  *
12  * -Redistribution in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * Neither the name of Sun Microsystems, Inc. or the names of contributors may
17  * be used to endorse or promote products derived from this software without
18  * specific prior written permission.
19  *
20  * This software is provided "AS IS," without a warranty of any kind. ALL
21  * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING
22  * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
23  * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN")
24  * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
25  * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
26  * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST
27  * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL,
28  * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY
29  * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE,
30  * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
31  *
32  * You acknowledge that this software is not designed, licensed or intended
33  * for use in the design, construction, operation or maintenance of any
34  * nuclear facility.
35  */

36
37 /*
38  * @(#)RangeMenu.java 1.18 05/11/17
39  */

40
41 import java.awt.BorderLayout JavaDoc;
42 import java.awt.Font JavaDoc;
43 import java.awt.event.ActionEvent JavaDoc;
44 import java.awt.event.ActionListener JavaDoc;
45 import java.awt.event.ItemEvent JavaDoc;
46 import java.awt.event.ItemListener JavaDoc;
47
48 import javax.swing.*;
49
50 /**
51  * RangeMenu.java
52  *
53  * @version @(#)RangeMenu.java 1.1 00/08/22
54  * @author Shinsuke Fukuda
55  * @author Ankit Patel [Conversion to Swing - 01/07/30]
56  */

57
58 /// Custom made choice menu that holds data for unicode range
59

60 public final class RangeMenu extends JComboBox implements ActionListener JavaDoc {
61
62     /// Painfully extracted from java.lang.Character.UnicodeBlock. Arrrgh!
63
/// Unicode 3.0 data.
64

65     private final int[][] UNICODE_RANGES = {
66         { 0x000000, 0x00007f }, /// BASIC_LATIN
67
{ 0x000080, 0x0000ff }, /// LATIN_1_SUPPLEMENT
68
{ 0x000100, 0x00017f }, /// LATIN_EXTENDED_A
69
{ 0x000180, 0x00024f }, /// LATIN_EXTENDED_B
70
{ 0x000250, 0x0002af }, /// IPA_EXTENSIONS
71
{ 0x0002b0, 0x0002ff }, /// SPACING_MODIFIER_LETTERS
72
{ 0x000300, 0x00036f }, /// COMBINING_DIACRITICAL_MARKS
73
{ 0x000370, 0x0003ff }, /// GREEK
74
{ 0x000400, 0x0004ff }, /// CYRILLIC
75
{ 0x000500, 0x00052f }, /// CYRILLIC_SUPPLEMENTARY
76
{ 0x000530, 0x00058f }, /// ARMENIAN
77
{ 0x000590, 0x0005ff }, /// HEBREW
78
{ 0x000600, 0x0006ff }, /// ARABIC
79
{ 0x000700, 0x00074f }, /// SYRIAC
80
{ 0x000780, 0x0007bf }, /// THAANA
81
{ 0x000900, 0x00097f }, /// DEVANAGARI
82
{ 0x000980, 0x0009ff }, /// BENGALI
83
{ 0x000a00, 0x000a7f }, /// GURMUKHI
84
{ 0x000a80, 0x000aff }, /// GUJARATI
85
{ 0x000b00, 0x000b7f }, /// ORIYA
86
{ 0x000b80, 0x000bff }, /// TAMIL
87
{ 0x000c00, 0x000c7f }, /// TELUGU
88
{ 0x000c80, 0x000cff }, /// KANNADA
89
{ 0x000d00, 0x000d7f }, /// MALAYALAM
90
{ 0x000d80, 0x000dff }, /// SINHALA
91
{ 0x000e00, 0x000e7f }, /// THAI
92
{ 0x000e80, 0x000eff }, /// LAO
93
{ 0x000f00, 0x000fff }, /// TIBETAN
94
{ 0x001000, 0x00109f }, /// MYANMAR
95
{ 0x0010a0, 0x0010ff }, /// GEORGIAN
96
{ 0x001100, 0x0011ff }, /// HANGUL_JAMO
97
{ 0x001200, 0x00137f }, /// ETHIOPIC
98
{ 0x0013a0, 0x0013ff }, /// CHEROKEE
99
{ 0x001400, 0x00167f }, /// UNIFIED_CANADIAN_ABORIGINAL_SYLLABICS
100
{ 0x001680, 0x00169f }, /// OGHAM
101
{ 0x0016a0, 0x0016ff }, /// RUNIC
102
{ 0x001700, 0x00171f }, /// TAGALOG
103
{ 0x001720, 0x00173f }, /// HANUNOO
104
{ 0x001740, 0x00175f }, /// BUHID
105
{ 0x001760, 0x00177f }, /// TAGBANWA
106
{ 0x001780, 0x0017ff }, /// KHMER
107
{ 0x001800, 0x0018af }, /// MONGOLIAN
108
{ 0x001900, 0x00194f }, /// LIMBU
109
{ 0x001950, 0x00197f }, /// TAI_LE
110
{ 0x0019e0, 0x0019ff }, /// KHMER_SYMBOLS
111
{ 0x001d00, 0x001d7f }, /// PHONETIC_EXTENSIONS
112
{ 0x001e00, 0x001eff }, /// LATIN_EXTENDED_ADDITIONAL
113
{ 0x001f00, 0x001fff }, /// GREEK_EXTENDED
114
{ 0x002000, 0x00206f }, /// GENERAL_PUNCTUATION
115
{ 0x002070, 0x00209f }, /// SUPERSCRIPTS_AND_SUBSCRIPTS
116
{ 0x0020a0, 0x0020cf }, /// CURRENCY_SYMBOLS
117
{ 0x0020d0, 0x0020ff }, /// COMBINING_MARKS_FOR_SYMBOLS
118
{ 0x002100, 0x00214f }, /// LETTERLIKE_SYMBOLS
119
{ 0x002150, 0x00218f }, /// NUMBER_FORMS
120
{ 0x002190, 0x0021ff }, /// ARROWS
121
{ 0x002200, 0x0022ff }, /// MATHEMATICAL_OPERATORS
122
{ 0x002300, 0x0023ff }, /// MISCELLANEOUS_TECHNICAL
123
{ 0x002400, 0x00243f }, /// CONTROL_PICTURES
124
{ 0x002440, 0x00245f }, /// OPTICAL_CHARACTER_RECOGNITION
125
{ 0x002460, 0x0024ff }, /// ENCLOSED_ALPHANUMERICS
126
{ 0x002500, 0x00257f }, /// BOX_DRAWING
127
{ 0x002580, 0x00259f }, /// BLOCK_ELEMENTS
128
{ 0x0025a0, 0x0025ff }, /// GEOMETRIC_SHAPES
129
{ 0x002600, 0x0026ff }, /// MISCELLANEOUS_SYMBOLS
130
{ 0x002700, 0x0027bf }, /// DINGBATS
131
{ 0x0027c0, 0x0027ef }, /// MISCELLANEOUS_MATHEMATICAL_SYMBOLS_A
132
{ 0x0027f0, 0x0027ff }, /// SUPPLEMENTAL_ARROWS_A
133
{ 0x002800, 0x0028ff }, /// BRAILLE_PATTERNS
134
{ 0x002900, 0x00297f }, /// SUPPLEMENTAL_ARROWS_B
135
{ 0x002980, 0x0029ff }, /// MISCELLANEOUS_MATHEMATICAL_SYMBOLS_B
136
{ 0x002a00, 0x002aff }, /// SUPPLEMENTAL_MATHEMATICAL_OPERATORS
137
{ 0x002b00, 0x002bff }, /// MISCELLANEOUS_SYMBOLS_AND_ARROWS
138
{ 0x002e80, 0x002eff }, /// CJK_RADICALS_SUPPLEMENT
139
{ 0x002f00, 0x002fdf }, /// KANGXI_RADICALS
140
{ 0x002ff0, 0x002fff }, /// IDEOGRAPHIC_DESCRIPTION_CHARACTERS
141
{ 0x003000, 0x00303f }, /// CJK_SYMBOLS_AND_PUNCTUATION
142
{ 0x003040, 0x00309f }, /// HIRAGANA
143
{ 0x0030a0, 0x0030ff }, /// KATAKANA
144
{ 0x003100, 0x00312f }, /// BOPOMOFO
145
{ 0x003130, 0x00318f }, /// HANGUL_COMPATIBILITY_JAMO
146
{ 0x003190, 0x00319f }, /// KANBUN
147
{ 0x0031a0, 0x0031bf }, /// BOPOMOFO_EXTENDED
148
{ 0x0031f0, 0x0031ff }, /// KATAKANA_PHONETIC_EXTENSIONS
149
{ 0x003200, 0x0032ff }, /// ENCLOSED_CJK_LETTERS_AND_MONTHS
150
{ 0x003300, 0x0033ff }, /// CJK_COMPATIBILITY
151
{ 0x003400, 0x004dbf }, /// CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
152
{ 0x004dc0, 0x004dff }, /// YIJING_HEXAGRAM_SYMBOLS
153
{ 0x004e00, 0x009fff }, /// CJK_UNIFIED_IDEOGRAPHS
154
{ 0x00a000, 0x00a48f }, /// YI_SYLLABLES
155
{ 0x00a490, 0x00a4cf }, /// YI_RADICALS
156
{ 0x00ac00, 0x00d7af }, /// HANGUL_SYLLABLES
157
{ 0x00d800, 0x00dfff }, /// SURROGATES_AREA
158
{ 0x00e000, 0x00f8ff }, /// PRIVATE_USE_AREA
159
{ 0x00f900, 0x00faff }, /// CJK_COMPATIBILITY_IDEOGRAPHS
160
{ 0x00fb00, 0x00fb4f }, /// ALPHABETIC_PRESENTATION_FORMS
161
{ 0x00fb50, 0x00fdff }, /// ARABIC_PRESENTATION_FORMS_A
162
{ 0x00fe00, 0x00fe0f }, /// VARIATION_SELECTORS
163
{ 0x00fe20, 0x00fe2f }, /// COMBINING_HALF_MARKS
164
{ 0x00fe30, 0x00fe4f }, /// CJK_COMPATIBILITY_FORMS
165
{ 0x00fe50, 0x00fe6f }, /// SMALL_FORM_VARIANTS
166
{ 0x00fe70, 0x00feff }, /// ARABIC_PRESENTATION_FORMS_B
167
{ 0x00ff00, 0x00ffef }, /// HALFWIDTH_AND_FULLWIDTH_FORMS
168
{ 0x00fff0, 0x00ffff }, /// SPECIALS
169
{ 0x010000, 0x01007f }, /// LINEAR_B_SYLLABARY
170
{ 0x010080, 0x0100ff }, /// LINEAR_B_IDEOGRAMS
171
{ 0x010100, 0x01013f }, /// AEGEAN_NUMBERS
172
{ 0x010300, 0x01032f }, /// OLD_ITALIC
173
{ 0x010330, 0x01034f }, /// GOTHIC
174
{ 0x010380, 0x01039f }, /// UGARITIC
175
{ 0x010400, 0x01044f }, /// DESERET
176
{ 0x010450, 0x01047f }, /// SHAVIAN
177
{ 0x010480, 0x0104af }, /// OSMANYA
178
{ 0x010800, 0x01083f }, /// CYPRIOT_SYLLABARY
179
{ 0x01d000, 0x01d0ff }, /// BYZANTINE_MUSICAL_SYMBOLS
180
{ 0x01d100, 0x01d1ff }, /// MUSICAL_SYMBOLS
181
{ 0x01d300, 0x01d35f }, /// TAI_XUAN_JING_SYMBOLS
182
{ 0x01d400, 0x01d7ff }, /// MATHEMATICAL_ALPHANUMERIC_SYMBOLS
183
{ 0x020000, 0x02a6df }, /// CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
184
{ 0x02f800, 0x02fa1f }, /// CJK_COMPATIBILITY_IDEOGRAPHS_SUPPLEMENT
185
{ 0x0e0000, 0x0e007f }, /// TAGS
186
{ 0x0e0100, 0x0e01ef }, /// VARIATION_SELECTORS_SUPPLEMENT
187
{ 0x0f0000, 0x0fffff }, /// SUPPLEMENTARY_PRIVATE_USE_AREA_A
188
{ 0x100000, 0x10ffff }, /// SUPPLEMENTARY_PRIVATE_USE_AREA_B
189
{ 0x000000, 0x00007f }, /// OTHER [USER DEFINED RANGE]
190
};
191
192     private final String JavaDoc[] UNICODE_RANGE_NAMES = {
193         "Basic Latin",
194         "Latin-1 Supplement",
195         "Latin Extended-A",
196         "Latin Extended-B",
197         "IPA Extensions",
198         "Spacing Modifier Letters",
199         "Combining Diacritical Marks",
200         "Greek",
201         "Cyrillic",
202         "Cyrillic Supplement",
203         "Armenian",
204         "Hebrew",
205         "Arabic",
206         "Syriac",
207         "Thaana",
208         "Devanagari",
209         "Bengali",
210         "Gurmukhi",
211         "Gujarati",
212         "Oriya",
213         "Tamil",
214         "Telugu",
215         "Kannada",
216         "Malayalam",
217         "Sinhala",
218         "Thai",
219         "Lao",
220         "Tibetan",
221         "Myanmar",
222         "Georgian",
223         "Hangul Jamo",
224         "Ethiopic",
225         "Cherokee",
226         "Unified Canadian Aboriginal Syllabics",
227         "Ogham",
228         "Runic",
229         "Tagalog",
230         "Hanunoo",
231         "Buhid",
232         "Tagbanwa",
233         "Khmer",
234         "Mongolian",
235         "Limbu",
236         "Tai Le",
237         "Khmer Symbols",
238         "Phonetic Extensions",
239         "Latin Extended Additional",
240         "Greek Extended",
241         "General Punctuation",
242         "Superscripts and Subscripts",
243         "Currency Symbols",
244         "Combining Marks for Symbols",
245         "Letterlike Symbols",
246         "Number Forms",
247         "Arrows",
248         "Mathematical Operators",
249         "Miscellaneous Technical",
250         "Control Pictures",
251         "Optical Character Recognition",
252         "Enclosed Alphanumerics",
253         "Box Drawing",
254         "Block Elements",
255         "Geometric Shapes",
256         "Miscellaneous Symbols",
257         "Dingbats",
258         "Miscellaneous Mathematical Symbols-A",
259         "Supplemental Arrows-A",
260         "Braille Patterns",
261         "Supplemental Arrows-B",
262         "Miscellaneous Mathematical Symbols-B",
263         "Supplemental Mathematical Operators",
264         "Miscellaneous Symbols and Arrows",
265         "CJK Radicals Supplement",
266         "Kangxi Radicals",
267         "Ideographic Description Characters",
268         "CJK Symbols and Punctuation",
269         "Hiragana",
270         "Katakana",
271         "Bopomofo",
272         "Hangul Compatibility Jamo",
273         "Kanbun",
274         "Bopomofo Extended",
275         "Katakana Phonetic Extensions",
276         "Enclosed CJK Letters and Months",
277         "CJK Compatibility",
278         "CJK Unified Ideographs Extension A",
279         "Yijing Hexagram Symbols",
280         "CJK Unified Ideographs",
281         "Yi Syllables",
282         "Yi Radicals",
283         "Hangul Syllables",
284         "Surrogates Area", // High Surrogates, High Private Use Surrogates, Low Surrogates
285
"Private Use Area",
286         "CJK Compatibility Ideographs",
287         "Alphabetic Presentation Forms",
288         "Arabic Presentation Forms-A",
289         "Variation Selectors",
290         "Combining Half Marks",
291         "CJK Compatibility Forms",
292         "Small Form Variants",
293         "Arabic Presentation Forms-B",
294         "Halfwidth and Fullwidth Forms",
295         "Specials",
296         "Linear B Syllabary",
297         "Linear B Ideograms",
298         "Aegean Numbers",
299         "Old Italic",
300         "Gothic",
301         "Ugaritic",
302         "Deseret",
303         "Shavian",
304         "Osmanya",
305         "Cypriot Syllabary",
306         "Byzantine Musical Symbols",
307         "Musical Symbols",
308         "Tai Xuan Jing Symbols",
309         "Mathematical Alphanumeric Symbols",
310         "CJK Unified Ideographs Extension B",
311         "CJK Compatibility Ideographs Supplement",
312         "Tags",
313         "Variation Selectors Supplement",
314         "Supplementary Private Use Area-A",
315         "Supplementary Private Use Area-B",
316         "Custom...",
317     };
318
319     private boolean useCustomRange = false;
320     private int[] customRange = { 0x0000, 0x007f };
321
322     /// Custom range dialog variables
323
private final JDialog customRangeDialog;
324     private final JTextField customRangeStart = new JTextField( "0000", 4 );
325     private final JTextField customRangeEnd = new JTextField( "007F", 4 );
326     private final int CUSTOM_RANGE_INDEX = UNICODE_RANGE_NAMES.length - 1;
327
328     /// Parent Font2DTest Object holder
329
private final Font2DTest parent;
330
331     public static final int SURROGATES_AREA_INDEX = 91;
332
333     public RangeMenu( Font2DTest demo, JFrame f ) {
334         super();
335         parent = demo;
336
337         for ( int i = 0; i < UNICODE_RANGE_NAMES.length; i++ )
338           addItem( UNICODE_RANGE_NAMES[i] );
339
340         setSelectedIndex( 0 );
341         addActionListener( this );
342
343         /// Set up custom range dialog...
344
customRangeDialog = new JDialog( f, "Custom Unicode Range", true );
345         customRangeDialog.setResizable( false );
346
347         JPanel dialogTop = new JPanel();
348         JPanel dialogBottom = new JPanel();
349         JButton okButton = new JButton("OK");
350         JLabel from = new JLabel( "From:" );
351         JLabel to = new JLabel("To:");
352         Font JavaDoc labelFont = new Font JavaDoc( "dialog", Font.BOLD, 12 );
353         from.setFont( labelFont );
354         to.setFont( labelFont );
355         okButton.setFont( labelFont );
356
357         dialogTop.add( from );
358         dialogTop.add( customRangeStart );
359         dialogTop.add( to );
360         dialogTop.add( customRangeEnd );
361         dialogBottom.add( okButton );
362         okButton.addActionListener( this );
363
364         customRangeDialog.getContentPane().setLayout( new BorderLayout JavaDoc() );
365         customRangeDialog.getContentPane().add( "North", dialogTop );
366         customRangeDialog.getContentPane().add( "South", dialogBottom );
367         customRangeDialog.pack();
368     }
369
370     /// Return the range that is currently selected
371

372     public int[] getSelectedRange() {
373         if ( useCustomRange ) {
374             int startIndex, endIndex;
375             String JavaDoc startText, endText;
376             String JavaDoc empty = "";
377             try {
378                 startText = customRangeStart.getText().trim();
379                 endText = customRangeEnd.getText().trim();
380                 if ( startText.equals(empty) && !endText.equals(empty) ) {
381                     endIndex = Integer.parseInt( endText, 16 );
382                     startIndex = endIndex - 7*25;
383                 }
384                 else if ( !startText.equals(empty) && endText.equals(empty) ) {
385                     startIndex = Integer.parseInt( startText, 16 );
386                     endIndex = startIndex + 7*25;
387                 }
388                 else {
389                     startIndex = Integer.parseInt( customRangeStart.getText(), 16 );
390                     endIndex = Integer.parseInt( customRangeEnd.getText(), 16 );
391                 }
392             }
393             catch ( Exception JavaDoc e ) {
394                 /// Error in parsing the hex number ---
395
/// Reset the range to what it was before and return that
396
customRangeStart.setText( Integer.toString( customRange[0], 16 ));
397                 customRangeEnd.setText( Integer.toString( customRange[1], 16 ));
398                 return customRange;
399             }
400
401             if ( startIndex < 0 )
402               startIndex = 0;
403             if ( endIndex > 0xffff )
404               endIndex = 0xffff;
405             if ( startIndex > endIndex )
406               startIndex = endIndex;
407
408             customRange[0] = startIndex;
409             customRange[1] = endIndex;
410             return customRange;
411         }
412         else
413           return UNICODE_RANGES[ getSelectedIndex() ];
414     }
415
416     /// Function used by loadOptions in Font2DTest main panel
417
/// to reset setting and range selection
418
public void setSelectedRange( String JavaDoc name, int start, int end ) {
419         setSelectedItem( name );
420         customRange[0] = start;
421         customRange[1] = end;
422         parent.fireRangeChanged();
423     }
424
425     /// ActionListener interface function
426
/// ABP
427
/// moved JComboBox event code into this fcn from
428
/// itemStateChanged() method. Part of change to Swing.
429
public void actionPerformed( ActionEvent JavaDoc e ) {
430         Object JavaDoc source = e.getSource();
431         
432         if ( source instanceof JComboBox ) {
433             String JavaDoc rangeName = (String JavaDoc)((JComboBox)source).getSelectedItem();
434
435             if ( rangeName.equals("Custom...") ) {
436                     useCustomRange = true;
437                     customRangeDialog.setLocationRelativeTo(parent);
438                 customRangeDialog.show();
439             }
440             else {
441               useCustomRange = false;
442             }
443             parent.fireRangeChanged();
444     }
445     else if ( source instanceof JButton ) {
446             /// Since it is only "OK" button that sends any action here...
447
customRangeDialog.hide();
448         }
449     }
450 }
451
Popular Tags