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.*;
045: import org.openide.explorer.propertysheet.editors.EnhancedPropertyEditor;
046:
047: /**
048: * Editor for Character.TYPE
049: * @author Petr Zajac, David Strupl
050: */
051: public class CharEditor extends PropertyEditorSupport implements
052: EnhancedPropertyEditor {
053:
054: /**
055: * Converts the char to String by either leaving
056: * the single char or by creating unicode escape.
057: */
058: public String getAsText() {
059: char value = ((Character) getValue()).charValue();
060: final StringBuffer buf = new StringBuffer(6);
061: switch (value) {
062: case '\b':
063: buf.append("\\b");
064: break; // NOI18N
065: case '\t':
066: buf.append("\\t");
067: break; // NOI18N
068: case '\n':
069: buf.append("\\n");
070: break; // NOI18N
071: case '\f':
072: buf.append("\\f");
073: break; // NOI18N
074: case '\r':
075: buf.append("\\r");
076: break; // NOI18N
077: case '\\':
078: buf.append("\\\\");
079: break; // NOI18N
080: default:
081: if (value >= 0x0020 && value <= 0x007f)
082: buf.append(value);
083: else {
084: buf.append("\\u"); // NOI18N
085: String hex = Integer.toHexString(value);
086: for (int j = 0; j < 4 - hex.length(); j++)
087: buf.append('0');
088: buf.append(hex);
089: }
090: }
091: return buf.toString();
092: }
093:
094: /**
095: * Set the property value by parsing given String.
096: * @param text The string to be parsed.
097: */
098: public void setAsText(String text) throws IllegalArgumentException {
099: if (text.length() < 1) {
100: // ignore empty value
101: return;
102: }
103: char value = 0;
104: if (text.charAt(0) == '\\') {
105: // backslash means unicode escape sequence
106: char ch = text.length() >= 2 ? text.charAt(1) : '\\';
107: switch (ch) {
108: case 'b':
109: value = '\b';
110: break;
111: case 't':
112: value = '\t';
113: break;
114: case 'n':
115: value = '\n';
116: break;
117: case 'f':
118: value = '\f';
119: break;
120: case 'r':
121: value = '\r';
122: break;
123: case '\\':
124: value = '\\';
125: break;
126: case 'u':
127: String num = text.substring(2, text.length());
128: if (num.length() > 4) {
129: // ignore longer strings
130: return;
131: }
132: try {
133: int intValue = Integer.parseInt(num, 16);
134: value = (char) intValue;
135: break;
136: } catch (NumberFormatException nfe) {
137: // ignore non parsable strings
138: return;
139: }
140: default:
141: // ignore non-chars after backslash
142: return;
143:
144: }
145: } else {
146: value = text.charAt(0);
147: }
148: setValue(Character.valueOf(value));
149: }
150:
151: /**
152: * Accepts Character and String values. If the argument is
153: * a String the first character is taken as the new value.
154: * @param v new value
155: */
156: public void setValue(Object newValue)
157: throws IllegalArgumentException {
158: if (newValue instanceof Character) {
159: super .setValue(newValue);
160: return;
161: }
162: if (newValue instanceof String) {
163: String text = (String) newValue;
164: if (text.length() >= 1) {
165: super .setValue(Character.valueOf(text.charAt(0)));
166: return;
167: }
168: }
169: if (newValue == null) {
170: super .setValue(Character.valueOf('\u0000')); // NOI18N
171: return;
172: }
173:
174: throw new IllegalArgumentException();
175: }
176:
177: /**
178: * This method is intended for use when generating Java code to set
179: * the value of the property. It should return a fragment of Java code
180: * that can be used to initialize a variable with the current property
181: * value.
182: * <p>
183: * Example results are "2", "new Color(127,127,34)", "Color.orange", etc.
184: *
185: * @return A fragment of Java code representing an initializer for the
186: * current value.
187: */
188: public String getJavaInitializationString() {
189: if (((Character) getValue()).charValue() == '\'')
190: return "'\\''"; // NOI18N
191: else
192: return "'" + getAsText() + "'"; // NOI18N
193: }
194:
195: /**
196: * We don't support in place custom editor.
197: * @return custom property editor to be shown inside the property
198: * sheet.
199: */
200: public java.awt.Component getInPlaceCustomEditor() {
201: return null;
202: }
203:
204: /**
205: * We don't support in place custom editor.
206: * @return true if this PropertyEditor provides a enhanced in-place custom
207: * property editor, false otherwise
208: */
209: public boolean hasInPlaceCustomEditor() {
210: return false;
211: }
212:
213: /**
214: * @return true if this property editor provides tagged values and
215: * a custom strings in the choice should be accepted too, false otherwise
216: */
217: public boolean supportsEditingTaggedValues() {
218: return true;
219: }
220: }
|