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: * BoolEditor.java
043: *
044: * Created on February 28, 2003, 1:13 PM
045: */
046:
047: package org.netbeans.beaninfo.editors;
048:
049: import java.beans.*;
050: import org.openide.explorer.propertysheet.ExPropertyEditor;
051: import org.openide.util.NbBundle;
052:
053: /** Replacement editor for boolean primitive values which supports
054: * internationalization and alternate string values that
055: * can be supplied to the property editor via adding an array
056: * returning an array of two Strings (false then true) from
057: * <code>env.getFeatureDescriptor().getValue()</code>. These
058: * string values will then be used for getAsText, setAsText, and getTags.
059: * These strings should be correctly internationalized if supplied
060: * by a module. String value matching in setAsText is non-case-sensitive
061: * ("TRUE" and "tRue" are equivalent).
062: *
063: * @author Tim Boudreau
064: */
065: public class BoolEditor extends ExPropertyEditorSupport {
066: String[] stringValues = null;
067:
068: /** Creates a new instance of BoolEditor */
069: public BoolEditor() {
070: }
071:
072: protected void attachEnvImpl(
073: org.openide.explorer.propertysheet.PropertyEnv env) {
074: stringValues = (String[]) env.getFeatureDescriptor().getValue(
075: "stringValues"); //NOI18N
076: }
077:
078: /** Throws an EnvException if the stringValues key is not 2 items in length. */
079: protected void validateEnv(
080: org.openide.explorer.propertysheet.PropertyEnv env) {
081: if (stringValues != null) {
082: if (stringValues.length != 2) {
083: throw new EnvException(
084: "String value hint for boolean editor must contain exactly 2 "
085: + "items. The supplied value contains "
086: + stringValues.length + " items: "
087: + arrToStr(stringValues));
088: }
089: }
090: }
091:
092: private String getStringRep(boolean val) {
093: if (stringValues != null) {
094: return stringValues[val ? 0 : 1];
095: }
096: String result;
097: if (val) {
098: result = NbBundle.getMessage(BoolEditor.class, "TRUE"); //NOI18N
099: } else {
100: result = NbBundle.getMessage(BoolEditor.class, "FALSE"); //NOI18N
101: }
102: return result;
103: }
104:
105: /** Returns Boolean.TRUE, Boolean.FALSE or null in the case of an
106: * unrecognized string. */
107: private Boolean stringVal(String val) {
108: String valToTest = val.trim().toUpperCase();
109: String test = getStringRep(true).toUpperCase();
110: if (test.equals(valToTest))
111: return Boolean.TRUE;
112: test = getStringRep(false).toUpperCase();
113: if (test.equals(valToTest))
114: return Boolean.FALSE;
115: return null;
116: }
117:
118: public String getJavaInitializationString() {
119: Boolean val = (Boolean) getValue();
120: if (val == null)
121: return "null"; //NOI18N
122: return Boolean.TRUE.equals(getValue()) ? "true" : "false"; //NOI18N
123: }
124:
125: public String[] getTags() {
126: return new String[] { getStringRep(true), getStringRep(false) };
127: }
128:
129: public String getAsText() {
130: Boolean val = (Boolean) getValue();
131: if (val == null)
132: return NbBundle.getMessage(BoolEditor.class, "NULL");
133: return getStringRep(Boolean.TRUE.equals(getValue()));
134: }
135:
136: public void setAsText(String txt) {
137: Boolean val = stringVal(txt);
138: boolean newVal = val == null ? false : val.booleanValue();
139: setValue(newVal ? Boolean.TRUE : Boolean.FALSE);
140: }
141: }
|