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.xml.wsdl.ui.api.property;
043:
044: import org.netbeans.modules.xml.wsdl.model.ExtensibilityElement;
045: import org.netbeans.modules.xml.wsdl.ui.commands.ConstraintNamedPropertyAdapter;
046: import org.netbeans.modules.xml.wsdl.ui.commands.NamedPropertyAdapter;
047: import org.netbeans.modules.xml.wsdl.ui.netbeans.module.Utility;
048: import org.netbeans.modules.xml.wsdl.ui.view.property.XSDBooleanAttributeProperty;
049: import org.netbeans.modules.xml.xam.NamedReferenceable;
050: import org.openide.util.NbBundle;
051:
052: public class ExtensibilityElementPropertyAdapter extends
053: PropertyAdapter {
054: private ExtensibilityElement element;
055: private String attributeName;
056: private String defaultValue;
057: private boolean supportsDefaultValue;
058: private String valueNotSetMessage = NbBundle.getMessage(
059: XSDBooleanAttributeProperty.class, "LBL_ValueNotSet");
060: private boolean isOptional;
061:
062: private NamedPropertyAdapter adapter;
063:
064: public ExtensibilityElementPropertyAdapter(
065: ExtensibilityElement element, String name,
066: boolean isOptional) {
067: super (element);
068: this .element = element;
069: this .attributeName = name;
070: this .isOptional = isOptional;
071:
072: //Provides refactoring support for name changes.
073: if (isNamedReferenceable()) {
074: if (name != null
075: && name.equals(NamedReferenceable.NAME_PROPERTY)) {
076: adapter = new ConstraintNamedPropertyAdapter(element) {
077:
078: @Override
079: public boolean isNameExists(String name) {
080: // TODO Auto-generated method stub
081: return false;
082: }
083:
084: };
085: }
086: }
087:
088: }
089:
090: public ExtensibilityElementPropertyAdapter(
091: ExtensibilityElement element, String name) {
092: this (element, name, false);
093: }
094:
095: public ExtensibilityElementPropertyAdapter(
096: ExtensibilityElement element, String name,
097: String defaultValue) {
098: this (element, name, false);
099: this .defaultValue = defaultValue;
100: this .supportsDefaultValue = true;
101: }
102:
103: /*
104: * this is the default implementation, subclasses can override this.
105: * @return value
106: */
107: public String getValue() {
108: if (adapter != null)
109: return adapter.getName();
110:
111: String value = element.getAttribute(attributeName);
112: if (value == null) {
113: value = "";
114: }
115: return value;
116: }
117:
118: /*
119: * this is the default implementation, subclasses can override this.
120: * @param value the value to be set
121: */
122: public void setValue(String value) {
123: if (value == null || value.trim().length() == 0
124: || value.equalsIgnoreCase(valueNotSetMessage)) {
125: value = null;
126: }
127:
128: if (adapter != null)
129: adapter.setName(value);
130:
131: String oldValue = element.getAttribute(attributeName);
132:
133: if ((oldValue == null ^ value == null)
134: || (oldValue != null && value != null && !oldValue
135: .equals(value))) {
136: boolean inTransaction = Utility.startTransaction(element
137: .getModel());
138: element.setAttribute(attributeName, value);
139: Utility.endTransaction(element.getModel(), inTransaction);
140: }
141: }
142:
143: /*
144: * generic setValue. if overridden, also override getValue
145: *
146: * @param val
147: */
148: public void setValue(Object val) {
149:
150: }
151:
152: public ExtensibilityElement getExtensibilityElement() {
153: return element;
154: }
155:
156: public String getDefaultValue() {
157: if (defaultValue == null) {
158: return valueNotSetMessage;
159: }
160: return defaultValue;
161: }
162:
163: public boolean supportsDefaultValue() {
164: return supportsDefaultValue && isWritable();
165: }
166:
167: public void setOptional(boolean bool) {
168: isOptional = bool;
169: }
170:
171: public boolean isOptional() {
172: return isOptional;
173: }
174:
175: public String getMessageForUnSet() {
176: return valueNotSetMessage;
177: }
178:
179: private boolean isNamedReferenceable() {
180: return element instanceof NamedReferenceable;
181: }
182: }
|