1 11 package org.eclipse.pde.internal.core.schema; 12 13 import java.io.PrintWriter ; 14 import java.util.Vector ; 15 16 import org.eclipse.pde.core.IModelChangedEvent; 17 import org.eclipse.pde.core.ModelChangedEvent; 18 import org.eclipse.pde.internal.core.ischema.ISchema; 19 import org.eclipse.pde.internal.core.ischema.ISchemaAttribute; 20 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType; 21 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor; 22 23 public class SchemaComplexType 24 extends SchemaType 25 implements ISchemaComplexType { 26 27 private static final long serialVersionUID = 1L; 28 public static final String P_COMPOSITOR = "compositorProperty"; private boolean mixed; 30 private ISchemaCompositor compositor; 31 private Vector attributes = new Vector (); 32 33 public SchemaComplexType(ISchema schema) { 34 this(schema, null); 35 } 36 public SchemaComplexType(ISchema schema, String typeName) { 37 super(schema, typeName != null ? typeName : "__anonymous__"); } 39 public void addAttribute(ISchemaAttribute attribute) { 40 addAttribute(attribute, null); 41 } 42 public void addAttribute( 43 ISchemaAttribute attribute, 44 ISchemaAttribute afterSibling) { 45 int index = -1; 46 if (afterSibling != null) { 47 index = attributes.indexOf(afterSibling); 48 } 49 if (index != -1) 50 attributes.add(index + 1, attribute); 51 else 52 attributes.addElement(attribute); 53 getSchema().fireModelChanged( 54 new ModelChangedEvent(getSchema(), 55 IModelChangedEvent.INSERT, 56 new Object [] { attribute }, 57 null)); 58 } 59 public void moveAttributeTo(ISchemaAttribute attribute, ISchemaAttribute sibling) { 60 int index = attributes.indexOf(attribute); 61 int newIndex; 62 if (sibling != null && attributes.contains(sibling)) 63 newIndex = attributes.indexOf(sibling); 64 else 65 newIndex = attributes.size() - 1; 66 67 if (index > newIndex) { 68 for (int i = index; i > newIndex; i--) { 69 attributes.set(i, attributes.elementAt(i - 1)); 70 } 71 } else if (index < newIndex) { 72 for (int i = index; i < newIndex; i++) { 73 attributes.set(i, attributes.elementAt(i + 1)); 74 } 75 } else return; 77 attributes.set(newIndex, attribute); 78 getSchema().fireModelChanged( 79 new ModelChangedEvent( 80 getSchema(), 81 IModelChangedEvent.CHANGE, 82 new Object [] { attribute.getParent() }, null)); 83 } 84 public ISchemaAttribute getAttribute(String name) { 85 for (int i = 0; i < attributes.size(); i++) { 86 ISchemaAttribute attribute = 87 (ISchemaAttribute) attributes.elementAt(i); 88 if (attribute.getName().equals(name)) 89 return attribute; 90 } 91 return null; 92 } 93 94 public int getAttributeCount() { 95 return attributes.size(); 96 } 97 public ISchemaAttribute[] getAttributes() { 98 ISchemaAttribute[] result = new ISchemaAttribute[attributes.size()]; 99 attributes.copyInto(result); 100 return result; 101 } 102 public ISchemaCompositor getCompositor() { 103 return compositor; 104 } 105 public boolean isMixed() { 106 return mixed; 107 } 108 public void removeAttribute(ISchemaAttribute attribute) { 109 attributes.removeElement(attribute); 110 getSchema().fireModelChanged( 111 new ModelChangedEvent(getSchema(), 112 IModelChangedEvent.REMOVE, 113 new Object [] { attribute }, 114 null)); 115 } 116 public void setCompositor(ISchemaCompositor newCompositor) { 117 Object oldValue = compositor; 118 compositor = newCompositor; 119 getSchema().fireModelObjectChanged( 120 this, 121 P_COMPOSITOR, 122 oldValue, 123 compositor); 124 } 125 public void setMixed(boolean newMixed) { 126 mixed = newMixed; 127 } 128 public void write(String indent, PrintWriter writer) { 129 writer.println(indent + "<complexType>"); String indent2 = indent + Schema.INDENT; 131 SchemaCompositor compositor = (SchemaCompositor) getCompositor(); 132 if (compositor != null) { 133 compositor.write(indent2, writer); 134 } 135 for (int i = 0; i < attributes.size(); i++) { 136 ISchemaAttribute attribute = 137 (ISchemaAttribute) attributes.elementAt(i); 138 attribute.write(indent2, writer); 139 } 140 writer.println(indent + "</complexType>"); } 142 } 143 | Popular Tags |