001: /*******************************************************************************
002: * Copyright (c) 2005, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.pde.internal.ui.editor.schema;
011:
012: import org.eclipse.pde.internal.core.ischema.ISchema;
013: import org.eclipse.pde.internal.core.ischema.ISchemaAttribute;
014: import org.eclipse.pde.internal.core.ischema.ISchemaComplexType;
015: import org.eclipse.pde.internal.core.ischema.ISchemaCompositor;
016: import org.eclipse.pde.internal.core.ischema.ISchemaElement;
017: import org.eclipse.pde.internal.core.ischema.ISchemaObject;
018: import org.eclipse.pde.internal.core.ischema.ISchemaObjectReference;
019: import org.eclipse.pde.internal.core.ischema.ISchemaRootElement;
020: import org.eclipse.pde.internal.core.ischema.ISchemaType;
021: import org.eclipse.pde.internal.core.schema.Schema;
022: import org.eclipse.pde.internal.core.schema.SchemaAttribute;
023: import org.eclipse.pde.internal.core.schema.SchemaComplexType;
024: import org.eclipse.pde.internal.core.schema.SchemaCompositor;
025: import org.eclipse.pde.internal.core.schema.SchemaElement;
026: import org.eclipse.pde.internal.core.schema.SchemaElementReference;
027: import org.eclipse.pde.internal.core.schema.SchemaSimpleType;
028: import org.eclipse.pde.internal.ui.util.PDELabelUtility;
029:
030: public class SchemaRearranger {
031:
032: private Schema fSchema;
033:
034: public SchemaRearranger(Schema schema) {
035: fSchema = schema;
036: }
037:
038: public void moveCompositor(ISchemaObject newParent,
039: ISchemaCompositor compositor) {
040: ISchemaObject oldParent = compositor.getParent();
041: if (!(newParent != null && compositor != null
042: && !newParent.equals(oldParent) && !newParent
043: .equals(compositor)))
044: return;
045: if (newParent instanceof SchemaElement) {
046: SchemaElement element = (SchemaElement) newParent;
047: SchemaComplexType type = null;
048: if (element.getType() instanceof SchemaComplexType) {
049: type = (SchemaComplexType) element.getType();
050: type.setCompositor(compositor);
051: } else {
052: type = new SchemaComplexType(element.getSchema());
053: type.setCompositor(compositor);
054: element.setType(type);
055: }
056: } else if (newParent instanceof SchemaCompositor) {
057: ((SchemaCompositor) newParent).addChild(compositor);
058: } else
059: // unknown new parent, abort
060: return;
061:
062: if (oldParent instanceof SchemaElement) {
063: ISchemaType oldType = ((SchemaElement) oldParent).getType();
064: if (oldType instanceof ISchemaComplexType) {
065: ((SchemaComplexType) oldType).setCompositor(null);
066: }
067: } else if (oldParent instanceof SchemaCompositor) {
068: ((SchemaCompositor) oldParent).removeChild(compositor);
069: }
070: compositor.setParent(newParent);
071: }
072:
073: public void moveReference(SchemaElementReference reference,
074: ISchemaCompositor compositor, ISchemaObject sibling) {
075: ISchemaCompositor oldCompositor = reference.getCompositor();
076: if (!(compositor != null && reference != null && oldCompositor != null))
077: return;
078: if (compositor instanceof SchemaCompositor) {
079: if (compositor.equals(oldCompositor)) {
080: ((SchemaCompositor) compositor).moveChildToSibling(
081: reference, sibling);
082: } else {
083: ((SchemaCompositor) oldCompositor)
084: .removeChild(reference);
085: reference.setCompositor(compositor);
086: ((SchemaCompositor) compositor).addChild(reference);
087: }
088: }
089: }
090:
091: public void moveElement(ISchemaObject parent,
092: ISchemaElement element, ISchemaObject sibling) {
093: if (element == null)
094: return;
095: if (fSchema.equals(parent)) {
096: fSchema.moveElementToSibling(element, sibling);
097: } else if (parent instanceof ISchemaCompositor) {
098: linkReference((ISchemaCompositor) parent, element, sibling);
099: }
100: }
101:
102: public void moveAttribute(ISchemaElement newParent,
103: ISchemaAttribute attribute, ISchemaAttribute sibling) {
104: ISchemaObject oldParent = attribute.getParent();
105: if (!(attribute != null && newParent != null && oldParent != null))
106: return;
107: SchemaComplexType type = null;
108: if (newParent.getType() instanceof SchemaComplexType) {
109: type = (SchemaComplexType) newParent.getType();
110: } else {
111: type = new SchemaComplexType(newParent.getSchema());
112: ((SchemaElement) newParent).setType(type);
113: }
114: if (newParent.equals(oldParent)) {
115: type.moveAttributeTo(attribute, sibling);
116: } else {
117: if (oldParent instanceof ISchemaElement
118: && ((ISchemaElement) oldParent).getType() instanceof SchemaComplexType) {
119: SchemaComplexType oldType = (SchemaComplexType) ((ISchemaElement) oldParent)
120: .getType();
121: oldType.removeAttribute(attribute);
122: }
123: attribute.setParent(newParent);
124: if (attribute instanceof SchemaAttribute)
125: ((SchemaAttribute) attribute).setName(PDELabelUtility
126: .generateName(newParent.getAttributeNames(),
127: PDELabelUtility.getBaseName(attribute
128: .getName(), false), false));
129: type.addAttribute(attribute, sibling);
130: }
131: }
132:
133: public void pasteCompositor(ISchemaObject realTarget,
134: ISchemaCompositor compositor, ISchemaObject sibling) {
135: if (realTarget instanceof SchemaElement) {
136: SchemaElement element = (SchemaElement) realTarget;
137: SchemaComplexType type = null;
138: if (element.getType() instanceof SchemaComplexType) {
139: type = (SchemaComplexType) element.getType();
140: type.setCompositor(compositor);
141: } else {
142: type = new SchemaComplexType(element.getSchema());
143: element.setType(type);
144: type.setCompositor(compositor);
145: }
146: } else if (realTarget instanceof SchemaCompositor) {
147: ((SchemaCompositor) realTarget).addChild(compositor,
148: sibling);
149: }
150: }
151:
152: public void pasteReference(ISchemaObject realTarget,
153: ISchemaObjectReference object, ISchemaObject sibling) {
154: if (realTarget instanceof SchemaCompositor) {
155: SchemaCompositor parent = (SchemaCompositor) realTarget;
156: ((SchemaElementReference) object).setCompositor(parent);
157: parent.addChild((SchemaElementReference) object, sibling);
158: }
159: }
160:
161: public void pasteElement(ISchemaElement object,
162: ISchemaObject sibling) {
163: SchemaElement element = (SchemaElement) object;
164: element.setParent(fSchema);
165: element.setName(PDELabelUtility.generateName(element
166: .getSchema().getElementNames(), PDELabelUtility
167: .getBaseName(element.getName(), false), false));
168: fSchema.addElement(element, (ISchemaElement) sibling);
169: fSchema.updateReferencesFor(element, ISchema.REFRESH_ADD);
170: }
171:
172: public void pasteAttribute(ISchemaElement realTarget,
173: ISchemaAttribute object, ISchemaObject sibling) {
174: SchemaElement element = (SchemaElement) realTarget;
175: SchemaAttribute attribute = (SchemaAttribute) object;
176: attribute.setParent(element);
177: attribute.setName(PDELabelUtility.generateName(element
178: .getAttributeNames(), PDELabelUtility.getBaseName(
179: attribute.getName(), false), false));
180: ISchemaType type = element.getType();
181: SchemaComplexType complexType = null;
182: if (!(type instanceof ISchemaComplexType)) {
183: complexType = new SchemaComplexType(element.getSchema());
184: element.setType(complexType);
185: } else {
186: complexType = (SchemaComplexType) type;
187: }
188: if (sibling instanceof ISchemaAttribute)
189: complexType.addAttribute(attribute,
190: (ISchemaAttribute) sibling);
191: else
192: complexType.addAttribute(attribute);
193: }
194:
195: public void linkReference(ISchemaCompositor realTarget,
196: ISchemaElement object, ISchemaObject sibling) {
197: if (sibling instanceof SchemaElementReference)
198: realTarget = ((SchemaElementReference) sibling)
199: .getCompositor();
200:
201: SchemaCompositor parent = (SchemaCompositor) realTarget;
202: String refName = object.getName();
203: SchemaElementReference reference = new SchemaElementReference(
204: parent, refName);
205: reference.setReferencedObject(fSchema.findElement(refName));
206: parent.addChild(reference, sibling);
207: }
208:
209: public void deleteCompositor(ISchemaCompositor compositor) {
210: ISchemaObject cparent = compositor.getParent();
211: if (cparent instanceof ISchemaElement) {
212: SchemaElement element = (SchemaElement) cparent;
213: ISchemaType type = element.getType();
214: if (type instanceof SchemaComplexType
215: && ((SchemaComplexType) type).getAttributeCount() != 0)
216: ((SchemaComplexType) type).setCompositor(null);
217: else
218: element.setType(new SchemaSimpleType(element
219: .getSchema(), "string")); //$NON-NLS-1$
220:
221: } else if (cparent instanceof SchemaCompositor) {
222: ((SchemaCompositor) cparent).removeChild(compositor);
223: }
224: }
225:
226: public void deleteAttribute(ISchemaAttribute attribute) {
227: ISchemaElement element = (ISchemaElement) attribute.getParent();
228: SchemaComplexType type = (SchemaComplexType) element.getType();
229: type.removeAttribute(attribute);
230: }
231:
232: public void deleteElement(ISchemaElement element) {
233: if (!(element instanceof ISchemaRootElement)) {
234: Schema schema = (Schema) element.getParent();
235: schema.removeElement(element);
236: schema.updateReferencesFor(element, ISchema.REFRESH_DELETE);
237: }
238: }
239:
240: public void deleteReference(SchemaElementReference reference) {
241: SchemaCompositor compositor = (SchemaCompositor) reference
242: .getCompositor();
243: compositor.removeChild(reference);
244: }
245: }
|