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.beans.PropertyEditorSupport;
045: import java.util.List;
046:
047: /**
048: * @author Ritesh Adval
049: * @version $Revision$
050: */
051: public class OptionListEditor extends PropertyEditorSupport {
052:
053: List optionList;
054: private OptionPropertySupport optionS;
055: private String[] tags;
056: private Object value = "ddd";
057:
058: public OptionListEditor() {
059: super ();
060: }
061:
062: @Override
063: public String getAsText() {
064: try {
065: String optionVal = "" + optionS.getValue();
066: BasicOption option = getOptionForValue(optionVal);
067: if (option != null) {
068: return option.getDisplayName();
069: }
070:
071: // if we have a default option defined in xml
072: int defOpt = optionS.getDefaultOption();
073: if (defOpt < optionList.size()) {
074: option = (BasicOption) optionList.get(defOpt);
075: optionVal = option.getDisplayName();
076: }
077:
078: return optionVal;
079: } catch (Exception ex) {
080: ex.printStackTrace();
081: }
082:
083: return "";
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:
095: return null;
096: }
097:
098: public BasicOption getOptionForValue(String value1) {
099: for (int i = 0; i < optionList.size(); i++) {
100: BasicOption option = (BasicOption) optionList.get(i);
101: if (value1 != null && value1.equals(option.getValue())) {
102: return option;
103: }
104: }
105:
106: return null;
107: }
108:
109: @Override
110: public String[] getTags() {
111: tags = new String[optionList.size()];
112: for (int i = 0; i < optionList.size(); i++) {
113: BasicOption option = (BasicOption) optionList.get(i);
114: tags[i] = option.getDisplayName();
115: }
116:
117: return tags;
118: }
119:
120: @Override
121: public Object getValue() {
122: return value;
123: }
124:
125: @Override
126: public void setAsText(String text) {
127: BasicOption option = null;
128:
129: try {
130: option = getOptionForDisplayName(text);
131: if (option != null) {
132: optionS.setValue(option.getValue());
133: setValue(option.getValue());
134: } else {
135: optionS.setValue(text);
136: setValue(text);
137: }
138: } catch (Exception ex) {
139: ex.printStackTrace();
140: }
141: }
142:
143: public void setCustomOptions(List customOptions) {
144: this .optionList = customOptions;
145: }
146:
147: public void setProperty(OptionPropertySupport optionS) {
148: this .optionS = optionS;
149: optionList = optionS.getOptions();
150: }
151:
152: @Override
153: public void setValue(Object val) {
154: this.value = val;
155: }
156: }
|