001: /**
002: * <copyright>
003: *
004: * Copyright 2002-2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */package org.cougaar.tools.csmart.society;
026:
027: import org.cougaar.tools.csmart.core.cdata.PGPropData;
028: import org.cougaar.tools.csmart.core.cdata.PGPropMultiVal;
029: import org.cougaar.tools.csmart.core.cdata.PropGroupData;
030: import org.cougaar.tools.csmart.core.property.ModifiableConfigurableComponent;
031: import org.cougaar.tools.csmart.core.property.Property;
032: import org.cougaar.tools.csmart.core.property.name.CompositeName;
033: import org.cougaar.tools.csmart.ui.viewer.CSMART;
034: import org.cougaar.util.log.Logger;
035:
036: import java.util.Collection;
037: import java.util.Iterator;
038:
039: /**
040: * PropGroupBase
041: *
042: *
043: * Created: Thu Feb 21 15:57:03 2002
044: *
045: */
046: public class PropGroupBase extends ModifiableConfigurableComponent
047: implements PropGroupComponent {
048:
049: private PropGroupData pgd;
050: private transient Logger log;
051:
052: public PropGroupBase(PropGroupData pgd) {
053: super (pgd.getName());
054: this .pgd = pgd;
055: createLogger();
056: }
057:
058: public void initProperties() {
059: Iterator iter = pgd.getPropertiesIterator();
060: while (iter.hasNext()) {
061: PGPropData data = (PGPropData) iter.next();
062: // Compose Name of Name and Type.
063: String name;
064: if (data.getSubType() == null
065: || data.getSubType().equals("")) {
066: name = data.getName() + " (" + data.getType() + ")";
067: } else {
068: name = data.getName() + " (" + data.getSubType() + ")";
069: }
070:
071: if (data.isListType())
072: addProperty(name, ((PGPropMultiVal) data.getValue())
073: .getValuesStringArray());
074: else
075: addProperty(name, data.getValue());
076: }
077: }
078:
079: private void createLogger() {
080: log = CSMART.createLogger(this .getClass().getName());
081: }
082:
083: private String getName(Property prop) {
084: String name = prop.getName().toString();
085: int index = name.lastIndexOf('.');
086: if (index != -1)
087: name = name.substring(index + 1);
088: name = name.substring(0, ((name.indexOf("(") == -1) ? name
089: .length() : name.indexOf("(")));
090: return name.trim();
091: }
092:
093: private String getType(Property prop) {
094: String name = prop.getName().toString();
095: int typeStart = name.indexOf("(");
096: if (typeStart == -1)
097: return new String("String");
098: else
099: return name.substring(name.indexOf("(") + 1, name
100: .indexOf(")"));
101: }
102:
103: private PGPropData getProp(String name) {
104: PGPropData[] props = pgd.getProperties();
105: for (int i = 0; i < props.length; i++) {
106: if (props[i].getName().equals(name)) {
107: return props[i];
108: }
109: }
110: return null;
111: }
112:
113: // For constructing component data
114: public PropGroupData getPropGroupData() {
115: PropGroupData pgData = new PropGroupData(this .getShortName());
116:
117: Iterator props = getSortedLocalPropertyNames();
118: while (props.hasNext()) {
119: Property prop;
120: CompositeName pname = (CompositeName) props.next();
121: if (pname != null)
122: prop = getProperty(pname);
123: else
124: continue;
125: PGPropData oldPG = getProp(getName(prop));
126: PGPropData pg = new PGPropData();
127: if (oldPG != null) {
128: pg.setName(oldPG.getName());
129: pg.setType(oldPG.getType());
130: pg.setSubType(oldPG.getSubType());
131: } else {
132: pg.setName(getName(prop));
133: String type = getType(prop);
134: // if the value for this property is a list, then this type
135: // is really the subtype
136: // do a prop.getPropertyClass()?
137: // This doesnt appear usually set, cant depend on it
138: // So what, get the value?
139: if ((prop.getValue() != null)
140: && (prop.getValue().getClass().isArray() || Collection.class
141: .isAssignableFrom(prop.getValue()
142: .getClass()))) {
143: pg.setSubType(type);
144: pg.setType("Collection");
145: } else {
146: // otherwise, set this as the type
147: pg.setType(type);
148: // Note you'll get a warning about a null subtype
149: // when saving to DB
150: }
151: }
152:
153: if (pg.isListType()) {
154: pg.setValue(new PGPropMultiVal(prop.getValue()));
155: } else {
156: pg.setValue(prop.getValue());
157: }
158:
159: pgData.addProperty(pg);
160: }
161:
162: return pgData;
163: }
164:
165: }
|