KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > util > SimulationHelper


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.xml.axi.util;
20
21 import java.io.IOException JavaDoc;
22 import org.netbeans.modules.xml.axi.AXIComponent;
23 import org.netbeans.modules.xml.axi.AXIContainer;
24 import org.netbeans.modules.xml.axi.AXIDocument;
25 import org.netbeans.modules.xml.axi.AXIModel;
26 import org.netbeans.modules.xml.axi.AXIType;
27 import org.netbeans.modules.xml.axi.Attribute;
28 import org.netbeans.modules.xml.axi.Compositor;
29 import org.netbeans.modules.xml.axi.Compositor.CompositorType;
30 import org.netbeans.modules.xml.axi.ContentModel;
31 import org.netbeans.modules.xml.axi.Element;
32 import org.netbeans.modules.xml.refactoring.RefactoringManager;
33 import org.netbeans.modules.xml.refactoring.RenameRequest;
34 import org.netbeans.modules.xml.schema.model.SchemaComponent;
35 import org.netbeans.modules.xml.schema.model.SchemaModel;
36 import org.netbeans.modules.xml.xam.Nameable;
37 import org.netbeans.modules.xml.xam.NamedReferenceable;
38
39 /**
40  *
41  * @author Samaresh
42  */

43 public class SimulationHelper {
44     
45     private AXIModel model;
46     
47     /**
48      * Creates a new instance of SimulationHelper
49      */

50     public SimulationHelper(AXIModel model) {
51         this.model = model;
52     }
53     
54     public Attribute dropAttributeOnElement(Element e, String JavaDoc name) {
55         AXIDocument doc = model.getRoot();
56         model.startTransaction();
57         Attribute a = model.getComponentFactory().createAttribute();
58         a.setName(name);
59         e.addAttribute(a);
60         model.endTransaction();
61         return a;
62     }
63     
64     public Element dropGlobalElement(String JavaDoc name) {
65         AXIDocument doc = model.getRoot();
66         model.startTransaction();
67         Element e = model.getComponentFactory().createElement();
68         e.setName(name);
69         doc.addElement(e);
70         model.endTransaction();
71         return e;
72     }
73     
74     public ContentModel dropGlobalComplexType(String JavaDoc name) {
75         AXIDocument doc = model.getRoot();
76         model.startTransaction();
77         ContentModel cm = model.getComponentFactory().createComplexType();
78         cm.setName(name);
79         doc.addContentModel(cm);
80         model.endTransaction();
81         return cm;
82     }
83     
84     public Element dropElement(AXIContainer parent, String JavaDoc name) {
85         model.startTransaction();
86         Element element = model.getComponentFactory().createElement();
87         element.setName(name);
88         parent.addElement(element);
89         model.endTransaction();
90         return element;
91     }
92     
93     public void setElementType(Element e, AXIType type) {
94         model.startTransaction();
95         e.setType(type);
96         model.endTransaction();
97     }
98     
99     public void delete(AXIComponent child) {
100         model.startTransaction();
101         child.getParent().removeChild(child);
102         model.endTransaction();
103     }
104     
105     public void clearAll() {
106         model.startTransaction();
107         model.getRoot().removeAllChildren();
108         model.endTransaction();
109     }
110         
111     public Compositor dropCompositor(AXIContainer parent, CompositorType type) {
112         model.startTransaction();
113         Compositor c = getCompositor(type);
114         parent.addCompositor(c);
115         model.endTransaction();
116         return c;
117     }
118     
119     public Element dropElementOnCompositor(Compositor c, String JavaDoc name) {
120         model.startTransaction();
121         Element e = model.getComponentFactory().createElement();
122         e.setName(name);
123         c.addElement(e);
124         model.endTransaction();
125         return e;
126     }
127
128     public void dropChildAtIndex(AXIComponent parent, AXIComponent child, int i) {
129         model.startTransaction();
130         parent.addChildAtIndex(child, i);
131         model.endTransaction();
132     }
133
134     public void setCompositorType(Compositor c, CompositorType type) {
135         model.startTransaction();
136         c.setType(type);
137         model.endTransaction();
138     }
139     
140     private Compositor getCompositor(CompositorType type) {
141         Compositor c = null;
142         switch(type) {
143             case SEQUENCE:
144                 c = model.getComponentFactory().createSequence();
145                 break;
146             case CHOICE:
147                 c = model.getComponentFactory().createChoice();
148                 break;
149             case ALL:
150                 c = model.getComponentFactory().createAll();
151                 break;
152         }
153         
154         return c;
155     }
156     
157     /**
158      * Checks if a component is part of an AXI model.
159      * Returns true if it has a valid parent and model, false otherwise.
160      */

161     public boolean inModel(AXIComponent c) {
162     //for everything else, both parent and model should be valid
163
return ( (c.getParent() != null) && (c.getModel() != null) && (c.getPeer() != null));
164     }
165     
166     public boolean refactorRename(AXIContainer container, String JavaDoc name) {
167         assert(container.getParent() instanceof AXIDocument);
168         NamedReferenceable ref = null;
169         SchemaComponent comp = container.getPeer();
170         if (comp instanceof NamedReferenceable) {
171             ref = NamedReferenceable.class.cast(comp);
172         }
173         
174         RenameRequest request = new RenameRequest((Nameable)ref, name);
175         request.setScopeLocal();
176         try {
177             SchemaModel sm = model.getSchemaModel();
178             RefactoringManager.getInstance().execute(request, false);
179             model.sync();
180         } catch (IOException JavaDoc ex) {
181             return false;
182         }
183         return true;
184     }
185
186 }
187
Popular Tags