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-2007 Sun
017: * Microsystems, Inc. All Rights Reserved.
018: */
019:
020: package org.netbeans.modules.bpel.mapper.tree.models;
021:
022: import java.util.ArrayList;
023: import java.util.Collection;
024: import java.util.Collections;
025: import java.util.List;
026: import java.util.Set;
027: import org.netbeans.modules.bpel.mapper.predicates.PredicateManager;
028: import org.netbeans.modules.bpel.mapper.multiview.BpelDesignContext;
029: import org.netbeans.modules.bpel.mapper.predicates.AbstractPredicate;
030: import org.netbeans.modules.bpel.mapper.predicates.SpecialStepManager;
031: import org.netbeans.modules.bpel.mapper.tree.spi.MapperTreeExtensionModel;
032: import org.netbeans.modules.bpel.mapper.tree.spi.TreeItemInfoProvider;
033: import org.netbeans.modules.bpel.model.api.AbstractVariableDeclaration;
034: import org.netbeans.modules.bpel.model.api.VariableDeclaration;
035: import org.netbeans.modules.bpel.model.api.VariableDeclarationScope;
036: import org.netbeans.modules.bpel.model.api.references.SchemaReference;
037: import org.netbeans.modules.bpel.model.api.references.WSDLReference;
038: import org.netbeans.modules.bpel.model.api.support.VisibilityScope;
039: import org.netbeans.modules.bpel.mapper.tree.spi.MapperTreeModel;
040: import org.netbeans.modules.bpel.mapper.tree.spi.RestartableIterator;
041: import org.netbeans.modules.bpel.model.api.Process;
042: import org.netbeans.modules.xml.xpath.ext.schema.FindAllChildrenSchemaVisitor;
043: import org.netbeans.modules.xml.schema.model.Attribute;
044: import org.netbeans.modules.xml.schema.model.Element;
045: import org.netbeans.modules.xml.schema.model.GlobalElement;
046: import org.netbeans.modules.xml.schema.model.GlobalType;
047: import org.netbeans.modules.xml.schema.model.SchemaComponent;
048: import org.netbeans.modules.xml.wsdl.model.Message;
049: import org.netbeans.modules.xml.wsdl.model.Part;
050: import org.netbeans.modules.xml.xam.dom.NamedComponentReference;
051: import org.netbeans.modules.xml.xpath.ext.LocationStep;
052: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext;
053: import org.netbeans.modules.xml.xpath.ext.XPathSchemaContext.SchemaCompPair;
054:
055: /**
056: * The implementation of the MapperTreeModel for the variables' tree.
057: *
058: * @author nk160297
059: */
060: public class VariableTreeModel implements
061: MapperTreeExtensionModel<Object> {
062:
063: private FindAllChildrenSchemaVisitor sSchemaSearcher = new FindAllChildrenSchemaVisitor(
064: true, true);
065:
066: private VisibilityScope mVisScope;
067: private Set<VariableDeclaration> mOverriddenVariables;
068: private BpelDesignContext mDesignContext;
069: private PredicateManager mPredManager;
070: private SpecialStepManager mSStepManager;
071: private TreeItemInfoProvider mTreeInfoProvider;
072:
073: public VariableTreeModel(BpelDesignContext context) {
074: this (context, new PredicateManager(), new SpecialStepManager());
075: }
076:
077: public VariableTreeModel(BpelDesignContext context,
078: PredicateManager predManager, SpecialStepManager stepManager) {
079: this (context, predManager, stepManager,
080: VariableTreeInfoProvider.getInstance());
081: }
082:
083: public VariableTreeModel(BpelDesignContext context,
084: PredicateManager predManager,
085: SpecialStepManager stepManager,
086: TreeItemInfoProvider treeInfoProvider) {
087: mDesignContext = context;
088: mPredManager = predManager;
089: mSStepManager = stepManager;
090: mTreeInfoProvider = treeInfoProvider;
091: //
092: mVisScope = new VisibilityScope(context.getSelectedEntity());
093: mOverriddenVariables = VisibilityScope.Utils
094: .getAllOverridenVariables(mVisScope);
095: }
096:
097: public List getChildren(
098: RestartableIterator<Object> dataObjectPathItr) {
099: Object parent = dataObjectPathItr.next();
100: if (parent == MapperTreeModel.TREE_ROOT) {
101: Process process = mDesignContext.getBpelModel()
102: .getProcess();
103: return Collections.singletonList(process);
104: }
105: if (parent instanceof VariableDeclarationScope) {
106: //
107: // Look for the parent in the scope chain.
108: List<VariableDeclarationScope> scopeChain = mVisScope
109: .getVarScopeChain();
110: int parentIndex = -1;
111: for (int index = 0; index < scopeChain.size(); index++) {
112: VariableDeclarationScope scopeFromChain = scopeChain
113: .get(index);
114: if (scopeFromChain == parent) {
115: parentIndex = index;
116: break;
117: }
118: }
119: //
120: if (parentIndex == -1) {
121: // The parent is not in any scope at all :-(
122: // it should not aver happened
123: return null;
124: }
125: //
126: VariableDeclarationScope nextScope = null;
127: if (parentIndex + 1 < scopeChain.size()) {
128: // Take the next scope if the parent scope is not the last in the chain
129: nextScope = scopeChain.get(parentIndex + 1);
130: }
131: //
132: List<VariableDeclaration> varList = VisibilityScope.Utils
133: .getVarDeclInScope((VariableDeclarationScope) parent);
134: varList.removeAll(mOverriddenVariables);
135: //
136: List<Object> childrenList = new ArrayList<Object>();
137: for (VariableDeclaration childVar : varList) {
138: if (childVar == parent) {
139: VariableDeclarationWrapper wrapper = new VariableDeclarationWrapper(
140: childVar);
141: childrenList.add(wrapper);
142: } else {
143: childrenList.add(childVar);
144: }
145: }
146: //
147: if (nextScope != null) {
148: childrenList.add(nextScope);
149: }
150: //
151: return childrenList;
152: } else if (parent instanceof AbstractVariableDeclaration) {
153: AbstractVariableDeclaration varDecl = (AbstractVariableDeclaration) parent;
154: WSDLReference<Message> msgRef = varDecl.getMessageType();
155: if (msgRef != null) {
156: Message msg = msgRef.get();
157: Collection<Part> parts = msg.getParts();
158: ArrayList<Part> partList = new ArrayList<Part>(parts);
159: return partList;
160: } else {
161: SchemaReference<GlobalType> gTypeRef = varDecl
162: .getType();
163: if (gTypeRef != null) {
164: GlobalType gType = gTypeRef.get();
165: if (gType != null) {
166: return loadSchemaComponents(dataObjectPathItr,
167: gType);
168: }
169: } else {
170: SchemaReference<GlobalElement> gElemRef = varDecl
171: .getElement();
172: if (gElemRef != null) {
173: GlobalElement gElem = gElemRef.get();
174: if (gElem != null) {
175: return loadSchemaComponents(
176: dataObjectPathItr, gElem);
177: }
178: }
179: }
180: }
181: } else if (parent instanceof Part) {
182: Part part = (Part) parent;
183: NamedComponentReference<GlobalType> gTypeRef = part
184: .getType();
185: if (gTypeRef != null) {
186: GlobalType gType = gTypeRef.get();
187: if (gType != null) {
188: return loadSchemaComponents(dataObjectPathItr,
189: gType);
190: }
191: } else {
192: NamedComponentReference<GlobalElement> gElemRef = part
193: .getElement();
194: if (gElemRef != null) {
195: GlobalElement gElem = gElemRef.get();
196: if (gElem != null) {
197: return loadSchemaComponents(dataObjectPathItr,
198: gElem);
199: }
200: }
201: }
202: } else if (parent instanceof SchemaComponent) {
203: return loadSchemaComponents(dataObjectPathItr,
204: (SchemaComponent) parent);
205: } else if (parent instanceof AbstractPredicate) {
206: SchemaComponent sComp = ((AbstractPredicate) parent)
207: .getSComponent();
208: if (sComp != null) {
209: return loadSchemaComponents(dataObjectPathItr, sComp);
210: }
211: } else if (parent instanceof LocationStep) {
212: LocationStep step = (LocationStep) parent;
213: XPathSchemaContext context = step.getSchemaContext();
214: if (context != null) {
215: Set<SchemaCompPair> sCompPairs = context
216: .getSchemaCompPairs();
217: ArrayList result = new ArrayList();
218: for (SchemaCompPair sCompPair : sCompPairs) {
219: SchemaComponent sComp = sCompPair.getComp();
220: result.addAll(loadSchemaComponents(
221: dataObjectPathItr, sComp));
222: }
223: return result;
224: }
225: }
226: //
227: return null;
228: }
229:
230: public PredicateManager getPredicateManager() {
231: return mPredManager;
232: }
233:
234: public SpecialStepManager getSStepManager() {
235: return mSStepManager;
236: }
237:
238: private List loadSchemaComponents(
239: RestartableIterator<Object> dataObjectPathItr,
240: SchemaComponent parent) {
241: //
242: sSchemaSearcher.lookForSubcomponents(parent);
243: List<SchemaComponent> childrenComp = sSchemaSearcher.getFound();
244: //
245: if (mPredManager == null && mSStepManager == null) {
246: return childrenComp;
247: }
248: //
249: List<Object> allChildren = new ArrayList<Object>(childrenComp
250: .size() + 5);
251: if (mSStepManager != null) {
252: //
253: // Look for the corresponding special nodes (text(), node(), comment()).
254: List<LocationStep> step = mSStepManager
255: .getSteps(dataObjectPathItr);
256: if (step != null && !step.isEmpty()) {
257: allChildren.addAll(step);
258: }
259: }
260: if (mPredManager != null) {
261: for (SchemaComponent sComp : childrenComp) {
262: allChildren.add(sComp);
263: //
264: // Look for the corresponding predicated nodes.
265: List<AbstractPredicate> predicates = mPredManager
266: .getPredicates(dataObjectPathItr, sComp);
267: allChildren.addAll(predicates);
268: }
269: }
270: //
271: return allChildren;
272: }
273:
274: public Boolean isLeaf(Object node) {
275: return null;
276: }
277:
278: public Boolean isConnectable(Object node) {
279: if (node instanceof Element || node instanceof Attribute
280: || node instanceof Part
281: || node instanceof AbstractPredicate
282: || node instanceof LocationStep) {
283: return Boolean.TRUE;
284: }
285: //
286: if (node instanceof AbstractVariableDeclaration
287: && !(node instanceof VariableDeclarationScope)) {
288: return Boolean.TRUE;
289: }
290: //
291: return null;
292: }
293:
294: public TreeItemInfoProvider getTreeItemInfoProvider() {
295: return mTreeInfoProvider;
296: }
297:
298: }
|