Help: Generating instances of different Objects

Hey experts,

I’d like to develop a plugin for VP with Eclipse, therefore I’ve made the tutorial unfortunately i can’t find any documentation on how to generate an instance of a class, use case … .

Is there any documentation of how to generate the different stereotypes,
Which class/method for what.

Thank you :blush:

P.S. or how did you guys find out what to use?

True, the documentation on that front is a bit lacking here and there but rumor has it that Visual Paradigm are busy looking into ways to expand on their documentation and manuals. Of course that’s hear say on my end (treat it as a rumor), but I think 2018 is going to be a very interesting year for Visual Paradigm.

But… back to the issue at hand.

First stop: get the Java documentation. You can access and/or download that here. I strongly suggest that you download the ZIP file and then add it to your IDE so that you can use that.

In all fairness: there isn’t as much explanation in the documentation as I’d hoped for, but it does a good job in giving you a solid overview and a brief description of what methods and properties do.

A good source of information is VP’s Know How site. I suggest that you specifically check out the Open API section because it has some really good articles which explain some of the basic development concepts in good detail.

I’ve made a few plugins myself in the past months and well… I basically went with trial and error combined with the know how section I mentioned above. I spend a lot of time just going over the methods in the API (I’m a NetBeans user myself, and you can expand any assigned libraries to get a good overview of its packages and classes).

It basically boiled down to skimming the library and finding some recognizable aspects for me. For example… When I looked over com.vp.plugin I discovered the ApplicationManager class (basically an abstract class). That stood out for me because the majority of the API consists of interfaces. So when I noticed an instance() method I knew I was onto something.

Maybe this can help… I made 3 plugins so far and eventually combined them into what I like to call the Catslair VP Suite (still thinking of a better name). Click the link to download, it contains all 3 my plugins as well as their source code.

I think you may especially enjoy “PluginReloader” because it does exactly what its name implies: it tells VP to reload the currently loaded plugins. Meaning that if you work on a plugin, make changes and then immediately deploy those changes into your VP Plugin directory then they will become immediately active. Check this thread for more info on that.

Next it contains a plugin which I made for another forum member: it resets the formatting of all the model elements in the currently active diagram. It does this by creating a model element of the same type, then copying all its formatting properties. I think this may also be of interest to you because I’m basically creating model elements.

And finally, being a really big fan of VPository myself I wanted an easier way to open its control panel. So I wrote a plugin for that :wink: If you’re using VPository then this gets saved in your data directory. My plugin checks that information and then uses it to open your default browser with your personal VPository URL.

Hope this can give you some ideas.

And don’t forget: if you have specific questions you can also ask 'm here on this forum :slight_smile:

1 Like

Hi, Tom

First, we should know there are 3 kinds of data in VP’s data structure: ModelElement, DiagramElement, Diagram.
1 Diagram contains N DiagramElements
1 ModelElement can be represented by N DiagramElements in 1 or many Diagram.

Model Element

com.vp.plugin.model.factory.IModelElementFactory can help to create Model Element (com.vp.plugin.model.IModelElement)
For example, to create a Class, you can call:
IModelElementFactory.instance().createClass() : IClass
where, the IClass is sub-class of IModelElement.
'* IModelElementFactory provides many createXXX() functions to create different type of model elements

to create Attribute into your Class, you can call from your IClass
IClass.createAttribute():IAttribute
'* Attribute = Property in UML spec

to move your Class into a Package, you can call
IPackage.addChild(your class)

to create a Generalization (super-class/sub-class relationship), you can call
IModelElementFactory.createGeneralization() : IGeneralization
generalization.setFrom(super-class)
generalization.setTo(sub-class)

to set (add/remove) Stereotype into your Class, you can call:
IClass.addStereotype(“Interface”) // “Interface” is the stereotype to represent the class is an Interface

References

as Peter Looyenga (ShelLuser) said, knowhow pages may help you, such as:

and

Please feel free to ask any question when you find any problem on your plugin. :blush:
e.g. I haven’t explain more details on Diagram & DIagram Element here :sweat_smile:

1 Like

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!

