Debian Codegraph @

plugins/org.eclipse.xsd.edit/src/org/eclipse/xsd/provider/XSDAttributeDeclarationItemProvider.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.Collection;
15 import java.util.List;
16
17 import org.eclipse.emf.common.notify.AdapterFactory;
18 import org.eclipse.emf.common.notify.Notification;
19 import org.eclipse.emf.ecore.EStructuralFeature;
20 import org.eclipse.emf.ecore.InternalEObject;
21 import org.eclipse.emf.ecore.impl.ENotificationImpl;
22 import org.eclipse.emf.edit.command.SetCommand;
23 import org.eclipse.emf.edit.domain.EditingDomain;
24 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
25 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
26 import org.eclipse.emf.edit.provider.IItemLabelProvider;
27 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
28 import org.eclipse.emf.edit.provider.IItemPropertySource;
29 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
30 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
31 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
32 import org.eclipse.emf.edit.provider.ItemProviderAdapter;
33
34 import org.eclipse.xsd.XSDAttributeDeclaration;
35 import org.eclipse.xsd.XSDAttributeUse;
36 import org.eclipse.xsd.XSDConcreteComponent;
37
38
39 /**
40 * This is the item provider adpater for a {@link org.eclipse.xsd.XSDAttributeDeclaration} object.
41 */
42 public class XSDAttributeDeclarationItemProvider
43 extends XSDFeatureItemProvider
44 implements
45 IEditingDomainItemProvider,
46 IStructuredItemContentProvider,
47 ITreeItemContentProvider,
48 IItemLabelProvider,
49 IItemPropertySource
50 {
51 /**
52 * This constructs an instance from a factory and a notifier.
53 */
54 public XSDAttributeDeclarationItemProvider(AdapterFactory adapterFactory)
55 {
56 super(adapterFactory);
57 }
58
59 /**
60 * This returns the property descriptors for the adapted class.
61 */
62 @Override
63 public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object)
64 {
65 if (itemPropertyDescriptors == null)
66 {
67 super.getPropertyDescriptors(object);
68 createTypeDefinitionPropertyDescriptor(object);
69 createAnnotationPropertyDescriptor(object);
70 createResolvedAttributeDeclarationPropertyDescriptor(object);
71 createAttributeFormDefaultPropertyDescriptor(object);
72 }
73 return itemPropertyDescriptors;
74 }
75
76 protected static class DelegatingItemPropertyDescriptor extends ItemPropertyDescriptor
77 {
78 public DelegatingItemPropertyDescriptor
79 (AdapterFactory adapterFactory,
80 String displayName,
81 String description,
82 EStructuralFeature feature,
83 boolean isSettable,
84 Object staticImage)
85 {
86 super(adapterFactory, displayName, description, feature, isSettable, staticImage);
87 }
88
89 @Override
90 public Object getPropertyValue(Object object)
91 {
92 return super.getPropertyValue(((XSDAttributeDeclaration)object).getResolvedAttributeDeclaration());
93 }
94
95 @Override
96 public void setPropertyValue(Object object, Object value)
97 {
98 super.setPropertyValue(((XSDAttributeDeclaration)object).getResolvedAttributeDeclaration(), value);
99 }
100 }
101
102 @Override
103 protected void createNamePropertyDescriptor(Object object)
104 {
105
106 // This is for the name feature.
107 //
108 itemPropertyDescriptors.add
109 (new DelegatingItemPropertyDescriptor
110 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
111 XSDEditPlugin.INSTANCE.getString("_UI_Name_label"),
112 XSDEditPlugin.INSTANCE.getString("_UI_Name_description"),
113 xsdPackage.getXSDNamedComponent_Name(),
114 true,
115 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
116 {
117 @Override
118 public void setPropertyValue(Object o, Object value)
119 {
120 XSDAttributeDeclaration xsdAttributeDeclaration = (XSDAttributeDeclaration)o;
121 if (xsdAttributeDeclaration.isAttributeDeclarationReference())
122 {
123 XSDAttributeDeclaration newResolvedAttributeDeclaration =
124 xsdAttributeDeclaration.resolveAttributeDeclaration(xsdAttributeDeclaration.getTargetNamespace(), (String)value);
125 EditingDomain editingDomain = getEditingDomain(xsdAttributeDeclaration);
126 if (editingDomain == null)
127 {
128 xsdAttributeDeclaration.setResolvedAttributeDeclaration(newResolvedAttributeDeclaration);
129 }
130 else
131 {
132 editingDomain.getCommandStack().execute
133 (SetCommand.create
134 (editingDomain,
135 xsdAttributeDeclaration,
136 xsdPackage.getXSDAttributeDeclaration_ResolvedAttributeDeclaration(),
137 newResolvedAttributeDeclaration));
138 }
139 }
140 else
141 {
142 super.setPropertyValue(o, value);
143 }
144 }
145 });
146 }
147
148 @Override
149 protected void createTargetNamespacePropertyDescriptor(Object object)
150 {
151
152 // This is for the targetNamespace feature.
153 //
154 itemPropertyDescriptors.add
155 (new DelegatingItemPropertyDescriptor
156 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
157 XSDEditPlugin.INSTANCE.getString("_UI_TargetNamespace_label"),
158 XSDEditPlugin.INSTANCE.getString("_UI_TargetNamespace_description"),
159 xsdPackage.getXSDNamedComponent_TargetNamespace(),
160 true,
161 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
162 {
163 @Override
164 public void setPropertyValue(Object o, Object value)
165 {
166 XSDAttributeDeclaration xsdAttributeDeclaration = (XSDAttributeDeclaration)o;
167 if (xsdAttributeDeclaration.isAttributeDeclarationReference())
168 {
169 String namespace = (String)value;
170 if (namespace.length() == 0)
171 {
172 namespace = null;
173 }
174 XSDAttributeDeclaration newResolvedAttributeDeclaration =
175 xsdAttributeDeclaration.resolveAttributeDeclaration(namespace, xsdAttributeDeclaration.getName());
176 EditingDomain editingDomain = getEditingDomain(xsdAttributeDeclaration);
177 if (editingDomain == null)
178 {
179 xsdAttributeDeclaration.setResolvedAttributeDeclaration(newResolvedAttributeDeclaration);
180 }
181 else
182 {
183 editingDomain.getCommandStack().execute
184 (SetCommand.create
185 (editingDomain,
186 xsdAttributeDeclaration,
187 xsdPackage.getXSDAttributeDeclaration_ResolvedAttributeDeclaration(),
188 newResolvedAttributeDeclaration));
189 }
190 }
191 else
192 {
193 super.setPropertyValue(o, value);
194 }
195 }
196 });
197 }
198
199 protected void createAttributeFormDefaultPropertyDescriptor(Object object)
200 {
201 // This is for the elementFormDefault feature.
202 //
203 itemPropertyDescriptors.add
204 (new ItemPropertyDescriptorWithDefault
205 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
206 XSDEditPlugin.INSTANCE.getString("_UI_Form_label"),
207 XSDEditPlugin.INSTANCE.getString("_UI_FormOfAttribute_description"),
208 xsdPackage.getXSDFeature_Form(),
209 true,
210 ItemPropertyDescriptor.TEXT_VALUE_IMAGE)
211 {
212 @Override
213 public Object getPropertyDefaultValue(Object o)
214 {
215 return
216 XSDEditPlugin.INSTANCE.getString
217 ("_UI_DefaultValue_label", new Object [] { ((XSDAttributeDeclaration)o).getSchema().getAttributeFormDefault().getName() });
218 }
219 });
220 }
221
222 @Override
223 protected void createLexicalValuePropertyDescriptor(Object object)
224 {
225 // This is for the value feature.
226 //
227 itemPropertyDescriptors.add
228 (new DelegatingItemPropertyDescriptor
229 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
230 XSDEditPlugin.INSTANCE.getString("_UI_LexicalValue_label"),
231 XSDEditPlugin.INSTANCE.getString("_UI_LexicalValueOfAttribute_description"),
232 xsdPackage.getXSDFeature_LexicalValue(),
233 true,
234 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
235 }
236
237 @Override
238 protected void createConstraintPropertyDescriptor(Object object)
239 {
240 // This is for the constraint feature.
241 //
242 itemPropertyDescriptors.add
243 (new DelegatingItemPropertyDescriptor
244 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
245 XSDEditPlugin.INSTANCE.getString("_UI_Constraint_label"),
246 XSDEditPlugin.INSTANCE.getString("_UI_ConstraintOfAttribute_description"),
247 xsdPackage.getXSDFeature_Constraint(),
248 true,
249 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
250 }
251
252 @Override
253 protected void createScopePropertyDescriptor(Object object)
254 {
255 // This is for the scope feature.
256 //
257 itemPropertyDescriptors.add
258 (new DelegatingItemPropertyDescriptor
259 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
260 XSDEditPlugin.INSTANCE.getString("_UI_Scope_label"),
261 XSDEditPlugin.INSTANCE.getString("_UI_ScopeOfAttribute_description"),
262 xsdPackage.getXSDFeature_Scope(),
263 false,
264 null));
265 }
266
267 protected void createTypeDefinitionPropertyDescriptor(Object object)
268 {
269 // This is for the typeDefinition feature.
270 //
271 itemPropertyDescriptors.add
272 (new DelegatingItemPropertyDescriptor
273 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
274 XSDEditPlugin.INSTANCE.getString("_UI_TypeDefinition_label"),
275 XSDEditPlugin.INSTANCE.getString("_UI_TypeDefinition_description"),
276 xsdPackage.getXSDAttributeDeclaration_TypeDefinition(),
277 false,
278 null));
279 }
280
281 protected void createAnnotationPropertyDescriptor(Object object)
282 {
283 // This is for the annotation feature.
284 //
285 itemPropertyDescriptors.add
286 (new ItemPropertyDescriptor
287 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
288 XSDEditPlugin.INSTANCE.getString("_UI_Annotation_label"),
289 XSDEditPlugin.INSTANCE.getString("_UI_AnnotationOfAttribute_description"),
290 xsdPackage.getXSDAttributeDeclaration_Annotation(),
291 false));
292 }
293
294 protected void createResolvedAttributeDeclarationPropertyDescriptor(Object object)
295 {
296 // This is for the resolvedAttributeDeclaration feature.
297 //
298 itemPropertyDescriptors.add
299 (new ItemPropertyDescriptor
300 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
301 XSDEditPlugin.INSTANCE.getString("_UI_ResolveAttributeDeclaration_label"),
302 XSDEditPlugin.INSTANCE.getString("_UI_ResolveAttributeDeclaration_description"),
303 xsdPackage.getXSDAttributeDeclaration_ResolvedAttributeDeclaration(),
304 false));
305 }
306
307 /**
308 * This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an
309 * {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or
310 * {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}.
311 */
312 @Override
313 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object)
314 {
315 if (childrenFeatures == null)
316 {
317 super.getChildrenFeatures(object);
318 childrenFeatures.add(xsdPackage.getXSDAttributeDeclaration_AnonymousTypeDefinition());
319 childrenFeatures.add(xsdPackage.getXSDAttributeDeclaration_Annotation());
320 }
321 return childrenFeatures;
322 }
323
324 /**
325 * This returns XSDAttributeDeclaration.gif.
326 */
327 @Override
328 public Object getImage(Object object)
329 {
330 XSDAttributeDeclaration xsdAttributeDeclaration = ((XSDAttributeDeclaration)object);
331 XSDAttributeDeclaration resolvedAttributeDeclaration = xsdAttributeDeclaration.getResolvedAttributeDeclaration();
332 return
333 XSDEditPlugin.INSTANCE.getImage
334 (resolvedAttributeDeclaration.getContainer() == null ?
335 "full/obj16/XSDAttributeUnresolved" :
336 xsdAttributeDeclaration.getResolvedAttributeDeclaration() == xsdAttributeDeclaration ?
337 "full/obj16/XSDAttributeDeclaration" :
338 "full/obj16/XSDAttributeUse");
339 }
340
341 @Override
342 public String getText(Object object)
343 {
344 XSDAttributeDeclaration xsdAttributeDeclaration = ((XSDAttributeDeclaration)object);
345 XSDAttributeDeclaration resolvedAttributeDeclaration = xsdAttributeDeclaration.getResolvedAttributeDeclaration();
346 String name =
347 xsdAttributeDeclaration != resolvedAttributeDeclaration ?
348 xsdAttributeDeclaration.getQName() :
349 xsdAttributeDeclaration.getName();
350
351 StringBuffer result = new StringBuffer();
352 if (name == null)
353 {
354 result.append(XSDEditPlugin.INSTANCE.getString("_UI_Absent_label"));
355 }
356 else
357 {
358 result.append(name);
359 }
360
361 if (resolvedAttributeDeclaration.getAnonymousTypeDefinition() == null && resolvedAttributeDeclaration.getTypeDefinition() != null)
362 {
363 result.append(" : ");
364 result.append(resolvedAttributeDeclaration.getTypeDefinition().getQName(xsdAttributeDeclaration));
365 }
366
367 return result.toString();
368 }
369
370 /**
371 * This handles notification by calling {@link #fireNotifyChanged fireNotifyChanged}.
372 */
373 @Override
374 public void notifyChanged(Notification msg)
375 {
376 XSDAttributeDeclaration xsdAttributeDeclaration = (XSDAttributeDeclaration)msg.getNotifier();
377 if (msg.getFeature() == xsdPackage.getXSDAttributeDeclaration_TypeDefinition() ||
378 msg.getFeature() == xsdPackage.getXSDAttributeDeclaration_AnonymousTypeDefinition() ||
379 msg.getFeature() == xsdPackage.getXSDAttributeDeclaration_ResolvedAttributeDeclaration() ||
380 msg.getFeature() == xsdPackage.getXSDAttributeDeclaration_Annotation())
381 {
382 fireNotifyChanged(msg);
383
384 XSDConcreteComponent container = xsdAttributeDeclaration.getContainer();
385 if (container instanceof XSDAttributeUse)
386 {
387 ((ItemProviderAdapter)adapterFactory.adapt(container, IItemLabelProvider.class)).fireNotifyChanged
388 (new ENotificationImpl
389 ((InternalEObject)container,
390 msg.getEventType(),
391 (EStructuralFeature)msg.getFeature(),
392 msg.getOldValue(),
393 msg.getNewValue(),
394 msg.getPosition()));
395 }
396 return;
397 }
398 super.notifyChanged(msg);
399 }
400
401 /**
402 * This adds {@link org.eclipse.emf.edit.command.CommandParameter}s describing the children
403 * that can be created under this object.
404 */
405 @Override
406 protected void collectNewChildDescriptors(Collection<Object> newChildDescriptors, Object object)
407 {
408 super.collectNewChildDescriptors(newChildDescriptors, object);
409 XSDAttributeDeclaration ad = (XSDAttributeDeclaration) object;
410
411 if (!ad.isAttributeDeclarationReference())
412 {
413 // annotation
414 newChildDescriptors.add(createChildParameter(xsdPackage.getXSDAttributeDeclaration_Annotation(), xsdFactory.createXSDAnnotation()));
415
416 // atomic, list, and union simple type definitions
417 addSimpleTypeDefinitionChildParameters(newChildDescriptors, ad, xsdPackage.getXSDAttributeDeclaration_AnonymousTypeDefinition(), true, true, true);
418 }
419 }
420 }