Debian Codegraph @

plugins/org.eclipse.xsd.edit/src/org/eclipse/xsd/provider/XSDItemProviderAdapter.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.HashSet;
17 import java.util.List;
18
19 import org.eclipse.emf.common.notify.AdapterFactory;
20 import org.eclipse.emf.ecore.EAttribute;
21 import org.eclipse.emf.ecore.EObject;
22 import org.eclipse.emf.ecore.EReference;
23 import org.eclipse.emf.ecore.EStructuralFeature;
24 import org.eclipse.emf.edit.command.CommandParameter;
25 import org.eclipse.emf.edit.command.CreateChildCommand;
26 import org.eclipse.emf.edit.provider.AdapterFactoryItemDelegator;
27 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
28 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
29 import org.eclipse.emf.edit.provider.ItemProviderAdapter;
30
31 import org.eclipse.xsd.XSDAnnotation;
32 import org.eclipse.xsd.XSDAttributeDeclaration;
33 import org.eclipse.xsd.XSDAttributeGroupDefinition;
34 import org.eclipse.xsd.XSDAttributeUse;
35 import org.eclipse.xsd.XSDComplexTypeDefinition;
36 import org.eclipse.xsd.XSDCompositor;
37 import org.eclipse.xsd.XSDConcreteComponent;
38 import org.eclipse.xsd.XSDElementDeclaration;
39 import org.eclipse.xsd.XSDFactory;
40 import org.eclipse.xsd.XSDForm;
41 import org.eclipse.xsd.XSDIdentityConstraintDefinition;
42 import org.eclipse.xsd.XSDModelGroup;
43 import org.eclipse.xsd.XSDModelGroupDefinition;
44 import org.eclipse.xsd.XSDNotationDeclaration;
45 import org.eclipse.xsd.XSDPackage;
46 import org.eclipse.xsd.XSDParticle;
47 import org.eclipse.xsd.XSDParticleContent;
48 import org.eclipse.xsd.XSDRedefine;
49 import org.eclipse.xsd.XSDSchema;
50 import org.eclipse.xsd.XSDSimpleTypeDefinition;
51 import org.eclipse.xsd.XSDTypeDefinition;
52 import org.eclipse.xsd.XSDVariety;
53 import org.eclipse.xsd.XSDWildcard;
54 import org.eclipse.xsd.XSDXPathDefinition;
55
56
57 /**
58 * This is the base for all adapters.
59 * It takes handles the {@link #getParent getParent()} implementation that
60 * is need for {@link org.eclipse.xsd.provider.XSDParticleItemProvider}
61 * and {@link org.eclipse.xsd.provider.XSDAttributeUseItemProvider}
62 * to skip up to a particle or attribute use.
63 * It also supports {@link CreateChildCommand} by supplying text and icons,
64 * and provides methods that assist the derived adapters in overriding
65 * {@link ItemProviderAdapter#collectNewChildDescriptors}.
66 */
67 public class XSDItemProviderAdapter extends ItemProviderAdapter
68 implements CreateChildCommand.Helper
69 {
70 protected static final XSDPackage xsdPackage = XSDPackage.eINSTANCE;
71 protected static final XSDFactory xsdFactory = XSDFactory.eINSTANCE;
72
73 protected AdapterFactoryItemDelegator itemDelegator;
74
75 /**
76 * This creates an instance from an adapter factory.
77 */
78 protected XSDItemProviderAdapter(AdapterFactory adapterFactory)
79 {
80 super(adapterFactory);
81 itemDelegator = new AdapterFactoryItemDelegator(adapterFactory);
82 }
83
84 /**
85 * This returns the parent.
86 */
87 @Override
88 public Object getParent(Object object)
89 {
90 EObject eObject = (EObject)object;
91 EObject parent = eObject.eContainer();
92 if (parent != null)
93 {
94 EObject grandParent = parent.eContainer();
95 if (grandParent instanceof XSDParticle ||
96 grandParent instanceof XSDAttributeUse)
97 {
98 return grandParent;
99 }
100 }
101 if (parent == null)
102 {
103 return eObject.eResource();
104 }
105 return parent;
106 }
107
108 @Override
109 public List<IItemPropertyDescriptor> getPropertyDescriptors(final Object object)
110 {
111 if (itemPropertyDescriptors == null)
112 {
113 itemPropertyDescriptors =
114 new ArrayList<IItemPropertyDescriptor>()
115 {
116 private static final long serialVersionUID = 1L;
117
118 @Override
119 public boolean add(IItemPropertyDescriptor o)
120 {
121 String id = o.getId(object);
122 for (IItemPropertyDescriptor propertyDescriptor : this)
123 {
124 if (id.equals(propertyDescriptor.getId(object)))
125 {
126 return false;
127 }
128 }
129 return super.add(o);
130 }
131 };
132
133 }
134 return itemPropertyDescriptors;
135 }
136
137 public static class ItemPropertyDescriptorWithDefault extends ItemPropertyDescriptor
138 {
139 public ItemPropertyDescriptorWithDefault
140 (AdapterFactory adapterFactory,
141 String displayName,
142 String description,
143 EStructuralFeature feature,
144 boolean isSettable,
145 Object staticImage)
146 {
147 super(adapterFactory, displayName, description, feature, isSettable, staticImage);
148 }
149
150 @Override
151 public Object getPropertyValue(Object o)
152 {
153 if (feature instanceof EAttribute)
154 {
155 EObject refObject = (EObject)o;
156 EAttribute attribute = (EAttribute)feature;
157 if (!attribute.isMany() && !refObject.eIsSet(attribute))
158 {
159 return createPropertyValueWrapper(o, getPropertyDefaultValue(o));
160 }
161 }
162 return super.getPropertyValue(o);
163 }
164
165 @Override
166 public void setPropertyValue(Object o, Object value)
167 {
168 if (feature instanceof EAttribute)
169 {
170 EAttribute attribute = (EAttribute)feature;
171 if (!attribute.isMany())
172 {
173 Object propertyDefaultValue = getPropertyDefaultValue(o);
174 if (propertyDefaultValue.equals(value))
175 {
176 resetPropertyValue(o);
177 return;
178 }
179 }
180 }
181 super.setPropertyValue(o, value);
182 }
183
184 @Override
185 public Collection<?> getChoiceOfValues(Object o)
186 {
187 Collection<?> result = super.getChoiceOfValues(o);
188 if (result != null && feature instanceof EAttribute)
189 {
190 EAttribute attribute = (EAttribute)feature;
191 if (!attribute.isMany())
192 {
193 List<Object> newResult = new ArrayList<Object>(result);
194 Object propertyDefaultValue = getPropertyDefaultValue(o);
195 newResult.add(propertyDefaultValue);
196 result = newResult;
197 }
198 }
199 return result;
200 }
201
202 public Object getPropertyDefaultValue(Object o)
203 {
204 if (feature instanceof EAttribute)
205 {
206 EAttribute attribute = (EAttribute)feature;
207 if (!attribute.isMany())
208 {
209 Object result = attribute.getDefaultValue();
210 if (result == null)
211 {
212 result = getDefaultValue(attribute.getEType());
213 }
214 if (result == null)
215 {
216 return XSDEditPlugin.INSTANCE.getString("_UI_DefaultValue_label", new Object [] { "" });
217 }
218 else
219 {
220 return XSDEditPlugin.INSTANCE.getString("_UI_DefaultValue_label", new Object [] { this.itemDelegator.getText(result) });
221 }
222 }
223 }
224 return null;
225 }
226 }
227
228 /**
229 * If <code>object</code> is a particle or an attribute use, this returns
230 * its content; otherwise, <code>object</code> itself.
231 */
232 protected Object getParticleOrAttributeUseContent(Object object)
233 {
234 if (object instanceof XSDParticle)
235 {
236 return ((XSDParticle) object).getContent();
237 }
238 else if (object instanceof XSDAttributeUse)
239 {
240 return ((XSDAttributeUse) object).getContent();
241 }
242 return object;
243 }
244
245 /**
246 * This is a convenience method for creating <code>CommandParameter</code>s
247 * for a given parent feature and child object.
248 */
249 protected CommandParameter createChildParameter(EReference feature,
250 XSDConcreteComponent child)
251 {
252 return new CommandParameter(null, feature, child);
253 }
254
255 /**
256 * This is a convenience method for creating {@link CommandParameter}s
257 * for model groups of two or all of the three compositor types
258 * (choice, sequence, and optionally all, depending on the value of
259 * <code>all</code>) and adding them to a <code>newChildDescriptors</code>
260 * collection. If <code>useParticle</code> is <code>true</code>, each
261 * model group will be the content of a new particle.
262 */
263 protected void addModelGroupChildParameters(Collection<Object> newChildDescriptors,
264 EReference feature,
265 boolean all,
266 boolean useParticle)
267 {
268 XSDCompositor[] compositor = { XSDCompositor.ALL_LITERAL, XSDCompositor.CHOICE_LITERAL, XSDCompositor.SEQUENCE_LITERAL };
269 for (int i = all ? 0 : 1; i < compositor.length; i++)
270 {
271 XSDModelGroup mg = xsdFactory.createXSDModelGroup();
272 mg.setCompositor(compositor[i]);
273 XSDConcreteComponent child = mg;
274 if (useParticle)
275 {
276 child = createParticle(mg, false);
277 }
278 newChildDescriptors.add(createChildParameter(feature, child));
279 }
280 }
281
282 /**
283 * This is a convenience method for creating {@link CommandParameter}s
284 * for simple type definitions of zero, one, two, or all of three
285 * varieties (atomic, list, and union), and adding them to a
286 * <code>newChildDescriptors</code> collection. The simple type
287 * definition objects are to be added to the specified
288 * <code>feature</code> of <code>parent</code>. The varieties for which
289 * to create simple type definitions and command parameters are specified
290 * by the three boolean arguments, <code>atomic</code>, <code>list</code>,
291 * and <code>union</code>.
292 */
293 protected void addSimpleTypeDefinitionChildParameters
294 (Collection<Object> newChildDescriptors,
295 XSDConcreteComponent parent,
296 EReference feature,
297 boolean atomic,
298 boolean list,
299 boolean union)
300 {
301 XSDSchema xsdSchema = parent.getSchema();
302 if (xsdSchema != null)
303 {
304 XSDSimpleTypeDefinition baseType = parent.getSchema().getSchemaForSchema().resolveSimpleTypeDefinition("string");
305
306 if (atomic)
307 {
308 XSDSimpleTypeDefinition xsdSimpleTypeDefinition = createSimpleTypeDefinition(parent);
309 xsdSimpleTypeDefinition.setVariety(XSDVariety.ATOMIC_LITERAL);
310 xsdSimpleTypeDefinition.setBaseTypeDefinition(baseType);
311 newChildDescriptors.add(createChildParameter(feature, xsdSimpleTypeDefinition));
312 }
313
314 if (list)
315 {
316 XSDSimpleTypeDefinition xsdSimpleTypeDefinition = createSimpleTypeDefinition(parent);
317 xsdSimpleTypeDefinition.setVariety(XSDVariety.LIST_LITERAL);
318 xsdSimpleTypeDefinition.setItemTypeDefinition(baseType);
319 newChildDescriptors.add(createChildParameter(feature, xsdSimpleTypeDefinition));
320 }
321
322 if (union)
323 {
324 XSDSimpleTypeDefinition xsdSimpleTypeDefinition = createSimpleTypeDefinition(parent);
325 xsdSimpleTypeDefinition.setVariety(XSDVariety.UNION_LITERAL);
326 xsdSimpleTypeDefinition.getMemberTypeDefinitions().add(baseType);
327 newChildDescriptors.add(createChildParameter(feature, xsdSimpleTypeDefinition));
328 }
329 }
330 }
331
332 /**
333 * This determines whether an object represents a global element,
334 * based on the type of its parent.
335 */
336 protected boolean isGlobal(Object parent)
337 {
338 return (parent instanceof XSDSchema || parent instanceof XSDRedefine);
339 }
340
341 /**
342 * This creates an object of type <code>XSDAttributeDeclaration</code>
343 * with a name that is unique globally (compared to other global attribute
344 * declarations) or locally (compared to sibling attribute declarations
345 * and children of sibling attribute groups), depending on the type of the
346 * specified <code>parent</code>. The created object will be initialized
347 * to resolve to itself and to have the <code>string</code> type
348 * definition.
349 *
350 * <p>Note: in determining local uniqueness, we do not consider any
351 * referenced uses of the parent group or type elsewhere in the schema.
352 */
353 protected XSDAttributeDeclaration createAttributeDeclaration(XSDConcreteComponent parent)
354 {
355 XSDAttributeDeclaration child = null;
356 String baseName = getNewObjectName(xsdPackage.getXSDAttributeDeclaration().getName());
357
358 if (isGlobal(parent))
359 {
360 String name = null;
361 int i = 0;
362 do
363 {
364 name = baseName + ((i > 0) ? String.valueOf(i) : "");
365 i = (i > 0) ? i + 1 : 1;
366 child = parent.resolveAttributeDeclaration(name);
367 } while (((EObject) child).eContainer() != null);
368 }
369 else
370 {
371 XSDSchema schema = parent.getSchema();
372
373 // use the namespace from the schema in determining local uniqueness
374 // only if new local declarations will be qualified by default
375 String namespace = null;
376 if (schema != null &&
377 schema.getAttributeFormDefault() == XSDForm.QUALIFIED_LITERAL)
378 {
379 namespace = schema.getTargetNamespace();
380 }
381 Collection<? extends XSDAttributeUse> siblings = getAttributeSiblings(parent);
382 String name = null;
383 int i = 0;
384 do
385 {
386 name = baseName + ((i > 0) ? String.valueOf(i) : "");
387 i = (i > 0) ? i + 1 : 1;
388 } while (!isUniqueAttributeDeclarationName(name, namespace, siblings));
389
390 child = xsdFactory.createXSDAttributeDeclaration();
391 child.setName(name);
392 if (namespace != null)
393 {
394 child.setTargetNamespace(namespace);
395 }
396 child.setResolvedAttributeDeclaration(child);
397 }
398
399 // initialize element type to be string
400 child.setTypeDefinition(parent.getSchema().getSchemaForSchema().resolveSimpleTypeDefinition("string"));
401 return child;
402 }
403
404 /**
405 * This gathers and returns the siblings by stepping up through any
406 * attribute group definitions to the top-most parent attribute definition
407 * or complex type definition, and returning the attribute uses for it.
408 * Returns <code>null</code> if there is no such parent.
409 */
410 protected Collection<? extends XSDAttributeUse> getAttributeSiblings(XSDConcreteComponent parent)
411 {
412 Collection<XSDAttributeUse> siblings = null;
413 if (parent instanceof XSDAttributeGroupDefinition)
414 {
415 XSDAttributeGroupDefinition group = (XSDAttributeGroupDefinition)parent;
416 while (group.eContainer() instanceof XSDAttributeGroupDefinition)
417 {
418 group = (XSDAttributeGroupDefinition) group.eContainer();
419 }
420
421 if (group.eContainer() instanceof XSDComplexTypeDefinition)
422 {
423 parent = (XSDComplexTypeDefinition) group.eContainer();
424 }
425 else
426 {
427 siblings = group.getAttributeUses();
428 }
429 }
430 if (parent instanceof XSDComplexTypeDefinition)
431 {
432 siblings = ((XSDComplexTypeDefinition) parent).getAttributeUses();
433 }
434 return siblings;
435 }
436
437 /**
438 * This tests whether the combination of given <code>localName</code> and
439 * <code>targetNamespace</code> is shared by an attribute declaration of
440 * an attribute use in the given collection. Returns <code>false</code>
441 * if so, <code>true</code> otherwise. Note that this method is tolerant
442 * of nulls: a result of <code>true</code> is returned if
443 * <code>attributeUse</code> is <code>null</code>, and two null strings
444 * are considered equal.
445 */
446 protected boolean isUniqueAttributeDeclarationName(String localName, String targetNamespace, Collection<? extends XSDAttributeUse> attributeUses)
447 {
448 if (attributeUses != null)
449 {
450 for (XSDAttributeUse attributeUse : attributeUses)
451 {
452 if (attributeUse.getAttributeDeclaration() != null)
453 {
454 XSDAttributeDeclaration other =
455 attributeUse.getAttributeDeclaration();
456 if (other.hasNameAndTargetNamespace(localName, targetNamespace))
457 {
458 return false;
459 }
460 }
461 }
462 }
463 return true;
464 }
465
466 /**
467 * This creates an object of type <code>XSDElementDeclaration</code>
468 * with a name that is unique globally or locally, depending on the type
469 * of the specified <code>parent</code>. The created object will be
470 * initialized to resolve to itself and to have the <code>string</code>
471 * type definition.
472 *
473 * <p>Note: in determining local uniqueness, we do not consider any
474 * referenced uses of the parent group or type elsewhere in the schema.
475 */
476 protected XSDElementDeclaration createElementDeclaration(XSDConcreteComponent parent)
477 {
478 XSDElementDeclaration child = null;
479 String baseName = getNewObjectName(xsdPackage.getXSDElementDeclaration().getName());
480
481 if (isGlobal(parent))
482 {
483 String name = null;
484 int i = 0;
485 do
486 {
487 name = baseName + ((i > 0) ? String.valueOf(i) : "");
488 i = (i > 0) ? i + 1 : 1;
489 child = parent.resolveElementDeclaration(name);
490 } while (((EObject) child).eContainer() != null);
491 }
492 else
493 {
494 XSDSchema schema = parent.getSchema();
495
496 // use the namespace from the schema in determining local uniqueness
497 // only if new local declarations will be qualified by default
498 String namespace = null;
499 if (schema != null &&
500 schema.getElementFormDefault() == XSDForm.QUALIFIED_LITERAL)
501 {
502 namespace = schema.getTargetNamespace();
503 }
504 XSDModelGroup modelGroup = getTopModelGroup(parent);
505 String name = null;
506 int i = 0;
507 do
508 {
509 name = baseName + ((i > 0) ? String.valueOf(i) : "");
510 i = (i > 0) ? i + 1 : 1;
511 } while (!isUniqueElementDeclarationName(name, namespace, modelGroup, null));
512 child = xsdFactory.createXSDElementDeclaration();
513 child.setName(name);
514 if (namespace != null)
515 {
516 child.setTargetNamespace(namespace);
517 }
518 child.setResolvedElementDeclaration(child);
519 }
520
521 // initialize attribute type to be string
522 child.setTypeDefinition(parent.getSchema().getSchemaForSchema().resolveSimpleTypeDefinition("string"));
523 return child;
524 }
525
526 /**
527 * If the specified parent is a model group, this finds and returns the
528 * top-most model group above it (within the same complex type or model
529 * group definition). Otherwise, returns <code>null</code>.
530 */
531 protected XSDModelGroup getTopModelGroup(XSDConcreteComponent parent)
532 {
533 XSDModelGroup modelGroup = null;
534
535 if (parent instanceof XSDModelGroup)
536 {
537 modelGroup = (XSDModelGroup) parent;
538 while (parent.eContainer() instanceof XSDParticle ||
539 parent.eContainer() instanceof XSDModelGroup)
540 {
541 parent = (XSDConcreteComponent) parent.eContainer();
542 if (parent instanceof XSDModelGroup)
543 {
544 modelGroup = (XSDModelGroup) parent;
545 }
546 }
547 }
548 return modelGroup;
549 }
550
551 /**
552 * This tests whether the combination of given <code>localName</code>
553 * and <code>targetNamespace</code> is shared by an element declaration in
554 * the given model group or a model group under it. Returns
555 * <code>false</code> if so, <code>true</code> otherwise. Note that this
556 * method is tolerant of nulls: a result of <code>true</code> is
557 * returned if <code>modelGroup</code> is <code>null</code>, and two
558 * null strings are considered equal. Also, note that the
559 * <code>visited</code> argument is used to avoid an infinite recursion
560 * situation, and should be <code>null</code> when the method is initially
561 * called.
562 */
563 protected boolean isUniqueElementDeclarationName(String localName, String targetNamespace, XSDModelGroup modelGroup, HashSet<XSDModelGroup> visited)
564 {
565 if (visited == null)
566 {
567 visited = new HashSet<XSDModelGroup>();
568 }
569
570 if (modelGroup != null && visited.add(modelGroup) && modelGroup.getParticles() != null)
571 {
572 for (XSDParticle particle : modelGroup.getParticles())
573 {
574 if (particle.getTerm() instanceof XSDElementDeclaration)
575 {
576 XSDElementDeclaration other =
577 (XSDElementDeclaration) particle.getTerm();
578 if (other.hasNameAndTargetNamespace(localName, targetNamespace))
579 {
580 return false;
581 }
582 }
583 else if (particle.getTerm() instanceof XSDModelGroup)
584 {
585 XSDModelGroup others = (XSDModelGroup) particle.getTerm();
586 if (!isUniqueElementDeclarationName(localName, targetNamespace, others, visited))
587 {
588 return false;
589 }
590 }
591 }
592 }
593 return true;
594 }
595
596 /**
597 * This creates an object of type <code>XSDAttributeGroupDefinition</code>
598 * with a name that is unique globally, and that resolves to
599 * itself. The created object is to be added under the specified
600 * <code>parent</code>.
601 */
602 protected XSDAttributeGroupDefinition createAttributeGroupDefinition(XSDConcreteComponent parent)
603 {
604 XSDAttributeGroupDefinition child = null;
605 String name = null;
606 String baseName = getNewObjectName(xsdPackage.getXSDAttributeGroupDefinition().getName());
607 int i = 0;
608 do
609 {
610 name = baseName + ((i > 0) ? String.valueOf(i) : "");
611 i = (i > 0) ? i + 1 : 1;
612 child = parent.resolveAttributeGroupDefinition(name);
613 } while (((EObject) child).eContainer() != null);
614
615 return child;
616 }
617
618 /**
619 * This creates an object of type <code>XSDModelGroupDefinition</code>
620 * with a name that is unique globally, and that resolves to
621 * itself. The created object is to be added under the specified
622 * <code>parent</code>.
623 */
624 protected XSDModelGroupDefinition createModelGroupDefinition(XSDConcreteComponent parent)
625 {
626 XSDModelGroupDefinition child = null;
627 String name = null;
628 String baseName = getNewObjectName(xsdPackage.getXSDModelGroupDefinition().getName());
629 int i = 0;
630 do
631 {
632 name = baseName + ((i > 0) ? String.valueOf(i) : "");
633 i = (i > 0) ? i + 1 : 1;
634 child = parent.resolveModelGroupDefinition(name);
635 } while (((EObject) child).eContainer() != null);
636
637 return child;
638 }
639
640 /**
641 * This creates an object of type <code>XSDComplexTypeDefinition</code>
642 * with a name that is unique globally, if this is to be a global
643 * definition; otherwise, with no name. The created object is to be
644 * added under the specified <code>parent</code>.
645 */
646 protected XSDComplexTypeDefinition createComplexTypeDefinition(XSDConcreteComponent parent)
647 {
648 XSDComplexTypeDefinition child = null;
649
650 if (isGlobal(parent))
651 {
652 String name = null;
653 String baseName = getNewObjectName(xsdPackage.getXSDComplexTypeDefinition().getName());
654 int i = 0;
655 do
656 {
657 name = baseName + ((i > 0) ? String.valueOf(i) : "");
658 i = (i > 0) ? i + 1 : 1;
659 child = parent.resolveComplexTypeDefinition(name);
660 } while (((EObject) child).eContainer() != null);
661 }
662 else
663 {
664 child = xsdFactory.createXSDComplexTypeDefinition();
665 }
666 return child;
667 }
668
669 /**
670 * This creates an object of type <code>XSDSimpleTypeDefinition</code>
671 * with a name that is unique globally, if this is to be a global
672 * definition; otherwise, with no name. The created object is to be
673 * added under the specified <code>parent</code>.
674 */
675 protected XSDSimpleTypeDefinition createSimpleTypeDefinition(XSDConcreteComponent parent)
676 {
677 XSDSimpleTypeDefinition child = null;
678
679 if (isGlobal(parent))
680 {
681 String name = null;
682 String baseName = getNewObjectName(xsdPackage.getXSDSimpleTypeDefinition().getName());
683 int i = 0;
684 do
685 {
686 name = baseName + ((i > 0) ? String.valueOf(i) : "");
687 i = (i > 0) ? i + 1 : 1;
688 child = parent.resolveSimpleTypeDefinition(name);
689 } while (((EObject) child).eContainer() != null);
690 }
691 else
692 {
693 child = xsdFactory.createXSDSimpleTypeDefinition();
694 }
695
696 return child;
697 }
698
699 /**
700 * This creates an object of type
701 * <code>XSDIdentityConstraintDefinition</code> with a name that is unique
702 * globally. The created object is to be added under the specified
703 * <code>parent</code>.
704 */
705 protected XSDIdentityConstraintDefinition createIdentityConstraintDefinition(XSDConcreteComponent parent)
706 {
707 XSDIdentityConstraintDefinition child = null;
708 String name = null;
709 String baseName = getNewObjectName(xsdPackage.getXSDIdentityConstraintDefinition().getName());
710 int i = 0;
711 do
712 {
713 name = baseName + ((i > 0) ? String.valueOf(i) : "");
714 i = (i > 0) ? i + 1 : 1;
715 child = parent.resolveIdentityConstraintDefinition(name);
716 } while (((EObject) child).eContainer() != null);
717
718 return child;
719 }
720
721 /**
722 * This creates an object of type <code>XSDNotationDeclaration</code> with
723 * a name that is unique globally. The created object, to be added
724 * under the specified <code>parent</code>, will have an empty string as
725 * its public identifier.
726 */
727 protected XSDNotationDeclaration createNotationDeclaration(XSDConcreteComponent parent)
728 {
729 XSDNotationDeclaration child = null;
730 String name = null;
731 String baseName = getNewObjectName(xsdPackage.getXSDNotationDeclaration().getName());
732 int i = 0;
733 do
734 {
735 name = baseName + ((i > 0) ? String.valueOf(i) : "");
736 i = (i > 0) ? i + 1 : 1;
737 child = parent.resolveNotationDeclaration(name);
738 } while (((EObject) child).eContainer() != null);
739
740 child.setPublicIdentifier("");
741 return child;
742 }
743
744 /**
745 * This creates an object of type <code>XSDAttributeUse</code> containing
746 * an object of type <code>XSDAttributeDeclaration</code> -- if
747 * <code>isReference</code> is <code>true</code>, the attribute use
748 * content will be a new attribute declaration that resolves to
749 * <code>attributeDeclaration</code>; otherwise, it will be simply
750 * <code>attributeDeclaration</code> itself.
751 */
752 protected XSDAttributeUse createAttributeUse(XSDAttributeDeclaration attributeDeclaration, boolean isReference)
753 {
754 XSDAttributeUse au = xsdFactory.createXSDAttributeUse();
755 if (isReference)
756 {
757 XSDAttributeDeclaration ref = xsdFactory.createXSDAttributeDeclaration();
758 ref.setResolvedAttributeDeclaration(attributeDeclaration);
759 au.setContent(ref);
760 }
761 else
762 {
763 au.setContent(attributeDeclaration);
764 }
765 return au;
766 }
767
768 /**
769 * This creates an object of type <code>XSDAttributeGroupDefinition</code>
770 * that resolves to <code>attributeGroupDefinition</code>.
771 */
772 protected XSDAttributeGroupDefinition createAttributeGroupDefinitionReference(XSDAttributeGroupDefinition attributeGroupDefinition)
773 {
774 XSDAttributeGroupDefinition ref =
775 xsdFactory.createXSDAttributeGroupDefinition();
776 ref.setResolvedAttributeGroupDefinition(attributeGroupDefinition);
777 return ref;
778 }
779
780 /**
781 * This creates an object of type <code>XSDParticle</code> containing
782 * an object of type <code>XSDParticleContent</code> -- if either
783 * <code>particleContent</code> is an element declaration and
784 * <code>isReference</code> is <code>true</code> or
785 * <code>particleContent</code> is a model group definition, the particle
786 * content will be a new element declaration that resolves to
787 * <code>particleContent</code>; otherwise, it will be simply
788 * <code>particleContent</code> itself.
789 */
790 protected XSDParticle createParticle(XSDParticleContent particleContent, boolean isReference)
791 {
792 XSDParticle p = xsdFactory.createXSDParticle();
793 if (particleContent instanceof XSDModelGroupDefinition)
794 {
795 XSDModelGroupDefinition ref = xsdFactory.createXSDModelGroupDefinition();
796 ref.setResolvedModelGroupDefinition((XSDModelGroupDefinition) particleContent);
797 p.setContent(ref);
798 }
799 else if (particleContent instanceof XSDElementDeclaration && isReference)
800 {
801 XSDElementDeclaration ref = xsdFactory.createXSDElementDeclaration();
802 ref.setResolvedElementDeclaration((XSDElementDeclaration) particleContent);
803 p.setContent(ref);
804 }
805 else
806 {
807 p.setContent(particleContent);
808
809 }
810 return p;
811 }
812
813 /**
814 * This returns the translated name for a new object with the specified
815 * type name.
816 */
817 protected String getNewObjectName(String typeName)
818 {
819 return XSDEditPlugin.INSTANCE.getString("_UI_" + typeName + "_new_object");
820 }
821
822 /**
823 * This returns the default result collection for {@link CreateChildCommand}.
824 */
825 @Override
826 public Collection<?> getCreateChildResult(Object child)
827 {
828 Collection<Object> result = new ArrayList<Object>(1);
829 result.add(child);
830 return result;
831 }
832
833 /**
834 * This returns the translated label for {@link CreateChildCommand}.
835 */
836 @Override
837 public String getCreateChildText(Object parent, Object feature,
838 Object child, Collection<?> selection)
839 {
840 child = getParticleOrAttributeUseContent(child);
841
842 EReference refFeature = feature instanceof EReference ?
843 (EReference) feature : null;
844 String qualifier = getTypeTextQualifier(parent, refFeature, child);
845
846 return getTypeText(child, qualifier);
847 }
848
849 /**
850 * This returns the translated description for {@link CreateChildCommand}.
851 */
852 @Override
853 public String getCreateChildDescription(Object parent, Object feature,
854 Object child, Collection<?> selection)
855 {
856 child = getParticleOrAttributeUseContent(child);
857
858 EReference refFeature = feature instanceof EReference ?
859 (EReference) feature : null;
860 String qualifier = getTypeTextQualifier(parent, refFeature, child);
861
862 Object selectedObject = selection == null || selection.isEmpty() ?
863 null : getParticleOrAttributeUseContent(selection.iterator().next());
864 if (parent != selectedObject)
865 {
866 return
867 XSDEditPlugin.INSTANCE.getString
868 ("_UI_CreateSibling_description", new Object [] { getTypeText(child, qualifier), getTypeText(selectedObject, null) });
869 }
870 else
871 {
872 return
873 XSDEditPlugin.INSTANCE.getString
874 ("_UI_CreateChild_description", new Object [] { getTypeText(child, qualifier), getTypeText(parent, null) });
875 }
876 }
877
878 /**
879 * This returns the translated tool tip for {@link CreateChildCommand}.
880 */
881 @Override
882 public String getCreateChildToolTipText(Object parent, Object feature,
883 Object child, Collection<?> selection)
884 {
885 child = getParticleOrAttributeUseContent(child);
886
887 EReference refFeature = feature instanceof EReference ?
888 (EReference) feature : null;
889 String qualifier = getTypeTextQualifier(parent, refFeature, child);
890
891 Object selectedObject = selection == null || selection.isEmpty() ?
892 null : getParticleOrAttributeUseContent(selection.iterator().next());
893 if (parent != selectedObject)
894 {
895 return
896 XSDEditPlugin.INSTANCE.getString
897 ("_UI_CreateSibling_tooltip", new Object [] { getTypeText(child, qualifier), getTypeText(selectedObject, null) });
898 }
899 else
900 {
901 return
902 XSDEditPlugin.INSTANCE.getString
903 ("_UI_CreateChild_tooltip", new Object [] { getTypeText(child, qualifier), getTypeText(parent, null) });
904 }
905 }
906
907 /**
908 * This gets the translated string for <code>object</code>'s type,
909 * qualified by <code>qualifier</code>, if it is non-null.
910 */
911 protected String getTypeText(Object object, String qualifier)
912 {
913 StringBuffer typeKey = new StringBuffer("_UI_");
914 typeKey.append(object instanceof EObject ?
915 ((EObject) object).eClass().getName() : "Unknown");
916 typeKey.append("_type");
917
918 if (qualifier != null)
919 {
920 typeKey.append('_');
921 typeKey.append(qualifier);
922 }
923 return XSDEditPlugin.INSTANCE.getString(typeKey.toString());
924 }
925
926 /**
927 * This returns the icon for {@link CreateChildCommand}.
928 */
929 @Override
930 public Object getCreateChildImage(Object parent, Object feature,
931 Object child, Collection<?> selection)
932 {
933 child = getParticleOrAttributeUseContent(child);
934 Object image = null;
935
936 if (child instanceof EObject)
937 {
938 StringBuffer imageName = new StringBuffer("full/obj16/");
939 String typeName = ((EObject) child).eClass().getName();
940
941 EReference refFeature = feature instanceof EReference ?
942 (EReference) feature : null;
943 String qualifier = getImageNameQualifier(parent, refFeature, child);
944
945 // kludge: drop Declaration or Definition when qualifier is Use
946 int i = "Use".equals(qualifier) ? typeName.lastIndexOf('D') : 0;
947 if (i > 0)
948 {
949 typeName = typeName.substring(0, i);
950 }
951 imageName.append(typeName);
952
953 if (qualifier != null)
954 {
955 imageName.append(qualifier);
956 }
957 image = XSDEditPlugin.INSTANCE.getImage(imageName.toString());
958 }
959 return image;
960 }
961
962 /**
963 * This generates and returns a qualifier string, for use in forming the
964 * type key, based on the state of the child object, its parent, and the
965 * feature under which it is to be added.
966 */
967 protected String getTypeTextQualifier(Object parent, EReference feature,
968 Object child)
969 {
970 return getQualifier(parent, feature, child);
971 }
972
973 /**
974 * This generates and returns a qualifier string, for use in forming the
975 * icon filename, based on the state of the child object, its parent, and
976 * the feature under which is to be added.
977 */
978 protected String getImageNameQualifier(Object parent, EReference feature,
979 Object child)
980 {
981 // kludge: there is only one annotation icon
982 Object p = child instanceof XSDAnnotation ? null : parent;
983 String qualifier = getQualifier(p, feature, child);
984
985 // a little translation
986 if ("keyref".equals(qualifier))
987 {
988 qualifier = "KeyReference";
989 }
990 else if ("reference".equals(qualifier))
991 {
992 qualifier = "Use";
993 }
994 else if (qualifier != null && qualifier.length() > 0)
995 {
996 // capitalize
997 StringBuffer buffer = new StringBuffer(qualifier);
998 char c = buffer.charAt(0);
999 buffer.setCharAt(0, Character.toUpperCase(c));
1000 qualifier = buffer.toString();
1001 }
1002 return qualifier;
1003 }
1004
1005 /**
1006 * This does common stuff for getTypeTextQualifier() and
1007 * getImageNameQualifier().
1008 */
1009 private String getQualifier(Object parent, EReference feature, Object child)
1010 {
1011 String qualifier = null;
1012
1013 // qualification for feature declaration or group definition reference
1014 if ((child instanceof XSDElementDeclaration &&
1015 ((XSDElementDeclaration) child).isElementDeclarationReference()) ||
1016 (child instanceof XSDAttributeDeclaration &&
1017 ((XSDAttributeDeclaration) child).isAttributeDeclarationReference()) ||
1018 (child instanceof XSDModelGroupDefinition &&
1019 ((XSDModelGroupDefinition) child).isModelGroupDefinitionReference()) ||
1020 (child instanceof XSDAttributeGroupDefinition &&
1021 ((XSDAttributeGroupDefinition)
1022 child).isAttributeGroupDefinitionReference()))
1023 {
1024 qualifier = "reference";
1025 }
1026
1027 // qualification by compositor for model group
1028 else if (child instanceof XSDModelGroup)
1029 {
1030 qualifier = ((XSDModelGroup) child).getCompositor().getName();
1031 }
1032
1033 // qualification by variety for simple type definition
1034 else if (child instanceof XSDSimpleTypeDefinition)
1035 {
1036 qualifier = ((XSDSimpleTypeDefinition) child).getVariety().getName();
1037 }
1038
1039 // qualification by category for identity constraint definition
1040 else if (child instanceof XSDIdentityConstraintDefinition)
1041 {
1042 qualifier = ((XSDIdentityConstraintDefinition) child).getIdentityConstraintCategory().getName();
1043 }
1044
1045 // qualification by variety for xpath definition
1046 else if (child instanceof XSDXPathDefinition)
1047 {
1048 qualifier = ((XSDXPathDefinition) child).getVariety().getName();
1049 }
1050
1051 // qualification according to parent for wildcard
1052 else if (child instanceof XSDWildcard)
1053 {
1054 if (parent instanceof XSDAttributeGroupDefinition ||
1055 parent instanceof XSDComplexTypeDefinition)
1056 {
1057 qualifier = "attribute";
1058 }
1059 else if (parent instanceof XSDModelGroup)
1060 {
1061 qualifier = "element";
1062 }
1063 }
1064
1065 // qualification by feature name for annotation under type definition
1066 else if (child instanceof XSDAnnotation &&
1067 parent instanceof XSDTypeDefinition &&
1068 feature != null)
1069 {
1070 qualifier = feature.getName();
1071 }
1072 return qualifier;
1073 }
1074 }