KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > core > schema > SchemaCompositor


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.core.schema;
12
13 import java.io.PrintWriter JavaDoc;
14 import java.util.Vector JavaDoc;
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 JavaDoc P_KIND = "p_kind"; //$NON-NLS-1$
32

33     private int kind;
34     private Vector JavaDoc children = new Vector JavaDoc();
35
36     public SchemaCompositor(ISchemaObject parent, int kind) {
37         super(parent, ""); //$NON-NLS-1$
38
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 JavaDoc 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 JavaDoc[] { 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 // don't need to move
85
return;
86         children.set(newIndex, element);
87         getSchema().fireModelChanged(new ModelChangedEvent(
88                 getSchema(), IModelChangedEvent.CHANGE,
89                 new Object JavaDoc[] { 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 JavaDoc[] { 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 JavaDoc[] { child },
131                 null));
132     }
133     public void setKind(int kind) {
134         if (this.kind != kind) {
135             Integer JavaDoc oldValue = new Integer JavaDoc(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 JavaDoc(kind));
156         }
157     }
158     public void updateReferencesFor(ISchemaElement element, int kind) {
159         for (int i = children.size() - 1; i >= 0; i--) {
160             Object JavaDoc child = children.elementAt(i);
161             if (child instanceof SchemaElementReference) {
162                 SchemaElementReference ref = (SchemaElementReference) child;
163                 String JavaDoc 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                         // Using the object comparison, try to
179
// resolve and set the name if there is
180
// a match. This is done to repair the
181
// reference when the referenced object's
182
// name changes.
183
if (ref.getReferencedElement() == element)
184                             ref.setReferenceName(element.getName());
185                         // Also handle the case where rename
186
// will satisfy a previously broken
187
// reference.
188
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 JavaDoc indent, PrintWriter JavaDoc writer) {
201         String JavaDoc tag = null;
202
203         switch (kind) {
204             case CHOICE :
205                 tag = "choice"; //$NON-NLS-1$
206
break;
207             case ALL :
208             case GROUP :
209             case SEQUENCE :
210                 tag = "sequence"; //$NON-NLS-1$
211
break;
212         }
213         if (tag == null)
214             return;
215         writer.print(indent + "<" + tag); //$NON-NLS-1$
216
if (getMinOccurs() != 1 || getMaxOccurs() != 1) {
217             String JavaDoc min = "" + getMinOccurs(); //$NON-NLS-1$
218
String JavaDoc max =
219                 getMaxOccurs() == Integer.MAX_VALUE
220                     ? "unbounded" //$NON-NLS-1$
221
: ("" + getMaxOccurs()); //$NON-NLS-1$
222
writer.print(
223                 " minOccurs=\"" + min + "\" maxOccurs=\"" + max + "\""); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
224
}
225         writer.println(">"); //$NON-NLS-1$
226
String JavaDoc indent2 = indent + Schema.INDENT;
227         for (int i = 0; i < children.size(); i++) {
228             Object JavaDoc obj = children.elementAt(i);
229             if (obj instanceof IWritable) {
230                 ((IWritable) obj).write(indent2, writer);
231             }
232         }
233         writer.println(indent + "</" + tag + ">"); //$NON-NLS-1$ //$NON-NLS-2$
234
}
235 }
236
Popular Tags