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-2006 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: package org.netbeans.modules.xml.axi.util;
042:
043: import java.io.IOException;
044: import org.netbeans.modules.xml.axi.AXIComponent;
045: import org.netbeans.modules.xml.axi.AXIContainer;
046: import org.netbeans.modules.xml.axi.AXIDocument;
047: import org.netbeans.modules.xml.axi.AXIModel;
048: import org.netbeans.modules.xml.axi.AXIType;
049: import org.netbeans.modules.xml.axi.Attribute;
050: import org.netbeans.modules.xml.axi.Compositor;
051: import org.netbeans.modules.xml.axi.Compositor.CompositorType;
052: import org.netbeans.modules.xml.axi.ContentModel;
053: import org.netbeans.modules.xml.axi.Element; //import org.netbeans.modules.xml.refactoring.spi.SharedUtils;
054: import org.netbeans.modules.xml.schema.model.SchemaComponent;
055: import org.netbeans.modules.xml.schema.model.SchemaModel;
056: import org.netbeans.modules.xml.xam.Nameable;
057: import org.netbeans.modules.xml.xam.NamedReferenceable;
058:
059: /**
060: *
061: * @author Samaresh
062: */
063: public class SimulationHelper {
064:
065: private AXIModel model;
066:
067: /**
068: * Creates a new instance of SimulationHelper
069: */
070: public SimulationHelper(AXIModel model) {
071: this .model = model;
072: }
073:
074: public Element dropGlobalElement(String name) {
075: AXIDocument doc = model.getRoot();
076: model.startTransaction();
077: Element e = model.getComponentFactory().createElement();
078: e.setName(name);
079: doc.addElement(e);
080: model.endTransaction();
081: return e;
082: }
083:
084: public ContentModel dropGlobalComplexType(String name) {
085: AXIDocument doc = model.getRoot();
086: model.startTransaction();
087: ContentModel cm = model.getComponentFactory()
088: .createComplexType();
089: cm.setName(name);
090: doc.addContentModel(cm);
091: model.endTransaction();
092: return cm;
093: }
094:
095: public Element dropElement(AXIContainer parent, String name) {
096: model.startTransaction();
097: Element element = model.getComponentFactory().createElement();
098: element.setName(name);
099: parent.addElement(element);
100: model.endTransaction();
101: return element;
102: }
103:
104: public void setElementType(Element e, AXIType type) {
105: model.startTransaction();
106: e.setType(type);
107: model.endTransaction();
108: }
109:
110: public void delete(AXIComponent child) {
111: model.startTransaction();
112: child.getParent().removeChild(child);
113: model.endTransaction();
114: }
115:
116: public void clearAll() {
117: model.startTransaction();
118: model.getRoot().removeAllChildren();
119: model.endTransaction();
120: }
121:
122: public Compositor dropCompositor(AXIContainer parent,
123: CompositorType type) {
124: model.startTransaction();
125: Compositor c = getCompositor(type);
126: parent.addCompositor(c);
127: model.endTransaction();
128: return c;
129: }
130:
131: public Attribute dropAttribute(AXIContainer parent, String name) {
132: model.startTransaction();
133: Attribute a = model.getComponentFactory().createAttribute();
134: a.setName(name);
135: parent.addAttribute(a);
136: model.endTransaction();
137: return a;
138: }
139:
140: public Element dropElementOnCompositor(Compositor c, String name) {
141: model.startTransaction();
142: Element e = model.getComponentFactory().createElement();
143: e.setName(name);
144: c.addElement(e);
145: model.endTransaction();
146: return e;
147: }
148:
149: public void dropChildAtIndex(AXIComponent parent,
150: AXIComponent child, int i) {
151: model.startTransaction();
152: parent.addChildAtIndex(child, i);
153: model.endTransaction();
154: }
155:
156: public void setCompositorType(Compositor c, CompositorType type) {
157: //model.startTransaction();
158: c.setType(type);
159: //model.endTransaction();
160: }
161:
162: private Compositor getCompositor(CompositorType type) {
163: Compositor c = null;
164: switch (type) {
165: case SEQUENCE:
166: c = model.getComponentFactory().createSequence();
167: break;
168: case CHOICE:
169: c = model.getComponentFactory().createChoice();
170: break;
171: case ALL:
172: c = model.getComponentFactory().createAll();
173: break;
174: }
175:
176: return c;
177: }
178:
179: /**
180: * Checks if a component is part of an AXI model.
181: * Returns true if it has a valid parent and model, false otherwise.
182: */
183: public boolean inModel(AXIComponent c) {
184: //for everything else, both parent and model should be valid
185: return ((c.getParent() != null) && (c.getModel() != null) && (c
186: .getPeer() != null));
187: }
188:
189: // public boolean refactorRename(AXIContainer container, String name) {
190: // assert(container.getParent() instanceof AXIDocument);
191: // NamedReferenceable ref = null;
192: // SchemaComponent comp = container.getPeer();
193: // if (comp instanceof NamedReferenceable) {
194: // ref = NamedReferenceable.class.cast(comp);
195: // }
196: //
197: //
198: // try {
199: // SchemaModel sm = model.getSchemaModel();
200: // SharedUtils.locallyRenameRefactor((Nameable)ref, name);
201: // model.sync();
202: // } catch (IOException ex) {
203: // return false;
204: // }
205: // return true;
206: // }
207:
208: }
|