Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Sirius » Using an operation to change the style of elements in my model using an action call/operation
Using an operation to change the style of elements in my model using an action call/operation [message #1841566] Thu, 20 May 2021 19:51 Go to next message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
Hi,

I have an operation that is defined in my metamodel that is accessed via java action call from a popup menu in my model. What I want to do is to be able to change the border of the elements that are returned from my operation. Not sure if it would be better to be done in the java action or the operation, or for that matter how to actually change the style of the elements using java. Any help or suggestions?

[Updated on: Fri, 21 May 2021 13:24]

Report message to a moderator

Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841660 is a reply to message #1841566] Tue, 25 May 2021 07:40 Go to previous messageGo to next message
Laurent Redor is currently offline Laurent RedorFriend
Messages: 296
Registered: July 2009
Senior Member
Hi,
You can use the java action. Here is an example of code to change the border of a container:
org.eclipse.sirius.diagram.DDiagramElementContainer myContainer = AAA;
org.eclipse.sirius.diagram.ContainerStyle containerStyle = myContainer.getOwnedStyle();

org.eclipse.sirius.diagram.LineStyle newBorderLineStyle = LineStyle.BBB;
int newBorderSize = CCC;


containerStyle.setBorderSize(newBorderSize);
containerStyle.setBorderLineStyle(newBorderLineStyle);
// You hava to add the changed features to the "custom features" list to avoid that your changes are "deleted" at the next refresh.
containerStyle.getCustomFeatures().add(DiagramPackage.eINSTANCE.getBorderedStyle_BorderSize().getName());
containerStyle.getCustomFeatures().add(DiagramPackage.eINSTANCE.getBorderedStyle_BorderLineStyle().getName());


Regards,


Laurent Redor - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841686 is a reply to message #1841660] Tue, 25 May 2021 17:10 Go to previous messageGo to next message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
For an element that isn't a container what is the command instead of getOwnedStyle()?

Also where can I find the whole example, it looks like there is no declaration of BBB or LineStyle so the line:
org.eclipse.sirius.diagram.LineStyle newBorderLineStyle = LineStyle.BBB;

Spits out an error of undeclared variables.

One more thing to note is that I run into this error whenever I try to declare a variable as type DDiagramElement*:
Caused by: java.lang.ClassCastException: class wFP_V2.impl.Data_ElementImpl cannot be cast to class org.eclipse.sirius.diagram.DDiagramElement (wFP_V2.impl.Data_ElementImpl is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @70161b7c; org.eclipse.sirius.diagram.DDiagramElement is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1478f7ec)


I have everything imported correctly I think so I'm not sure how to resolve this class error.
The full error message that I receive is:
!ENTRY org.eclipse.gmf.runtime.diagram.ui 4 4 2021-05-25 14:03:46.150
!MESSAGE execute
!STACK 0
org.eclipse.core.commands.ExecutionException: While executing the operation, an exception occurred
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:504)
	at org.eclipse.sirius.diagram.ui.tools.internal.editor.DDiagramCommandStack.execute(DDiagramCommandStack.java:73)
	at org.eclipse.sirius.diagram.ui.tools.internal.editor.DDiagramCommandStack.execute(DDiagramCommandStack.java:54)
	at org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack.execute(DiagramCommandStack.java:157)
	at org.eclipse.sirius.diagram.ui.tools.internal.menu.PopupMenuContribution$1.run(PopupMenuContribution.java:423)
	at org.eclipse.jface.action.Action.runWithEvent(Action.java:474)
	at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:580)
	at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:414)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
	at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1037)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
	at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
	at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:564)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1461)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1434)
Caused by: java.lang.ClassCastException: class wFP_V2.impl.Data_ElementImpl cannot be cast to class org.eclipse.sirius.diagram.DDiagramElement (wFP_V2.impl.Data_ElementImpl is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @70161b7c; org.eclipse.sirius.diagram.DDiagramElement is in unnamed module of loader org.eclipse.osgi.internal.loader.EquinoxClassLoader @1478f7ec)
	at WFP.design.myAction.execute(myAction.java:64)
	at org.eclipse.sirius.tools.api.command.view.JavaActionFromToolCommand.doExecute(JavaActionFromToolCommand.java:95)
	at org.eclipse.emf.transaction.RecordingCommand.execute(RecordingCommand.java:135)
	at org.eclipse.emf.common.command.CompoundCommand.execute(CompoundCommand.java:261)
	at org.eclipse.sirius.diagram.ui.tools.api.command.GMFCommandWrapper.doExecuteWithResult(GMFCommandWrapper.java:104)
	at org.eclipse.gmf.runtime.emf.commands.core.command.AbstractTransactionalCommand.doExecute(AbstractTransactionalCommand.java:248)
	at org.eclipse.emf.workspace.AbstractEMFOperation.execute(AbstractEMFOperation.java:150)
	at org.eclipse.sirius.diagram.ui.tools.internal.commands.WrappingCommandIgnoringAffectedFiles.execute(WrappingCommandIgnoringAffectedFiles.java:125)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:496)

[Updated on: Tue, 25 May 2021 18:08]

Report message to a moderator

Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841694 is a reply to message #1841660] Tue, 25 May 2021 20:03 Go to previous messageGo to next message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
Also if it helps here is my code where I am trying to type cast from one of my defined types to the DDiagramElement type:

