Debian Codegraph @

plugins/org.eclipse.xsd.edit/src/org/eclipse/xsd/provider/XSDAttributeUseItemProvider.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 import java.util.ArrayList;
14 import java.util.Collection;
15 import java.util.Collections;
16 import java.util.List;
17
18 import org.eclipse.emf.common.command.Command;
19 import org.eclipse.emf.common.command.CommandWrapper;
20 import org.eclipse.emf.common.command.UnexecutableCommand;
21 import org.eclipse.emf.common.notify.AdapterFactory;
22 import org.eclipse.emf.common.notify.Notification;
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.command.RemoveCommand;
27 import org.eclipse.emf.edit.domain.EditingDomain;
28 import org.eclipse.emf.edit.provider.ComposeableAdapterFactory;
29 import org.eclipse.emf.edit.provider.ComposedImage;
30 import org.eclipse.emf.edit.provider.IEditingDomainItemProvider;
31 import org.eclipse.emf.edit.provider.IItemLabelProvider;
32 import org.eclipse.emf.edit.provider.IItemPropertyDescriptor;
33 import org.eclipse.emf.edit.provider.IItemPropertySource;
34 import org.eclipse.emf.edit.provider.IStructuredItemContentProvider;
35 import org.eclipse.emf.edit.provider.ITreeItemContentProvider;
36 import org.eclipse.emf.edit.provider.ItemPropertyDescriptor;
37 import org.eclipse.emf.edit.provider.ItemPropertyDescriptorDecorator;
38
39 import org.eclipse.xsd.XSDAttributeDeclaration;
40 import org.eclipse.xsd.XSDAttributeUse;
41 import org.eclipse.xsd.XSDAttributeUseCategory;
42 import org.eclipse.xsd.XSDPackage;
43
44
45 /**
46 * This is the item provider adpater for a {@link org.eclipse.xsd.XSDAttributeUse} object.
47 */
48 public class XSDAttributeUseItemProvider
49 extends XSDComponentItemProvider
50 implements
51 IEditingDomainItemProvider,
52 IStructuredItemContentProvider,
53 ITreeItemContentProvider,
54 IItemLabelProvider,
55 IItemPropertySource
56 {
57 /**
58 * This constructs an instance from a factory and a notifier.
59 */
60 public XSDAttributeUseItemProvider(AdapterFactory adapterFactory)
61 {
62 super(adapterFactory);
63 }
64
65 protected XSDAttributeDeclaration getDelegate(XSDAttributeUse xsdAttributeUse)
66 {
67 return xsdAttributeUse.getContent();
68 }
69
70 /**
71 * This returns the property descriptors for the adapted class.
72 */
73 @Override
74 public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object)
75 {
76 itemPropertyDescriptors = null;
77 super.getPropertyDescriptors(object);
78
79 XSDAttributeUse xsdAttributeUse = ((XSDAttributeUse)object);
80 XSDAttributeDeclaration xsdAttributeDeclaration = getDelegate(xsdAttributeUse);
81 if (xsdAttributeDeclaration != null)
82 {
83 for (IItemPropertyDescriptor delegatedItemPropertyDescriptor : itemDelegator.getPropertyDescriptors(xsdAttributeDeclaration))
84 {
85 Object feature = delegatedItemPropertyDescriptor.getFeature(object);
86 if (feature != XSDPackage.eINSTANCE.getXSDFeature_Constraint() &&
87 feature != XSDPackage.eINSTANCE.getXSDFeature_Value() &&
88 feature != XSDPackage.eINSTANCE.getXSDFeature_LexicalValue())
89 {
90 itemPropertyDescriptors.add(new ItemPropertyDescriptorDecorator(xsdAttributeDeclaration, delegatedItemPropertyDescriptor));
91 }
92 }
93 }
94
95 // This is for the required feature.
96 //
97 itemPropertyDescriptors.add
98 (new ItemPropertyDescriptor
99 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
100 XSDEditPlugin.INSTANCE.getString("_UI_Required_label"),
101 XSDEditPlugin.INSTANCE.getString("_UI_Required_description"),
102 xsdPackage.getXSDAttributeUse_Required(),
103 true,
104 ItemPropertyDescriptor.BOOLEAN_VALUE_IMAGE));
105
106 // This is for the use feature.
107 //
108 itemPropertyDescriptors.add
109 (new ItemPropertyDescriptor
110 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
111 XSDEditPlugin.INSTANCE.getString("_UI_Use_label"),
112 XSDEditPlugin.INSTANCE.getString("_UI_Use_description"),
113 xsdPackage.getXSDAttributeUse_Use(),
114 true,
115 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
116
117 // This is for the value feature.
118 //
119 itemPropertyDescriptors.add
120 (new ItemPropertyDescriptor
121 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
122 XSDEditPlugin.INSTANCE.getString("_UI_LexicalValue_label"),
123 XSDEditPlugin.INSTANCE.getString("_UI_LexicalValueOfAttributeUse_description"),
124 xsdPackage.getXSDAttributeUse_LexicalValue(),
125 true,
126 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
127
128 // This is for the constraint feature.
129 //
130 itemPropertyDescriptors.add
131 (new ItemPropertyDescriptor
132 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
133 XSDEditPlugin.INSTANCE.getString("_UI_Constraint_label"),
134 XSDEditPlugin.INSTANCE.getString("_UI_ConstraintOfAttributeUse_description"),
135 xsdPackage.getXSDAttributeUse_Constraint(),
136 true,
137 ItemPropertyDescriptor.TEXT_VALUE_IMAGE));
138
139 // This is for the attributeDeclaration feature.
140 //
141 itemPropertyDescriptors.add
142 (new ItemPropertyDescriptor
143 (((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(),
144 XSDEditPlugin.INSTANCE.getString("_UI_AttributeDeclaration_label"),
145 XSDEditPlugin.INSTANCE.getString("_UI_AttributeDeclaration_description"),
146 xsdPackage.getXSDAttributeUse_AttributeDeclaration(),
147 false));
148
149 return itemPropertyDescriptors;
150 }
151
152 @Override
153 public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object)
154 {
155 if (childrenFeatures == null)
156 {
157 super.getChildrenFeatures(object);
158 childrenFeatures.clear();
159 /*
160 XSDAttributeUse xsdAttributeUse = ((XSDAttributeUse)object);
161 childrenFeatures.add(xsdPackage.getXSDAttributeUse_Content());
162 */
163 }
164 return childrenFeatures;
165 }
166
167 @Override
168 public Collection<?> getChildren(Object object)
169 {
170 XSDAttributeUse xsdAttributeUse = (XSDAttributeUse)object;
171 XSDAttributeDeclaration xsdAttributeDeclaration = getDelegate(xsdAttributeUse);
172 if (xsdAttributeDeclaration != null)
173 {
174 return itemDelegator.getChildren(xsdAttributeDeclaration);
175 }
176 else
177 {
178 return Collections.EMPTY_LIST;
179 }
180 }
181
182 @Override
183 public boolean hasChildren(Object object)
184 {
185 XSDAttributeUse xsdAttributeUse = (XSDAttributeUse)object;
186 XSDAttributeDeclaration xsdAttributeDeclaration = getDelegate(xsdAttributeUse);
187 return
188 xsdAttributeDeclaration != null && itemDelegator.hasChildren(xsdAttributeDeclaration);
189 }
190
191 /**
192 * This returns XSDAttributeUse.gif.
193 */
194 @Override
195 public Object getImage(Object object)
196 {
197 XSDAttributeUse xsdAttributeUse = ((XSDAttributeUse)object);
198 XSDAttributeDeclaration xsdAttributeDeclaration = getDelegate(xsdAttributeUse);
199 if (xsdAttributeDeclaration != null)
200 {
201 Collection<Object> images = new ArrayList<Object>();
202 images.add(itemDelegator.getImage(xsdAttributeDeclaration));
203
204 // Try this way, i.e., not showing the 1 case.
205 //
206 if (XSDAttributeUseCategory.REQUIRED_LITERAL != xsdAttributeUse.getUse())
207 {
208 String imageName =
209 "full/obj16/XSDOccurrence" +
210 (XSDAttributeUseCategory.PROHIBITED_LITERAL == xsdAttributeUse.getUse() ?
211 "Zero" :
212 XSDAttributeUseCategory.REQUIRED_LITERAL == xsdAttributeUse.getUse() ?
213 "One" :
214 "ZeroToOne");
215 images.add(XSDEditPlugin.INSTANCE.getImage(imageName));
216 }
217
218 return new ComposedImage(images);
219 }
220 else
221 {
222 return XSDEditPlugin.INSTANCE.getImage("full/obj16/XSDAttributeUse");
223 }
224 }
225
226 @Override
227 public String getText(Object object)
228 {
229 return getText(object, true);
230 }
231
232 public String getText(Object object, boolean showType)
233 {
234 XSDAttributeUse xsdAttributeUse = ((XSDAttributeUse)object);
235 XSDAttributeDeclaration xsdAttributeDeclaration = getDelegate(xsdAttributeUse);
236 StringBuffer result = new StringBuffer();
237 if (xsdAttributeDeclaration != null)
238 {
239 result.append(itemDelegator.getText(xsdAttributeDeclaration));
240 }
241
242 if (xsdAttributeUse.isSetConstraint())
243 {
244 if (result.length() != 0)
245 {
246 result.append(" ");
247 }
248 result.append('<');
249 result.append(xsdAttributeUse.getConstraint());
250 result.append("=\"");
251 result.append(xsdAttributeUse.getLexicalValue());
252 result.append("\">");
253 }
254
255 return result.toString();
256 }
257
258 /**
259 * This handles notification by calling {@link #fireNotifyChanged fireNotifyChanged}.
260 */
261 @Override
262 public void notifyChanged(Notification msg)
263 {
264 if (msg.getFeature() == xsdPackage.getXSDAttributeUse_Required() ||
265 msg.getFeature() == xsdPackage.getXSDAttributeUse_LexicalValue() ||
266 msg.getFeature() == xsdPackage.getXSDAttributeUse_Constraint() ||
267 msg.getFeature() == xsdPackage.getXSDAttributeUse_AttributeDeclaration() ||
268 msg.getFeature() == xsdPackage.getXSDAttributeUse_Use())
269 {
270 fireNotifyChanged(msg);
271 return;
272 }
273 super.notifyChanged(msg);
274 }
275
276 /**
277 * This returns a list of child descriptors based on the attributeUse
278 * content, not the attributeUse itself.
279 */
280 @Override
281 public Collection<?> getNewChildDescriptors(Object object, EditingDomain domain, Object sibling)
282 {
283 Object content = ((XSDAttributeUse) object).getContent();
284 return domain.getNewChildDescriptors(content, sibling);
285 }
286
287 /**
288 * This returns Remove and CreateChild commands (at least) that are based
289 * on the attributeUse content, not the attributeUse itself.
290 */
291 @Override
292 public Command createCommand(final Object object, final EditingDomain domain, Class<? extends Command> commandClass, CommandParameter commandParameter)
293 {
294 if (commandClass == RemoveCommand.class ||
295 commandClass == CreateChildCommand.class)
296 {
297 Object owner = ((XSDAttributeUse) object).getContent();
298 if (owner == null)
299 {
300 return UnexecutableCommand.INSTANCE;
301 }
302
303 commandParameter.setOwner(owner);
304
305 // RemoveCommand requires a wrapper that returns the correct affected
306 // object after an execute or redo (the particle or attribute use
307 // itself, instead of its content)
308 if (commandClass == RemoveCommand.class)
309 {
310 return new CommandWrapper(
311 domain.createCommand(commandClass, commandParameter))
312 {
313 private Collection<?> affectedObjects = Collections.EMPTY_SET;
314
315 @Override
316 public void execute()
317 {
318 super.execute();
319 affectedObjects = Collections.singleton(object);
320 }
321
322 @Override
323 public void undo()
324 {
325 super.undo();
326 affectedObjects = super.getAffectedObjects();
327 }
328
329 @Override
330 public void redo()
331 {
332 super.redo();
333 affectedObjects = Collections.singleton(object);
334 }
335
336 @Override
337 public Collection<?> getAffectedObjects()
338 {
339 return affectedObjects;
340 }
341 };
342 }
343
344 // CreateChildCommand handles its affected objects correctly
345 return domain.createCommand(commandClass, commandParameter);
346 }
347 return super.createCommand(object, domain, commandClass, commandParameter);
348 }
349 }