1 11 package org.eclipse.pde.internal.ui.editor.schema; 12 13 import org.eclipse.pde.internal.core.ischema.ISchema; 14 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 15 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType; 16 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor; 17 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 18 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 19 import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference; 20 import org.eclipse.pde.internal.core.ischema.ISchemaRootElement; 21 import org.eclipse.pde.internal.core.ischema.ISchemaType; 22 import org.eclipse.pde.internal.core.schema.Schema; 23 import org.eclipse.pde.internal.core.schema.SchemaAttribute; 24 import org.eclipse.pde.internal.core.schema.SchemaComplexType; 25 import org.eclipse.pde.internal.core.schema.SchemaCompositor; 26 import org.eclipse.pde.internal.core.schema.SchemaElement; 27 import org.eclipse.pde.internal.core.schema.SchemaElementReference; 28 import org.eclipse.pde.internal.core.schema.SchemaSimpleType; 29 30 public class SchemaRearranger { 31 32 private Schema fSchema; 33 34 public SchemaRearranger(Schema schema) { 35 fSchema = schema; 36 } 37 38 39 public void moveCompositor(ISchemaObject newParent, ISchemaCompositor compositor) { 40 ISchemaObject oldParent = compositor.getParent(); 41 if (!(newParent != null 42 && compositor != null 43 && !newParent.equals(oldParent) 44 && !newParent.equals(compositor))) 45 return; 46 if (newParent instanceof SchemaElement) { 47 SchemaElement element = (SchemaElement)newParent; 48 SchemaComplexType type = null; 49 if (element.getType() instanceof SchemaComplexType) { 50 type = (SchemaComplexType) element.getType(); 51 type.setCompositor(compositor); 52 } else { 53 type = new SchemaComplexType(element.getSchema()); 54 type.setCompositor(compositor); 55 element.setType(type); 56 } 57 } else if (newParent instanceof SchemaCompositor) { 58 ((SchemaCompositor) newParent).addChild(compositor); 59 } else return; 61 62 if (oldParent instanceof SchemaElement) { 63 ISchemaType oldType = ((SchemaElement)oldParent).getType(); 64 if (oldType instanceof ISchemaComplexType) { 65 ((SchemaComplexType)oldType).setCompositor(null); 66 } 67 } else if (oldParent instanceof SchemaCompositor) { 68 ((SchemaCompositor)oldParent).removeChild(compositor); 69 } 70 compositor.setParent(newParent); 71 } 72 73 public void moveReference(SchemaElementReference reference, ISchemaCompositor compositor, ISchemaObject sibling) { 74 ISchemaCompositor oldCompositor = reference.getCompositor(); 75 if (!(compositor != null 76 && reference != null 77 && oldCompositor != null)) 78 return; 79 if (compositor instanceof SchemaCompositor) { 80 if (compositor.equals(oldCompositor)) { 81 ((SchemaCompositor)compositor).moveChildToSibling(reference, sibling); 82 } else { 83 ((SchemaCompositor) oldCompositor).removeChild(reference); 84 reference.setCompositor(compositor); 85 ((SchemaCompositor) compositor).addChild(reference); 86 } 87 } 88 } 89 90 public void moveElement(ISchemaObject parent, ISchemaElement element, ISchemaObject sibling) { 91 if (element == null) 92 return; 93 if (fSchema.equals(parent)) { 94 fSchema.moveElementToSibling(element, sibling); 95 } else if (parent instanceof ISchemaCompositor) { 96 linkReference((ISchemaCompositor)parent, element, sibling); 97 } 98 } 99 100 public void moveAttribute(ISchemaElement newParent, ISchemaAttribute attribute, ISchemaAttribute sibling) { 101 ISchemaObject oldParent = attribute.getParent(); 102 if (!(attribute != null 103 && newParent != null 104 && oldParent != null)) 105 return; 106 SchemaComplexType type = null; 107 if (newParent.getType() instanceof SchemaComplexType) { 108 type = (SchemaComplexType) newParent.getType(); 109 } else { 110 type = new SchemaComplexType(newParent.getSchema()); 111 ((SchemaElement)newParent).setType(type); 112 } 113 if (newParent.equals(oldParent)) { 114 type.moveAttributeTo(attribute, sibling); 115 } else { 116 if (oldParent instanceof ISchemaElement 117 && ((ISchemaElement)oldParent).getType() instanceof SchemaComplexType) { 118 SchemaComplexType oldType = (SchemaComplexType)((ISchemaElement)oldParent).getType(); 119 oldType.removeAttribute(attribute); 120 } 121 attribute.setParent(newParent); 122 type.addAttribute(attribute, sibling); 123 } 124 } 125 126 public void pasteCompositor(ISchemaObject realTarget, ISchemaCompositor compositor, ISchemaObject sibling) { 127 if (realTarget instanceof SchemaElement) { 128 SchemaElement element = (SchemaElement)realTarget; 129 SchemaComplexType type = null; 130 if (element.getType() instanceof SchemaComplexType) { 131 type = (SchemaComplexType) element.getType(); 132 type.setCompositor(compositor); 133 } else { 134 type = new SchemaComplexType(element.getSchema()); 135 element.setType(type); 136 type.setCompositor(compositor); 137 } 138 } else if (realTarget instanceof SchemaCompositor) { 139 ((SchemaCompositor) realTarget).addChild(compositor, sibling); 140 } 141 } 142 143 public void pasteReference(ISchemaObject realTarget, ISchemaObjectReference object, ISchemaObject sibling) { 144 if (realTarget instanceof SchemaCompositor) { 145 SchemaCompositor parent = (SchemaCompositor) realTarget; 146 ((SchemaElementReference)object).setCompositor(parent); 147 parent.addChild((SchemaElementReference)object, sibling); 148 } 149 } 150 151 public void pasteElement(ISchemaElement object, ISchemaObject sibling) { 152 SchemaElement element = (SchemaElement) object; 153 element.setParent(fSchema); 154 fSchema.addElement(element, (ISchemaElement) sibling); 155 fSchema.updateReferencesFor(element, ISchema.REFRESH_ADD); 156 } 157 158 public void pasteAttribute(ISchemaElement realTarget, ISchemaAttribute object, ISchemaObject sibling) { 159 SchemaElement element = (SchemaElement) realTarget; 160 SchemaAttribute attribute = (SchemaAttribute) object; 161 attribute.setParent(element); 162 ISchemaType type = element.getType(); 163 SchemaComplexType complexType = null; 164 if (!(type instanceof ISchemaComplexType)) { 165 complexType = new SchemaComplexType(element.getSchema()); 166 element.setType(complexType); 167 } else { 168 complexType = (SchemaComplexType) type; 169 } 170 if (sibling instanceof ISchemaAttribute) 171 complexType.addAttribute(attribute, (ISchemaAttribute) sibling); 172 else 173 complexType.addAttribute(attribute); 174 } 175 176 public void linkReference(ISchemaCompositor realTarget, ISchemaElement object, ISchemaObject sibling) { 177 if (sibling instanceof SchemaElementReference) 178 realTarget = ((SchemaElementReference)sibling).getCompositor(); 179 180 SchemaCompositor parent = (SchemaCompositor) realTarget; 181 String refName = object.getName(); 182 SchemaElementReference reference = new SchemaElementReference(parent, refName); 183 reference.setReferencedObject(fSchema.findElement(refName)); 184 parent.addChild(reference, sibling); 185 } 186 187 public void deleteCompositor(ISchemaCompositor compositor) { 188 ISchemaObject cparent = compositor.getParent(); 189 if (cparent instanceof ISchemaElement) { 190 SchemaElement element = (SchemaElement) cparent; 191 ISchemaType type = element.getType(); 192 if (type instanceof SchemaComplexType 193 && ((SchemaComplexType)type).getAttributeCount() != 0) 194 ((SchemaComplexType)type).setCompositor(null); 195 else 196 element.setType(new SchemaSimpleType(element.getSchema(), "string")); 198 } else if (cparent instanceof SchemaCompositor) { 199 ((SchemaCompositor) cparent).removeChild(compositor); 200 } 201 } 202 203 public void deleteAttribute(ISchemaAttribute attribute) { 204 ISchemaElement element = (ISchemaElement)attribute.getParent(); 205 SchemaComplexType type = (SchemaComplexType) element.getType(); 206 type.removeAttribute(attribute); 207 } 208 209 public void deleteElement(ISchemaElement element) { 210 if (!(element instanceof ISchemaRootElement)) { 211 Schema schema = (Schema) element.getParent(); 212 schema.removeElement(element); 213 schema.updateReferencesFor(element, ISchema.REFRESH_DELETE); 214 } 215 } 216 217 public void deleteReference(SchemaElementReference reference) { 218 SchemaCompositor compositor = (SchemaCompositor)reference.getCompositor(); 219 compositor.removeChild(reference); 220 } 221 } 222 | Popular Tags |