Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Modeling » EMF » Change color font through Colorprovider
Change color font through Colorprovider [message #1081775] Wed, 07 August 2013 17:30 Go to next message
Phil H is currently offline Phil H
Messages: 267
Registered: November 2012
Senior Member
Hi,

I would like to change the font color of a item in my editor. So I turned the color Providers property inside the genmodel to true and the items now implement the IItemColorProvider interface. Accordingly to this I tried to implement the getForeground(Object object) method, but it's seems that it's never called. I searched a long while now, but couldn't find any implementation example and there is not much documentation how to implement this. That's my code for now:

public Object getForeground(Object object) {
		Device device = Display.getCurrent();
		Color background = new Color(device, 255, 200, 100);
		Color foreground = new Color(device, 12, 20, 120);
		AdapterFactoryLabelProvider.ColorProvider colorProvider = new ColorProvider(getAdapterFactory(), foreground, background);
		return colorProvider;
	}


As written int the java docs, the method should do the same thing as IColorProvider.getForeground, but that didn't help me much, as there are a lot of different implementation out.

Cheers,
Phil
Re: Change color font through Colorprovider [message #1081789 is a reply to message #1081775] Wed, 07 August 2013 17:47 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Phil,

You'll need to use
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider
in place of where you use just
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider before,
probably using the
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider.ColorProvider(AdapterFactory,
Viewer) constructor. Be sure to read the Javadoc for
org.eclipse.emf.edit.provider.IItemColorProvider, i.e., you're typically
expected to return a URI to represent the color and the ColorProvider
will ensure your method is actually called while
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.getColorFromObject(Object)
will ensure that the URI is mapped to a correct instance of a cached
color object; you need to be careful not to create too many Color
instances or you're exhaust the supply of available handles, so
definitely don't do anything like what you're doing below.


On 07/08/2013 7:31 PM, Phil H wrote:
> Hi,
>
> I would like to change the font color of a item in my editor. So I
> turned the color Providers property inside the genmodel to true and
> the items now implement the IItemColorProvider interface. Accordingly
> to this I tried to implement the getForeground(Object object) method,
> but it's seems that it's never called. I searched a long while now,
> but couldn't find any implementation example and there is not much
> documentation how to implement this. That's my code for now:
>
>
> public Object getForeground(Object object) {
> Device device = Display.getCurrent();
> Color background = new Color(device, 255, 200, 100);
> Color foreground = new Color(device, 12, 20, 120);
> AdapterFactoryLabelProvider.ColorProvider colorProvider = new
> ColorProvider(getAdapterFactory(), foreground, background);
> return colorProvider;
> }
>
>
> As written int the java docs, the method should do the same thing as
> IColorProvider.getForeground, but that didn't help me much, as there
> are a lot of different implementation out.
>
> Cheers,
> Phil
Re: Change color font through Colorprovider [message #1082230 is a reply to message #1081789] Thu, 08 August 2013 09:19 Go to previous messageGo to next message
Phil H is currently offline Phil H
Messages: 267
Registered: November 2012
Senior Member
Thx Ed,

Quote:
You'll need to use
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider
in place of where you use just
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider


that's what I've done?

Quote:
probably using the
org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider.ColorProvider(AdapterFactory,
Viewer)


Yep, that's what I've tried first, but my problem that I don_t know how to get the viewer object.

	public Object getForeground(Object object) {
	     org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider.ColorProvider(getAdapterFactory(), ????)
		return URI.createURI("color://hsb///0.3");
	}
Re: Change color font through Colorprovider [message #1082245 is a reply to message #1082230] Thu, 08 August 2013 09:37 Go to previous messageGo to next message
Mikael Barbero is currently offline Mikael Barbero
Messages: 54
Registered: July 2009
Member
Phil,

The label provider has to be changed for your viewer (probably in your Editor or View class). The method getForeground(Object) does not need to create a label provider, it just needs to return the color's URI.


Best regards,

Mikael Barbero
Obeo
Re: Change color font through Colorprovider [message #1082249 is a reply to message #1082230] Thu, 08 August 2013 09:42 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Phil,

Comments below.

On 08/08/2013 11:19 AM, Phil H wrote:
> Thx Ed,
>
> Quote:
>> You'll need to use
>> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider
>> in place of where you use just
>> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider
>
>
> that's what I've done?
You seemed to indicate you had code like this in your derived item
provider adapter:

public Object getForeground(Object object) {
Device device = Display.getCurrent();
Color background = new Color(device, 255, 200, 100);
Color foreground = new Color(device, 12, 20, 120);
AdapterFactoryLabelProvider.ColorProvider colorProvider = new
ColorProvider(getAdapterFactory(), foreground, background);
return colorProvider;
}

But that's totally bogus (if I understood correctly). You use the
ColorProvider variant of the AdapterFactoryLabelProvider when you set
the label provider for the viewer, and then of course you should have no
problem locating the viewer because that's where you set the label
provider...
>
> Quote:
>> probably using the
>> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider.ColorProvider(AdapterFactory,
>> Viewer)
>
>
> Yep, that's what I've tried first, but my problem that I don_t know
> how to get the viewer object.
Yes, because you don't do that part in your item provider; you return a
URI representing the color as you show in the last line. The
ColorProvider is used to set the label provider for the viewer, which is
done in your editor where the viewers are created.
>
>
> public Object getForeground(Object object) {
> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider.ColorProvider(getAdapterFactory(),
> ????)
> return URI.createURI("color://hsb///0.3");
> }
>
Re: Change color font through Colorprovider [message #1082250 is a reply to message #1082245] Thu, 08 August 2013 09:44 Go to previous messageGo to next message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Phil is likely to be interested in your contribution to support styled
text:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=411890

But that's new to EMF 2.10...

On 08/08/2013 11:37 AM, Mikael Barbero wrote:
> Phil,
>
> The label provider has to be changed for your viewer (probably in your
> Editor or View class). The method getForeground(Object) does not need
> to create a label provider, it just needs to return the color's URI.
Re: Change color font through Colorprovider [message #1082576 is a reply to message #1082250] Thu, 08 August 2013 19:38 Go to previous messageGo to next message
Phil H is currently offline Phil H
Messages: 267
Registered: November 2012
Senior Member
Quote:

Phil is likely to be interested in your contribution to support styled
text:

https://bugs.eclipse.org/bugs/show_bug.cgi?id=411890

But that's new to EMF 2.10...


Thx Ed, that sounds good. I'll wait for EMF 2.10 and have a look at it. When can a release of EMF 2.10 be expected?

Quote:

Yes, because you don't do that part in your item provider; you return a URI representing the color as you show in the last line. The ColorProvider is used to set the label provider for the viewer, which is done in your editor where the viewers are created.


Ok I think I understand now. I thought that this would be done automatically by the generator. Is there a reason why not?

I changed now from
contentOutlineViewer.setLabelProvider(new AdapterFactoryLabelProvider(adapterFactory));

to
contentOutlineViewer.setLabelProvider(new org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider(adapterFactory, selectionViewer)); 


but it still doesn't work and I get the following error msg:

java.lang.NullPointerException
	at org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider$ColorProvider.<init>(AdapterFactoryLabelProvider.java:114)
	at pld.presentation.PldEditor$1MyContentOutlinePage.createControl(PldEditor.java:1188)
	at org.eclipse.ui.views.contentoutline.ContentOutline.doCreatePage(ContentOutline.java:137)
	at org.eclipse.ui.part.PageBookView.createPage(PageBookView.java:411)
	at org.eclipse.ui.part.PageBookView.partActivated(PageBookView.java:754)
	at org.eclipse.ui.part.PageBookView.showBootstrapPart(PageBookView.java:923)
	at org.eclipse.ui.part.PageBookView.createPartControl(PageBookView.java:494)
	at org.eclipse.ui.views.contentoutline.ContentOutline.createPartControl(ContentOutline.java:121)
	at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl(CompatibilityPart.java:129)
	at org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl(CompatibilityView.java:155)
	at org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create(CompatibilityPart.java:300)
	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)


SelectionViewer should be the right viewer, or? Despite that I tried also other viewers like treeViewer, but it never worked..

[Updated on: Thu, 08 August 2013 21:58]

Report message to a moderator

Re: Change color font through Colorprovider [message #1082838 is a reply to message #1082576] Fri, 09 August 2013 05:05 Go to previous message
Ed Merks is currently offline Ed Merks
Messages: 26138
Registered: July 2009
Senior Member
Phil,

Comments below.

On 08/08/2013 9:38 PM, Phil H wrote:
> Quote:
>> Phil is likely to be interested in your contribution to support
>> styled text:
>>
>> https://bugs.eclipse.org/bugs/show_bug.cgi?id=411890
>>
>> But that's new to EMF 2.10...
>
>
> Thx Ed, that sounds good. I'll wait for EMF 2.10 and have a look at
> it. When can a release of EMF 2.10 be expected?
It will release with Luna next July.
>
> Quote:
>> Yes, because you don't do that part in your item provider; you return
>> a URI representing the color as you show in the last line. The
>> ColorProvider is used to set the label provider for the viewer, which
>> is done in your editor where the viewers are created.
>
>
> Ok I think I understand now. I thought that this would be done
> automatically by the generator. Is there a reason why not?
No particularly good reason. The styled label support does generate the
corresponding editor support and it's a far more flexible way of
providing font and color support...
>
> I changed now from
> contentOutlineViewer.setLabelProvider(new
> AdapterFactoryLabelProvider(adapterFactory));
>
> to
> contentOutlineViewer.setLabelProvider(new
> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider.ColorProvider(adapterFactory,
> selectionViewer));
You'd generally use the same viewer as the one to which you're setting
the label provider.
> but it still doesn't work and I get the following error msg:
>
>
> java.lang.NullPointerException
> at
> org.eclipse.emf.edit.ui.provider.AdapterFactoryLabelProvider$ColorProvider.<init>(AdapterFactoryLabelProvider.java:114)
So it appears that either the selectionViewer is null or it's control is
null, right?
> at
> pld.presentation.PldEditor$1MyContentOutlinePage.createControl(PldEditor.java:1188)
>
> at
> org.eclipse.ui.views.contentoutline.ContentOutline.doCreatePage(ContentOutline.java:137)
> at org.eclipse.ui.part.PageBookView.createPage(PageBookView.java:411)
> at
> org.eclipse.ui.part.PageBookView.partActivated(PageBookView.java:754)
> at
> org.eclipse.ui.part.PageBookView.showBootstrapPart(PageBookView.java:923)
> at
> org.eclipse.ui.part.PageBookView.createPartControl(PageBookView.java:494)
> at
> org.eclipse.ui.views.contentoutline.ContentOutline.createPartControl(ContentOutline.java:121)
> at
> org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.createPartControl(CompatibilityPart.java:129)
> at
> org.eclipse.ui.internal.e4.compatibility.CompatibilityView.createPartControl(CompatibilityView.java:155)
> at
> org.eclipse.ui.internal.e4.compatibility.CompatibilityPart.create(CompatibilityPart.java:300)
> 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)
>
>
> SelectionViewer should be the right viewer, or? Despite that I treid
> also treeViewer, but it never worked..
You've tried the viewer to which you're setting the label provider?
Previous Topic:Error when I try generate GMF Editor
Next Topic:[CDO] CDOView.queryXRefs() vs. EcoreUtil.CrossReferencer.find()
Goto Forum:
  


Current Time: Fri Oct 24 09:39:48 GMT 2014

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

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