Skip to content

Instantly share code, notes, and snippets.

@bmaggi
Last active January 10, 2018 09:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bmaggi/88a118249705ae3ba0719a8eee83b944 to your computer and use it in GitHub Desktop.
Save bmaggi/88a118249705ae3ba0719a8eee83b944 to your computer and use it in GitHub Desktop.
A utility to set all icon/eannotation for Papyrus images in a profile.
package org.eclipse.papyrus.sysml.tests;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import org.eclipse.emf.common.util.EMap;
import org.eclipse.emf.common.util.TreeIterator;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EAnnotation;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.uml2.uml.Image;
import org.eclipse.uml2.uml.Stereotype;
import org.eclipse.uml2.uml.util.UMLUtil;
import org.junit.Test;
/**
* Copy/paste and run As Junit Plugin Test
* @author Benoit Maggi
*
*/
public class SetPapyrusIcon {
public static final String IMAGE_PAPYRUS_EA = "image_papyrus"; //$NON-NLS-1$
/**
* KEY of the EAnnotation where the image's name is stored
*
* @see {@link #getName(Image)}
* @see {@link #setName(Image, String)}
*/
public static final String IMAGE_NAME_KEY = "image_name_key"; //$NON-NLS-1$
public static final String IMAGE_KIND_KEY = "image_kind_key"; //$NON-NLS-1$
@Test
public void updateIcon() throws IOException {
// params
String profileURI = "org.eclipse.papyrus.sysml/resources/profile/SysML.profile.uml";
String prefixImageLocation = "platform:/plugin/org.eclipse.papyrus.sysml.edit/icons/full/obj16/";
// open uml file
URI createPlatformPluginURI = URI.createPlatformPluginURI(profileURI, true);
ResourceSetImpl resourceSetImpl = new ResourceSetImpl();
Resource resource = resourceSetImpl.getResource(createPlatformPluginURI, true);
// for each stereotype
TreeIterator<EObject> allContents = resource.getAllContents();
while (allContents.hasNext()) {
EObject eObject = allContents.next();
if (eObject instanceof Stereotype) {
Stereotype stereotype = (Stereotype) eObject;
Map<String, Image> images = getImages(stereotype);
if (images.isEmpty()) {
String name = stereotype.getName();
addImage(stereotype, name, prefixImageLocation+name+".gif","icon","GIF");
System.out.println("Image added for "+ name);
}
}
}
//
OutputStream outputStream = new FileOutputStream("out.uml");
resource.save(outputStream, null);
}
public static Map<String, Image> getImages(Stereotype stereotype) {
Map<String, Image> icons = new HashMap<>();
for (Image image : stereotype.getIcons()) {
EAnnotation eAnnotation = UMLUtil.getEAnnotation(image, IMAGE_PAPYRUS_EA, false);
String string = eAnnotation.getDetails().get(IMAGE_NAME_KEY);
icons.put(string, image);
}
return icons;
}
public static void addImage(Stereotype stereotype, String imageName, String imageLocation, String imageKind, String imageFormat) {
Image icon = stereotype.createIcon(imageLocation);
icon.setFormat(imageFormat);
EAnnotation imageEAnnotation = UMLUtil.createEAnnotation(icon, IMAGE_PAPYRUS_EA);
EMap<String, String> details = imageEAnnotation.getDetails();
details.put(IMAGE_KIND_KEY, imageKind);
details.put(IMAGE_NAME_KEY, imageName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment