KickJava   Java API By Example, From Geeks To Geeks.

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


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.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 JavaDoc P_COMPOSITOR = "compositorProperty"; //$NON-NLS-1$
29
private boolean mixed;
30     private ISchemaCompositor compositor;
31     private Vector JavaDoc attributes = new Vector JavaDoc();
32
33     public SchemaComplexType(ISchema schema) {
34         this(schema, null);
35     }
36     public SchemaComplexType(ISchema schema, String JavaDoc typeName) {
37         super(schema, typeName != null ? typeName : "__anonymous__"); //$NON-NLS-1$
38
}
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 JavaDoc[] { 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 // don't need to move
76
return;
77         attributes.set(newIndex, attribute);
78         getSchema().fireModelChanged(
79                 new ModelChangedEvent(
80                         getSchema(),
81                         IModelChangedEvent.CHANGE,
82                         new Object JavaDoc[] { attribute.getParent() }, null));
83     }
84     public ISchemaAttribute getAttribute(String JavaDoc 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 JavaDoc[] { attribute },
114                 null));
115     }
116     public void setCompositor(ISchemaCompositor newCompositor) {
117         Object JavaDoc 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 JavaDoc indent, PrintWriter JavaDoc writer) {
129         writer.println(indent + "<complexType>"); //$NON-NLS-1$
130
String JavaDoc 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>"); //$NON-NLS-1$
141
}
142 }
143
Popular Tags