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: package org.netbeans.modules.sql.framework.ui.editor.property.impl;
042:
043: import java.awt.BorderLayout;
044: import java.awt.Component;
045: import java.util.ArrayList;
046: import java.util.HashMap;
047: import java.util.List;
048:
049: import javax.swing.JLabel;
050: import javax.swing.JPanel;
051:
052: import org.netbeans.modules.sql.framework.ui.editor.property.IProperty;
053: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertyGroup;
054: import org.netbeans.modules.sql.framework.ui.editor.property.IPropertySheet;
055: import org.openide.explorer.propertysheet.PropertySheet;
056: import org.openide.nodes.Node;
057:
058: /**
059: * @author Ritesh Adval
060: * @version $Revision$
061: */
062: public class BasicPropertySheet extends JPanel implements
063: IPropertySheet {
064:
065: private Object bean;
066: private PropertyNode pNode;
067:
068: /** Creates a new instance of PropertySheet */
069: public BasicPropertySheet(Object bean, PropertyNode node) {
070: this (node);
071: this .bean = bean;
072: }
073:
074: /** Creates a new instance of PropertySheet */
075: public BasicPropertySheet(PropertyNode node) {
076: this .pNode = node;
077: init();
078: }
079:
080: /**
081: * commit the changes in property to the bean
082: */
083: public void commitChanges() {
084: if (bean != null) {
085: PropUtil.setModifiedPropertyValues(bean, pNode);
086: }
087: }
088:
089: /**
090: * get a list of IProperty objects
091: *
092: * @return list of IProperty objects
093: */
094: public List<IProperty> getProperties() {
095: ArrayList<IProperty> list = new ArrayList<IProperty>();
096:
097: Node.PropertySet[] pSets = pNode.getPropertySets();
098: for (int i = 0; i < pSets.length; i++) {
099: Node.PropertySet pSet = pSets[i];
100: Node.Property[] properties = pSet.getProperties();
101: for (int j = 0; j < properties.length; j++) {
102: Node.Property property = properties[j];
103: IProperty p = (IProperty) property;
104: list.add(p);
105: }
106: }
107: return list;
108: }
109:
110: /**
111: * get a property group based on its name
112: *
113: * @return property group
114: */
115: public IPropertyGroup getPropertyGroup(String groupName) {
116: return pNode.getPropertyGroup(groupName);
117: }
118:
119: /**
120: * get thr ui component used for displaying properties
121: *
122: * @return ui component
123: */
124: public Component getPropertySheet() {
125: return this ;
126: }
127:
128: /**
129: * get current property values
130: *
131: * @return map of property name and values
132: */
133: public HashMap getPropertyValues() {
134: return getPropertyValues(pNode);
135: }
136:
137: /**
138: * set the bean whose properties are reflected set initialze value from bean to noe
139: *
140: * @param bean bean
141: */
142: public void setBean(Object bean) {
143: this .bean = bean;
144: PropUtil.setInitialPropertyValues(bean, null, pNode);
145: }
146:
147: /**
148: * set the bean whose properties are reflected. set the template node too.
149: *
150: * @param bean bean
151: * @param node xml desc node
152: */
153: public void setBean(Object bean, Node node) {
154: this .pNode = (PropertyNode) node;
155: setBean(bean); // set initialze value from bean to noe
156: init();
157: }
158:
159: private void addPropertyValues(HashMap<String, Object> map,
160: Node.PropertySet pSet) {
161: Node.Property[] properties = pSet.getProperties();
162: for (int i = 0; i < properties.length; i++) {
163: Node.Property property = properties[i];
164: IProperty p = (IProperty) property;
165: if (p.isReadOnly()) {
166: continue;
167: }
168:
169: try {
170: map.put(property.getName(), property.getValue());
171: } catch (Exception ex) {
172: ex.printStackTrace(); // XXX: use logger
173: }
174: }
175: }
176:
177: private HashMap<String, Object> getPropertyValues(PropertyNode node) {
178: HashMap<String, Object> map = new HashMap<String, Object>();
179: Node.PropertySet[] pSets = node.getPropertySets();
180: for (int i = 0; i < pSets.length; i++) {
181: Node.PropertySet pSet = pSets[i];
182: addPropertyValues(map, pSet);
183: }
184: return map;
185: }
186:
187: private void init() {
188: this .setLayout(new BorderLayout());
189: PropertySheet pSheet = new PropertySheet();
190: // hack don't show tool bar
191: pSheet.add(new JLabel(), BorderLayout.NORTH);
192: this .add(pSheet, BorderLayout.CENTER);
193: pSheet.setNodes(new Node[] { pNode });
194:
195: // set sorting mode to unsorted so that we get properties sorted as defined by
196: // position attribute in xml
197: try {
198: pSheet.setSortingMode(PropertySheet.UNSORTED);
199: } catch (Exception ex) {
200: }
201: }
202: }
|