file [Solved] Creating a new component

2 years 1 week ago - 2 years 1 day ago #4299 by kurales
Hi,

I'm trying to create some element from Palette. This should be done not directly but through an additional window, in which I choose the type of this element. So I have created this particular code, but it is not working: nothing happens, there is no new element and no error.

In the method actionPerformed I call a function "open" to show the window:
public void actionPerformed(IDiagramHandle diagramHandle, IDiagramGraphic graphic,
			Rectangle rect) {
             open(diagramHandle, graphic, rect);
	        }

In the function "open" I show types to choose and then call for example a method to create one Element:
public void open(IDiagramHandle diagramHandle, IDiagramGraphic graphic, Rectangle rect) {
		   Shell shell = new Shell();

			...

                        Button bt1 = new Button(shell, SWT.RADIO);
			bt1.setText("Component");
			bt1.setSelection(true);

		    Button okButton = new Button(shell, SWT.PUSH);
		    okButton.setText("OK");

			okButton.addSelectionListener(new SelectionAdapter() {
			   	 public void widgetSelected(SelectionEvent event) {

			   	 if(bt1.getSelection() == true) {
				   		createBox(diagramHandle, graphic, rect, 0);
				   		shell.dispose();
				   	 	};
			   	 }
			   	});
			shell.open ();
	   }

CreateBox-method:
public boolean createBox(IDiagramHandle diagramHandle, IDiagramGraphic graphic,
			Rectangle rect, Integer m) {

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

	        	ModelElement parent = null;
	            
	            if (graphic instanceof IDiagramDG) {
	                parent = diagramHandle.getDiagram().getOrigin();
	            } else {
	                parent = (ModelElement) graphic.getElement();
	            }
	        
	            String name = getParameter("name");
                if (name == null) {
                    name = getLabel();
                }
                name = getModule().getModuleContext().getI18nSupport().getString(name);

	                String stereotype = getParameter("stereotype");
	                Stereotype ster = session.getMetamodelExtensions().getStereotype(
                 		stereotype, Metamodel.getMClass("Component"));

	                Component createdElement = modelFactory.createComponent("Component",
	        				(NameSpace) parent, ster);
	                
	                modelFactory.getDefaultNameService().setDefaultName((ModelElement) createdElement, name);

	                List<IDiagramGraphic> graph = diagramHandle.unmask(createdElement, rect.x+m, rect.y);

                    if ((graph != null) && (graph.size() > 0) && (graph.get(0) instanceof IDiagramNode)){
                        IDiagramNode dnode = (IDiagramNode)graph.get(0);                    
                        dnode.setProperty("SHOWTAGS", "TRUE");
                    }
	        		 diagramHandle.save();
	        		 tr.commit();
	        		 return true;
        } catch (RuntimeException e) {
            getModule().getModuleContext().getLogService().error(e);
            return false;
        }
}

When I call createBox-method from actionPerformed, everything is fine.

I can't understand what is the reason and what I should do. Can somebody help me?

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

2 years 4 days ago #4307 by kurales
The problem is inside of this section:
okButton.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
...

If I call my createBox-function there, the diagramHandler is undefined or something like this. If I initialize the handler like this:
for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
if (rootPackage.equals(diagram.getOrigin())) {
IDiagramHandle diagramHandle = diagramServices.getDiagramHandle(diagram);
the code works fine.

So if this is only one solution, how can I get exatly the diagram, that I need (on which I am currently working)?

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

2 years 1 day ago - 2 years 1 day ago #4317 by kurales
I have found the solution: I need to initialize diagramHandler inside of this section again
public Component createBox(IDiagramGraphic graphic, Rectangle rect) {
IModuleContext ctx = getModule().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();
            
            for (StaticDiagram diagram : session.findByClass(StaticDiagram.class)) {
     	          if (rootPackage.equals(diagram.getOrigin()) && graphic.getName() == diagram.getName()) {
     	        	  
     	        	 IDiagramHandle diagramHandle = diagramServices.getDiagramHandle(diagram);

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

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