Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » Papyrus » Change color of UML element
Change color of UML element [message #1374802] Thu, 22 May 2014 20:18 Go to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Hi everyone,

I can find and access a UML element in Model Explorer and Editor, using ID (from "XML schema") to find it.

ModelSet papyrusModelSet = registry.getService(ModelSet.class);
UmlModel umlModel = (UmlModel)papyrusModelSet.getModel(UmlModel.MODEL_ID);
EObject modelRoot = umlModel.lookupRoot();
			
Resource umlResource = modelRoot.eResource(); 
			
EObject umlElement = umlResource.getEObject(elementID.trim());


This UML element can be a State, a Transition, a Parameter, a Message...

In the case of State, the umlElement is a EObject/StateImpl. I know that I need to change the color in Shape#setFillColor(), but how I go from EObject/StateImpl to Shape/ShapeImpl?

I'm using Papyrus 1.0 in Eclipse Luna.

thank you

[Updated on: Mon, 14 July 2014 23:43]

Report message to a moderator

Re: Change color of UML element [message #1400943 is a reply to message #1374802] Mon, 14 July 2014 23:43 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Anyone?
Re: Change color of UML element [message #1401270 is a reply to message #1374802] Tue, 15 July 2014 11:22 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. DamusFriend
Messages: 1128
Registered: July 2009
Location: Canada
Senior Member

Hi,

To get the View that visualizes your model element in a particular
diagram (this view may be a Shape or Edge of some kind), you can use
the org.eclipse.papyrus.uml.diagram.common.util.CrossReferenceUtil API.

Once you have the view, you can set colours and other visual attributes
in the notation model styles, yes, but you can also use CSS to specify
the same visual attributes. With a CSS stylesheet you can apply these
attributes more broadly for a diagram or all of the diagrams in a
project, using selectors to apply them automatically to views of
elements matching some criteria. See the on-line help for details.

HTH,

Christian

On 2014-05-22 20:18:35 +0000, Vinicius Pereira said:

> Hi everyone,
>
> I can find and access a UML element in Model Explorer and Editor, using
> ID (from "XML schema") to find it.
>
>
> ModelSet papyrusModelSet = registry.getService(ModelSet.class);
> UmlModel umlModel = (UmlModel)papyrusModelSet.getModel(UmlModel.MODEL_ID);
> EObject modelRoot = umlModel.lookupRoot();
>
> Resource umlResource = modelRoot.eResource();
> EObject umlElement = umlResource.getEObject(elementID.trim());
>
>
> This UML element can be a State, a Transition, a Parameter, a Message...
>
> In the case of State, the umlElement is a EObject/StateImpl. I know
> that I need to change the color in Shape#setFillColor(), but how I go
> from EObject/StateImpl to Shape/ShapeImpl?
>
>
> Another thing: for State I need Shape, for Transition I need Connector
> and Message I need Edge... but why I cannot change edge's color?
>
> I'm using Papyrus 1.0 in Eclipse Luna M7.
>
> thank you
Re: Change color of UML element [message #1402158 is a reply to message #1401270] Wed, 16 July 2014 15:58 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Christian W. Damus wrote on Tue, 15 July 2014 07:22
Hi,

To get the View that visualizes your model element in a particular
diagram (this view may be a Shape or Edge of some kind), you can use
the org.eclipse.papyrus.uml.diagram.common.util.CrossReferenceUtil API.


Thanks for the tip! I will try.

Christian W. Damus wrote on Tue, 15 July 2014 07:22

Once you have the view, you can set colours and other visual attributes
in the notation model styles, yes, but you can also use CSS to specify
the same visual attributes. With a CSS stylesheet you can apply these
attributes more broadly for a diagram or all of the diagrams in a
project, using selectors to apply them automatically to views of
elements matching some criteria. See the on-line help for details.


For the CSS I saw that I cannot apply styles for each element. For example, State A with Blue and State B with Green.

But if I manage to get to Shape and use setFillColor() I will be happy Smile

Thanks
Re: Change color of UML element [message #1402201 is a reply to message #1402158] Wed, 16 July 2014 17:25 Go to previous messageGo to next message
Christian W. Damus is currently offline Christian W. DamusFriend
Messages: 1128
Registered: July 2009
Location: Canada
Senior Member

Hi,

I suppose it depends on what the colours and other styling
characteristics are intended to signify. If they are meant to indicate
some kind of abstract properties of your model elements, then likely
you can devise selector criteria in your CSS rules based on the
attributes of those elements (including stereotypes applied to them):
the selector language pretty much has full access to the UML metamodel
(and applied profiles).

If it's just a matter of colour-coding specific elements using
arbitrary, then yes, the basic GMF notation styles should be sufficient.

Cheers,

Christian

On 2014-07-16 15:58:21 +0000, Vinicius Pereira said:

> Christian W. Damus wrote on Tue, 15 July 2014 07:22
>> Hi,
>>
>> To get the View that visualizes your model element in a particular
>> diagram (this view may be a Shape or Edge of some kind), you can use
>> the org.eclipse.papyrus.uml.diagram.common.util.CrossReferenceUtil API.
>
>
> Thanks for the tip! I will try.
>
> Christian W. Damus wrote on Tue, 15 July 2014 07:22
>> Once you have the view, you can set colours and other visual attributes
>> in the notation model styles, yes, but you can also use CSS to specify
>> the same visual attributes. With a CSS stylesheet you can apply these
>> attributes more broadly for a diagram or all of the diagrams in a
>> project, using selectors to apply them automatically to views of
>> elements matching some criteria. See the on-line help for details.
>
>
> For the CSS I saw that I cannot apply styles for each element. For
> example, State A with Blue and State B with Green.
>
> But if I manage to get to Shape and use setFillColor() I will be happy :)
>
> Thanks
Re: Change color of UML element [message #1403222 is a reply to message #1402201] Tue, 22 July 2014 12:56 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Christian W. Damus wrote on Wed, 16 July 2014 13:25
Hi,

I suppose it depends on what the colours and other styling
characteristics are intended to signify. If they are meant to indicate
some kind of abstract properties of your model elements, then likely
you can devise selector criteria in your CSS rules based on the
attributes of those elements (including stereotypes applied to them):
the selector language pretty much has full access to the UML metamodel
(and applied profiles).

If it's just a matter of colour-coding specific elements using
arbitrary, then yes, the basic GMF notation styles should be sufficient.

Cheers,

Christian


I would like to use colors to show something like "right now (in this moment) I'm in State A... now (next moment) I'm in State B". Just to be better visualize by the user.
Re: Change color of UML element [message #1403298 is a reply to message #1403222] Tue, 22 July 2014 17:39 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
And thanks Christian!! I changed colors in Shape format Very Happy
Re: Change color of UML element [message #1714961 is a reply to message #1403298] Wed, 18 November 2015 02:06 Go to previous messageGo to next message
Da Mist is currently offline Da MistFriend
Messages: 9
Registered: November 2015
Junior Member
Hi Vinicius,

Could you please provide some information that how did you change the color of a shape? I am also trying to change the color of a shape, but couldn't manage to do it yet.

Thanks
Re: Change color of UML element [message #1714989 is a reply to message #1714961] Wed, 18 November 2015 11:00 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Hi,

To change the Shape color you need to access its correspondent CSSShape object and use the setFillColor() method.

And you need to do it inside a TED, like this:

TransactionalEditingDomain ted = TransactionUtil.getEditingDomain(cssShapeImpl);
ted.getCommandStack().execute(new RecordingCommand(ted) {
			
	@Override
	protected void doExecute() {
		cssShapeImpl.setFillColor(myNewColor);
	}
});
Re: Change color of UML element [message #1730458 is a reply to message #1714989] Mon, 25 April 2016 23:53 Go to previous messageGo to next message
Da Mist is currently offline Da MistFriend
Messages: 9
Registered: November 2015
Junior Member
Hi Vinicius,

Could you please tell me, how to get CSSShapeImpl without selecting any object in the editor?
I got the vertex of the region of a particular shatemachine by using region.getSubvertex(stateName).
This is giving me a org.eclipse.uml2.uml type vertex.
But I am not able to convert it CSSShapeImpl.
Could you please help me in this regard?

Thanks!

[Updated on: Mon, 25 April 2016 23:54]

Report message to a moderator

Re: Change color of UML element [message #1730459 is a reply to message #1730458] Tue, 26 April 2016 00:13 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Hi Da Mist,

First I got the EObject and its View.
Then if EObject instance is a instanceof State (org.eclipse.uml2.uml.State) I verified if the View is a instanceof CSSShapeImpl.

If both are correct, then I cast the View:
CSSShapeImpl cssShapeImpl = (CSSShapeImpl)view;


I don't know if it will help you with the Vertex, but I did the same logic with Shape (CSSShapeImpl), Edge and Connector.


Cya
Re: Change color of UML element [message #1730467 is a reply to message #1730459] Tue, 26 April 2016 05:31 Go to previous messageGo to next message
Da Mist is currently offline Da MistFriend
Messages: 9
Registered: November 2015
Junior Member
Hi Vinicius,

Thank you so much for your reply.
Right now I am able to get EObject. But I am not getting the view.
What is the type of the view? And how to get it from EObject?

Here is how I have tried to do it:

State st = (State) region.getSubvertex(stateName);
EObject eobj = st;
if(eobj instanceof State) {
   View view = (View)((State) eobj).getModel();
  if(view instanceof CSSShapeImpl) {
             CSSShapeImpl spI = (CSSShapeImpl) view;
            TransactionalEditingDomain domain = TransactionUtil.getEditingDomain(spI);
             domain.getCommandStack().execute(new RecordingCommand(domain) {
			@Override
			protected void doExecute() {
			// TODO Auto-generated method stub
			spI.setFillColor(432718);
		}
		});
  }


But it is giving this error:
org.eclipse.uml2.uml.internal.impl.ModelImpl cannot be cast to org.eclipse.gmf.runtime.notation.View


Please help me!

Thanks!

[Updated on: Tue, 26 April 2016 05:59]

Report message to a moderator

Re: Change color of UML element [message #1730499 is a reply to message #1730467] Tue, 26 April 2016 11:36 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Hi Da Mist,

I got the View from here:

// possible referencing Objects - In my case should be just ONE...
Set<View> referencingObjects = new HashSet<View>();
		
// getCrossReferencingViews returns a Set of Views which contains the element found -- elementFound is my EObject
referencingObjects = CrossReferencerUtil.getCrossReferencingViews(elementFound, null);



CrossReferencerUtil is a static class from Papyrus. I don't remember why the second parameter is null...

Then I did a for each in the referencingObjects (in my case it has only one element):

for (View view : referencingObjects)


Now I have one EObject and one View. I don't know if it is the best approach, but it works.
Re: Change color of UML element [message #1730554 is a reply to message #1730499] Tue, 26 April 2016 15:59 Go to previous messageGo to next message
Da Mist is currently offline Da MistFriend
Messages: 9
Registered: November 2015
Junior Member
Hi Vinicius,

It worked ! Smile

Thank you so much for your help!
Re: Change color of UML element [message #1730557 is a reply to message #1730554] Tue, 26 April 2016 16:07 Go to previous messageGo to next message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
No problem!

Glad that I helped you Smile
Re: Change color of UML element [message #1731249 is a reply to message #1730557] Tue, 03 May 2016 18:35 Go to previous messageGo to next message
Da Mist is currently offline Da MistFriend
Messages: 9
Registered: November 2015
Junior Member
Hi Vinicius,

Sorry for bothering you again!
I am able to change color of a state or transition by the process you have mentioned. The problem is, I have a list of states and when I tried to changed color of states one by one from a different thread, it gives an exception (Still it changes the color of the element). But after sometime it freezes the view.

The exception is:
org.eclipse.swt.SWTException: Invalid thread access
	at org.eclipse.swt.SWT.error(SWT.java:4533)
	at org.eclipse.swt.SWT.error(SWT.java:4448)
	at org.eclipse.swt.SWT.error(SWT.java:4419)
	at org.eclipse.swt.widgets.Widget.error(Widget.java:483)
	at org.eclipse.swt.widgets.Widget.checkWidget(Widget.java:421)
	at org.eclipse.swt.widgets.Widget.getData(Widget.java:509)
	at org.eclipse.ui.internal.ide.handlers.PagePropertyTester.test(PagePropertyTester.java:36)
	at org.eclipse.core.internal.expressions.Property.test(Property.java:58)
	at org.eclipse.core.internal.expressions.TestExpression.evaluate(TestExpression.java:100)
	at org.eclipse.core.internal.expressions.CompositeExpression.evaluateAnd(CompositeExpression.java:53)
	at org.eclipse.core.internal.expressions.WithExpression.evaluate(WithExpression.java:75)
	at org.eclipse.core.internal.expressions.CompositeExpression.evaluateOr(CompositeExpression.java:68)
	at org.eclipse.core.internal.expressions.OrExpression.evaluate(OrExpression.java:22)
	at org.eclipse.core.internal.expressions.ReferenceExpression.evaluate(ReferenceExpression.java:71)
	at org.eclipse.ui.internal.services.EvaluationReference.evaluate(EvaluationReference.java:72)
	at org.eclipse.ui.internal.services.EvaluationReference.evaluate(EvaluationReference.java:97)
	at org.eclipse.ui.internal.services.EvaluationService.requestEvaluation(EvaluationService.java:294)
	at org.eclipse.ui.internal.handlers.DirtyStateTracker.update(DirtyStateTracker.java:45)
	at org.eclipse.ui.internal.handlers.DirtyStateTracker.propertyChanged(DirtyStateTracker.java:107)
	at org.eclipse.ui.part.WorkbenchPart.firePropertyChange(WorkbenchPart.java:120)
	at org.eclipse.papyrus.infra.core.sasheditor.editor.AbstractMultiPageSashEditor.access$0(AbstractMultiPageSashEditor.java:1)
	at org.eclipse.papyrus.infra.core.sasheditor.editor.AbstractMultiPageSashEditor$1.pageFirePropertyChange(AbstractMultiPageSashEditor.java:62)
	at org.eclipse.papyrus.infra.core.sasheditor.internal.SashContainerEventsProvider.firePageFirePropertyChangeEvent(SashContainerEventsProvider.java:130)
	at org.eclipse.papyrus.infra.core.sasheditor.internal.EditorPart.handlePropertyChange(EditorPart.java:380)
	at org.eclipse.papyrus.infra.core.sasheditor.internal.EditorPart.access$3(EditorPart.java:376)
	at org.eclipse.papyrus.infra.core.sasheditor.internal.EditorPart$3.propertyChanged(EditorPart.java:300)
	at org.eclipse.ui.part.WorkbenchPart.firePropertyChange(WorkbenchPart.java:120)
	at org.eclipse.gef.ui.parts.GraphicalEditor.firePropertyChange(GraphicalEditor.java:201)
	at org.eclipse.gmf.runtime.diagram.ui.resources.editor.parts.DiagramDocumentEditor.firePropertyChange(DiagramDocumentEditor.java:1036)
	at org.eclipse.papyrus.uml.diagram.statemachine.part.UMLDiagramEditor.access$0(UMLDiagramEditor.java:1)
	at org.eclipse.papyrus.uml.diagram.statemachine.part.UMLDiagramEditor$1.commandStackChanged(UMLDiagramEditor.java:248)
	at org.eclipse.gmf.runtime.diagram.ui.parts.DiagramCommandStack$1.historyNotification(DiagramCommandStack.java:122)
	at org.eclipse.core.commands.operations.DefaultOperationHistory$2.run(DefaultOperationHistory.java:870)
	at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.notifyListeners(DefaultOperationHistory.java:859)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.notifyAdd(DefaultOperationHistory.java:914)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.add(DefaultOperationHistory.java:188)
	at org.eclipse.core.commands.operations.DefaultOperationHistory.execute(DefaultOperationHistory.java:505)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.doExecute(CheckedOperationHistory.java:206)
	at org.eclipse.papyrus.infra.emf.gmf.command.CheckedOperationHistory.execute(CheckedOperationHistory.java:195)
	at org.eclipse.papyrus.infra.emf.gmf.command.NotifyingWorkspaceCommandStack.doExecute(NotifyingWorkspaceCommandStack.java:264)
	at org.eclipse.emf.transaction.impl.AbstractTransactionalCommandStack.execute(AbstractTransactionalCommandStack.java:165)
	at org.eclipse.emf.transaction.impl.AbstractTransactionalCommandStack.execute(AbstractTransactionalCommandStack.java:219)
	at org.eclipse.papyrus.infra.emf.gmf.command.NestingNotifyingWorkspaceCommandStack.execute(NestingNotifyingWorkspaceCommandStack.java:130)
	at ca.queensu.ca.lttng.tracedisplayui.views.HighlightModelElement.highlightState(HighlightModelElement.java:114)
	at ca.queensu.ca.lttng.tracedisplayui.views.HighlightModelElement.processHighlightingElement(HighlightModelElement.java:254)
	at ca.queensu.ca.lttng.tracedisplayui.views.CommandExecution.run(CommandExecution.java:102)
	at java.lang.Thread.run(Thread.java:745)


Do you have any idea why I get this? And how to deal with the freezing issue?
P.S: I tried to access some commands from a different thread. And from that thread I colored model elements.

Thank you!
Re: Change color of UML element [message #1732611 is a reply to message #1731249] Wed, 18 May 2016 06:32 Go to previous message
Vinicius Pereira is currently offline Vinicius PereiraFriend
Messages: 50
Registered: March 2014
Member
Sorry for be late...

In my case, the user clicks at one line in a text view, then I check the UML element equivalent to that line, and change the color of the UML element.

I save the previous element, so at the click I change back the previous UML element to its default and change the color of the new UML element.

This works fine to me...
Previous Topic:add interaction operands to a combined fragment
Next Topic:Comments visible in class compartments
Goto Forum:
  


Current Time: Thu Dec 08 16:20:22 GMT 2016

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

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