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.sql.framework.ui.editor.property.impl;
043:
044: import java.util.ArrayList;
045: import java.util.List;
046:
047: import org.netbeans.modules.sql.framework.ui.editor.property.IElement;
048: import org.netbeans.modules.sql.framework.ui.editor.property.IOptionProperty;
049: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyCustomizer;
050:
051: /**
052: * @author Ritesh Adval
053: * @version $Revision$
054: */
055: public class OptionPropertySupport extends BasicPropertySupport
056: implements IOptionProperty {
057:
058: private int defaultOption = 0;
059:
060: private ArrayList<IElement> optionList = new ArrayList<IElement>();
061:
062: public OptionPropertySupport() {
063: super ();
064: }
065:
066: /** Creates a new instance of OptionPropertySupport */
067: public OptionPropertySupport(String valueType) {
068: super (valueType);
069: }
070:
071: /**
072: * add a element in the node
073: *
074: * @param element element to add
075: */
076: @Override
077: public void add(IElement element) {
078: element.setParent(this );
079: optionList.add(element);
080: }
081:
082: public int getDefaultOption() {
083: return defaultOption;
084: }
085:
086: public BasicOption getOptionForDisplayName(String displayName) {
087: for (int i = 0; i < optionList.size(); i++) {
088: BasicOption option = (BasicOption) optionList.get(i);
089: if (displayName != null
090: && displayName.equals(option.getDisplayName())) {
091: return option;
092: }
093: }
094: return null;
095: }
096:
097: public BasicOption getOptionForValue(String value) {
098: for (int i = 0; i < optionList.size(); i++) {
099: BasicOption option = (BasicOption) optionList.get(i);
100: if (value != null && value.equals(option.getValue())) {
101: return option;
102: }
103: }
104: return null;
105: }
106:
107: public List getOptions() {
108: return optionList;
109: }
110:
111: public void setDefaultOption(String defOption) {
112: defaultOption = Integer.parseInt(defOption);
113: }
114:
115: /**
116: * set the optional property editor which can be used to edit this property
117: *
118: * @return property editor
119: */
120: @Override
121: public void setEditorClass(String editorClass) {
122: super .setEditorClass(editorClass);
123: if (this .getPropertyEditor() instanceof OptionListEditor) {
124: ((OptionListEditor) this .getPropertyEditor())
125: .setProperty(this );
126: }
127: }
128:
129: /**
130: * set the property customizer
131: *
132: * @param customizer customizer
133: */
134: @Override
135: public void setPropertyCustomizer(IPropertyCustomizer customizer) {
136: super .setPropertyCustomizer(customizer);
137: if (customizer == null) {
138: return;
139: }
140:
141: if (this .getPropertyEditor() instanceof OptionListEditor) {
142: OptionListEditor listEditor = (OptionListEditor) this
143: .getPropertyEditor();
144: listEditor.setCustomOptions(customizer.getOptions());
145:
146: }
147: }
148:
149: // override set value to handle Integer options; if optionProperty type is integer we
150: // want to convert passed String optiong value to integer
151: @Override
152: public void setValue(Object obj)
153: throws java.lang.IllegalAccessException,
154: java.lang.IllegalArgumentException,
155: java.lang.reflect.InvocationTargetException {
156: if (this .getValueType() == Integer.class
157: && obj instanceof String) {
158: try {
159: super .setValue(new Integer((String) obj));
160: } catch (NumberFormatException ex) {
161: ex.printStackTrace();
162: // log me
163: }
164: } else {
165: super.setValue(obj);
166: }
167: }
168: }
|