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-2007 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: package com.sun.jsfcl.std;
042:
043: import java.awt.Component;
044: import java.beans.PropertyEditorSupport;
045: import javax.faces.convert.BooleanConverter;
046: import javax.faces.convert.ByteConverter;
047: import javax.faces.convert.CharacterConverter;
048: import javax.faces.convert.Converter;
049: import javax.faces.convert.DateTimeConverter;
050: import javax.faces.convert.DoubleConverter;
051: import javax.faces.convert.FloatConverter;
052: import javax.faces.convert.IntegerConverter;
053: import javax.faces.convert.LongConverter;
054: import javax.faces.convert.NumberConverter;
055: import javax.faces.convert.ShortConverter;
056: import javax.faces.convert.BigDecimalConverter;
057: import javax.faces.el.ValueBinding;
058: import com.sun.jsfcl.util.ComponentBundle;
059: import com.sun.rave.designtime.DesignBean;
060: import com.sun.rave.designtime.DesignProperty;
061: import com.sun.rave.designtime.PropertyEditor2;
062: import com.sun.rave.designtime.faces.FacesBindingPropertyEditor;
063:
064: public class ConverterPropertyEditor extends PropertyEditorSupport
065: implements PropertyEditor2, FacesBindingPropertyEditor {
066: // Private variables
067:
068: private static final ComponentBundle bundle = ComponentBundle
069: .getBundle(ConverterPropertyEditor.class);
070:
071: private DesignProperty liveProperty;
072:
073: private Class[] classes = new Class[] { null,
074: BigDecimalConverter.class, BooleanConverter.class,
075: ByteConverter.class, CharacterConverter.class,
076: DateTimeConverter.class, DoubleConverter.class,
077: FloatConverter.class, IntegerConverter.class,
078: LongConverter.class, NumberConverter.class,
079: ShortConverter.class };
080:
081: private String[] prettyClasses = new String[] {
082: bundle.getMessage("parenNewBDC"), //NOI18N
083: bundle.getMessage("parenNewBoolC"), //NOI18N
084: bundle.getMessage("parenNewByteC"), //NOI18N
085: bundle.getMessage("parentNewCC"), //NOI18N
086: bundle.getMessage("parenNewDTC"), //NOI18N
087: bundle.getMessage("parenNewDC"), //NOI18N
088: bundle.getMessage("parenNewFC"), //NOI18N
089: bundle.getMessage("parenNewIC"), //NOI18N
090: bundle.getMessage("parenNewLC"), //NOI18N
091: bundle.getMessage("parenNewNC"), //NOI18N
092: bundle.getMessage("parenNewSC"), //NOI18N
093: bundle.getMessage("parenNewSTC"), //NOI18N
094: bundle.getMessage("parenNewCLC") //NOI18N
095: };
096:
097: // Methods
098:
099: public String[] getTags() {
100: DesignBean[] beans = getConverterBeans();
101: String[] tags = new String[beans.length + prettyClasses.length
102: + 1];
103:
104: int index = 0;
105: tags[index++] = "";
106: for (int i = 0; i < beans.length; i++) {
107: tags[index++] = beans[i].getInstanceName();
108: }
109: for (int i = 0; i < prettyClasses.length; i++) {
110: tags[index++] = prettyClasses[i];
111: }
112: return tags;
113: }
114:
115: public void setAsText(String text) throws IllegalArgumentException {
116: Converter converter = null;
117: DesignBean[] beans = getConverterBeans();
118:
119: if (text == null || text.trim().length() == 0) {
120: converter = null;
121: this .setValue(null);
122: return;
123: }
124:
125: for (int i = 0; i < beans.length; i++) {
126: if (beans[i].getInstanceName().equals(text)) {
127: converter = (Converter) beans[i].getInstance();
128: break;
129: }
130: }
131:
132: if (converter == null) {
133: for (int i = 0; i < prettyClasses.length; i++) {
134: if (prettyClasses[i].equals(text)) {
135: // Need to shift the index by 1 since the first one in classes array is null
136: DesignBean createResult = liveProperty
137: .getDesignBean().getDesignContext()
138: .createBean(classes[i + 1].getName(), null,
139: null);
140: if (createResult != null) {
141: converter = (Converter) createResult
142: .getInstance();
143: }
144:
145: break;
146: }
147: }
148: }
149: setValue(converter);
150: }
151:
152: public String getAsText() {
153: Object value = getValue();
154: if (value instanceof ValueBinding) {
155: return ((ValueBinding) value).getExpressionString();
156: }
157: if (value instanceof String) {
158: return (String) value;
159: }
160: DesignBean designBean = getDesignBean();
161: if (designBean != null) {
162: if (designBean.getInstance() instanceof NumberConverter) {
163: // Here are the defautls the NumberConverter has:
164: // - min integer digits to 1
165: // - max integer digits to 40
166: // - min fractional digites to 0
167: // - max fractional digits to 3
168: designBean.getProperty("minIntegerDigits").setValue(
169: new Integer(1));
170: designBean.getProperty("maxIntegerDigits").setValue(
171: new Integer(40));
172: designBean.getProperty("minFractionDigits").setValue(
173: new Integer(0));
174: designBean.getProperty("maxFractionDigits").setValue(
175: new Integer(3));
176: }
177: }
178: return (designBean == null) ? "" : designBean.getInstanceName(); //NOI18N
179: }
180:
181: public String getJavaInitializationString() {
182: return getAsText();
183: }
184:
185: public boolean supportsCustomEditor() {
186: return false;
187: }
188:
189: public Component getCustomEditor() {
190: return null;
191: }
192:
193: public void setDesignProperty(DesignProperty liveProperty) {
194: this .liveProperty = liveProperty;
195: }
196:
197: private DesignBean getDesignBean() {
198:
199: Object value = getValue();
200: DesignBean[] lbeans = getConverterBeans();
201: for (int i = 0; i < lbeans.length; i++) {
202: if (lbeans[i].getInstance() == value) {
203: return lbeans[i];
204: }
205: }
206: return null;
207: }
208:
209: private DesignBean[] getConverterBeans() {
210: return (liveProperty == null) ? new DesignBean[0]
211: : liveProperty.getDesignBean().getDesignContext()
212: .getBeansOfType(Converter.class);
213: }
214: }
|