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: * ComponentBeforeVisitor.java
043: *
044: * Created on August 30, 2006, 8:27 PM
045: *
046: * To change this template, choose Tools | Template Manager
047: * and open the template in the editor.
048: */
049:
050: package org.netbeans.modules.xml.schema.abe.visitors;
051:
052: import java.util.List;
053: import org.netbeans.modules.xml.axi.AXIComponent;
054: import org.netbeans.modules.xml.axi.AXIContainer;
055: import org.netbeans.modules.xml.axi.AbstractAttribute;
056: import org.netbeans.modules.xml.axi.AbstractElement;
057: import org.netbeans.modules.xml.schema.abe.ABEBaseDropPanel;
058: import org.netbeans.modules.xml.schema.abe.AttributePanel;
059: import org.netbeans.modules.xml.schema.abe.CompositorPanel;
060: import org.netbeans.modules.xml.schema.abe.ContainerPanel;
061: import org.netbeans.modules.xml.schema.abe.ElementPanel;
062: import org.netbeans.modules.xml.schema.abe.GlobalComplextypeContainerPanel;
063: import org.netbeans.modules.xml.schema.abe.GlobalElementsContainerPanel;
064: import org.netbeans.modules.xml.schema.abe.NamespacePanel;
065: import org.netbeans.modules.xml.schema.abe.StartTagPanel;
066:
067: /**
068: *
069: * @author girix
070: */
071: public class BackTraversalVisitor extends TraversalVisitor {
072: ABEBaseDropPanel currentComponent;
073: ABEBaseDropPanel result;
074:
075: /** Creates a new instance of ComponentBeforeVisitor */
076: public BackTraversalVisitor(ABEBaseDropPanel currentComponent) {
077: this .currentComponent = currentComponent;
078: }
079:
080: public ABEBaseDropPanel getResult() {
081: return result;
082: }
083:
084: public void visit(GlobalComplextypeContainerPanel panel) {
085: if (currentComponent instanceof NamespacePanel) {
086: //go to the last element in the panel
087: result = getPrevComponent(panel, null);
088: } else {
089:
090: result = getPrevComponent(panel, currentComponent);
091: }
092:
093: if (result == null) {
094: currentComponent = panel;
095: visit(panel.getContext().getInstanceDesignerPanel()
096: .getGlobalElementsPanel());
097: }
098: }
099:
100: public void visit(AttributePanel panel) {
101: StartTagPanel stp = (StartTagPanel) panel.getParent();
102: visit(stp);
103: }
104:
105: public void visit(GlobalElementsContainerPanel panel) {
106: if (currentComponent instanceof GlobalComplextypeContainerPanel) {
107: //point to the last element in the panel
108: result = getPrevComponent(panel, null);
109: } else {
110: result = getPrevComponent(panel, currentComponent);
111: }
112: if (result == null) {
113: //means this is the first comp, so backward is actually the NS
114: result = panel.getContext().getNamespacePanel();
115: }
116: }
117:
118: public void visit(StartTagPanel panel) {
119: if (panel == null)
120: return;
121: if (currentComponent instanceof AttributePanel) {
122: //go the the Attr before the current attar
123: List<AbstractAttribute> attrList = ((AXIContainer) panel
124: .getAXIComponent()).getAttributes();
125: int i = attrList
126: .indexOf((AbstractAttribute) ((AttributePanel) currentComponent)
127: .getAXIComponent());
128: if (i - 1 > 0) {
129: //not the first attr, so get the prev
130: result = panel.getChildUIComponentFor(attrList
131: .get(i - 1));
132: return;
133: } else {
134: result = panel;
135: if (result instanceof StartTagPanel) {
136: ((StartTagPanel) result).hideAttributes();
137: }
138: return;
139: }
140: }
141:
142: //else go up to the next peer of me
143: ElementPanel ep = (ElementPanel) panel.getParent();
144: //upward traversal visitors job
145: TraversalVisitor tv = new UpTraversalVisitor(panel);
146: ep.accept(tv);
147: result = tv.getResult();
148:
149: }
150:
151: public void visit(NamespacePanel panel) {
152: //back from NSP is actually the GCT
153: GlobalComplextypeContainerPanel gecp = panel.getContext()
154: .getInstanceDesignerPanel().getGlobalComplextypePanel();
155: visit(gecp);
156: }
157:
158: public void visit(CompositorPanel panel) {
159: if (currentComponent instanceof CompositorPanel) {
160: ABEBaseDropPanel elmp = (ABEBaseDropPanel) panel
161: .getParentContainerPanel();
162: TraversalVisitor tv = new UpTraversalVisitor(panel);
163: elmp.accept(tv);
164: result = tv.getResult();
165: return;
166: }
167: result = getPrevComponent(panel, currentComponent);
168: if (result == null) {
169: result = panel;
170: } else {
171: //we have a panel but we need to go the last attr in the last tag
172: List<AbstractElement> child = result.getAXIComponent()
173: .getChildElements();
174: if (child.size() > 0) {
175: ElementPanel ep = (ElementPanel) result.getParent();
176: List list = ep.getAXIContainer().getChildren(
177: TraversalVisitor.getEnCFilterList());
178: if (list.size() > 0) {
179: //return last element
180: result = ep
181: .getChildUIComponentFor((AXIComponent) list
182: .get(list.size() - 1));
183: return;
184: }
185: } else
186: return;
187: }
188: }
189:
190: private ABEBaseDropPanel getPrevComponent(ContainerPanel panel,
191: ABEBaseDropPanel current) {
192: if (current != null) {
193: //get the component before current
194: AXIComponent comp = current.getAXIComponent();
195: int i = panel.getAXIChildren().indexOf(comp);
196: if (i - 1 >= 0) {
197: //already at the first
198: return panel.getChildUIComponentFor(panel
199: .getAXIChildren().get(i - 1));
200: } else
201: //already at the last
202: return null;
203: } else {
204: //just return the last comp
205: if (panel.getAXIChildren().size() > 0) {
206: //return the last one
207: return panel.getChildUIComponentFor(panel
208: .getAXIChildren().get(
209: panel.getAXIChildren().size() - 1));
210: }
211: }
212: return null;
213: }
214:
215: }
|