Home » Modeling » EMF » Change color font through Colorprovider
Change color font through Colorprovider [message #1081775] |
Wed, 07 August 2013 13:30  |
Eclipse User |
|
|
|
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 13:47   |
Eclipse User |
|
|
|
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 #1082249 is a reply to message #1082230] |
Thu, 08 August 2013 05:42   |
Eclipse User |
|
|
|
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 #1082838 is a reply to message #1082576] |
Fri, 09 August 2013 01:05  |
Eclipse User |
|
|
|
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?
|
|
|
Goto Forum:
Current Time: Wed Jul 23 12:35:34 EDT 2025
Powered by FUDForum. Page generated in 0.10759 seconds
|