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: * FirstComponentVisitor.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.schema.abe.ABEBaseDropPanel;
057: import org.netbeans.modules.xml.schema.abe.AttributePanel;
058: import org.netbeans.modules.xml.schema.abe.CompositorPanel;
059: import org.netbeans.modules.xml.schema.abe.ContainerPanel;
060: import org.netbeans.modules.xml.schema.abe.ElementPanel;
061: import org.netbeans.modules.xml.schema.abe.GlobalComplextypeContainerPanel;
062: import org.netbeans.modules.xml.schema.abe.GlobalElementsContainerPanel;
063: import org.netbeans.modules.xml.schema.abe.NamespacePanel;
064: import org.netbeans.modules.xml.schema.abe.StartTagPanel;
065:
066: /**
067: *
068: * @author girix
069: */
070: public class FrontTraversalVisitor extends TraversalVisitor {
071: ABEBaseDropPanel currentComponent;
072: ABEBaseDropPanel result;
073:
074: /** Creates a new instance of FirstComponentVisitor */
075: public FrontTraversalVisitor(ABEBaseDropPanel currentComponent) {
076: this .currentComponent = currentComponent;
077: }
078:
079: public ABEBaseDropPanel getResult() {
080: return result;
081: }
082:
083: public void visit(GlobalComplextypeContainerPanel panel) {
084: if (currentComponent instanceof GlobalElementsContainerPanel) {
085: //just point to the first complex type panel
086: result = getNextComponent(panel, null);
087: } else {
088: //means traverse to next comp
089: result = getNextComponent(panel, currentComponent);
090: }
091: if (result == null) {
092: //means there are no comps or reached already last comp
093: //froward from GCTP is actually NSP
094: result = panel.getContext().getNamespacePanel();
095: }
096: }
097:
098: public void visit(AttributePanel panel) {
099: StartTagPanel stp = (StartTagPanel) panel.getParent();
100: visit(stp);
101: }
102:
103: public void visit(GlobalElementsContainerPanel panel) {
104: if (currentComponent instanceof NamespacePanel) {
105: //just point to the first element panel
106: result = getNextComponent(panel, null);
107: } else {
108: //means traverse to next comp
109: result = getNextComponent(panel, currentComponent);
110: }
111: if (result == null) {
112: //means there are no comps or reached already last comp
113: //froward from GEP is actually GCT
114: currentComponent = panel;
115: visit(panel.getContext().getInstanceDesignerPanel()
116: .getGlobalComplextypePanel());
117: }
118: }
119:
120: public void visit(StartTagPanel panel) {
121: if (currentComponent == panel) {
122: //go to first attribute if available
123: List<AbstractAttribute> attrList = ((AXIContainer) panel
124: .getAXIComponent()).getAttributes();
125: if ((attrList != null) && (attrList.size() > 0)) {
126: result = panel.getChildUIComponentFor(attrList.get(0));
127: return;
128: }
129: } else if (currentComponent instanceof AttributePanel) {
130: //then go to next available attr
131: List<AbstractAttribute> attrList = ((AXIContainer) panel
132: .getAXIComponent()).getAttributes();
133: int i = attrList
134: .indexOf((AbstractAttribute) ((AttributePanel) currentComponent)
135: .getAXIComponent());
136: if (i + 1 < attrList.size()) {
137: //more attrs available. Go to next
138: result = panel.getChildUIComponentFor(attrList
139: .get(i + 1));
140: return;
141: }
142: }
143: //else go down to next peer of me
144: ElementPanel ep = (ElementPanel) panel.getParent();
145: //this is a downward traversal visitors job
146: TraversalVisitor tv = new DownTraversalVisitor(panel);
147: ep.accept(tv);
148: result = tv.getResult();
149: }
150:
151: public void visit(NamespacePanel panel) {
152: //froward from NSP is actually GEP
153: GlobalElementsContainerPanel gecp = panel.getContext()
154: .getInstanceDesignerPanel().getGlobalElementsPanel();
155: visit(gecp);
156: }
157:
158: public void visit(CompositorPanel panel) {
159: result = getNextComponent(panel, currentComponent);
160: if (result == null) {
161: //then we reached last comp. Mode down in the element panel
162: ABEBaseDropPanel elmp = (ABEBaseDropPanel) panel
163: .getParentContainerPanel();
164: TraversalVisitor tv = new DownTraversalVisitor(panel);
165: elmp.accept(tv);
166: result = tv.getResult();
167: }
168: }
169:
170: public void visit(ElementPanel elementPanel) {
171: //has to be alway the start tag panel
172: if (currentComponent instanceof ContainerPanel) {
173: //show children then put focus on start tag
174: elementPanel.expandChild();
175: result = elementPanel.getStartTagPanel();
176: }
177: }
178:
179: private ABEBaseDropPanel getNextComponent(ContainerPanel panel,
180: ABEBaseDropPanel current) {
181: if (current != null) {
182: //get the next after current
183: AXIComponent comp = current.getAXIComponent();
184: int i = panel.getAXIChildren().indexOf(comp);
185: if (i + 1 < panel.getAXIChildren().size()) {
186: return panel.getChildUIComponentFor(panel
187: .getAXIChildren().get(i + 1));
188:
189: } else {
190: //already at the last comp
191: return null;
192: }
193: } else {
194: //just return the first comp
195: if (panel.getAXIChildren().size() > 0) {
196: //return the first one
197: return panel.getChildUIComponentFor(panel
198: .getAXIChildren().get(0));
199: }
200: }
201: return null;
202:
203: }
204:
205: }
|