KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > xml > axi > Compositor


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.modules.xml.axi;
20
21 import java.io.IOException JavaDoc;
22 import org.netbeans.modules.xml.axi.impl.AXIModelImpl;
23 import org.netbeans.modules.xml.axi.visitor.AXIVisitor;
24 import org.netbeans.modules.xml.axi.Compositor.CompositorType;
25 import org.netbeans.modules.xml.schema.model.All;
26 import org.netbeans.modules.xml.schema.model.Choice;
27 import org.netbeans.modules.xml.schema.model.SchemaComponent;
28 import org.netbeans.modules.xml.schema.model.Sequence;
29
30 /**
31  * Represents a compositor in XML Schema. A compositor can be
32  * one of Sequence, Choice or All.
33  *
34  * @author Samaresh (Samaresh.Panda@Sun.Com)
35  */

36 public class Compositor extends AXIComponent {
37             
38     /**
39      * Represents the type of this compositor.
40      * Can be one of sequence, choice and all.
41      */

42     public static enum CompositorType {
43         SEQUENCE, CHOICE, ALL;
44                 
45         public String JavaDoc getName() {
46             return toString();
47         }
48         
49         public String JavaDoc toString() {
50             String JavaDoc retValue = super.toString();
51             return retValue.substring(0,1) + retValue.substring(1).toLowerCase();
52         }
53     }
54     
55     /**
56      * Creates a new instance of Compositor
57      */

58     Compositor(AXIModel model, CompositorType type) {
59         super(model);
60         this.type = type;
61     }
62     
63     /**
64      * Creates a new instance of Compositor
65      */

66     Compositor(AXIModel model, SchemaComponent schemaComponent) {
67         super(model, schemaComponent);
68         if(schemaComponent instanceof Sequence)
69             type = CompositorType.SEQUENCE;
70         if(schemaComponent instanceof Choice)
71             type = CompositorType.CHOICE;
72         if(schemaComponent instanceof All)
73             type = CompositorType.ALL;
74     }
75     
76     /**
77      * Creates a proxy Compositor
78      */

79     public Compositor(AXIModel model, AXIComponent sharedComponent) {
80         super(model, sharedComponent);
81     }
82     
83     
84     public void accept(AXIVisitor visitor) {
85         visitor.visit(this);
86     }
87     
88     /**
89      * Returns the type of this compositor.
90      */

91     public CompositorType getType() {
92         return type;
93     }
94         
95     /**
96      * Sets the type of this compositor.
97      */

98     public void setType(CompositorType value) {
99         getModel().startTransaction();
100         try{
101             firePropertyChangeEvent(Compositor.PROP_TYPE, getType(), value);
102         }finally{
103             getModel().endTransaction();
104             try {
105                 ((AXIModelImpl)getModel()).setForceSync(true);
106                 getModel().sync();
107             } catch(IOException JavaDoc iox) {
108             } finally {
109                 if(getModel() != null)
110                     ((AXIModelImpl)getModel()).setForceSync(false);
111             }
112         }
113     }
114         
115     void setCompositorType(Compositor.CompositorType newType) {
116         this.type = newType;
117     }
118     
119     /**
120      * Returns the min occurs.
121      */

122     public String JavaDoc getMinOccurs() {
123         return minOccurs;
124     }
125     
126     public void setMinOccurs(String JavaDoc value) {
127         String JavaDoc oldValue = getMinOccurs();
128         if( (oldValue == null && value == null) ||
129             (oldValue != null && oldValue.equals(value)) ) {
130             return;
131         }
132         this.minOccurs = value;
133         firePropertyChangeEvent(PROP_MINOCCURS, oldValue, value);
134     }
135     
136     /**
137      * Returns the max occurs.
138      */

139     public String JavaDoc getMaxOccurs() {
140         return maxOccurs;
141     }
142     
143     public void setMaxOccurs(String JavaDoc value) {
144         String JavaDoc oldValue = getMaxOccurs();
145         if( (oldValue == null && value == null) ||
146             (oldValue != null && oldValue.equals(value)) ) {
147             return;
148         }
149         this.maxOccurs = value;
150         firePropertyChangeEvent(PROP_MAXOCCURS, oldValue, value);
151     }
152         
153     /**
154      * Adds a Compositor as its child.
155      */

156     public void addCompositor(Compositor compositor) {
157         appendChild(Compositor.PROP_COMPOSITOR, compositor);
158     }
159     
160     /**
161      * Removes an Compositor.
162      */

163     public void removeCompositor(Compositor compositor) {
164         removeChild(Compositor.PROP_COMPOSITOR, compositor);
165     }
166     
167     /**
168      * Adds an Element as its child.
169      */

170     public void addElement(Element element) {
171         appendChild(Element.PROP_ELEMENT, element);
172     }
173         
174     /**
175      * Removes an Element.
176      */

177     public void removeElement(Element element) {
178         removeChild(Element.PROP_ELEMENT, element);
179     }
180     
181     /**
182      * true if #getMaxOccurs() and #getMinOccurs() allow multiciplity outside
183      * [0,1], false otherwise. This method is only accurate after the element
184      * has been inserted into the model.
185      */

186     public boolean allowsFullMultiplicity() {
187         return !(getParent() instanceof Compositor &&
188                 ((Compositor)getParent()).getType() == CompositorType.ALL);
189     }
190     
191     public String JavaDoc toString() {
192         if(type == null)
193             return null;
194         
195         return type.toString();
196     }
197     
198     private CompositorType type;
199     private String JavaDoc minOccurs;
200     private String JavaDoc maxOccurs;
201     
202     public static final String JavaDoc PROP_COMPOSITOR = "compositor"; // NOI18N
203
public static final String JavaDoc PROP_MINOCCURS = "minOccurs"; // NOI18N
204
public static final String JavaDoc PROP_MAXOCCURS = "maxOccurs"; // NOI18N
205
public static final String JavaDoc PROP_TYPE = "type"; // NOI18N
206
}
207
Popular Tags