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:
042: package org.netbeans.modules.websvc.saas.ui.wizards;
043:
044: import java.util.List;
045: import javax.swing.DefaultCellEditor;
046: import javax.swing.table.TableCellEditor;
047: import org.netbeans.swing.outline.OutlineModel;
048: import org.netbeans.swing.outline.NodeRowModel;
049: import javax.swing.tree.DefaultMutableTreeNode;
050: import java.util.Calendar;
051: import java.util.Date;
052: import java.text.DateFormat;
053: import java.text.ParseException;
054: import java.net.URI;
055: import java.net.URISyntaxException;
056:
057: import java.math.BigDecimal;
058: import java.math.BigInteger;
059: import java.awt.Component;
060:
061: import javax.swing.JTextField;
062: import javax.swing.JComboBox;
063: import java.awt.event.ActionEvent;
064: import org.openide.ErrorManager;
065:
066: /**
067: *
068: * @author david
069: */
070: public class TypeCellEditor extends DefaultCellEditor implements
071: TableCellEditor {
072: Component lastComponent;
073: String type;
074: private ClassLoader classLoader;
075:
076: /** Creates a new instance of TypeCellRenderer */
077: public TypeCellEditor(ClassLoader loader) {
078: super (new JTextField());
079: this .setClickCountToStart(1);
080: this .classLoader = loader;
081: }
082:
083: public void cancelCellEditing() {
084: return;
085: }
086:
087: public boolean stopCellEditing() {
088: return super .stopCellEditing();
089: }
090:
091: /**
092: * return the value of the last component.
093: */
094: public Object getCellEditorValue() {
095: if (null == type) {
096: return ((JTextField) lastComponent).getText();
097: } else {
098: if (lastComponent instanceof JTextField) {
099: String valueString = ((JTextField) lastComponent)
100: .getText();
101: Object value = createValue(valueString);
102: return value;
103: } else if (lastComponent instanceof JComboBox) {
104: return ((JComboBox) lastComponent).getSelectedItem();
105: } else
106: return null;
107: }
108:
109: }
110:
111: public java.awt.Component getTableCellEditorComponent(
112: javax.swing.JTable table, Object value, boolean isSelected,
113: int row, int column) {
114: /**
115: * We need to create the correct editing component for the type of field we have.
116: * JavaSimpleTypes all except Date and Calendar - JTextField()
117: * JavaEnumerationType - JComboBox
118: */
119:
120: /**
121: * First, we need to get the JavaType for the node of the object to be edited.
122: */
123:
124: NodeRowModel rowModel = ((OutlineModel) table.getModel())
125: .getRowNodeModel();
126: DefaultMutableTreeNode node = (DefaultMutableTreeNode) rowModel
127: .getNodeForRow(row);
128: /**
129: * Now depending on the type, create a component to edit/display the type.
130: */
131: if (null == node.getUserObject()) {
132: JTextField txtField = new JTextField();
133: txtField.setText((String) value);
134: lastComponent = (Component) txtField;
135:
136: } else {
137: TypeNodeData data = (TypeNodeData) node.getUserObject();
138: type = data.getTypeClass();
139:
140: if (ReflectionHelper.isSimpleType(type, classLoader)) {
141: /**
142: * If the type is boolean or Boolean, create a JComboBox with true,false
143: */
144: if (type.equalsIgnoreCase(boolean.class.getName())
145: || type.equalsIgnoreCase(Boolean.class
146: .getName())) {
147: JComboBox combo = new JComboBox();
148: lastComponent = (Component) combo;
149: combo.addItem(new Boolean(true));
150: combo.addItem(new Boolean(false));
151:
152: /**
153: * Set the value as the current Enumeration value.
154: */
155:
156: Object parameterValue = data.getTypeValue();
157:
158: combo.setSelectedItem(parameterValue);
159: combo
160: .addActionListener(new java.awt.event.ActionListener() {
161: public void actionPerformed(
162: java.awt.event.ActionEvent evt) {
163: comboActionPerformed(evt);
164: }
165: });
166: } else {
167:
168: JTextField txtField = new JTextField();
169: /**
170: * figure out what kind of simple field this is to set the value.
171: */
172: if (null != value) {
173: txtField.setText(value.toString());
174: }
175: lastComponent = (Component) txtField;
176: }
177:
178: } else if (ReflectionHelper
179: .isEnumeration(type, classLoader)) {
180: try {
181: JComboBox combo = new JComboBox();
182: List<String> enumTypes = ReflectionHelper
183: .getEnumerationValues(type, classLoader);
184: for (String enumType : enumTypes) {
185: Object nextEnum = ReflectionHelper
186: .getEnumeration(type, enumType,
187: classLoader);
188: combo.addItem(nextEnum);
189: }
190: lastComponent = combo;
191:
192: combo.setSelectedItem(data.getTypeValue());
193: combo
194: .addActionListener(new java.awt.event.ActionListener() {
195:
196: public void actionPerformed(
197: java.awt.event.ActionEvent evt) {
198: comboActionPerformed(evt);
199: }
200: });
201: } catch (WebServiceReflectionException wsre) {
202: Throwable cause = wsre.getCause();
203: ErrorManager.getDefault().notify(cause);
204: ErrorManager
205: .getDefault()
206: .log(
207: this .getClass().getName()
208: + ": Error retrieving Enum type on: "
209: + "WebServiceReflectionException="
210: + cause);
211: }
212: }
213: }
214:
215: return lastComponent;
216: }
217:
218: private void comboActionPerformed(ActionEvent evt) {
219: JComboBox combo = (JComboBox) evt.getSource();
220: this .fireEditingStopped();
221:
222: }
223:
224: private Object createValue(String inValue) {
225: Object returnValue = null;
226: String currentType = type;
227:
228: if (currentType.equalsIgnoreCase("int")
229: || currentType.equalsIgnoreCase("java.lang.Integer")) {
230: try {
231: returnValue = new Integer(inValue);
232: } catch (NumberFormatException nfe) {
233: returnValue = new Integer(0);
234: }
235: } else if (currentType.equalsIgnoreCase("byte")
236: || currentType.equalsIgnoreCase("java.lang.Byte")) {
237: try {
238: returnValue = new Byte(inValue);
239: } catch (NumberFormatException nfe) {
240: returnValue = new Byte("0");
241: }
242: } else if (currentType.equalsIgnoreCase("boolean")
243: || currentType.equalsIgnoreCase("java.lang.Boolean")) {
244: try {
245: returnValue = new Boolean(inValue);
246: } catch (NumberFormatException nfe) {
247: returnValue = new Boolean(false);
248: }
249: } else if (currentType.equalsIgnoreCase("float")
250: || currentType.equalsIgnoreCase("java.lang.Float")) {
251: try {
252: returnValue = new Float(inValue);
253: } catch (NumberFormatException nfe) {
254: returnValue = new Float(0);
255: }
256: } else if (currentType.equalsIgnoreCase("double")
257: || currentType.equalsIgnoreCase("java.lang.Double")) {
258: try {
259: returnValue = new Double(inValue);
260: } catch (NumberFormatException nfe) {
261: returnValue = new Double(0);
262: }
263: } else if (currentType.equalsIgnoreCase("long")
264: || currentType.equalsIgnoreCase("java.lang.Long")) {
265: try {
266: returnValue = new Long(inValue);
267: } catch (NumberFormatException nfe) {
268: returnValue = new Long(0);
269: }
270: } else if (currentType.equalsIgnoreCase("short")
271: || currentType.equalsIgnoreCase("java.lang.Short")) {
272: try {
273: returnValue = new Short(inValue);
274: } catch (NumberFormatException nfe) {
275: returnValue = new Short(" ");
276: }
277: } else if (currentType.equalsIgnoreCase("java.lang.String")) {
278: returnValue = inValue;
279: } else if (currentType.equalsIgnoreCase("java.math.BigDecimal")) {
280: try {
281: returnValue = new BigDecimal(inValue);
282: } catch (NumberFormatException nfe) {
283: returnValue = new BigDecimal(0);
284: }
285: } else if (currentType.equalsIgnoreCase("java.math.BigInteger")) {
286: try {
287: returnValue = new BigInteger(inValue);
288: } catch (NumberFormatException nfe) {
289: returnValue = new BigInteger("0");
290: }
291: } else if (currentType.equalsIgnoreCase("java.net.URI")) {
292: try {
293: returnValue = new URI(inValue);
294: } catch (URISyntaxException uri) {
295: try {
296: returnValue = new URI("http://java.sun.com");
297: } catch (URISyntaxException uri2) {
298: }
299: }
300: } else if (currentType.equalsIgnoreCase("java.util.Calendar")) {
301: returnValue = Calendar.getInstance();
302: } else if (currentType.equalsIgnoreCase("java.util.Date")) {
303: try {
304: returnValue = DateFormat.getInstance().parse(inValue);
305: } catch (ParseException pe) {
306: returnValue = new Date();
307: }
308: }
309:
310: return returnValue;
311:
312: }
313:
314: }
|