KickJava   Java API By Example, From Geeks To Geeks.

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


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.PropertyEditorSupport JavaDoc;
23
24 // bugfix# 9219 for attachEnv() method
25
import org.openide.explorer.propertysheet.ExPropertyEditor;
26 import org.openide.explorer.propertysheet.PropertyEnv;
27 import java.beans.FeatureDescriptor JavaDoc;
28 import org.openide.nodes.Node;
29
30
31 /** A property editor for String class.
32 * @author Ian Formanek
33 * @version 1.00, 18 Sep, 1998
34 */

35 public class StringEditor extends PropertyEditorSupport JavaDoc implements ExPropertyEditor
36 {
37     private static boolean useRaw = Boolean.getBoolean("netbeans.stringEditor.useRawCharacters");
38     
39    // bugfix# 9219 added editable field and isEditable() "getter" to be used in StringCustomEditor
40
private boolean editable=true;
41     /** gets information if the text in editor should be editable or not */
42     public boolean isEditable(){
43         return (editable);
44     }
45                 
46     /** sets new value */
47     public void setAsText(String JavaDoc s) {
48         if ( "null".equals( s ) && getValue() == null ) // NOI18N
49
return;
50         setValue(s);
51     }
52
53     public String JavaDoc getJavaInitializationString () {
54         String JavaDoc s = (String JavaDoc) getValue ();
55         return "\"" + toAscii(s) + "\""; // NOI18N
56
}
57
58     public boolean supportsCustomEditor () {
59         return customEd;
60     }
61
62     public java.awt.Component JavaDoc getCustomEditor () {
63         Object JavaDoc val = getValue();
64         String JavaDoc s = ""; // NOI18N
65
if (val != null) {
66             s = val instanceof String JavaDoc ? (String JavaDoc) val : val.toString();
67         }
68         return new StringCustomEditor (s, isEditable(), oneline, instructions, this, env); // NOI18N
69
}
70
71     private static String JavaDoc toAscii(String JavaDoc str) {
72         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(str.length() * 6); // x -> \u1234
73
char[] chars = str.toCharArray();
74         for (int i = 0; i < chars.length; i++) {
75             char c = chars[i];
76             switch (c) {
77             case '\b': buf.append("\\b"); break; // NOI18N
78
case '\t': buf.append("\\t"); break; // NOI18N
79
case '\n': buf.append("\\n"); break; // NOI18N
80
case '\f': buf.append("\\f"); break; // NOI18N
81
case '\r': buf.append("\\r"); break; // NOI18N
82
case '\"': buf.append("\\\""); break; // NOI18N
83
// case '\'': buf.append("\\'"); break; // NOI18N
84
case '\\': buf.append("\\\\"); break; // NOI18N
85
default:
86                 if (c >= 0x0020 && (useRaw || c <= 0x007f))
87                     buf.append(c);
88                 else {
89                     buf.append("\\u"); // NOI18N
90
String JavaDoc hex = Integer.toHexString(c);
91                     for (int j = 0; j < 4 - hex.length(); j++)
92                         buf.append('0');
93                     buf.append(hex);
94                 }
95             }
96         }
97         return buf.toString();
98     }
99     
100     private String JavaDoc instructions=null;
101     private boolean oneline=false;
102     private boolean customEd=true;
103     private PropertyEnv env;
104
105     // bugfix# 9219 added attachEnv() method checking if the user canWrite in text box
106
public void attachEnv(PropertyEnv env) {
107         this.env = env;
108
109         FeatureDescriptor JavaDoc desc = env.getFeatureDescriptor();
110         if (desc instanceof Node.Property){
111             Node.Property prop = (Node.Property)desc;
112             editable = prop.canWrite();
113             //enh 29294 - support one-line editor & suppression of custom
114
//editor
115
instructions = (String JavaDoc) prop.getValue ("instructions"); //NOI18N
116
oneline = Boolean.TRUE.equals (prop.getValue ("oneline")); //NOI18N
117
customEd = !Boolean.TRUE.equals (prop.getValue
118                 ("suppressCustomEditor")); //NOI18N
119
}
120     }
121 }
122
Popular Tags