Debian Codegraph @

plugins/org.eclipse.xsd.edit/src/org/eclipse/xsd/provider/XSDSchemaItemProvider.java

raw
1 /**
2 * Copyright (c) 2002-2007 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 v2.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v20.html
7 *
8 * Contributors:
9 * IBM - Initial API and implementation
10 */
11 package org.eclipse.xsd.provider;
12
13
14 import java.util.ArrayList;
15 import java.util.Collection;
16 import java.util.List;
17
18 import org.eclipse.emf.common.command.Command;
19 import org.eclipse.emf.common.notify.AdapterFactory;
20 import org.eclipse.emf.common.notify.Notification;
21 import org.eclipse.emf.common.util.URI;
22 import org.eclipse.emf.ecore.EAttribute;
23 import org.eclipse.emf.ecore.EObject;
24 import org.eclipse.emf.ecore.EReference;
25 import org.eclipse.emf.ecore.EStructuralFeature;
26 import org.eclipse.emf.edit.command.CopyCommand;
27 import org.eclipse.emf.edit.command.InitializeCopyCommand;
28 import org.eclipse.emf.edit.domain.EditingDomain;
29 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
30 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
31 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
32
33 import org.eclipse.xsd.XSDInclude;
34 import org.eclipse.xsd.XSDRedefine;
35 import org.eclipse.xsd.XSDSchema;
36
37
38 /**
39 * This is the item provider adpater for a {@link org.eclipse.xsd.XSDSchema} object.
40 */
41 public class XSDSchemaItemProvider
42 extends XSDComponentItemProvider
43 {
44 /**
45 * This constructs an instance from a factory and a notifier.
46 */
47 public XSDSchemaItemProvider(AdapterFactory adapterFactory)
48 {
49 super(adapterFactory);
50 }
51
52 /**
53 * This returns the property descriptors for the adapted class.
54 */
55 @Override
56 public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object)
57 {
58 if (itemPropertyDescriptors == null)
59 {
60 super.getPropertyDescriptors(object);
61 createSchemaLocationPropertyDescriptor(object);
62 createTargetNamespacePropertyDescriptor(object);
63 createFinalDefaultPropertyDescriptor(object);
64 createBlockDefaultPropertyDescriptor(object);
65 createAttributeFormDefaultPropertyDescriptor(object);
66 createElementFormDefaultPropertyDescriptor(object);
67 }
68 return itemPropertyDescriptors;
69 }
70
71
72 protected void createSchemaLocationPropertyDescriptor(Object object)
73 {
74 // This is for the schemaLocation feature.
75 //
76 itemPropertyDescriptors.add
77 (new ItemPropertyDescriptor
78 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
79 XSDEditPlugin.INSTANCE.getString("_UI_SchemaLocation_label"),
80 XSDEditPlugin.INSTANCE.getString("_UI_SchemaLocationOfSchema_description"),
81 xsdPackage.getXSDSchema_SchemaLocation(),
82 false,
83 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
84 }
85
86 protected void createTargetNamespacePropertyDescriptor(Object object)
87 {
88 // This is for the targetNamespace feature.
89 //
90 itemPropertyDescriptors.add
91 (new ItemPropertyDescriptor
92 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
93 XSDEditPlugin.INSTANCE.getString("_UI_TargetNamespace_label"),
94 XSDEditPlugin.INSTANCE.getString("_UI_TargetNamespaceOfSchema_description"),
95 xsdPackage.getXSDSchema_TargetNamespace(),
96 true,
97 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
98 {
99 @Override
100 public void setPropertyValue(Object o, Object value)
101 {
102 if ("".equals(value))
103 {
104 super.setPropertyValue(o, null);
105 }
106 else
107 {
108 super.setPropertyValue(o, value);
109 }
110 }
111 });
112 }
113
114 protected void createFinalDefaultPropertyDescriptor(Object object)
115 {
116 // This is for the finalDefault feature.
117 //
118 itemPropertyDescriptors.add
119 (new ItemPropertyDescriptor
120 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
121 XSDEditPlugin.INSTANCE.getString("_UI_FinalDefault_label"),
122 XSDEditPlugin.INSTANCE.getString("_UI_FinalDefault_description"),
123 xsdPackage.getXSDSchema_FinalDefault(),
124 true,
125 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
126 {
127 @Override
128 public Object getPropertyValue(Object o)
129 {
130 return ((XSDSchema)o).getStringFinalDefault();
131 }
132 @Override
133 public void setPropertyValue(Object o, Object value)
134 {
135 ((XSDSchema)o).setStringFinalDefault((String)value);
136 }
137 @Override
138 public Collection<?> getChoiceOfValues(Object o)
139 {
140 Collection<Object> result = new ArrayList<Object>();
141 result.add("");
142 result.add("#all");
143 result.add("extension");
144 result.add("restriction");
145 result.add("restriction extension");
146 return result;
147 }
148 });
149 }
150
151 protected void createBlockDefaultPropertyDescriptor(Object object)
152 {
153 // This is for the blockDefault feature.
154 //
155 itemPropertyDescriptors.add
156 (new ItemPropertyDescriptor
157 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
158 XSDEditPlugin.INSTANCE.getString("_UI_BlockDefault_label"),
159 XSDEditPlugin.INSTANCE.getString("_UI_BlockDefault_description"),
160 xsdPackage.getXSDSchema_BlockDefault(),
161 true,
162 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
163 {
164 @Override
165 public Object getPropertyValue(Object o)
166 {
167 return ((XSDSchema)o).getStringBlockDefault();
168 }
169 @Override
170 public void setPropertyValue(Object o, Object value)
171 {
172 ((XSDSchema)o).setStringBlockDefault((String)value);
173 }
174 @Override
175 public Collection<?> getChoiceOfValues(Object o)
176 {
177 Collection<Object> result = new ArrayList<Object>();
178 result.add("");
179 result.add("#all");
180 result.add("extension");
181 result.add("restriction");
182 result.add("substitution");
183 result.add("extension restriction");
184 result.add("extension restriction substitution");
185 result.add("extension substitution");
186 result.add("restriction substitution");
187 return result;
188 }
189 });
190 }
191
192 protected void createElementFormDefaultPropertyDescriptor(Object object)
193 {
194 // This is for the elementFormDefault feature.
195 //
196 itemPropertyDescriptors.add
197 (new ItemPropertyDescriptorWithDefault
198 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
199 XSDEditPlugin.INSTANCE.getString("_UI_ElementFormDefault_label"),
200 XSDEditPlugin.INSTANCE.getString("_UI_ElementFormDefault_description"),
201 xsdPackage.getXSDSchema_ElementFormDefault(),
202 true,
203 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
204 }
205
206 protected void createAttributeFormDefaultPropertyDescriptor(Object object)
207 {
208 // This is for the elementFormDefault feature.
209 //
210 itemPropertyDescriptors.add
211 (new ItemPropertyDescriptorWithDefault
212 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
213 XSDEditPlugin.INSTANCE.getString("_UI_AttributeFormDefault_label"),
214 XSDEditPlugin.INSTANCE.getString("_UI_AttributeFormDefault_description"),
215 xsdPackage.getXSDSchema_AttributeFormDefault(),
216 true,
217 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
218 }
219
220 /**
221 * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
222 * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
223 * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}.
224 */
225 @Override
226 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object)
227 {
228 if (childrenFeatures == null)
229 {
230 super.getChildrenFeatures(object);
231 childrenFeatures.add(xsdPackage.getXSDSchema_Contents());
232 }
233 return childrenFeatures;
234 }
235
236 /**
237 * This returns XSDSchema.gif.
238 */
239 @Override
240 public Object getImage(Object object)
241 {
242 return XSDEditPlugin.INSTANCE.getImage("full/obj16/XSDSchema");
243 }
244
245 @Override
246 public String getText(Object object)
247 {
248 XSDSchema xsdSchema = (XSDSchema)object;
249 String result = xsdSchema.getSchemaLocation();
250 if (result == null)
251 {
252 return "";
253 }
254 else
255 {
256 return URI.createURI(result).lastSegment();
257 }
258 }
259
260 /**
261 * This handles notification by calling {@link #fireNotifyChanged fireNotifyChanged}.
262 */
263 @Override
264 public void notifyChanged(Notification msg)
265 {
266 if (
267 msg.getFeature() == xsdPackage.getXSDSchema_SchemaLocation() ||
268 msg.getFeature() == xsdPackage.getXSDSchema_TypeDefinitions() ||
269 msg.getFeature() == xsdPackage.getXSDSchema_ElementDeclarations() ||
270 msg.getFeature() == xsdPackage.getXSDSchema_AttributeGroupDefinitions() ||
271 msg.getFeature() == xsdPackage.getXSDSchema_NotationDeclarations() ||
272 msg.getFeature() == xsdPackage.getXSDSchema_Contents() ||
273 msg.getFeature() == xsdPackage.getXSDSchema_ModelGroupDefinitions() ||
274 msg.getFeature() == xsdPackage.getXSDSchema_AttributeDeclarations() ||
275 msg.getFeature() == xsdPackage.getXSDSchema_AttributeFormDefault() ||
276 msg.getFeature() == xsdPackage.getXSDSchema_ElementFormDefault() ||
277 msg.getFeature() == xsdPackage.getXSDSchema_BlockDefault() ||
278 msg.getFeature() == xsdPackage.getXSDSchema_FinalDefault() ||
279 msg.getFeature() == xsdPackage.getXSDSchema_Annotations()
280 )
281 {
282 fireNotifyChanged(msg);
283 return;
284 }
285 super.notifyChanged(msg);
286 }
287
288 /**
289 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
290 * that can be created under this object.
291 */
292 @Override
293 protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object)
294 {
295 super.collectNewChildDescriptors(newChildDescriptors, object);
296 XSDSchema xsdSchema = (XSDSchema) object;
297
298 // If the schema isn't a really bad one...
299 //
300 if (xsdSchema.getSchemaForSchema() != null)
301 {
302 EReference schemaContents = xsdPackage.getXSDSchema_Contents();
303 // annotation
304 newChildDescriptors.add(createChildParameter(schemaContents, xsdFactory.createXSDAnnotation()));
305
306 // import
307 newChildDescriptors.add(createChildParameter(schemaContents, xsdFactory.createXSDImport()));
308
309 // include
310 XSDInclude i = xsdFactory.createXSDInclude();
311 i.setSchemaLocation("");
312 newChildDescriptors.add(createChildParameter(schemaContents, i));
313
314 // redefine
315 XSDRedefine r = xsdFactory.createXSDRedefine();
316 r.setSchemaLocation("");
317 newChildDescriptors.add(createChildParameter(schemaContents, r));
318
319 // element declaration
320 newChildDescriptors.add(createChildParameter(schemaContents, createElementDeclaration(xsdSchema)));
321
322 // attribute declaration
323 newChildDescriptors.add(createChildParameter(schemaContents, createAttributeDeclaration(xsdSchema)));
324
325 // model group definition
326 newChildDescriptors.add(createChildParameter(schemaContents, createModelGroupDefinition(xsdSchema)));
327
328 // attribute group definition
329 newChildDescriptors.add(createChildParameter(schemaContents, createAttributeGroupDefinition(xsdSchema)));
330
331 // atomic, list, and union simple type definitions
332 addSimpleTypeDefinitionChildParameters(newChildDescriptors, xsdSchema, schemaContents, true, true, true);
333
334 // complex type definition
335 newChildDescriptors.add(createChildParameter(schemaContents, createComplexTypeDefinition(xsdSchema)));
336
337 // notation declaration
338 newChildDescriptors.add(createChildParameter(schemaContents, createNotationDeclaration(xsdSchema)));
339 }
340 }
341
342 /**
343 * We need to copy the prefix to namespace map as well as the schema for schema prefix.
344 */
345 @Override
346 protected Command createInitializeCopyCommand(EditingDomain domain, EObject owner, CopyCommand.Helper helper)
347 {
348 return
349 new InitializeCopyCommand(domain, owner, helper)
350 {
351 @Override
352 protected Collection<? extends EAttribute> getAttributesToCopy()
353 {
354 Collection<EAttribute> result = new ArrayList<EAttribute>(this.owner.eClass().getEAllAttributes());
355 result.remove(xsdPackage.getXSDConcreteComponent_Element());
356 result.remove(xsdPackage.getXSDSchema_Document());
357 return result;
358 }
359
360 @Override
361 protected void copyAttributes()
362 {
363 XSDSchema ownerSchema = (XSDSchema)owner;
364 XSDSchema copySchema = (XSDSchema)copy;
365 copySchema.getQNamePrefixToNamespaceMap().putAll(ownerSchema.getQNamePrefixToNamespaceMap());
366 copySchema.setSchemaForSchemaQNamePrefix(ownerSchema.getSchemaForSchemaQNamePrefix());
367 super.copyAttributes();
368 }
369 };
370 }
371 }