~ubuntu-branches/ubuntu/trusty/netbeans/trusty

« back to all changes in this revision

Viewing changes to scripting/php/debugger/src/org/netbeans/modules/php/dbgp/models/nodes/AbstractModelNode.java

  • Committer: Bazaar Package Importer
  • Author(s): Marek Slama
  • Date: 2008-01-29 14:11:22 UTC
  • Revision ID: james.westby@ubuntu.com-20080129141122-fnzjbo11ntghxfu7
Tags: upstream-6.0.1
ImportĀ upstreamĀ versionĀ 6.0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * The contents of this file are subject to the terms of the Common Development
 
3
 * and Distribution License (the License). You may not use this file except in
 
4
 * compliance with the License.
 
5
 *
 
6
 * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
 
7
 * or http://www.netbeans.org/cddl.txt.
 
8
 
 
9
 * When distributing Covered Code, include this CDDL Header Notice in each file
 
10
 * and include the License file at http://www.netbeans.org/cddl.txt.
 
11
 * If applicable, add the following below the CDDL Header, with the fields
 
12
 * enclosed by brackets [] replaced by your own identifying information:
 
13
 * "Portions Copyrighted [year] [name of copyright owner]"
 
14
 *
 
15
 * The Original Software is NetBeans. The Initial Developer of the Original
 
16
 * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
 
17
 * Microsystems, Inc. All Rights Reserved.
 
18
 */
 
19
package org.netbeans.modules.php.dbgp.models.nodes;
 
20
 
 
21
import java.util.ArrayList;
 
22
import java.util.Collection;
 
23
import java.util.Iterator;
 
24
import java.util.List;
 
25
import java.util.Set;
 
26
 
 
27
import org.netbeans.modules.php.dbgp.models.VariablesModel;
 
28
import org.netbeans.modules.php.dbgp.models.VariablesModelFilter.FilterType;
 
29
import org.netbeans.modules.php.dbgp.packets.Property;
 
30
import org.netbeans.spi.viewmodel.ModelEvent;
 
31
 
 
32
 
 
33
/**
 
34
 * @author ads
 
35
 *
 
36
 */
 
