KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > tax > beans > editor > NullStringEditor


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 package org.netbeans.modules.xml.tax.beans.editor;
20
21 import java.awt.Component JavaDoc;
22 import java.beans.PropertyEditorSupport JavaDoc;
23 import java.beans.FeatureDescriptor JavaDoc;
24
25 import org.openide.nodes.Node;
26 import org.openide.explorer.propertysheet.ExPropertyEditor;
27 import org.openide.explorer.propertysheet.PropertyEnv;
28
29 /**
30  *
31  * @author Libor Kramolis
32  * @version 0.1
33  */

34 public class NullStringEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor {
35
36     /** */
37     protected static final String JavaDoc DEFAULT_NULL = Util.THIS.getString ("TEXT_DEFAULT");
38
39     /** */
40     private boolean editable;
41
42
43     //
44
// init
45
//
46

47     /** Creates new NullStringEditor */
48     public NullStringEditor () {
49         super();
50         editable = true;
51     }
52
53     
54     //
55
// PropertyEditor
56
//
57

58     /**
59      */

60     public void setAsText (String JavaDoc text) throws IllegalArgumentException JavaDoc {
61         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("NullStringEditor::setAsText: text = " + text); // NOI18N
62

63     if ( DEFAULT_NULL.equals (text) ) {
64         setValue (null);
65     } else if ( text.length() == 0 ) {
66         setValue (null);
67     } else {
68         setValue (text);
69     }
70     }
71
72     /**
73      */

74     public String JavaDoc getAsText () {
75     Object JavaDoc value = super.getValue();
76
77         if ( Util.THIS.isLoggable() ) /* then */ Util.THIS.debug ("NullStringEditor::getAsText: value = " + value); // NOI18N
78

79     if ( value == null ) {
80         return DEFAULT_NULL;
81     } else {
82         String JavaDoc text = value.toString();
83         if ( text.length() == 0) {
84         return DEFAULT_NULL;
85         }
86         return text;
87     }
88     }
89
90     /**
91      */

92     public boolean supportsCustomEditor () {
93         return true;
94     }
95
96     /**
97      */

98     public Component JavaDoc getCustomEditor () {
99         return new NullStringCustomEditor (this);
100     }
101
102     /**
103      */

104     public String JavaDoc getJavaInitializationString () {
105         String JavaDoc s = (String JavaDoc) getValue ();
106         return "\"" + toAscii (s) + "\""; // NOI18N
107
}
108
109
110     //
111
// ExPropertyEditor
112
//
113

114     /**
115      */

116     public void attachEnv (PropertyEnv env) {
117         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
118
119         if (desc instanceof Node.Property){
120             Node.Property prop = (Node.Property)desc;
121
122             editable = prop.canWrite();
123         }
124     }
125
126
127     //
128
// EnhancedPropertyEditor
129
//
130

131     /**
132      */

133     public boolean hasInPlaceCustomEditor () {
134         return false;
135     }
136
137     /**
138      */

139     public Component JavaDoc getInPlaceCustomEditor () {
140         return null;
141     }
142
143     /**
144      */

145     public boolean supportsEditingTaggedValues () {
146         return false;
147     }
148
149
150     //
151
// itself
152
//
153

154     /**
155      */

156     public boolean isEditable () {
157         return editable;
158     }
159
160     /**
161      */

162     private static String JavaDoc toAscii (String JavaDoc str) {
163         StringBuffer JavaDoc buf = new StringBuffer JavaDoc (str.length() * 6); // x -> \u1234
164
char[] chars = str.toCharArray();
165         for (int i = 0; i < chars.length; i++) {
166             char c = chars[i];
167             switch (c) {
168             case '\b': buf.append ("\\b"); break; // NOI18N
169
case '\t': buf.append ("\\t"); break; // NOI18N
170
case '\n': buf.append ("\\n"); break; // NOI18N
171
case '\f': buf.append ("\\f"); break; // NOI18N
172
case '\r': buf.append ("\\r"); break; // NOI18N
173
case '\"': buf.append ("\\\""); break; // NOI18N
174
// case '\'': buf.append ("\\'"); break; // NOI18N
175
case '\\': buf.append ("\\\\"); break; // NOI18N
176
default:
177                 if (c >= 0x0020 && c <= 0x007f)
178                     buf.append (c);
179                 else {
180                     buf.append ("\\u"); // NOI18N
181
String JavaDoc hex = Integer.toHexString (c);
182                     for (int j = 0; j < 4 - hex.length(); j++)
183                         buf.append ('0');
184                     buf.append (hex);
185                 }
186             }
187         }
188         return buf.toString();
189     }
190
191 }
192
Popular Tags