4

I am looking for APIs that can draw UML Class diagrams and present them in a JPanel (or any other suitable UI entity) for a window application. It has to be embedded within the application, so I am not looking for some standalone tool that can generate UMLs based on java files or some plugin. I need actual jars that can be implemented for creating class diagrams so I can use them in a window application. I've looked into several but all of the sources that I am finding are either standalone programs or cannot be implemented within an application and need to take the user's focus away from the app. I am using NetBeans IDE, but I also have Eclipse installed.

SOLVED:

I used PlantUML API. I manually input a string in accordance with the PlantUML input language syntax and then used a simple and straightforward generateImage method to populate a byte array which I then converted into an image and saved it to my desktop. This fits what I wanted because it keeps the user focused on my application and mine alone. Alternatively, one can produce the buffered image on a window or something. The PlantUML API needs to be imported to the application package. This code creates an image in my desktop (don't forget to change the directory path) with a UML class image for the class, Person:

public class PaintUML {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) throws IOException, InterruptedException {
    // TODO code application logic here
    ByteArrayOutputStream bous = new ByteArrayOutputStream();
    String source = "@startuml\n";
    source += "class Person {\n";
    source += "String name\n";
    source += "int age\n";
    source += "int money\n";
    source += "String getName()\n";
    source += "void setName(String name)\n";
    source += "}\n";
    source += "@enduml\n";

    SourceStringReader reader = new SourceStringReader(source);
    // Write the first image to "png"
    String desc = reader.generateImage(bous);
    // Return a null string if no generation
    byte [] data = bous.toByteArray();

    InputStream in = new ByteArrayInputStream(data);
    BufferedImage convImg = ImageIO.read(in);

    ImageIO.write(convImg, "png", new File("C:\\Users\\Aaron\\Desktop\\image.png"));

    System.out.print(desc);
}
}
Wolfgang Fahl
  • 15,016
  • 11
  • 93
  • 186
user1028408
  • 1,220
  • 3
  • 15
  • 17

2 Answers2

3

Have you seen PlantUML?

http://plantuml.sourceforge.net

It's open source so you might be able to pick some bits to suit.

Paolo
  • 22,188
  • 6
  • 42
  • 49
1

Have a look at the Eclipse UML2 API and Eclipse Papyrus. They should provide the functionality what your are searching for. For drawing support in JPanels, you might need to do some extra work.

The Eclipse UML2 API provides a Java interface for UML2 meta model. Papyrus is a set of components allowing to build diagrams and graphical editors for UML models.

SpaceTrucker
  • 13,377
  • 6
  • 60
  • 99
  • I'm more interested in the drawing support than anything else. I've tried using SVGSalamander but the when I tried the example program it didn't work for me. I posted a separate question about it but nobody helped me yet: http://stackoverflow.com/questions/14936597/producing-an-svg-diagram-on-java I would much rather manage to use SVGSalamander as part of an application rather than have to rely on an IDE tool like Papyrus. The app has to be installable and usable without dependence on Eclipse/NetBeans or other separate applications. – user1028408 Feb 19 '13 at 11:31
  • Having a diagram editor might be a rather complex task. So I recommend to build upon something preexisting. You are not forced to build an rcp application to use these components. You can also use them embedded in the background of your swing app or whatever. But you would be forced to use osgi. – SpaceTrucker Feb 19 '13 at 12:18
  • I want that whatever I end up using can be called with using Java code. I want the UML class diagram to be generated as the last step of a series of other procedures in a Java Program. The important thing is that the user would not have to manually do anything to get this result. Do you think it is possible to embed something like PaintUML with osgi? – user1028408 Feb 19 '13 at 12:35