001: /*
002: * The contents of this file are subject to the terms of the Common Development
003: * and Distribution License (the License). You may not use this file except in
004: * compliance with the License.
005: *
006: * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
007: * or http://www.netbeans.org/cddl.txt.
008:
009: * When distributing Covered Code, include this CDDL Header Notice in each file
010: * and include the License file at http://www.netbeans.org/cddl.txt.
011: * If applicable, add the following below the CDDL Header, with the fields
012: * enclosed by brackets [] replaced by your own identifying information:
013: * "Portions Copyrighted [year] [name of copyright owner]"
014: *
015: * The Original Software is NetBeans. The Initial Developer of the Original
016: * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019: package org.netbeans.modules.php.dbgp.models.nodes;
020:
021: import java.util.ArrayList;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Set;
026:
027: import org.netbeans.modules.php.dbgp.models.VariablesModel;
028: import org.netbeans.modules.php.dbgp.models.VariablesModelFilter.FilterType;
029: import org.netbeans.modules.php.dbgp.packets.Property;
030: import org.netbeans.spi.viewmodel.ModelEvent;
031:
032: /**
033: * @author ads
034: *
035: */
036: public abstract class AbstractModelNode {
037:
038: private static final String RESOURCE = "resource"; // NOI18N
039:
040: private static final String OBJECT = "object"; // NOI18N
041:
042: private static final String ARRAY = "array"; // NOI18N
043:
044: private static final String STRING = "string"; // NOI18N
045:
046: private static final String UNDEF = "uninitialized"; // NOI18N
047:
048: private static final String NULL = "null"; // NOI18N
049:
050: AbstractModelNode(AbstractModelNode parent,
051: List<Property> properties) {
052: myParent = parent;
053: initVariables(properties);
054: }
055:
056: public AbstractModelNode getParent() {
057: return myParent;
058: }
059:
060: public boolean hasType(Set<FilterType> set) {
061: return isTypeApplied(set);
062: }
063:
064: public static org.netbeans.modules.php.dbgp.models.VariablesModel.AbstractVariableNode createVariable(
065: Property property, AbstractModelNode parent) {
066: String type = property.getType();
067: if (STRING.equals(type)) {
068: return new StringVariableNode(property, parent);
069: } else if (ARRAY.equals(type)) {
070: return new ArrayVariableNode(property, parent);
071: } else if (UNDEF.equals(type)) {
072: return new UndefinedVariableNode(property, parent);
073: } else if (NULL.equals(type)) {
074: return new NullVariableNode(property, parent);
075: } else if (OBJECT.equals(type)) {
076: return new ObjectVariableNode(property, parent);
077: } else if (RESOURCE.equals(type)) {
078: return new ResourceVariableNode(property, parent);
079: } else if (ScalarTypeVariableNode.BOOLEAN.equals(type)
080: || ScalarTypeVariableNode.BOOL.equals(type)
081: || ScalarTypeVariableNode.INTEGER.equals(type)
082: || ScalarTypeVariableNode.INT.equals(type)
083: || ScalarTypeVariableNode.FLOAT.equals(type)) {
084: return new ScalarTypeVariableNode(property, parent);
085: } else {
086: return new BaseVariableNode(property, parent);
087: }
088: }
089:
090: protected abstract boolean isTypeApplied(Set<FilterType> set);
091:
092: protected List<AbstractVariableNode> getVariables() {
093: return myVars;
094: }
095:
096: protected void initVariables(List<Property> properties) {
097: if (properties == null) {
098: return;
099: }
100: myVars = new ArrayList<AbstractVariableNode>();
101: for (Property property : properties) {
102: org.netbeans.modules.php.dbgp.models.VariablesModel.AbstractVariableNode var = createVariable(
103: property, this );
104: myVars.add(var);
105: }
106: }
107:
108: protected void setVars(List<AbstractVariableNode> variables) {
109: myVars = variables;
110: }
111:
112: protected boolean addAbsentChildren(AbstractModelNode node) {
113: boolean hasChanged = false;
114: if (node.getVariables() != null
115: && node.getVariables().size() > 0) {
116: Iterator<AbstractVariableNode> iterator = node
117: .getVariables().iterator();
118:
119: while (iterator.hasNext()) {
120: AbstractVariableNode newChild = iterator.next();
121:
122: getVariables().add(newChild);
123: hasChanged = true;
124: }
125: }
126: return hasChanged;
127: }
128:
129: protected boolean updateExistedChildren(
130: VariablesModel variablesModel, AbstractModelNode node,
131: Collection<ModelEvent> events) {
132: boolean hasChanged = false;
133: List<AbstractVariableNode> list = new ArrayList<AbstractVariableNode>(
134: getVariables());
135:
136: int currentIndx = 0;
137: for (AbstractVariableNode child : list) {
138: Property property = child.getProperty();
139:
140: int newIndex = node.findChild(property);
141:
142: if (newIndex == -1) {
143: getVariables().remove(currentIndx);
144: hasChanged = true;
145: continue;
146: }
147: AbstractVariableNode newChild = node.getVariables().get(
148: newIndex);
149: Property newProp = newChild.getProperty();
150: if (property.getType().equals(newProp.getType())) {
151: // properties are absolutely equal , need just update children and value
152: node.getVariables().remove(newIndex);
153: child.collectUpdates(variablesModel, newChild, events);
154: } else {
155: /*
156: * Properties have the same name only. But we need to change
157: * class for variable ( because they have different types ).
158: */
159: getVariables().remove(currentIndx);
160: getVariables().add(currentIndx, newChild);
161: node.getVariables().remove(newIndex);
162: hasChanged = true;
163: }
164: currentIndx++;
165: }
166: return hasChanged;
167: }
168:
169: protected boolean updatePage(AbstractVariableNode node) {
170: Property property = node.getProperty();
171: if (property.getPageSize() > 0 && property.getPage() > 0) {
172: addAbsentChildren(node);
173: return true;
174: } else {
175: return false;
176: }
177: }
178:
179: protected int findChild(Property property) {
180: int index = 0;
181: for (AbstractVariableNode node : getVariables()) {
182: Property prop = node.getProperty();
183: String propName = prop.getName();
184: //String propType = prop.getType();
185: if (propName.equals(property.getName()))
186: // && propType.equals( property.getType()))
187: {
188: return index;
189: }
190: index++;
191: }
192: return -1;
193: }
194:
195: private List<AbstractVariableNode> myVars;
196:
197: private AbstractModelNode myParent;
198:
199: }
|