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.PropertyEditor;
045: import java.util.Map;
046:
047: import org.netbeans.modules.sql.framework.ui.editor.property.INode;
048: import org.netbeans.modules.sql.framework.ui.editor.property.IProperty;
049: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyCustomizer;
050: import org.openide.nodes.Node;
051:
052: /**
053: * @author Ritesh Adval
054: * @version $Revision$
055: */
056: public class PropUtil {
057:
058: public static void setInitialPropertyValues(Map map,
059: Map customizerMap, PropertyNode pNode) {
060: Node.PropertySet[] pSets = pNode.getPropertySets();
061: for (int i = 0; i < pSets.length; i++) {
062: Node.PropertySet pSet = pSets[i];
063: setInitialPropertyValues(map, customizerMap, pSet);
064: }
065: }
066:
067: public static void setInitialPropertyValues(Object bean,
068: Map customizerMap, PropertyNode pNode) {
069: Node.PropertySet[] pSets = pNode.getPropertySets();
070: for (int i = 0; i < pSets.length; i++) {
071: Node.PropertySet pSet = pSets[i];
072: setInitialPropertyValues(bean, customizerMap, pSet);
073: }
074: }
075:
076: public static void setModifiedPropertyValues(Object bean,
077: PropertyNode pNode) {
078: Node.PropertySet[] pSets = pNode.getPropertySets();
079: for (int i = 0; i < pSets.length; i++) {
080: Node.PropertySet pSet = pSets[i];
081: setModifiedPropertyValues(bean, pSet);
082: }
083: }
084:
085: @SuppressWarnings("unchecked")
086: private static void setInitialPropertyValues(Map map,
087: Map customizerMap, Node.PropertySet pSet) {
088: Node.Property[] properties = pSet.getProperties();
089: for (int i = 0; i < properties.length; i++) {
090: Node.Property property = properties[i];
091: Object value = map.get(property.getName());
092: if (value != null) {
093: try {
094: IProperty p = (IProperty) property;
095: setPropertyCustomizer(customizerMap, p);
096: property.setValue(value);
097: } catch (Exception ex) {
098: }
099: }
100: }
101: }
102:
103: @SuppressWarnings("unchecked")
104: private static void setInitialPropertyValues(Object bean,
105: Map customizerMap, Node.PropertySet pSet) {
106: Node.Property[] properties = pSet.getProperties();
107: for (int i = 0; i < properties.length; i++) {
108: Node.Property property = properties[i];
109: try {
110: IProperty p = (IProperty) property;
111: setPropertyCustomizer(customizerMap, p);
112: property.setValue(TemplateFactory.invokeGetter(bean,
113: property.getName(), null, null));
114:
115: // check if property has a custom editor if so get it from the bean
116: if (p.isCustomEditor()) {
117: p
118: .setPropertyEditor((PropertyEditor) TemplateFactory
119: .invokeGetter(
120: bean,
121: "customEditor",
122: new Class[] { Node.Property.class },
123: new Object[] { property }));
124: }
125: } catch (Exception ex) {
126: ex.printStackTrace();
127: }
128: }
129: }
130:
131: private static void setModifiedPropertyValues(Object bean,
132: Node.PropertySet pSet) {
133: Node.Property[] properties = pSet.getProperties();
134: for (int i = 0; i < properties.length; i++) {
135: Node.Property property = properties[i];
136: IProperty p = (IProperty) property;
137: if (p.isReadOnly()) {
138: continue;
139: }
140:
141: try {
142: // if value is differnt then only set it
143: Object oldVal = TemplateFactory.invokeGetter(bean,
144: property.getName(), null, null);
145: if (oldVal != null
146: && oldVal.equals(property.getValue())) {
147: continue;
148: }
149:
150: TemplateFactory.invokeSetter(bean, property.getName(),
151: property.getValue());
152: } catch (Exception ex) {
153: ex.printStackTrace();
154: }
155: }
156: }
157:
158: private static void setPropertyCustomizer(Map customizerMap,
159: IProperty property) {
160: if (customizerMap == null) {
161: return;
162: }
163:
164: IPropertyCustomizer pCustomizer = (IPropertyCustomizer) customizerMap
165: .get(property.getName());
166: if (pCustomizer != null) {
167: String gName = pCustomizer.getPropertyGroupName();
168: INode pgNode = property.getParent();
169: if (gName != null && pgNode != null
170: && gName.equals(pgNode.getName())) {
171: property.setPropertyCustomizer(pCustomizer);
172: }
173: }
174: }
175:
176: /** Creates a new instance of PropUtil */
177: private PropUtil() {
178: }
179: }
|