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