KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > form > editors > MnemonicEditor


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.modules.form.editors;
21
22 import java.beans.*;
23 import org.netbeans.modules.form.NamedPropertyEditor;
24 import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
25 import org.openide.util.NbBundle;
26
27 /**
28  * Editor for mnemonic property
29  * @author Josef Kozak
30  */

31 public class MnemonicEditor extends PropertyEditorSupport
32         implements EnhancedPropertyEditor, NamedPropertyEditor {
33
34     /**
35      * Converts the char to String by either leaving
36      * the single char or by creating unicode escape.
37      */

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

74     public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
75         if (text.length() < 1) {
76             setValue(new Integer JavaDoc(0));
77             return;
78         }
79         
80         if (text.length() == 1 && text.charAt(0) != '\\') {
81             setValue(new Character JavaDoc(text.charAt(0)));
82             return;
83         }
84                 
85         if (text.charAt(0) == '\\') {
86             // backslash means unicode escape sequence
87
char value = 0;
88             char ch = text.length() >=2 ? text.charAt(1) : '\\';
89             switch (ch) {
90                 case 'b': value = '\b'; break;
91                 case 't': value = '\t'; break;
92                 case 'n': value = '\n'; break;
93                 case 'f': value = '\f'; break;
94                 case 'r': value = '\r'; break;
95                 case '\\': value = '\\' ; break;
96                 case 'u' :
97                     String JavaDoc num = text.substring(2,text.length());
98                     if (num.length () > 4) {
99                         // ignore longer strings
100
return;
101                     }
102                     try {
103                         int intValue = Integer.parseInt(num,16);
104                         value = (char) intValue;
105                         break;
106                     } catch (NumberFormatException JavaDoc nfe) {
107                         // ignore non parsable strings
108
return;
109                     }
110                 default:
111                         // ignore non-chars after backslash
112
return;
113                         
114             }
115             setValue(new Character JavaDoc(value));
116             return;
117         }
118         
119         try {
120             setValue(new Integer JavaDoc(text));
121             return;
122         } catch (NumberFormatException JavaDoc e) {
123             setValue(text);
124             return;
125         }
126         
127     }
128     
129     /**
130      * Accepts Integer, Character and String values. If the argument is
131      * a String the first character is taken as the new value.
132      * @param newValue new value
133      */

134     public void setValue(Object JavaDoc newValue) throws IllegalArgumentException JavaDoc {
135         if (newValue instanceof Integer JavaDoc) {
136             super.setValue(newValue);
137             return;
138         }
139         if (newValue instanceof Character JavaDoc) {
140             super.setValue(new Integer JavaDoc((int)(((Character JavaDoc)newValue).charValue())));
141             return;
142         }
143         if (newValue instanceof String JavaDoc) {
144             String JavaDoc text = (String JavaDoc ) newValue;
145             if (text.length() >= 1) {
146                 super.setValue(new Integer JavaDoc((int)text.charAt(0)));
147                 return;
148             }
149         }
150         throw new IllegalArgumentException JavaDoc();
151     }
152     
153     /**
154      * This method is intended for use when generating Java code to set
155      * the value of the property. It should return a fragment of Java code
156      * that can be used to initialize a variable with the current property
157      * value.
158      * <p>
159      * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
160      *
161      * @return A fragment of Java code representing an initializer for the
162      * current value.
163      */

164     public String JavaDoc getJavaInitializationString() {
165     return "'" + getAsText() + "'"; // NOI18N
166
}
167
168     /**
169      * We don't support in place custom editor.
170      * @return custom property editor to be shown inside the property
171      * sheet.
172      */

173     public java.awt.Component JavaDoc getInPlaceCustomEditor () {
174         return null;
175     }
176     
177     /**
178      * We don't support in place custom editor.
179      * @return true if this PropertyEditor provides a enhanced in-place custom
180      * property editor, false otherwise
181      */

182     public boolean hasInPlaceCustomEditor () {
183         return false;
184     }
185     
186     /**
187      * @return true if this property editor provides tagged values and
188      * a custom strings in the choice should be accepted too, false otherwise
189      */

190     public boolean supportsEditingTaggedValues () {
191         return true;
192     }
193     
194     // NamedPropertyEditor implementation
195
public String JavaDoc getDisplayName() {
196         return NbBundle.getBundle(getClass()).getString("CTL_MnemonicsEditor_DisplayName"); // NOI18N
197
}
198
199 }
200
Popular Tags