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.IWritable; 18 import org.eclipse.pde.core.ModelChangedEvent; 19 import org.eclipse.pde.internal.core.PDECoreMessages; 20 import org.eclipse.pde.internal.core.ischema.ISchema; 21 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor; 22 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 23 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 24 25 public class SchemaCompositor 26 extends RepeatableSchemaObject 27 implements ISchemaCompositor { 28 29 private static final long serialVersionUID = 1L; 30 31 public static final String P_KIND = "p_kind"; 33 private int kind; 34 private Vector children = new Vector (); 35 36 public SchemaCompositor(ISchemaObject parent, int kind) { 37 super(parent, ""); this.kind = kind; 39 switch (kind) { 40 case ALL : 41 fName = PDECoreMessages.SchemaCompositor_all; 42 break; 43 case CHOICE : 44 fName = PDECoreMessages.SchemaCompositor_choice; 45 break; 46 case GROUP : 47 fName = PDECoreMessages.SchemaCompositor_group; 48 break; 49 case SEQUENCE : 50 fName = PDECoreMessages.SchemaCompositor_sequence; 51 break; 52 } 53 } 54 public SchemaCompositor(ISchemaObject parent, String id, int kind) { 55 super(parent, id); 56 this.kind = kind; 57 } 58 public void addChild(ISchemaObject child) { 59 children.addElement(child); 60 child.setParent(this); 61 getSchema().fireModelChanged( 62 new ModelChangedEvent(getSchema(), 63 IModelChangedEvent.INSERT, 64 new Object [] { child }, 65 null)); 66 } 67 68 public void moveChildToSibling(ISchemaObject element, ISchemaObject sibling) { 69 int index = children.indexOf(element); 70 int newIndex; 71 if (sibling != null && children.contains(sibling)) 72 newIndex = children.indexOf(sibling); 73 else 74 newIndex = children.size() - 1; 75 76 if (index > newIndex) { 77 for (int i = index; i > newIndex; i--) { 78 children.set(i, children.elementAt(i - 1)); 79 } 80 } else if (index < newIndex) { 81 for (int i = index; i < newIndex; i++) { 82 children.set(i, children.elementAt(i + 1)); 83 } 84 } else return; 86 children.set(newIndex, element); 87 getSchema().fireModelChanged(new ModelChangedEvent( 88 getSchema(), IModelChangedEvent.CHANGE, 89 new Object [] { this }, null)); 90 } 91 92 public void addChild(ISchemaObject newChild, ISchemaObject afterSibling) { 93 int index = -1; 94 if (afterSibling != null) { 95 index = children.indexOf(afterSibling); 96 } 97 if (index != -1) 98 children.add(index + 1, newChild); 99 else 100 children.addElement(newChild); 101 getSchema().fireModelChanged(new ModelChangedEvent( 102 getSchema(), IModelChangedEvent.INSERT, 103 new Object [] { newChild }, null)); 104 } 105 106 public int getChildCount() { 107 return children.size(); 108 } 109 public ISchemaObject[] getChildren() { 110 ISchemaObject[] result = new ISchemaObject[children.size()]; 111 children.copyInto(result); 112 return result; 113 } 114 115 public void setParent(ISchemaObject parent) { 116 super.setParent(parent); 117 for (int i = 0; i < children.size(); i++) { 118 ISchemaObject child = (ISchemaObject) children.get(i); 119 child.setParent(this); 120 } 121 } 122 public int getKind() { 123 return kind; 124 } 125 public void removeChild(ISchemaObject child) { 126 children.removeElement(child); 127 getSchema().fireModelChanged( 128 new ModelChangedEvent(getSchema(), 129 IModelChangedEvent.REMOVE, 130 new Object [] { child }, 131 null)); 132 } 133 public void setKind(int kind) { 134 if (this.kind != kind) { 135 Integer oldValue = new Integer (this.kind); 136 this.kind = kind; 137 switch (kind) { 138 case ALL : 139 fName = PDECoreMessages.SchemaCompositor_all; 140 break; 141 case CHOICE : 142 fName = PDECoreMessages.SchemaCompositor_choice; 143 break; 144 case GROUP : 145 fName = PDECoreMessages.SchemaCompositor_group; 146 break; 147 case SEQUENCE : 148 fName = PDECoreMessages.SchemaCompositor_sequence; 149 break; 150 } 151 getSchema().fireModelObjectChanged( 152 this, 153 P_KIND, 154 oldValue, 155 new Integer (kind)); 156 } 157 } 158 public void updateReferencesFor(ISchemaElement element, int kind) { 159 for (int i = children.size() - 1; i >= 0; i--) { 160 Object child = children.elementAt(i); 161 if (child instanceof SchemaElementReference) { 162 SchemaElementReference ref = (SchemaElementReference) child; 163 String refName = ref.getReferenceName(); 164 switch (kind) { 165 case ISchema.REFRESH_ADD : 166 if (element.getName().equals(refName)) { 167 ref.setReferencedObject(element); 168 getSchema().fireModelObjectChanged(ref, null, null, null); 169 } 170 break; 171 case ISchema.REFRESH_DELETE : 172 if (element.getName().equals(refName)) { 173 removeChild(ref); 174 getSchema().fireModelObjectChanged(this, null, ref, null); 175 } 176 break; 177 case ISchema.REFRESH_RENAME : 178 if (ref.getReferencedElement() == element) 184 ref.setReferenceName(element.getName()); 185 else if (element.getName().equals(refName)) { 189 ref.setReferencedObject(element); 190 getSchema().fireModelObjectChanged(ref, null, null, null); 191 } 192 break; 193 } 194 } else { 195 SchemaCompositor compositor = (SchemaCompositor) child; 196 compositor.updateReferencesFor(element, kind); 197 } 198 } 199 } 200 public void write(String indent, PrintWriter writer) { 201 String tag = null; 202 203 switch (kind) { 204 case CHOICE : 205 tag = "choice"; break; 207 case ALL : 208 case GROUP : 209 case SEQUENCE : 210 tag = "sequence"; break; 212 } 213 if (tag == null) 214 return; 215 writer.print(indent + "<" + tag); if (getMinOccurs() != 1 || getMaxOccurs() != 1) { 217 String min = "" + getMinOccurs(); String max = 219 getMaxOccurs() == Integer.MAX_VALUE 220 ? "unbounded" : ("" + getMaxOccurs()); writer.print( 223 " minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\""); } 225 writer.println(">"); String indent2 = indent + Schema.INDENT; 227 for (int i = 0; i < children.size(); i++) { 228 Object obj = children.elementAt(i); 229 if (obj instanceof IWritable) { 230 ((IWritable) obj).write(indent2, writer); 231 } 232 } 233 writer.println(indent + "</" + tag + ">"); } 235 } 236 | Popular Tags |