public EList forwardImpactAnalysis() {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		EList<? extends Input> inputs = getInput();
		Iterator<? extends Input> itInput = inputs.iterator();
		while (itInput.hasNext()) {
			Input nextInput = itInput.next();
			EList<? extends Output> outputs = nextInput.getTgt().getOutput();
			Iterator<? extends Output> itOutput = outputs.iterator();
			EList<Data_Element> dataOutputs = new BasicEList();
			while (itOutput.hasNext()) {
				Output nextOutput = itOutput.next();
				dataOutputs.add((Data_Element) nextOutput.getTgt());
				DDiagramElement myElement = (DDiagramElement) nextOutput;
				System.out.println(myElement);
			}
			return (EList)dataOutputs;
		}
		//throw new UnsupportedOperationException();
		EList empty = new BasicEList();
		return empty;

	}


The print statements are just there to help with debugging and understanding how a lot of these commands work and what their outputs are.

[Updated on: Tue, 25 May 2021 20:04]

Report message to a moderator

Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841702 is a reply to message #1841694] Wed, 26 May 2021 07:05 Go to previous messageGo to next message
Laurent Redor is currently offline Laurent RedorFriend
Messages: 296
Registered: July 2009
Senior Member
Hi,

As the javadoc says "ClassCastException is thrown to indicate that the code has attempted to cast an object to a subclass of which it is not an instance.". Here you try to cast a wFP_V2.impl.Data_ElementImpl (a class of your metamodel I suppose) into a org.eclipse.sirius.diagram.DDiagramElement. It can't work, there is no magic. To retrieve a DDiagramElement from a semantic element, you can see how to do it in the Cookbook.

I extract this piece of code from the test org.eclipse.sirius.tests.unit.diagram.compartment.CompartmentsLayoutTest.

And indeed, AAA, BBB, CCC do not exist. You have to replace them by the values you want...

Regards,


Laurent Redor - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841722 is a reply to message #1841702] Wed, 26 May 2021 14:24 Go to previous messageGo to next message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
OH well that was dumb of me, should have realized that!

Also thanks for the cookbook, I'll see if I can cook something up that can work for me!
Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841753 is a reply to message #1841702] Thu, 27 May 2021 14:52 Go to previous messageGo to next message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
I do have a follow-up question with regards to the line from the cookbook:
Collection<EObject> result = new EObjectQuery(mySemanticElement).getInverseReferences(ViewpointPackage.Literals.DSEMANTIC_DECORATOR__TARGET);


This line does return a collection of the DDiagramElements that I want, however it seems to return the same element multiple times. I switched it to a Set instead of a Collection and it still returns the same element multiple times and I'm not sure why or how. My diagram only has one element that it should be returning so I don't understand why this is happening.
Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841785 is a reply to message #1841753] Fri, 28 May 2021 15:38 Go to previous messageGo to next message
Laurent Redor is currently offline Laurent RedorFriend
Messages: 296
Registered: July 2009
Senior Member
The inverse references returned is for all the session; so for all diagrams. Maybe your semantic element is represented in several diagrams. But even with that, the DDiagramElement should not be the same.

I don't really understand.


Laurent Redor - Obeo

Need training or professional services for Sirius?
http://www.obeodesigner.com/sirius
Re: Using an operation to change the style of elements in my model using an action call/operation [message #1841904 is a reply to message #1841785] Wed, 02 June 2021 16:59 Go to previous message
Thomas Chiang is currently offline Thomas ChiangFriend
Messages: 83
Registered: March 2020
Member
I figured out a workaround for the issue of there being multiples of the same element within a set. Still not sure why it was happening but it's working well enough now.

I do have another follow-up question now however. For my editor I can create multiple viewpoints of the same semantic model. All of these viewpoints are unsynchronized so that the user can then drag and drop the specific elements that they want to use, edit, and manipulate without having to navigate the whole model. My issue now is when I try to change the border color of the nodes. Since the viewpoints are all unsynchronized I assumed that the border changes would happen locally within the viewpoint where the method is being called. But for whatever reason when the method is called it changes the color randomly throughout all the viewpoints and sometimes won't change the color at all. And then when I try a containerStyle.getBordercolor() returns that the RGB values have been changed despite not showing up in my model. Any idea about how to resolve this to get the changes to happen locally to the viewpoint and not randomly throughout the model?

This is the code that I came up with to change the color recursively throughout my model based on the element that I want:
public void forwardImpactAnalysis() {
		// TODO: implement this method
		// Ensure that you remove @generated or mark it @generated NOT
		EList<? extends Input> inputs = this.getInput();
		Iterator<? extends Input> itInput = inputs.iterator();
		while (itInput.hasNext()) {
			Input nextInput = itInput.next();
			EList<? extends Output> outputs = nextInput.getTgt().getOutput();
			Iterator<? extends Output> itOutput = outputs.iterator();
			while (itOutput.hasNext()) {
				Data dataOutput = (Data) itOutput.next().getTgt();
				//This line will return the same element that is put in, but instead of as an ecore class it will be of a sirius class that will allow for the manipulation of the concrete syntax in the model instance.
				Set<? extends DSemanticDecorator> result = (Set<? extends DSemanticDecorator>) new EObjectQuery(
						dataOutput).getInverseReferences(ViewpointPackage.Literals.DSEMANTIC_DECORATOR__TARGET);
				DDiagramElementContainer myContainer = (DDiagramElementContainer) result.iterator().next();
				ContainerStyle containerStyle = myContainer.getOwnedStyle();
				RGBValues newBorderColor = RGBValues.create(255, 0, 0);
				containerStyle.setBorderColor(newBorderColor);
				containerStyle.getCustomFeatures().add(DiagramPackage.Literals.BORDERED_STYLE__BORDER_COLOR.getName());
				dataOutput.forwardImpactAnalysis();
			}

		}
		//throw new UnsupportedOperationException();
	}
Previous Topic:How to set table cell values programmatically?
Next Topic:[ANN] Sirius 6.5.0
Goto Forum:
  


Current Time: Sun Sep 25 21:56:05 GMT 2022

Powered by FUDForum. Page generated in 0.02472 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top