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.visualweb.gravy.properties;
043:
044: import java.io.IOException;
045:
046: import java.util.Hashtable;
047:
048: /**
049: * PropertyFactory class
050: * <p>
051: *
052: *
053: */
054:
055: public class PropertyFactory {
056:
057: // Debug info reader/writer
058: private PropertyReaderWriter debugrw = null;
059:
060: // Element name
061: private String elementName;
062:
063: // Property readers array
064: private PropertyReader preaders[] = null;
065:
066: // Property writers array
067: private PropertyReaderWriter pwriters[] = null;
068:
069: // Property readers hashtable
070: private Hashtable prhash = new Hashtable();
071:
072: // Property writers hashtable
073: private Hashtable prwhash = new Hashtable();
074:
075: /**
076: * Constructs PropertyFactory object
077: *
078: * @param elementName name of component
079: */
080: public PropertyFactory(String elementName) {
081: this .elementName = elementName;
082: initialize();
083: }
084:
085: protected void initialize() {
086: // Creates arrays of Readers and ReadersWriters
087: preaders = new PropertyReader[1];
088: pwriters = new PropertyReaderWriter[1];
089:
090: preaders[0] = pwriters[0] = new PropertySheetReaderWriter();
091:
092: // Creates hashtables using arrays of Readers and ReadersWriters
093: for (int i = 0; i < getReadersNum(); i++) {
094: prhash.put(preaders[i].getName(), preaders[i]);
095: }
096: for (int i = 0; i < getWritersNum(); i++) {
097: prwhash.put(pwriters[i].getName(), pwriters[i]);
098: }
099:
100: // Insert debugging enable/disable code
101: if (false) {
102: try {
103: debugrw = new DebugReaderWriter(elementName,
104: "c:/temp/testdebug.out");
105: } catch (IOException e) {
106: throw new RuntimeException(e);
107: }
108: }
109: }
110:
111: /**
112: * Return number of available PropertyReaders
113: *
114: * @return number of readers
115: */
116: public int getReadersNum() {
117: return (preaders == null) ? 0 : preaders.length;
118: }
119:
120: /**
121: * Return number of available PropertyReaderWriters
122: *
123: * @return number of writers
124: */
125: public int getWritersNum() {
126: return (pwriters == null) ? 0 : pwriters.length;
127: }
128:
129: /**
130: * Set property value by specified writer
131: *
132: * @param writer writer index
133: * @param name property name
134: * @param value property value to set
135: */
136: public void setValue(int writer, String name, Object value) {
137: if (writer < 0 || writer >= getWritersNum())
138: throw new ArrayIndexOutOfBoundsException(writer);
139: pwriters[writer].setPropertyValue(name, value);
140: if (debugrw != null)
141: debugrw.setPropertyValue(name, value);
142: }
143:
144: /**
145: * Get property value by specified reader
146: *
147: * @param reader reader index
148: * @param name property name
149: * @return property value
150: */
151: public Object getValue(int reader, String name) {
152: if (reader < 0 || reader >= getReadersNum())
153: throw new ArrayIndexOutOfBoundsException(reader);
154: if (debugrw != null)
155: debugrw.getPropertyValue(name);
156: return preaders[reader].getPropertyValue(name);
157: }
158:
159: /**
160: * Set property value by specified writer
161: *
162: * @param writer writer name
163: * @param name property name
164: * @param value property value to set
165: */
166: public void setValue(String writer, String name, Object value) {
167: if (!prwhash.containsKey(writer))
168: throw new IllegalArgumentException(
169: "There is no PropertyReaderWriter with name "
170: + writer);
171: ((PropertyReaderWriter) prwhash.get(writer)).setPropertyValue(
172: name, value);
173: if (debugrw != null)
174: debugrw.setPropertyValue(name, value);
175: }
176:
177: /**
178: * Get property value by specified reader
179: *
180: * @param reader reader name
181: * @param name property name
182: * @return property value
183: */
184: public Object getValue(String reader, String name) {
185: if (!prhash.containsKey(reader))
186: throw new IllegalArgumentException(
187: "There is no PropertyReader with name " + reader);
188: if (debugrw != null)
189: debugrw.getPropertyValue(name);
190: return ((PropertyReader) prhash.get(reader))
191: .getPropertyValue(name);
192: }
193: }
|