file [Solved] Copy of Element through command

1 year 2 days ago - 1 year 2 days ago #4306 by kurales
It works, but there is the following problem: when there is only one diagram in the project, then is all ok, but when I create another one, the new element does not appear. This occurs in both diagrams. If I remove one of them, then it works fine again.My code of the command:
public boolean createBox(Component el, IModule module) {
		
		IModuleContext ctx = module.getModuleContext();
		IModelingSession session = ctx.getModelingSession();
		IDiagramService diagramServices = ctx.getModelioServices().getDiagramService();
		MObject root = session.getModel().getModelRoots().get(0);
		Package rootPackage = ((Project) root).getModel();

        try (ITransaction tr = session.createTransaction("Create box")) {
            IUmlModel modelFactory = session.getModel();

	        String name = el.getName();
                name = module.getModuleContext().getI18nSupport().getString(name);
            
            
            List<String> stereotypeNames = new ArrayList<>();
             for (Stereotype s : el.getExtension()) {
               stereotypeNames.add(s.getName());
             }

             Stereotype stereotype;
             stereotype = session.getMetamodelExtensions().getStereotype(stereotypeNames.get(0),Metamodel.getMClass("Component"));

	     Component createdElement = modelFactory.createComponent("Component", rootPackage, stereotype);
	      
	     modelFactory.getDefaultNameService().setDefaultName((ModelElement) createdElement, name);
	         
	         for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
 	          if (rootPackage.equals(diagram.getOrigin())) {
 	               
 	               IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);
 	               IDiagramNode dg = (IDiagramNode) dh.getDiagramGraphics(el).get(0); 

 	                List<IDiagramGraphic> graph = dh.unmask(createdElement, dg.getBounds().x+200, dg.getBounds().y);
 	                IDiagramNode dnode = (IDiagramNode)graph.get(0);                    
                        dnode.setProperty("SHOWTAGS", "TRUE");

 	                    dh.save();
 	                    dh.close();
 	                }
 	          }
	         
           tr.commit();
	   return true;
        } catch (RuntimeException e) {
           module.getModuleContext().getLogService().error(e);.getModuleContext().getLogService().error(e);
           return false;
        }
	
}

I think there is some trouble in thise section of code:
for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
 	          if (rootPackage.equals(diagram.getOrigin())) {
 	               IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);

But how can i get the right diagram in this case?

Please Log in or Create an account to join the conversation.

11 months 4 weeks ago #4309 by pan
Just to be sure, each diagram contains the Component "el"?
On this line
IDiagramNode dg = (IDiagramNode) dh.getDiagramGraphics(el).get(0);
If el is not represented in the diagram it could lead to an error.

Please Log in or Create an account to join the conversation.

11 months 4 weeks ago #4310 by kurales
Hi pan,

no, I would like to copy the element inside of one diagram and want to know, which diagram ist it. So it must be checked, that the element, that I have choosed, belongs to one diagram, in which I perform the copy-operation. Then can I get the handler with this diagram in the statement IDiagramHandle dh = diagramServices.getDiagramHandle(diagram);

Please Log in or Create an account to join the conversation.

11 months 4 weeks ago #4311 by pan
OK. There is 2 ways to do it.

1 - You can do:
dh.getDiagramGraphics(el)
but be careful if you do dh.getDiagramGraphics(el).get(0) it could lead to an error if dh.getDiagramGraphics(el) returns an empty list.

2 - From the element itself you can retrieve the diagrams in which it is shown by using:
element.getDiagramElement()
View the Element javadoc
The following user(s) said Thank You: kurales

Please Log in or Create an account to join the conversation.

11 months 3 weeks ago #4313 by kurales
The second way ist what I needed. Thank you!

Please Log in or Create an account to join the conversation.

Moderators: tmachmaab
Time to create page: 0.035 seconds
^ Back to Top