001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common
008: * Development and Distribution License("CDDL") (collectively, the
009: * "License"). You may not use this file except in compliance with the
010: * License. You can obtain a copy of the License at
011: * http://www.netbeans.org/cddl-gplv2.html
012: * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
013: * specific language governing permissions and limitations under the
014: * License. When distributing the software, include this License Header
015: * Notice in each file and include the License file at
016: * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
017: * particular file as subject to the "Classpath" exception as provided
018: * by Sun in the GPL Version 2 section of the License file that
019: * accompanied this code. If applicable, add the following below the
020: * License Header, with the fields enclosed by brackets [] replaced by
021: * your own identifying information:
022: * "Portions Copyrighted [year] [name of copyright owner]"
023: *
024: * Contributor(s):
025: *
026: * The Original Software is NetBeans. The Initial Developer of the Original
027: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
028: * Microsystems, Inc. All Rights Reserved.
029: *
030: * If you wish your version of this file to be governed by only the CDDL
031: * or only the GPL Version 2, indicate your decision by adding
032: * "[Contributor] elects to include this software in this distribution
033: * under the [CDDL or GPL Version 2] license." If you do not indicate a
034: * single choice of license, a recipient has the option to distribute
035: * your version of this file under either the CDDL, the GPL Version 2 or
036: * to extend the choice of license to its licensees as provided above.
037: * However, if you add GPL Version 2 code and therefore, elected the GPL
038: * Version 2 license, then the option applies only if the new code is
039: * made subject to such option by the copyright holder.
040: */
041:
042: package org.netbeans.beaninfo.editors;
043:
044: import java.beans.PropertyEditorSupport;
045:
046: // bugfix# 9219 for attachEnv() method
047: import org.openide.explorer.propertysheet.ExPropertyEditor;
048: import org.openide.explorer.propertysheet.PropertyEnv;
049: import java.beans.FeatureDescriptor;
050: import org.openide.nodes.Node;
051:
052: /** A property editor for String class.
053: * @author Ian Formanek
054: * @version 1.00, 18 Sep, 1998
055: */
056: public class StringEditor extends PropertyEditorSupport implements
057: ExPropertyEditor {
058: private static boolean useRaw = Boolean
059: .getBoolean("netbeans.stringEditor.useRawCharacters");
060:
061: // bugfix# 9219 added editable field and isEditable() "getter" to be used in StringCustomEditor
062: private boolean editable = true;
063:
064: /** gets information if the text in editor should be editable or not */
065: public boolean isEditable() {
066: return (editable);
067: }
068:
069: /** sets new value */
070: public void setAsText(String s) {
071: if ("null".equals(s) && getValue() == null) // NOI18N
072: return;
073: setValue(s);
074: }
075:
076: public String getJavaInitializationString() {
077: String s = (String) getValue();
078: return "\"" + toAscii(s) + "\""; // NOI18N
079: }
080:
081: public boolean supportsCustomEditor() {
082: return customEd;
083: }
084:
085: public java.awt.Component getCustomEditor() {
086: Object val = getValue();
087: String s = ""; // NOI18N
088: if (val != null) {
089: s = val instanceof String ? (String) val : val.toString();
090: }
091: return new StringCustomEditor(s, isEditable(), oneline,
092: instructions, this , env); // NOI18N
093: }
094:
095: private static String toAscii(String str) {
096: StringBuffer buf = new StringBuffer(str.length() * 6); // x -> \u1234
097: char[] chars = str.toCharArray();
098: for (int i = 0; i < chars.length; i++) {
099: char c = chars[i];
100: switch (c) {
101: case '\b':
102: buf.append("\\b");
103: break; // NOI18N
104: case '\t':
105: buf.append("\\t");
106: break; // NOI18N
107: case '\n':
108: buf.append("\\n");
109: break; // NOI18N
110: case '\f':
111: buf.append("\\f");
112: break; // NOI18N
113: case '\r':
114: buf.append("\\r");
115: break; // NOI18N
116: case '\"':
117: buf.append("\\\"");
118: break; // NOI18N
119: // case '\'': buf.append("\\'"); break; // NOI18N
120: case '\\':
121: buf.append("\\\\");
122: break; // NOI18N
123: default:
124: if (c >= 0x0020 && (useRaw || c <= 0x007f))
125: buf.append(c);
126: else {
127: buf.append("\\u"); // NOI18N
128: String hex = Integer.toHexString(c);
129: for (int j = 0; j < 4 - hex.length(); j++)
130: buf.append('0');
131: buf.append(hex);
132: }
133: }
134: }
135: return buf.toString();
136: }
137:
138: private String instructions = null;
139: private boolean oneline = false;
140: private boolean customEd = true;
141: private PropertyEnv env;
142:
143: // bugfix# 9219 added attachEnv() method checking if the user canWrite in text box
144: public void attachEnv(PropertyEnv env) {
145: this .env = env;
146:
147: FeatureDescriptor desc = env.getFeatureDescriptor();
148: if (desc instanceof Node.Property) {
149: Node.Property prop = (Node.Property) desc;
150: editable = prop.canWrite();
151: //enh 29294 - support one-line editor & suppression of custom
152: //editor
153: instructions = (String) prop.getValue("instructions"); //NOI18N
154: oneline = Boolean.TRUE.equals(prop.getValue("oneline")); //NOI18N
155: customEd = !Boolean.TRUE.equals(prop
156: .getValue("suppressCustomEditor")); //NOI18N
157: }
158: }
159: }
|