KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > beaninfo > editors > CharEditor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.beaninfo.editors;
21
22 import java.beans.*;
23 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
24
25 /**
26  * Editor for Character.TYPE
27  * @author Petr Zajac, David Strupl
28  */

29 public class CharEditor extends PropertyEditorSupport implements EnhancedPropertyEditor {
30
31     /**
32      * Converts the char to String by either leaving
33      * the single char or by creating unicode escape.
34      */

35     public String JavaDoc getAsText () {
36         char value = ((Character JavaDoc)getValue()).charValue();
37         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc(6);
38         switch (value) {
39             case '\b': buf.append("\\b"); break; // NOI18N
40
case '\t': buf.append("\\t"); break; // NOI18N
41
case '\n': buf.append("\\n"); break; // NOI18N
42
case '\f': buf.append("\\f"); break; // NOI18N
43
case '\r': buf.append("\\r"); break; // NOI18N
44
case '\\': buf.append("\\\\"); break; // NOI18N
45
default:
46                 if (value >= 0x0020 && value <= 0x007f)
47                     buf.append(value);
48                 else {
49                     buf.append("\\u"); // NOI18N
50
String JavaDoc hex = Integer.toHexString(value);
51                     for (int j = 0; j < 4 - hex.length(); j++)
52                         buf.append('0');
53                     buf.append(hex);
54                 }
55         }
56         return buf.toString() ;
57     }
58     /**
59      * Set the property value by parsing given String.
60      * @param text The string to be parsed.
61      */

62     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
63         if (text.length() < 1) {
64             // ignore empty value
65
return;
66         }
67         char value = 0;
68         if (text.charAt(0) == '\\') {
69             // backslash means unicode escape sequence
70
char ch = text.length() >=2 ? text.charAt(1) : '\\';
71             switch (ch) {
72                 case 'b': value = '\b'; break;
73                 case 't': value = '\t'; break;
74                 case 'n': value = '\n'; break;
75                 case 'f': value = '\f'; break;
76                 case 'r': value = '\r'; break;
77                 case '\\': value = '\\' ; break;
78                 case 'u' :
79                     String JavaDoc num = text.substring(2,text.length());
80                     if (num.length () > 4) {
81                         // ignore longer strings
82
return;
83                     }
84                     try {
85                         int intValue = Integer.parseInt(num,16);
86                         value = (char) intValue;
87                         break;
88                     } catch (NumberFormatException JavaDoc nfe) {
89                         // ignore non parsable strings
90
return;
91                     }
92                 default:
93                         // ignore non-chars after backslash
94
return;
95                         
96             }
97         } else {
98             value = text.charAt(0);
99         }
100         setValue(Character.valueOf(value));
101     }
102     
103     /**
104      * Accepts Character and String values. If the argument is
105      * a String the first character is taken as the new value.
106      * @param v new value
107      */

108     public void setValue(Object JavaDoc newValue) throws IllegalArgumentException JavaDoc {
109         if (newValue instanceof Character JavaDoc ) {
110             super.setValue(newValue);
111             return;
112         }
113         if (newValue instanceof String JavaDoc) {
114             String JavaDoc text = (String JavaDoc ) newValue;
115             if (text.length() >= 1) {
116                 super.setValue(Character.valueOf(text.charAt(0)));
117                 return;
118             }
119         }
120         if (newValue == null ) {
121             super.setValue( Character.valueOf( '\u0000' ) ); // NOI18N
122
return;
123         }
124         
125         throw new IllegalArgumentException JavaDoc();
126     }
127     
128     /**
129      * This method is intended for use when generating Java code to set
130      * the value of the property. It should return a fragment of Java code
131      * that can be used to initialize a variable with the current property
132      * value.
133      * <p>
134      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
135      *
136      * @return A fragment of Java code representing an initializer for the
137      * current value.
138      */

139     public String JavaDoc getJavaInitializationString() {
140         if ( ((Character JavaDoc)getValue()).charValue() == '\'' )
141             return "'\\''"; // NOI18N
142
else
143             return "'" + getAsText() + "'"; // NOI18N
144
}
145
146     /**
147      * We don't support in place custom editor.
148      * @return custom property editor to be shown inside the property
149      * sheet.
150      */

151     public java.awt.Component JavaDoc getInPlaceCustomEditor () {
152         return null;
153     }
154     
155     /**
156      * We don't support in place custom editor.
157      * @return true if this PropertyEditor provides a enhanced in-place custom
158      * property editor, false otherwise
159      */

160     public boolean hasInPlaceCustomEditor () {
161         return false;
162     }
163     
164     /**
165      * @return true if this property editor provides tagged values and
166      * a custom strings in the choice should be accepted too, false otherwise
167      */

168     public boolean supportsEditingTaggedValues () {
169         return true;
170     }
171 }
172
Popular Tags