1 17 package org.eclipse.emf.edit.provider; 18 19 20 import java.util.Collections ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.eclipse.emf.common.command.Command; 25 import org.eclipse.emf.common.command.UnexecutableCommand; 26 import org.eclipse.emf.common.notify.AdapterFactory; 27 import org.eclipse.emf.common.util.ResourceLocator; 28 import org.eclipse.emf.ecore.EAttribute; 29 import org.eclipse.emf.ecore.EDataType; 30 import org.eclipse.emf.ecore.EObject; 31 import org.eclipse.emf.ecore.EStructuralFeature; 32 import org.eclipse.emf.ecore.util.EcoreUtil; 33 import org.eclipse.emf.ecore.util.ExtendedMetaData; 34 import org.eclipse.emf.ecore.util.FeatureMap; 35 import org.eclipse.emf.ecore.util.FeatureMapUtil; 36 import org.eclipse.emf.ecore.xml.type.XMLTypePackage; 37 import org.eclipse.emf.edit.EMFEditPlugin; 38 import org.eclipse.emf.edit.command.CommandParameter; 39 import org.eclipse.emf.edit.command.CopyCommand; 40 import org.eclipse.emf.edit.command.SetCommand; 41 import org.eclipse.emf.edit.domain.EditingDomain; 42 43 44 52 public class FeatureMapEntryWrapperItemProvider extends DelegatingWrapperItemProvider 53 { 54 57 protected ResourceLocator resourceLocator; 58 59 67 public FeatureMapEntryWrapperItemProvider( 68 FeatureMap.Entry entry, 69 EObject owner, 70 EAttribute attribute, 71 int index, 72 AdapterFactory adapterFactory, 73 ResourceLocator resourceLocator) 74 { 75 super(entry, owner, attribute, index, adapterFactory); 76 } 77 78 87 public FeatureMapEntryWrapperItemProvider( 88 FeatureMap.Entry entry, 89 EObject owner, 90 EAttribute attribute, 91 int index, 92 AdapterFactory adapterFactory) 93 { 94 this(entry, owner, attribute, index, adapterFactory, null); 95 } 96 97 100 protected Object getEntryValue() 101 { 102 return ((FeatureMap.Entry)value).getValue(); 103 } 104 105 108 protected EStructuralFeature getEntryFeature() 109 { 110 return ((FeatureMap.Entry)value).getEStructuralFeature(); 111 } 112 113 116 protected boolean isEntryAttribute() 117 { 118 return getEntryFeature() instanceof EAttribute; 119 } 120 121 125 protected Object getDelegateValue() 126 { 127 return isEntryAttribute() ? null : getEntryValue(); 128 } 129 130 136 public String getText(Object object) 137 { 138 String text = null; 139 XMLTypePackage xmlPackage = XMLTypePackage.eINSTANCE; 140 141 if (getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_Text()) 142 { 143 text = encode(getEntryValue().toString()); 144 } 145 else if (getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_CDATA()) 146 { 147 text = "<![CDATA[" + encode(getEntryValue().toString()) + "]]>"; 148 } 149 else if (getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_Comment()) 150 { 151 text = "<!--" + encode(getEntryValue().toString()) + "-->"; 152 } 153 else if (ExtendedMetaData.INSTANCE.getFeatureKind(feature) == ExtendedMetaData.ATTRIBUTE_WILDCARD_FEATURE && isEntryAttribute()) 154 { 155 text = getEntryFeature().getName() + "='" + 156 EcoreUtil.convertToString((EDataType)getEntryFeature().getEType(), getEntryValue()) + "'"; 157 } 158 else if (getEntryValue() == null) 159 { 160 text = "<" + getEntryFeature().getName() + " xsi:nil=\"true\"/>"; 161 } 162 else if (isEntryAttribute()) 163 { 164 text = addEntryFeature(EcoreUtil.convertToString((EDataType)getEntryFeature().getEType(), getEntryValue())); 165 } 166 else 167 { 168 text = addEntryFeature(super.getText(object)); 169 } 170 return text; 171 } 172 173 176 protected String addEntryFeature(String text) 177 { 178 return "<" + getEntryFeature().getName() + "> " + text; 179 } 180 181 186 public Object getImage(Object object) 187 { 188 Object image = null; 189 XMLTypePackage xmlPackage = XMLTypePackage.eINSTANCE; 190 191 if (getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_Text() || 192 getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_CDATA() || 193 getEntryFeature() == xmlPackage.getXMLTypeDocumentRoot_Comment()) 194 { 195 image = EMFEditPlugin.INSTANCE.getImage("full/obj16/TextValue"); 196 } 197 else if (isEntryAttribute()) 198 { 199 image = EMFEditPlugin.INSTANCE.getImage("full/obj16/GenericValue"); 200 } 201 else if (getDelegateValue() == null) 202 { 203 image = EMFEditPlugin.INSTANCE.getImage("full/obj16/Item"); 204 } 205 else 206 { 207 image = super.getImage(object); 208 } 209 return image; 210 } 211 212 216 protected String encode(String s) 217 { 218 StringBuffer result = new StringBuffer (s.length()); 219 for (int i = 0, len = s.length(); i < len; i++) 220 { 221 char c = s.charAt(i); 222 switch (c) 223 { 224 case '\\': 225 { 226 result.append("\\\\"); 227 break; 228 } 229 case '\b': 230 { 231 result.append("\\b"); 232 break; 233 } 234 case '\t': 235 { 236 result.append("\\t"); 237 break; 238 } 239 case '\n': 240 { 241 result.append("\\n"); 242 break; 243 } 244 case '\f': 245 { 246 result.append("\\f"); 247 break; 248 } 249 case '\r': 250 { 251 result.append("\\r"); 252 break; 253 } 254 default: 255 { 256 result.append(c); 257 break; 258 } 259 } 260 } 261 return result.toString(); 262 } 263 264 267 protected String decode(String s) 268 { 269 StringBuffer result = new StringBuffer (s.length()); 270 for (int i = 0, len = s.length(); i < len; i++) 271 { 272 char c = s.charAt(i); 273 switch (c) 274 { 275 case '\\': 276 { 277 if (++i < len) 278 { 279 c = s.charAt(i); 280 switch (c) 281 { 282 case '\\': 283 { 284 result.append('\\'); 285 break; 286 } 287 case 'b': 288 { 289 result.append('\b'); 290 break; 291 } 292 case 't': 293 { 294 result.append('\t'); 295 break; 296 } 297 case 'n': 298 { 299 result.append('\n'); 300 break; 301 } 302 case 'f': 303 { 304 result.append('\f'); 305 break; 306 } 307 case '\r': 308 { 309 result.append('\r'); 310 break; 311 } 312 default: 313 { 314 result.append('\\'); 315 --i; 316 break; 317 } 318 } 319 } 320 else 321 { 322 result.append(c); 323 } 324 break; 325 } 326 default: 327 { 328 result.append(c); 329 break; 330 } 331 } 332 } 333 return result.toString(); 334 } 335 336 340 public List getPropertyDescriptors(Object object) 341 { 342 if (isEntryAttribute()) 343 { 344 if (propertyDescriptors == null) 345 { 346 propertyDescriptors = Collections.singletonList( 347 new WrapperItemPropertyDescriptor(resourceLocator, getEntryFeature()) 348 { 349 protected Object getValue(EObject object, EStructuralFeature feature) 350 { 351 357 FeatureMap featureMap = (FeatureMap)((EObject)owner).eGet(FeatureMapEntryWrapperItemProvider.this.feature); 358 Object result = index >= 0 && index < featureMap.size() ? featureMap.getValue(index) : getEntryValue(); 359 return needsEncoding(feature) ? encode((String )result) : result; 360 } 361 362 protected void setValue(EObject object, EStructuralFeature feature, Object value) 363 { 364 if (needsEncoding(feature)) 365 { 366 value = decode((String )value); 367 } 368 ((FeatureMap)((EObject)owner).eGet(FeatureMapEntryWrapperItemProvider.this.feature)).setValue(index, value); 369 } 370 371 protected Command createSetCommand(EditingDomain domain, Object owner, Object feature, Object value) 372 { 373 if (needsEncoding(feature)) 374 { 375 value = decode((String )value); 376 } 377 return SetCommand.create(domain, getCommandOwner(FeatureMapEntryWrapperItemProvider.this), null, value); 378 } 379 380 protected boolean needsEncoding(Object feature) 381 { 382 return 383 feature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Text() || 384 feature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_CDATA() || 385 feature == XMLTypePackage.eINSTANCE.getXMLTypeDocumentRoot_Comment(); 386 } 387 }); 388 } 389 return propertyDescriptors; 390 } 391 else 392 { 393 return super.getPropertyDescriptors(object); 394 } 395 } 396 397 400 public Object getEditableValue(Object object) 401 { 402 return isEntryAttribute() ? getEntryValue() : super.getEditableValue(object); 403 } 404 405 408 protected boolean isPropertySettable() 409 { 410 return getEntryFeature().isChangeable(); 411 } 412 413 417 protected Object getPropertyImage() 418 { 419 return getPropertyImage(getEntryFeature().getEType().getInstanceClass()); 420 } 421 422 425 public Command createCommand(Object object, EditingDomain domain, Class commandClass, CommandParameter commandParameter) 426 { 427 if (getDelegateValue() == null) 428 { 429 return baseCreateCommand(object, domain, commandClass, commandParameter); 430 } 431 return super.createCommand(object, domain, commandClass, commandParameter); 432 } 433 434 440 protected Command wrapCommand(Command command, Class commandClass) 441 { 442 if (commandClass == CopyCommand.class) 443 { 444 return new WrappingCopyCommand(command) 445 { 446 public IWrapperItemProvider copy() 447 { 448 Iterator i = getCommand().getResult().iterator(); 449 return new FeatureMapEntryWrapperItemProvider( 450 FeatureMapUtil.createEntry(getEntryFeature(), i.next()), (EObject)owner, (EAttribute)feature, index, adapterFactory); 451 } 452 }; 453 } 454 return super.wrapCommand(command, commandClass); 455 } 456 457 461 protected Command createSetCommand(EditingDomain domain, Object owner, Object feature, Object value, int index) 462 { 463 if (getEntryFeature().getEType().isInstance(value)) 466 { 467 FeatureMap.Entry entry = FeatureMapUtil.createEntry(getEntryFeature(), value); 468 return new ReplacementAffectedObjectCommand(SetCommand.create(domain, this.owner, this.feature, entry, this.index)); 469 } 470 return UnexecutableCommand.INSTANCE; 471 } 472 473 477 protected Command createCopyCommand(EditingDomain domain, Object owner, CopyCommand.Helper helper) 478 { 479 return new SimpleCopyCommand(domain) 480 { 481 public IWrapperItemProvider copy() 482 { 483 Object entryValueCopy = null; 484 Object entryValue = getEntryValue(); 485 486 if (entryValue != null) 487 { 488 EDataType dataType = (EDataType)getEntryFeature().getEType(); 489 String serialization = EcoreUtil.convertToString(dataType, entryValue); 490 entryValueCopy = EcoreUtil.createFromString(dataType, serialization); 491 if (serialization == entryValue && serialization == entryValueCopy) 492 { 493 entryValueCopy = new String ((String )entryValue); 494 } 495 } 496 497 return new FeatureMapEntryWrapperItemProvider( 498 FeatureMapUtil.createEntry(getEntryFeature(), entryValueCopy), 499 (EObject)FeatureMapEntryWrapperItemProvider.this.owner, 500 (EAttribute)feature, 501 index, 502 adapterFactory); 503 } 504 }; 505 } 506 } 507 | Popular Tags |