Help: Generating instances of different Objects

I hope you don’t mind me trying to combine the threads a bit together, but this will make it much easier for everyone to follow. Not just us, but also any other users who might be interested in all this.

First it’s important to know about the basic elements of a plugin. First you have the main class which implements the VPPlugin interface. This class controls the actions taken when the plugin is loaded and unloaded.

Next you have the so called “action controller class”, this is basically the heart of your plugin. It contains all the things which you want your plugin to do. You create this by implementing the VPActionController interface.

To actually make your plugin ‘do’ something you’ll need to override the performAction() method and put your code in there.

(I know you did the tutorial and all that, I just like to be thorough. It also helps that I’m really enjoying this; I think that plugin support is one of Visual Paradigm’s best features :sunglasses: )

So… lets start by creating a new class model. We are going to ‘cheat’ a little bit by making sure that the currently active diagram is actually a Class diagram (makes it easier on me), so all I have to do is get the current diagram and make sure that VP knows its a Class diagram:

DiagramManager dm = ApplicationManager.instance().getDiagramManager();
IDiagramUIModel currentDiagram = dm.getActiveDiagram();
IClassDiagramUIModel currentClassDiagram = (IClassDiagramUIModel) currentDiagram;

So now that we have the diagram let’s add an actual model element.

First you need to know that there are 2 ‘parts’ about a model element. It has properties and it has a shape. And Visual Paradigm treats (and creates) those in a different way.

Let’s start with the properties, or the actual model element itself.

A common used approach in Java is the use of so called factory classes. For example… If you want to create a new SSL (“X509”) certificate then you’d use a so called CertificateFactory.

Visual Paradigm fully adapts this workflow and as such we have the IModelElementFactory at our disposal. This is another example of skimming the API documentation and/or library. I mean… com.vp.plugin.model.factory does stand out a bit.

SO lets use this:

IModelElementFactory mef = IModelElementFactory.instance();
IClass myClass = mef.createClass();
myClass.setName("ShellClass");

Now that I have created the actual class element we now need to create its shape and add that to our class diagram. To do that I’m going to need the so called Diagram Manager which I’ve created earlier:

IDiagramElement myClassShape = dm.createDiagramElement(currentClassDiagram, myClass);

This basically “connects” our diagram element with our diagram, and by doing so creates the shape which visualizes the actual class.

So let me summarize what I got so far:

    public void performAction(VPAction arg0) {
        DiagramManager dm = ApplicationManager.instance().getDiagramManager();
       
        IDiagramUIModel currentDiagram = dm.getActiveDiagram();
        IClassDiagramUIModel currentClassDiagram = (IClassDiagramUIModel) currentDiagram;

        IModelElementFactory mef = IModelElementFactory.instance();
        IClass myClass = mef.createClass();
        myClass.setName("ShellClass");
        
        IDiagramElement myClassShape = dm.createDiagramElement(currentClassDiagram, myClass);
    }

Of course one problem still remains… What about the stereotype?

Well, this is where my current expertise unfortunately ends (but I may follow up). Even so, remember that Model Element factory? Guess what? createStereotype() is a thing. :sunglasses:

And IClass also has an addStereotype() method.

However… as mentioned, this is something I’d need to sort out myself first. I gave it a try and that didn’t work out too well. So I’m going to get back to you.

But the result so far:

ClassDiagram

Hope that this can also help!