4
votes

my question: How can I create a new linked document and insert (or connect) it into an element (in my case a Note-Element of an activity diagram).

The Element-Class supports the three Methods:

GetLinkedDocument ()
LoadLinkedDocument (string Filename)
SaveLinkedDocument (string Filename)

I missing a function like CreateLinkedDocument (string Filename)

My goal: I create an activity diagram programmatically and some notes are to big to display it pretty in the activity diagram. So my goal is to put this text into an linked document instead of directly in the activity diagram.

Regards

EDIT

Thank you very much to Uffe for the solution of my problem. Here is my solution code:

public void addLinkedDocumentToElement(Element element, String noteText) {
    String filePath = "C:\\rtfNote.rtf";
    PrintWriter writer;

    //create new file on the disk
    writer = new PrintWriter(filePath, "UTF-8");

    //convert string to ea-rtf format
    String rtfText = repository.GetFormatFromField("RTF", noteText);

    //write content to file
    writer.write(rtfText);
    writer.close();

    //create linked document to element by loading the before created rtf file
    element.LoadLinkedDocument(filePath);
    element.Update();
}

EDIT EDIT

It is also possible to work with a temporary file:

File f = File.createTempFile("rtfdoc", ".rtf");
FileOutputStream fos = new FileOutputStream(f);
String rtfText = repository.GetFormatFromField("RTF", noteText);
fos.write(rtfText.getBytes());
fos.flush();
fos.close();
element.LoadLinkedDocument(f.getAbsolutePath());
element.Update();
1

1 Answers

3
votes

First up, let's separate the linked document, which is stored in the EA project and displayed in EA's built-in RTF viewer, from an RTF file, which is stored on disk.

Element.LoadLinkedDocument() is the only way to create a linked document. It reads an RTF file and stores its contents as the element's linked document. An element can only have one linked document, and I think it is overwritten if the method is called again but I'm not absolutely sure (you could get an error instead, but the EA API tends not to work that way).

In order to specify the contents of your linked document, you must create the file and then load it. The only other way would be to go hacking around in EA's internal and undocumented database, which people sometimes do but which I strongly advise against.

In .NET you can create RTF documents using Microsoft's Word API, but to my knowledge there is no corresponding API for Java. A quick search turns up jRTF, an open-source RTF library for Java. I haven't tested it but it looks as if it'll do the trick.

You can also use EA's API to create RTF data. You would then create your intended content in EA's internal display format and use Repository.GetFormatFromField() to convert it to RTF, which you would then save in the file.

If you need to, you can use Repository.GetFieldFromFormat() to convert plain-text or HTML-formatted text to EA's internal format.