1 19 package org.netbeans.modules.xml.axi.util; 20 21 import java.io.IOException ; 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 43 public class SimulationHelper { 44 45 private AXIModel model; 46 47 50 public SimulationHelper(AXIModel model) { 51 this.model = model; 52 } 53 54 public Attribute dropAttributeOnElement(Element e, String 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 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 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 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 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 161 public boolean inModel(AXIComponent c) { 162 return ( (c.getParent() != null) && (c.getModel() != null) && (c.getPeer() != null)); 164 } 165 166 public boolean refactorRename(AXIContainer container, String 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 ex) { 181 return false; 182 } 183 return true; 184 } 185 186 } 187 | Popular Tags |