I promised a follow-up and here we go. As usual with these things it’s important to pay attention to details. Creating a stereotype and then assigning (‘adding’) it to my class wasn’t all too difficult, but I overlooked the fact that a stereotype needs a so called basetype.

So:

        IStereotype sType = mef.createStereotype();
        sType.setBaseType("Class");
        sType.setName("ShellType");
        myClass.addStereotype(sType);

And that should do it. Of course you can do a lot more with stereotypes, but I’ll leave that up to you :slight_smile:

@ShelLuser @peter.wong

THANK YOU VERY VERY MUCH! :heart_eyes::heart_eyes::heart_eyes:
It’s really a big help and i’m really happy knowing such good guys around :kissing_heart:

1 Like

I was now able to generate classes, but unfortunately the Stereotypes aren’t as hoped.

I have added several stereotypes to VP e.g. AdobeForms.

It looks different to the common stereotypes(green background), but if I use the Add Stereotype method, it just creates a new Stereotype which is called AdobeForms but looks different.

Now my question is: How can I change my Class to the existing stereotypes
and getting my green Layout :confused:

edit:

Thanks :slight_smile:

Well, as mentioned above I’m new to stereotype usage because I seldomly use those myself, but lets see how far we can take this.

First, just to rule a few possibilities out: how did you add these? For example:

new_stereotype

Does this look familiar? So basically: you open a class specification, then open the stereotype tab. and then use the option “Edit Stereotypes” from which you can create new stereotypes (as shown above).

The reason I ask is because in my example the new stereotype will look exactly the same as the others. This is the list of stereotypes after I’ve created my testcase:

custom_stereotype

As you can see it looks pretty much the same. So is it possible that you did something different here?

In the mean time I’m going to do some testing of my own and I’ll get back to you.

I imported an XML containing those Stereotypes:

Modeling/Configure Stereotypes…/Import/myXML

actually the stereotypes are there, but not as they should look like.

On the Left you can see the generated one and on the Right, how it should look like

grafik

Yeah, now we’re both getting somewhere.

First: you already did it. Things work as expected. You created a class model element and have also successfully applied the stereotype. The only problem you have right now is the formatting; so defining how your class model element should look like.

What happens if you create a new class within Visual Paradigm? For example by dragging it in from the palette? Am I right to assume that you’ll end up with a blue model element (as shown on the left)?

There are 2 ways how you can solve this. First is to define a default formatting within Visual Paradigm, this will ensure that every class model element you create will be using the same format. It’s really easy:

  • Right click on a model element, then select “Styles and Formatting => Formats…” (so: hover over the Styles and Formatting section, then select Formats from the new menu).
  • Select the format you want set default (in the above example that would be the background), then click the “Set as Default” option. Be sure to limit yourself to the class type if you want to use a different color scheme for different model elements.

Example:

formats

The second option, a bit harder, is to apply this formatting manually to your newly created model element.

Note: when I talk about model elements I’m talking about the different components within Visual Paradigm which make up your diagram. But within a plugin these consist of 2 parts which @peter.wong already mentioned above: the model element and its shape.

And a diagram shape roughly defines how the model element should look. Think of the width and height. But sometimes a model element has more properties, such as the fill color mentioned above. That’s when you need to use shape models within the plugin.

Something like this:

// Add the newly created IClass model element to the current diagram
IDiagramElement myClassElement = dm.createDiagramElement(currentClassDiagram, myClass);

if (myClassElement instanceof IShapeUIModel) {
   IShapeUIModel myClassShape = (IShapeUIModel) myClassElement;
   myClassShape.getFillColor().setColor1(Color.orange);
}

The result would be your newly created class model element now getting an orange fill color.

Hope this can give you some ideas… This can be a bit complex, so if something isn’t fully clear then please let me know.

But bottom line: you already solved the problem with creating a class while using a specific stereotype!

In model element level, assigning a stereotype to a model element, the ‘format’ of the stereotype won’t be applied to any diagram element automatically.

However, I am sorry about that, OpenAPI miss to provide API to get the ‘format’ from a stereotype.
Means, you have no way to do it yourself in your plugin. :disappointed_relieved:

