Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » GMF (Graphical Modeling Framework) » Change background color of a shape programmatically
Change background color of a shape programmatically [message #896623] Thu, 19 July 2012 07:41 Go to next message
Chris B. is currently offline Chris B.Friend
Messages: 37
Registered: November 2011
Member
Hi everyone,

I've added a new item to the context menu of the diagram editor and associated it with an AbstractHandler. When the item is clicked the execute method is invoked. Here i want to change the color of a shape dynamically depending on the shape's state.
This is how my code looks like:

public Object execute(ExecutionEvent event) throws ExecutionException
    {
        EvaluationContext evalContext = (EvaluationContext) event.getApplicationContext();
        Object var = evalContext.getDefaultVariable();
        ArrayList al = new ArrayList((Collection) var);
        Object o = al.get(0);
        if (o instanceof ShapeNodeEditPart)
        {
            ShapeNodeEditPart editPart = (ShapeNodeEditPart) o;
            ShapeImpl notationView = (ShapeImpl) editPart.getNotationView();
            GradientData gdata = new GradientData();
            gdata.setGradientColor1(Color.HSBtoRGB(1.0f, 1.0f, 1.0f));
            gdata.setGradientColor2(0x33);
            notationView.setGradient(gdata);
        }
        return null;
    }


Calling the method results in the following exception:
java.lang.IllegalStateException: Cannot modify resource set without a write transaction
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.assertWriting(TransactionChangeRecorder.java:348)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.appendNotification(TransactionChangeRecorder.java:302)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.processObjectNotification(TransactionChangeRecorder.java:284)
	at org.eclipse.emf.transaction.impl.TransactionChangeRecorder.notifyChanged(TransactionChangeRecorder.java:240)
	at org.eclipse.emf.common.notify.impl.BasicNotifierImpl.eNotify(BasicNotifierImpl.java:380)
	at org.eclipse.gmf.runtime.notation.impl.ShapeImpl.setGradient(ShapeImpl.java:589)
	at GraphicalEventModeller.diagram.part.SetBreakpoint.execute(SetBreakpoint.java:67)
	at org.eclipse.ui.internal.handlers.HandlerProxy.execute(HandlerProxy.java:293)
	at org.eclipse.core.commands.Command.executeWithChecks(Command.java:476)
	at org.eclipse.core.commands.ParameterizedCommand.executeWithChecks(ParameterizedCommand.java:508)
	at org.eclipse.ui.internal.handlers.HandlerService.executeCommand(HandlerService.java:169)
	at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
	at org.eclipse.ui.internal.handlers.SlaveHandlerService.executeCommand(SlaveHandlerService.java:241)
	at org.eclipse.ui.menus.CommandContributionItem.handleWidgetSelection(CommandContributionItem.java:820)
	at org.eclipse.ui.menus.CommandContributionItem.access$19(CommandContributionItem.java:806)
	at org.eclipse.ui.menus.CommandContributionItem$5.handleEvent(CommandContributionItem.java:796)
	at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
	at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053)
	at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4066)
	at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3657)
	at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2640)
	at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2604)
	at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2438)
	at org.eclipse.ui.internal.Workbench$7.run(Workbench.java:671)
	at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
	at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:664)
	at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
	at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:115)
	at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:196)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
	at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:369)
	at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:620)
	at org.eclipse.equinox.launcher.Main.basicRun(Main.java:575)
	at org.eclipse.equinox.launcher.Main.run(Main.java:1408)
	at org.eclipse.equinox.launcher.Main.main(Main.java:1384)



I came across this and similar messages several times in GMF. I think i need to execute the code in a read/write transaction but i don't know the approach. Can anybody help me solving this issue and give a sample code how to change appearance properties of shapes programmatically?

regards
Chris
Re: Change background color of a shape programmatically [message #896642 is a reply to message #896623] Thu, 19 July 2012 09:01 Go to previous messageGo to next message
Aurelien Pupier is currently offline Aurelien PupierFriend
Messages: 579
Registered: July 2009
Location: Grenoble, FRANCE
Senior Member

Hi,

you need to use an AbstractTransactionalCommand to execute the code in a read/write transaction.

Regards,


Aurélien Pupier - Bonitasoft S.A.
My company Eclipse-related blog
Re: Change background color of a shape programmatically [message #896669 is a reply to message #896642] Thu, 19 July 2012 10:55 Go to previous messageGo to next message
Chris B. is currently offline Chris B.Friend
Messages: 37
Registered: November 2011
Member
Hi,

thanks for your reply. I've created a new AbstractTransactionalCommand and shifted the code to the doExecuteWithResult method. The command is executed but the changes are not applied.What's going wrong? Any suggestions?

regards
Chris

 @Override
    protected CommandResult doExecuteWithResult(IProgressMonitor monitor, IAdaptable info) throws ExecutionException
    {
        ShapeImpl notationView = (ShapeImpl) editPart.getNotationView();
        GradientData gdata = new GradientData();
        gdata.setGradientColor1(Color.HSBtoRGB(5.0f, 5.0f, 5.0f));
        gdata.setGradientColor2(Color.HSBtoRGB(105.0f, 25.0f, 95.0f));
        notationView.setGradient(gdata);
        return CommandResult.newOKCommandResult();
    }



Re: Change background color of a shape programmatically [message #896672 is a reply to message #896669] Thu, 19 July 2012 10:56 Go to previous messageGo to next message
Aurelien Pupier is currently offline Aurelien PupierFriend
Messages: 579
Registered: July 2009
Location: Grenoble, FRANCE
Senior Member

Hi,

I think that you need to call a refresh on the editpart.

regards,


Aurélien Pupier - Bonitasoft S.A.
My company Eclipse-related blog
Re: Change background color of a shape programmatically [message #896716 is a reply to message #896672] Thu, 19 July 2012 13:22 Go to previous message
Chris B. is currently offline Chris B.Friend
Messages: 37
Registered: November 2011
Member
Works fine, thank you very much!

regards
Chris
Previous Topic:Problem when delete a node in a subdiagram
Next Topic:templates: change code of inner classes
Goto Forum:
  


Current Time: Fri Dec 19 22:58:36 GMT 2014

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

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