37
public abstract class AbstractModelNode {
 
38
 
 
39
    private static final String RESOURCE = "resource";      // NOI18N
 
40
 
 
41
    private static final String OBJECT  = "object";         // NOI18N
 
42
 
 
43
    private static final String ARRAY   = "array";          // NOI18N
 
44
    
 
45
    private static final String STRING  = "string";         // NOI18N
 
46
    
 
47
    private static final String UNDEF   = "uninitialized";  // NOI18N
 
48
    
 
49
    private static final String NULL    = "null";           // NOI18N
 
50
    
 
51
    AbstractModelNode( AbstractModelNode parent , List<Property> properties ){
 
52
        myParent = parent;
 
53
        initVariables( properties );
 
54
    }
 
55
    
 
56
    public AbstractModelNode getParent() {
 
57
        return myParent;
 
58
    }
 
59
    
 
60
    public boolean hasType( Set<FilterType> set ) {
 
61
        return isTypeApplied( set );
 
62
    }
 
63
    
 
64
    public static org.netbeans.modules.php.dbgp.models.
 
65
        VariablesModel.AbstractVariableNode createVariable( Property property , 
 
66
            AbstractModelNode parent ) 
 
67
    {
 
68
        String type = property.getType();
 
69
        if ( STRING.equals(type)) {
 
70
            return new StringVariableNode( property ,  parent );
 
71
        }
 
72
        else if ( ARRAY.equals( type )) {
 
73
            return new ArrayVariableNode( property , parent );
 
74
        }
 
75
        else if ( UNDEF.equals( type )){
 
76
            return new UndefinedVariableNode( property, parent );
 
77
        }
 
78
        else if ( NULL.equals( type )){
 
79
            return new NullVariableNode( property, parent );
 
80
        }
 
81
        else if ( OBJECT.equals(type )) {
 
82
            return new ObjectVariableNode( property , parent );
 
83
        }
 
84
        else if ( RESOURCE.equals(type)) {
 
85
            return new ResourceVariableNode( property , parent );
 
86
        }
 
87
        else if ( ScalarTypeVariableNode.BOOLEAN.equals(type) ||
 
88
                ScalarTypeVariableNode.BOOL.equals(type) ||
 
89
                ScalarTypeVariableNode.INTEGER.equals(type) || 
 
90
                ScalarTypeVariableNode.INT.equals(type) ||
 
91
                ScalarTypeVariableNode.FLOAT.equals(type) 
 
92
                ) 
 
93
        {
 
94
            return new ScalarTypeVariableNode( property , parent );
 
95
        }
 
96
        else {
 
97
            return new BaseVariableNode( property , parent ); 
 
98
        }
 
99
    }
 
100
    
 
101
    protected abstract boolean isTypeApplied( Set<FilterType> set );
 
102
    
 
103
    protected List<AbstractVariableNode> getVariables()
 
104
    {
 
105
        return myVars;
 
106
    }
 
107
    
 
108
    protected void initVariables( List<Property> properties ) {
 
109
        if ( properties == null ) {
 
110
            return;
 
111
        }
 
112
        myVars = new ArrayList<AbstractVariableNode>( );        
 
113
        for (Property property : properties) {
 
114
            org.netbeans.modules.php.dbgp.models.VariablesModel.
 
115
                AbstractVariableNode var = createVariable( property , this );
 
116
            myVars.add(var);
 
117
        }
 
118
    }
 
119
    
 
120
    protected void setVars( List<AbstractVariableNode> variables ) 
 
121
    {
 
122
        myVars = variables;
 
123
    }
 
124
 
 
125
    protected boolean addAbsentChildren( AbstractModelNode node) {
 
126
        boolean hasChanged = false;
 
127
        if (node.getVariables() != null && node.getVariables().size() > 0) {
 
128
            Iterator<AbstractVariableNode> iterator = node.getVariables().iterator();
 
129
            
 
130
            while (iterator.hasNext()) {
 
131
                AbstractVariableNode newChild = iterator.next();
 
132
                
 
133
                getVariables().add(newChild);
 
134
                hasChanged = true;
 
135
            }
 
136
        }
 
137
        return hasChanged;
 
138
    }
 
139
 
 
140
    protected boolean updateExistedChildren( VariablesModel variablesModel,
 
141
            AbstractModelNode node, Collection<ModelEvent> events )
 
142
    {
 
143
        boolean hasChanged = false;
 
144
        List<AbstractVariableNode> list = new ArrayList<AbstractVariableNode>( 
 
145
                getVariables() );
 
146
 
 
147
        int currentIndx = 0; 
 
148
        for( AbstractVariableNode child : list ) {
 
149
            Property property = child.getProperty();
 
150
 
 
151
            int newIndex = node.findChild(property);
 
152
 
 
153
            if (newIndex == -1) {
 
154
                getVariables().remove( currentIndx );
 
155
                hasChanged = true;
 
156
                continue;
 
157
            }
 
158
            AbstractVariableNode newChild = node.getVariables().get(newIndex);
 
159
            Property newProp = newChild.getProperty();
 
160
            if (property.getType().equals(newProp.getType())) {
 
161
                // properties are absolutely equal , need just update children and value
 
162
                node.getVariables().remove( newIndex );
 
163
                child.collectUpdates(variablesModel, newChild, events);
 
164
            }
 
165
            else {
 
166
                /*
 
167
                 * Properties have the same name only. But we need to change
 
168
                 * class for variable ( because they have different types ).
 
169
                 */
 
170
                getVariables().remove(currentIndx);
 
171
                getVariables().add(currentIndx, newChild);
 
172
                node.getVariables().remove( newIndex );
 
173
                hasChanged = true;
 
174
            }
 
175
            currentIndx++;
 
176
        }
 
177
        return hasChanged;
 
178
    }
 
179
    
 
180
    protected boolean updatePage( AbstractVariableNode node ) {
 
181
        Property property = node.getProperty();
 
182
        if ( property.getPageSize() >0 && property.getPage() >0 ){
 
183
            addAbsentChildren(node);
 
184
            return true;
 
185
        }
 
186
        else {
 
187
            return false;
 
188
        }
 
189
    }
 
190
 
 
191
    protected  int findChild( Property property ) {
 
192
        int index = 0;
 
193
        for( AbstractVariableNode node : getVariables() ) {
 
194
            Property prop = node.getProperty();
 
195
            String propName = prop.getName();
 
196
            //String propType = prop.getType();
 
197
            if ( propName.equals( property.getName())  )
 
198
                    // && propType.equals( property.getType()))
 
199
            {
 
200
                return index;
 
201
            }
 
202
            index++;
 
203
        }
 
204
        return -1;
 
205
    }
 
206
 
 
207
    private List<AbstractVariableNode> myVars;
 
208
    
 
209
    private AbstractModelNode myParent;
 
210
 
 
211
 
 
212
}