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-2006 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.vmd.properties;
043:
044: import java.beans.PropertyEditor;
045: import java.lang.reflect.InvocationTargetException;
046: import java.util.List;
047: import org.netbeans.modules.vmd.api.model.DesignComponent;
048: import org.netbeans.modules.vmd.api.model.PropertyValue;
049: import org.netbeans.modules.vmd.api.properties.DesignPropertyEditor;
050: import org.netbeans.modules.vmd.api.properties.DesignPropertyDescriptor;
051: import org.openide.nodes.PropertySupport;
052:
053: /**
054: *
055: * @author Karol Harezlak
056: */
057: public abstract class DefaultPropertySupport extends PropertySupport {
058:
059: public static final String PROPERTY_VALUE_NULL = "PROPERTY_NULL_VALUE_FOR_FEATURE_DESCRIPTOR"; //NOI18N //work around for hashmap which cant accept null
060: public static final String PROPERYT_INPLACE_EDITOR = "inplaceEditor"; //NOI18N
061: public static final String PROPERTY_CUSTOM_EDITOR_TITLE = "title"; //NOI18N
062: private PropertyValue propertyValue;
063: private PropertyEditor propertyEditor;
064: private List<String> propertyNames;
065: private DesignPropertyDescriptor designPropertyDescriptor;
066:
067: @SuppressWarnings(value="unchecked")
068: DefaultPropertySupport(
069: DesignPropertyDescriptor designerPropertyDescriptor,
070: Class type) {
071: super (designerPropertyDescriptor.getPropertyNames().iterator()
072: .next(), type, designerPropertyDescriptor
073: .getPropertyDisplayName(), designerPropertyDescriptor
074: .getPropertyToolTip(), true, true);
075: this .designPropertyDescriptor = designerPropertyDescriptor;
076: propertyEditor = designerPropertyDescriptor.getPropertyEditor();
077: propertyNames = designerPropertyDescriptor.getPropertyNames();
078: update();
079: if (getPropertyEditor() instanceof DesignPropertyEditor
080: && ((DesignPropertyEditor) getPropertyEditor())
081: .getInplaceEditor() != null) {
082: setValue(PROPERYT_INPLACE_EDITOR,
083: ((DesignPropertyEditor) getPropertyEditor())
084: .getInplaceEditor());
085: }
086: }
087:
088: @Override
089: public PropertyEditor getPropertyEditor() {
090: if (propertyEditor != null) {
091: return propertyEditor;
092: }
093: return super .getPropertyEditor();
094: }
095:
096: protected PropertyValue readPropertyValue(
097: final DesignComponent component, final String propertyName) {
098: assert component != null;
099: component.getDocument().getTransactionManager().readAccess(
100: new Runnable() {
101:
102: public void run() {
103: propertyValue = component
104: .readProperty(propertyName);
105: }
106: });
107: return propertyValue;
108: }
109:
110: public boolean canWrite() {
111: if (propertyEditor instanceof DesignPropertyEditor) {
112: return ((DesignPropertyEditor) propertyEditor).canWrite();
113: }
114: return super .canWrite();
115: }
116:
117: @SuppressWarnings(value="unchecked")
118: public void restoreDefaultValue() throws IllegalAccessException,
119: InvocationTargetException {
120: if (propertyEditor instanceof DesignPropertyEditor
121: && propertyNames != null && (!propertyNames.isEmpty())) {
122: final DesignPropertyEditor dpe = (DesignPropertyEditor) propertyEditor;
123: if (dpe.isResetToDefaultAutomatically()) {
124: setValue(dpe.getDefaultValue());
125: } else {
126: designPropertyDescriptor.getComponent().getDocument()
127: .getTransactionManager().writeAccess(
128: new Runnable() {
129: public void run() {
130: dpe
131: .customEditorResetToDefaultButtonPressed();
132: }
133: });
134: }
135: } else {
136: super .restoreDefaultValue();
137: }
138: }
139:
140: public boolean isDefaultValue() {
141: if (propertyEditor instanceof DesignPropertyEditor) {
142: return ((DesignPropertyEditor) propertyEditor)
143: .isDefaultValue();
144: }
145: return super .isDefaultValue();
146: }
147:
148: public boolean supportsDefaultValue() {
149: if (propertyEditor instanceof DesignPropertyEditor) {
150: return ((DesignPropertyEditor) propertyEditor)
151: .supportsDefaultValue();
152: }
153: return super .supportsDefaultValue();
154: }
155:
156: protected DesignPropertyDescriptor getDesignPropertyDescriptor() {
157: return designPropertyDescriptor;
158: }
159:
160: protected abstract void update();
161: }
|