I think we should support the following:

IClass.addStereotype(..., isApplyStereotypeFormat:boolean)
IClass.removeStereotype(..., isApplyStereotypeFormat:boolean)

passing true will apply the stereotypes’ format to all diagram elements of the model element.

We will post the news here after we did it, please wait… :grimacing:

1 Like

I’ll try that out, unfortunately there are like 50 of those stereotypes so that would be a lot of code :confused:

I was now able to generate many classes and i’m facing two problems:

  1. The generated Classes are overlaying each other,
    I’ve tried .setLocation( 10, Variable ) with an Variable changing the position, but is there another way, for positioning the classes without setting my own values?

  2. Is kind of the same as 1. Those generated classes have a fixed weight and heigt, i’ve checked https://knowhow.visual-paradigm.com/openapi/class-diagram/
    but there is also used .setShape with fixed Values.
    Is it possible to generate flexible weight and heights, depending on e.g. How long the Class name is or how many attributes/methods it has?
    Like:rightclick/Selection/Fit Size(in the graphic)
    grafik

This is a classic example why I like you guys so much. Always keeping your eyes open for possible ideas for improvement and when you see something which could be done better you usually jump right in! :slight_smile:

In that case you should probably follow up on my first idea: define a default formatting for your class model element and then let your plugin simply create them. After doing that it’ll automatically apply the default formatting.

Not that I’m aware off. It basically mimics the way in which you’d manually create a diagram: you don’t just add model elements and rely on an auto placer but you normally put them into a very specific location.

Yes there is: fitSize() is what you’re looking for I think. This method is usable for instances of the IShapeUIModel which I’ve shown above.

Location

You can try to use 'layout’
you can use com.vp.plugin.DiagramManager (you can get it by com.vp.plugin.ApplicationManager.instance().getDiagramManager())
DiagramManager provides different layout option (such as Hierarchical, BalloonTree, etc…) by its functions createXxxxxLayoutOption() : LayoutOption
and then you can call
DiagramManager.openAndLayoutDiagram(your-diagram, your-layout-option)
to open your diagram with layout its shapes automatically.

Size

The diagram element of classes are sub-class of IShapeUIModel
IShapeUIModel provides following functions to set its size to be ‘default size’ or ‘fit its size’

 setRequestDefaultSize(true)
 setRequestFitSize(true)

PS:
These functions will not resize the shape immediately if the diagram is not opened.
Means, if you try to get size from the shapes before opening the diagram, the returned size is not updated yet.

PS 2:
Oh! Seems we provided duplicated functions: fitSize(), setRequestFitSize() :sweat_smile:

1 Like

Well, better one function too many than getting limited functionality :smile:

Seriously though, I’m not 100% sure but isn’t setRequestFitSize() tied into the format copier function? Together with isRequestFitSize()? A major difference is that setRequestFitSize() also requires a Boolean operator.

I tried those methods before and noticed that they don’t work right away, but only if you have another model element selected. Which makes me believe that although their name implies the same functionality they’re still different, as said: tied into the format copier feature from what I can tell.

And good call on the auto layout feature, I hadn’t thought about that! I’m also going to experiment with that a bit more today.

What is setRequestFitSize()

Since ‘Fit Size’ depends on how is the shape painted.
It may be affected by many factors, such as font, show as box/symbol (archimate’s shape), stereotype (sequence diagram’s lifeline’s Control/Boundary/etc…), etc…

So, the ‘fit size’ is performed after the shape is shown on a diagram (after the diagram is opened, the shape is painted on it. So, can know the size for fitting it).

So, request-FitSize = true means, we want the shape will be fit-size when it is opened.

Is following scenario possible and how :smiley: :

-I start my plugin
-A pop up opens:

In the pop up i can write down two String parameters:
1st The stereotype name
2nd a example class name.

I confirm.

And than my code can use those parameters to generate wished classes

note: I’m working with Visual Paradigm 10.2

thank you :slight_smile:

Edit: since it’s such an import Topic(IDialogs) I opened a new Thread for it.