Guest User

Untitled

a guest
Jan 19th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.03 KB | None | 0 0
  1. package org.eclipse.papyrus.sysml.tests;
  2. import java.io.FileOutputStream;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import java.util.HashMap;
  6. import java.util.Map;
  7.  
  8. import org.eclipse.emf.common.util.EMap;
  9. import org.eclipse.emf.common.util.TreeIterator;
  10. import org.eclipse.emf.common.util.URI;
  11. import org.eclipse.emf.ecore.EAnnotation;
  12. import org.eclipse.emf.ecore.EObject;
  13. import org.eclipse.emf.ecore.resource.Resource;
  14. import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
  15. import org.eclipse.uml2.uml.Image;
  16. import org.eclipse.uml2.uml.Stereotype;
  17. import org.eclipse.uml2.uml.util.UMLUtil;
  18. import org.junit.Test;
  19.  
  20.  
  21. /**
  22. * Copy/paste and run As Junit Plugin Test
  23. * @author Benoit Maggi
  24. *
  25. */
  26. public class SetPapyrusIcon {
  27.  
  28. public static final String IMAGE_PAPYRUS_EA = "image_papyrus"; //$NON-NLS-1$
  29.  
  30. /**
  31. * KEY of the EAnnotation where the image's name is stored
  32. *
  33. * @see {@link #getName(Image)}
  34. * @see {@link #setName(Image, String)}
  35. */
  36. public static final String IMAGE_NAME_KEY = "image_name_key"; //$NON-NLS-1$
  37.  
  38. public static final String IMAGE_KIND_KEY = "image_kind_key"; //$NON-NLS-1$
  39.  
  40. @Test
  41. public void updateIcon() throws IOException {
  42. // params
  43. String profileURI = "org.eclipse.papyrus.sysml/resources/profile/SysML.profile.uml";
  44. String prefixImageLocation = "platform:/plugin/org.eclipse.papyrus.sysml.edit/icons/full/obj16/";
  45.  
  46. // open uml file
  47. URI createPlatformPluginURI = URI.createPlatformPluginURI(profileURI, true);
  48. ResourceSetImpl resourceSetImpl = new ResourceSetImpl();
  49. Resource resource = resourceSetImpl.getResource(createPlatformPluginURI, true);
  50.  
  51.  
  52. // for each stereotype
  53. TreeIterator<EObject> allContents = resource.getAllContents();
  54. while (allContents.hasNext()) {
  55. EObject eObject = allContents.next();
  56.  
  57. if (eObject instanceof Stereotype) {
  58. Stereotype stereotype = (Stereotype) eObject;
  59. Map<String, Image> images = getImages(stereotype);
  60. if (images.isEmpty()) {
  61. String name = stereotype.getName();
  62. addImage(stereotype, name, prefixImageLocation+name+".gif","icon","GIF");
  63. System.out.println("Image added for "+ name);
  64. }
  65. }
  66. }
  67. //
  68.  
  69. OutputStream outputStream = new FileOutputStream("out.uml");
  70. resource.save(outputStream, null);
  71. }
  72.  
  73.  
  74. public static Map<String, Image> getImages(Stereotype stereotype) {
  75. Map<String, Image> icons = new HashMap<>();
  76. for (Image image : stereotype.getIcons()) {
  77. EAnnotation eAnnotation = UMLUtil.getEAnnotation(image, IMAGE_PAPYRUS_EA, false);
  78. String string = eAnnotation.getDetails().get(IMAGE_NAME_KEY);
  79. icons.put(string, image);
  80. }
  81. return icons;
  82. }
  83.  
  84. public static void addImage(Stereotype stereotype, String imageName, String imageLocation, String imageKind, String imageFormat) {
  85. Image icon = stereotype.createIcon(imageLocation);
  86. icon.setFormat(imageFormat);
  87. EAnnotation imageEAnnotation = UMLUtil.createEAnnotation(icon, IMAGE_PAPYRUS_EA);
  88. EMap<String, String> details = imageEAnnotation.getDetails();
  89. details.put(IMAGE_KIND_KEY, imageKind);
  90. details.put(IMAGE_NAME_KEY, imageName);
  91. }
  92.  
  93. }
Add Comment
Please, Sign In to add comment