| 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.Collections; |
| 17 |
import java.util.List; |
| 18 |
|
| 19 |
import org.eclipse.emf.common.command.Command; |
| 20 |
import org.eclipse.emf.common.command.CommandWrapper; |
| 21 |
import org.eclipse.emf.common.command.UnexecutableCommand; |
| 22 |
import org.eclipse.emf.common.notify.AdapterFactory; |
| 23 |
import org.eclipse.emf.common.notify.Notification; |
| 24 |
import org.eclipse.emf.ecore.EStructuralFeature; |
| 25 |
import org.eclipse.emf.edit.command.CommandParameter; |
| 26 |
import org.eclipse.emf.edit.command.CreateChildCommand; |
| 27 |
import org.eclipse.emf.edit.command.RemoveCommand; |
| 28 |
import org.eclipse.emf.edit.domain.EditingDomain; |
| 29 |
import org.eclipse.emf.edit.provider.ComposeableAdapterFactory; |
| 30 |
import org.eclipse.emf.edit.provider.ComposedImage; |
| 31 |
import org.eclipse.emf.edit.provider.IEditingDomainItemProvider; |
| 32 |
import org.eclipse.emf.edit.provider.IItemLabelProvider; |
| 33 |
import org.eclipse.emf.edit.provider.IItemPropertyDescriptor; |
| 34 |
import org.eclipse.emf.edit.provider.IItemPropertySource; |
| 35 |
import org.eclipse.emf.edit.provider.IStructuredItemContentProvider; |
| 36 |
import org.eclipse.emf.edit.provider.ITreeItemContentProvider; |
| 37 |
import org.eclipse.emf.edit.provider.ItemPropertyDescriptor; |
| 38 |
import org.eclipse.emf.edit.provider.ItemPropertyDescriptorDecorator; |
| 39 |
|
| 40 |
import org.eclipse.xsd.XSDParticle; |
| 41 |
import org.eclipse.xsd.XSDParticleContent; |
| 42 |
|
| 43 |
|
| 44 |
/** |
| 45 |
* This is the item provider adpater for a {@link org.eclipse.xsd.XSDParticle} object. |
| 46 |
*/ |
| 47 |
public class XSDParticleItemProvider |
| 48 |
extends XSDComplexTypeContentItemProvider |
| 49 |
implements |
| 50 |
IEditingDomainItemProvider, |
| 51 |
IStructuredItemContentProvider, |
| 52 |
ITreeItemContentProvider, |
| 53 |
IItemLabelProvider, |
| 54 |
IItemPropertySource |
| 55 |
{ |
| 56 |
/** |
| 57 |
* This constructs an instance from a factory and a notifier. |
| 58 |
*/ |
| 59 |
public XSDParticleItemProvider(AdapterFactory adapterFactory) |
| 60 |
{ |
| 61 |
super(adapterFactory); |
| 62 |
} |
| 63 |
|
| 64 |
protected XSDParticleContent getDelegate(XSDParticle xsdParticle) |
| 65 |
{ |
| 66 |
return xsdParticle.getContent(); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* This returns the property descriptors for the adapted class. |
| 71 |
*/ |
| 72 |
@Override |
| 73 |
public List<IItemPropertyDescriptor> getPropertyDescriptors(Object object) |
| 74 |
{ |
| 75 |
itemPropertyDescriptors = null; |
| 76 |
super.getPropertyDescriptors(object); |
| 77 |
|
| 78 |
XSDParticle xsdParticle = (XSDParticle)object; |
| 79 |
XSDParticleContent xsdParticleContent = getDelegate(xsdParticle); |
| 80 |
if (xsdParticleContent != null) |
| 81 |
{ |
| 82 |
for (IItemPropertyDescriptor delegatedItemPropertyDescriptor : itemDelegator.getPropertyDescriptors(xsdParticleContent)) |
| 83 |
{ |
| 84 |
itemPropertyDescriptors.add(new ItemPropertyDescriptorDecorator(xsdParticleContent, delegatedItemPropertyDescriptor)); |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
// This is for the minOccurs feature. |
| 90 |
// |
| 91 |
itemPropertyDescriptors.add |
| 92 |
(new ItemPropertyDescriptor |
| 93 |
(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), |
| 94 |
XSDEditPlugin.INSTANCE.getString("_UI_MinOccurs_label"), |
| 95 |
XSDEditPlugin.INSTANCE.getString("_UI_MinOccurs_description"), |
| 96 |
xsdPackage.getXSDParticle_MinOccurs(), |
| 97 |
true, |
| 98 |
ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE)); |
| 99 |
|
| 100 |
// This is for the maxOccurs feature. |
| 101 |
// |
| 102 |
itemPropertyDescriptors.add |
| 103 |
(new ItemPropertyDescriptor |
| 104 |
(((ComposeableAdapterFactory)adapterFactory).getRootAdapterFactory(), |
| 105 |
XSDEditPlugin.INSTANCE.getString("_UI_MaxOccurs_label"), |
| 106 |
XSDEditPlugin.INSTANCE.getString("_UI_MaxOccurs_description"), |
| 107 |
xsdPackage.getXSDParticle_MaxOccurs(), |
| 108 |
true, |
| 109 |
ItemPropertyDescriptor.INTEGRAL_VALUE_IMAGE)); |
| 110 |
|
| 111 |
return itemPropertyDescriptors; |
| 112 |
} |
| 113 |
|
| 114 |
/** |
| 115 |
* This specifies how to implement {@link #getChildren} and is used to deduce an appropriate feature for an |
| 116 |
* {@link org.eclipse.emf.edit.command.AddCommand}, {@link org.eclipse.emf.edit.command.RemoveCommand} or |
| 117 |
* {@link org.eclipse.emf.edit.command.MoveCommand} in {@link #createCommand}. |
| 118 |
*/ |
| 119 |
@Override |
| 120 |
public Collection<? extends EStructuralFeature> getChildrenFeatures(Object object) |
| 121 |
{ |
| 122 |
if (childrenFeatures == null) |
| 123 |
{ |
| 124 |
super.getChildrenFeatures(object); |
| 125 |
childrenFeatures.clear(); |
| 126 |
/* |
| 127 |
XSDParticle xsdParticle = ((XSDParticle)object); |
| 128 |
childrenFeatures.add(xsdPackage.getXSDParticle_Content()); |
| 129 |
*/ |
| 130 |
} |
| 131 |
return childrenFeatures; |
| 132 |
} |
| 133 |
|
| 134 |
@Override |
| 135 |
public Collection<?> getChildren(Object object) |
| 136 |
{ |
| 137 |
XSDParticle xsdParticle = (XSDParticle)object; |
| 138 |
XSDParticleContent xsdParticleContent = getDelegate(xsdParticle); |
| 139 |
if (xsdParticleContent != null) |
| 140 |
{ |
| 141 |
return itemDelegator.getChildren(xsdParticleContent); |
| 142 |
} |
| 143 |
else |
| 144 |
{ |
| 145 |
return Collections.EMPTY_LIST; |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public boolean hasChildren(Object object) |
| 151 |
{ |
| 152 |
XSDParticle xsdParticle = (XSDParticle)object; |
| 153 |
XSDParticleContent xsdParticleContent = getDelegate(xsdParticle); |
| 154 |
return |
| 155 |
xsdParticleContent != null && itemDelegator.hasChildren(xsdParticleContent); |
| 156 |
} |
| 157 |
|
| 158 |
/** |
| 159 |
* This returns XSDParticle.gif. |
| 160 |
*/ |
| 161 |
@Override |
| 162 |
public Object getImage(Object object) |
| 163 |
{ |
| 164 |
XSDParticle xsdParticle = ((XSDParticle)object); |
| 165 |
XSDParticleContent xsdParticleContent = getDelegate(xsdParticle); |
| 166 |
if (xsdParticleContent != null) |
| 167 |
{ |
| 168 |
Collection<Object> images = new ArrayList<Object>(); |
| 169 |
images.add(itemDelegator.getImage(xsdParticleContent)); |
| 170 |
String imageName = "full/obj16/XSDOccurrence"; |
| 171 |
int minOccurs = xsdParticle.getMinOccurs(); |
| 172 |
int maxOccurs = xsdParticle.getMaxOccurs(); |
| 173 |
if (minOccurs >= 0 && (minOccurs <= maxOccurs || maxOccurs == -1)) |
| 174 |
{ |
| 175 |
switch (minOccurs) |
| 176 |
{ |
| 177 |
case 0: |
| 178 |
{ |
| 179 |
imageName += "Zero"; |
| 180 |
break; |
| 181 |
} |
| 182 |
case 1: |
| 183 |
{ |
| 184 |
imageName += "One"; |
| 185 |
break; |
| 186 |
} |
| 187 |
default: |
| 188 |
{ |
| 189 |
imageName += "N"; |
| 190 |
break; |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
if (minOccurs != maxOccurs) |
| 195 |
{ |
| 196 |
switch (maxOccurs) |
| 197 |
{ |
| 198 |
case -1: |
| 199 |
{ |
| 200 |
imageName += "ToUnbounded"; |
| 201 |
break; |
| 202 |
} |
| 203 |
case 0: |
| 204 |
{ |
| 205 |
break; |
| 206 |
} |
| 207 |
case 1: |
| 208 |
{ |
| 209 |
imageName += "ToOne"; |
| 210 |
break; |
| 211 |
} |
| 212 |
default: |
| 213 |
{ |
| 214 |
imageName += xsdParticle.getMinOccurs() <= 1 ? "ToN" : "ToM"; |
| 215 |
break; |
| 216 |
} |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
else |
| 221 |
{ |
| 222 |
imageName += "NToM"; |
| 223 |
} |
| 224 |
|
| 225 |
if (!imageName.equals("full/obj16/XSDOccurrenceOne")) |
| 226 |
{ |
| 227 |
images.add(XSDEditPlugin.INSTANCE.getImage(imageName)); |
| 228 |
} |
| 229 |
|
| 230 |
return new ComposedImage(images); |
| 231 |
} |
| 232 |
else |
| 233 |
{ |
| 234 |
return XSDEditPlugin.INSTANCE.getImage("full/obj16/XSDParticle"); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
@Override |
| 239 |
public String getText(Object object) |
| 240 |
{ |
| 241 |
XSDParticle xsdParticle = ((XSDParticle)object); |
| 242 |
XSDParticleContent xsdParticleContent = getDelegate(xsdParticle); |
| 243 |
if (xsdParticleContent != null) |
| 244 |
{ |
| 245 |
return itemDelegator.getText(xsdParticleContent); |
| 246 |
} |
| 247 |
else |
| 248 |
{ |
| 249 |
return |
| 250 |
xsdParticle.getMinOccurs() + |
| 251 |
".." + |
| 252 |
(xsdParticle.getMaxOccurs() == -1 ? "unbounded" : Integer.toString(xsdParticle.getMaxOccurs())); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** |
| 257 |
* This handles notification by calling {@link #fireNotifyChanged fireNotifyChanged}. |
| 258 |
*/ |
| 259 |
@Override |
| 260 |
public void notifyChanged(Notification msg) |
| 261 |
{ |
| 262 |
if ( |
| 263 |
msg.getFeature() == xsdPackage.getXSDParticle_MinOccurs() || |
| 264 |
msg.getFeature() == xsdPackage.getXSDParticle_MaxOccurs() || |
| 265 |
msg.getFeature() == xsdPackage.getXSDParticle_Content() || |
| 266 |
msg.getFeature() == xsdPackage.getXSDParticle_Term() |
| 267 |
) |
| 268 |
{ |
| 269 |
fireNotifyChanged(msg); |
| 270 |
return; |
| 271 |
} |
| 272 |
super.notifyChanged(msg); |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* This returns a list of child descriptors based on the particle content, |
| 277 |
* not the particle itself. |
| 278 |
*/ |
| 279 |
@Override |
| 280 |
public Collection<?> getNewChildDescriptors(Object object, EditingDomain domain, Object sibling) |
| 281 |
{ |
| 282 |
Object content = ((XSDParticle) object).getContent(); |
| 283 |
return domain.getNewChildDescriptors(content, sibling); |
| 284 |
} |
| 285 |
|
| 286 |
/** |
| 287 |
* This returns Remove and CreateChild commands (at least) that are based |
| 288 |
* on the attributeUse content, not the attributeUse itself. |
| 289 |
*/ |
| 290 |
@Override |
| 291 |
public Command createCommand(final Object object, final EditingDomain domain, Class<? extends Command> commandClass, CommandParameter commandParameter) |
| 292 |
{ |
| 293 |
if (commandClass == RemoveCommand.class || |
| 294 |
commandClass == CreateChildCommand.class) |
| 295 |
{ |
| 296 |
Object owner = ((XSDParticle) object).getContent(); |
| 297 |
if (owner == null) |
| 298 |
{ |
| 299 |
return UnexecutableCommand.INSTANCE; |
| 300 |
} |
| 301 |
|
| 302 |
commandParameter.setOwner(owner); |
| 303 |
|
| 304 |
// RemoveCommand requires a wrapper that returns the correct affected |
| 305 |
// object after an execute or redo (the particle or attribute use |
| 306 |
// itself, instead of its content) |
| 307 |
if (commandClass == RemoveCommand.class) |
| 308 |
{ |
| 309 |
return new CommandWrapper( |
| 310 |
domain.createCommand(commandClass, commandParameter)) |
| 311 |
{ |
| 312 |
private Collection<?> affectedObjects = Collections.EMPTY_SET; |
| 313 |
|
| 314 |
@Override |
| 315 |
public void execute() |
| 316 |
{ |
| 317 |
super.execute(); |
| 318 |
affectedObjects = Collections.singleton(object); |
| 319 |
} |
| 320 |
|
| 321 |
@Override |
| 322 |
public void undo() |
| 323 |
{ |
| 324 |
super.undo(); |
| 325 |
affectedObjects = super.getAffectedObjects(); |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public void redo() |
| 330 |
{ |
| 331 |
super.redo(); |
| 332 |
affectedObjects = Collections.singleton(object); |
| 333 |
} |
| 334 |
|
| 335 |
@Override |
| 336 |
public Collection<?> getAffectedObjects() |
| 337 |
{ |
| 338 |
return affectedObjects; |
| 339 |
} |
| 340 |
}; |
| 341 |
} |
| 342 |
|
| 343 |
// CreateChildCommand handles its affected objects correctly |
| 344 |
return domain.createCommand(commandClass, commandParameter); |
| 345 |
} |
| 346 |
return super.createCommand(object, domain, commandClass, commandParameter); |
| 347 |
} |
| 348 |
|
| 349 |
/* |
| 350 |
protected Command factorRemoveCommand(EditingDomain domain, CommandParameter commandParameter) |
| 351 |
{ |
| 352 |
XSDParticle xsdParticle = (XSDParticle)commandParameter.getRefOwner(); |
| 353 |
return |
| 354 |
domain.createCommand |
| 355 |
(RemoveCommand.class, |
| 356 |
new CommandParameter |
| 357 |
(getDelegate(xsdParticle), |
| 358 |
commandParameter.getFeature(), |
| 359 |
commandParameter.getCollection(), |
| 360 |
commandParameter.getIndex())); |
| 361 |
} |
| 362 |
|
| 363 |
protected Command factorAddCommand(EditingDomain domain, CommandParameter commandParameter) |
| 364 |
{ |
| 365 |
XSDParticle xsdParticle = (XSDParticle)commandParameter.getRefOwner(); |
| 366 |
return |
| 367 |
domain.createCommand |
| 368 |
(AddCommand.class, |
| 369 |
new CommandParameter |
| 370 |
(getDelegate(xsdParticle), |
| 371 |
commandParameter.getFeature(), |
| 372 |
commandParameter.getCollection(), |
| 373 |
commandParameter.getIndex())); |
| 374 |
} |
| 375 |
|
| 376 |
protected Command factorMoveCommand(EditingDomain domain, CommandParameter commandParameter) |
| 377 |
{ |
| 378 |
XSDParticle xsdParticle = (XSDParticle)commandParameter.getRefOwner(); |
| 379 |
return |
| 380 |
domain.createCommand |
| 381 |
(MoveCommand.class, |
| 382 |
new CommandParameter |
| 383 |
(getDelegate(xsdParticle), |
| 384 |
commandParameter.getFeature(), |
| 385 |
commandParameter.getValue(), |
| 386 |
commandParameter.getIndex())); |
| 387 |
} |
| 388 |
*/ |
| 389 |
} |