Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Problems with Decorator
Problems with Decorator [message #547961] Tue, 20 July 2010 09:14 Go to next message
mho is currently offline mhoFriend
Messages: 11
Registered: July 2010
Junior Member
I tried to change my font settings on a TreeViewer using a Decorator. I
read sime examples for this in the web and hoped to be able to implement
something similar. Ok, I finally managed to implement something to
change my font, but I had to realize, that it didn't work. Though I
started a little debugging to see where the code went, I saw that the
app called my decorateFont method and oviously returned a font, but it
did nothing. So, what is the code? Here it is:
a) method in a decorator that implements ILabelDecorator as well as
IFontDecorator:

public Font decorateFont(Object element) {
FontDescriptor fd =
JFaceResources.getFontRegistry().getDescriptor(JFaceResource s.DEFAULT_FONT);
fd.setHeight(20);
fd.setStyle(SWT.BOLD);
Font createFont = resourceManager.createFont(fd);
if (element instanceof CollectionItem)
{
return createFont;
}
return null;
}

b) calling method in the View class:

public void createPartControl(Composite parent)
{
treeViewer = new TreeViewer(parent, SWT.BORDER | SWT.MULTI
| SWT.V_SCROLL);
ObservableListTreeContentProvider contentProvider =
new ListContentProvider();

ObservableMapLabelProvider labelProvider =
new ListLabelProvider(
contentProvider.getKnownElements());
DecoratingLabelProvider decoProvider =
new DecoratingLabelProvider(
labelProvider,
new ListDecorator());
treeViewer.setLabelProvider(decoProvider);

treeViewer.setContentProvider(contentProvider);

treeViewer.setInput(Model.getInstance());

getSite().setSelectionProvider(treeViewer);

}

Maybe anyone sees my mistake or something that's missing?
Thanks in advance, mho
Re: Problems with Decorator [message #548524 is a reply to message #547961] Thu, 22 July 2010 09:13 Go to previous messageGo to next message
mho is currently offline mhoFriend
Messages: 11
Registered: July 2010
Junior Member
I had to try some alternatives myself as nobody has answered so far. In a newsgroup I found a possibility to change my font in another way: https://bugs.eclipse.org/bugs/show_bug.cgi?id=162698 . As this doesn't use a Decorator I hoped this would do the job. What I did was just implementing the IFontTableProvider. Now I had a getFont() method in my LabelProvider which is supposed to change my font. But again I had to see that nothing happened. The getFont() method is never called! Is there something special that I have to do for calling the method in order for it to work fine? Thx for your help!
mho

[Updated on: Thu, 22 July 2010 09:16]

Report message to a moderator

Re: Problems with Decorator [message #548563 is a reply to message #548524] Thu, 22 July 2010 10:52 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 22.07.2010 11:13, mho wrote:
> I had to try dome alternatives myself as nobody has answered so far.

This is probably due to the fact that you did not provide a
complete snippet that demonstrates the problem. I have not
heart of problems of the type you report, therefore it
would strongly help, if you would provide a jface-snippet
of the usual style, as shown in e.g.

http://wiki.eclipse.org/JFace_Data_Binding/Snippets

Greetings from Bremen,

Daniel Krügler
Re: Problems with Decorator [message #548622 is a reply to message #548563] Thu, 22 July 2010 13:47 Go to previous messageGo to next message
mho is currently offline mhoFriend
Messages: 11
Registered: July 2010
Junior Member
Daniel Krügler schrieb:
> On 22.07.2010 11:13, mho wrote:
>> I had to try dome alternatives myself as nobody has answered so far.
>
> This is probably due to the fact that you did not provide a
> complete snippet that demonstrates the problem. I have not
> heart of problems of the type you report, therefore it
> would strongly help, if you would provide a jface-snippet
> of the usual style, as shown in e.g.
>
> http://wiki.eclipse.org/JFace_Data_Binding/Snippets
>
> Greetings from Bremen,
>
> Daniel Krügler
Hi Daniel,
thank you for this hint! Thus I sat down to drop all the code in a
snippet to see the effect in just one class. Unfortunately I don't have
some webspace to provide the example. In the end this means that I just
post the snippet here to show the mistake(?) or just the code. Ok, here
it is:
Thx, mho
package org.mho.testit;

import java.util.ArrayList;
import org.eclipse.core.databinding.beans.BeanProperties;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.Realm;
import
org.eclipse.core.databinding.observable.masterdetail.IObserv ableFactory;
import org.eclipse.core.databinding.observable.set.IObservableSet;
import org.eclipse.core.databinding.property.Properties;
import org.eclipse.jface.databinding.swt.SWTObservables;
import
org.eclipse.jface.databinding.viewers.ObservableListTreeCont entProvider;
import org.eclipse.jface.databinding.viewers.ObservableMapLabelProv ider;
import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;
import org.eclipse.jface.resource.FontRegistry;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableFontProvider;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.plugin.AbstractUIPlugin;
import org.mho.collect.general.Application;

public class SnippetTest {

public static void main(String[] args) {
final Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
Shell shell = new Shell(display);
shell.setText("Snippet");
shell.setLayout(new GridLayout());

Tree tree = new Tree(shell, SWT.SINGLE | SWT.H_SCROLL
| SWT.V_SCROLL | SWT.BORDER);
GridData gridData = new GridData(SWT.FILL, SWT.FILL, true, true);
final TreeViewer viewer = new TreeViewer(tree);

ListContentProvider contentProvider = new ListContentProvider();
viewer.setContentProvider(contentProvider);

class ListLabelProvider extends ObservableMapLabelProvider
implements ITableFontProvider {
private ImageDescriptor gfImage, rImage, tdImage;

private FontRegistry registry = new FontRegistry();

public ListLabelProvider(IObservableSet knownElements) {
super(Properties.observeEach(knownElements, BeanProperties
.values(new String[] { "title", "name", "type" })));
initializeImageDescriptors();
}

private void initializeImageDescriptors() {
gfImage = getDescriptor("abc.jpg");
rImage = getDescriptor("cde.jpg");
tdImage = getDescriptor("xyzr.jpg");
}

private ImageDescriptor getDescriptor(String fileName)
{
return
AbstractUIPlugin.imageDescriptorFromPlugin(Application.PLUGI N_ID,
"icons/" + fileName);
}

public String getText(Object element) {
if (element instanceof CollectionItem) {
return ((CollectionItem) element).getTitle();
}
return null;
}

public Image getImage(Object element) {
if (element instanceof CollectionItem)
{
CollectionItem ci = (CollectionItem) element;
if ("abc".equalsIgnoreCase(ci.getTitle())) {
return getDescriptor("abc.jpg").createImage();
}
else if ("cde".equalsIgnoreCase(ci.getTitle())) {
return getDescriptor("cde.jpg").createImage();
}
else if ("xyz".equalsIgnoreCase(ci.getTitle())) {
return getDescriptor("xyz.jpg").createImage();
}
}
return null;
}

public void dispose() {
super.dispose();
}

public Font getFont(Object element, int columnIndex) {
return
registry.getBold(Display.getCurrent().getSystemFont().getFon tData()[0].getName());
}

}
viewer.setLabelProvider(new
ListLabelProvider(contentProvider.getKnownElements()));

viewer.setInput(Model.getInstance());
tree.pack();
shell.setSize(300, 400);
shell.open();

while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
});
display.dispose();
}

static class Model
{
private Model parent;
private ArrayList child = new ArrayList();
private int counter;

private Model(int counter, Model parent) {
this.parent = parent;
this.counter = counter;
}

public String toString() {
String rv = "Item ";
if( parent != null ) {
rv = parent.toString() + ".";
}
rv += counter;
return rv;
}


public static Model getInstance() {
Model root = new Model(0,null);
root.counter = 0;
CollectionItem tmp;

tmp = new CollectionItem(0, root);
tmp.setTitle("Good Fellas");
tmp.setType("Film");
root.child.add(tmp);

tmp = new CollectionItem(1, root);
tmp.setTitle("Ronin");
tmp.setType("Film");
root.child.add(tmp);

tmp = new CollectionItem(2, root);
tmp.setTitle("Taxi Driver");
tmp.setType("Film");
root.child.add(tmp);

return root;
}
}

static class CollectionItem
{
private String title;
private String type;
private int counter;
private Model parent;
public ArrayList child = new ArrayList();

public CollectionItem(int counter, Model parent)
{
this.counter = counter;
this.parent = parent;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getType() {
return type;
}

public void setType(String type) {
this.type = type;
}
}

static class ListContentProvider extends ObservableListTreeContentProvider
implements IStructuredContentProvider {

public ListContentProvider() {
super(getObservableListFactory(), getTreeStructureAdvisor());
}

private static IObservableFactory getObservableListFactory() {
return new IObservableFactory() {
public IObservable createObservable(Object parent) {
if (parent instanceof CollectionItem) {
return BeanProperties.list("title").observe(parent);
}
return null;
}
};
}

private static TreeStructureAdvisor getTreeStructureAdvisor() {
return new TreeStructureAdvisor() {
public Object getParent(Object element) {
if (element instanceof CollectionItem) {
return ((CollectionItem) element).getTitle();
}
return super.getParent(element);
}
};
}

public Object[] getElements(Object inputElement) {
if (inputElement instanceof Model) {
return ((Model)inputElement).child.toArray();
}
if (inputElement instanceof CollectionItem) {
return ((CollectionItem)inputElement).child.toArray();
}
return null;
}

public void inputChanged(Viewer viewer, Object oldInput, Object
newInput) { }

public Object[] getChildren(Object parentElement) {
return getElements(parentElement);
}

public Object getParent(Object element) {
if( element == null) {
return null;
}
return ((Model)element).parent;
}

public boolean hasChildren(Object element) {
if (element instanceof CollectionItem) {
return ((CollectionItem)element).child.size() > 0;
}
return false;
}

public void dispose() { }
}
}
Re: Problems with Decorator [message #548673 is a reply to message #548622] Thu, 22 July 2010 15:23 Go to previous messageGo to next message
Daniel Krügler is currently offline Daniel KrüglerFriend
Messages: 853
Registered: July 2009
Senior Member
On 22.07.2010 15:47, Michael Holste wrote:
> Daniel Krügler schrieb:
>> On 22.07.2010 11:13, mho wrote:
>>> I had to try dome alternatives myself as nobody has answered so far.
>>
>> This is probably due to the fact that you did not provide a
>> complete snippet that demonstrates the problem. I have not
>> heart of problems of the type you report, therefore it
>> would strongly help, if you would provide a jface-snippet
>> of the usual style, as shown in e.g.
>>
>> http://wiki.eclipse.org/JFace_Data_Binding/Snippets
>>
>> Greetings from Bremen,
>>
>> Daniel Krügler
> Hi Daniel,
> thank you for this hint! Thus I sat down to drop all the code in a
> snippet to see the effect in just one class. Unfortunately I don't have
> some webspace to provide the example. In the end this means that I just
> post the snippet here to show the mistake(?) or just the code. Ok, here
> it is:

[For the next time, please don't make the code dependent on files.
Replace your images by a simple loop as follows:

final Image[] images = { new Image(display, 16, 16),
new Image(display, 16, 16), new Image(display, 16, 16) };
int[] colors = { SWT.COLOR_BLUE, SWT.COLOR_RED, SWT.COLOR_GREEN };
for (int i = 0; i < images.length; ++i) {
GC gc = new GC(images[i]);
gc.setBackground(display.getSystemColor(colors[i]));
gc.fillRectangle(images[i].getBounds());
gc.dispose();
}
]

OK, the bug was easy to find: First note that the spec of
ObservableMapLabelProvider says:

"Clients may customize by subclassing and overriding
getColumnText(Object, int), getColumnImage(Object, int), for tables or
trees with columns, or by implementing additional mixin interfaces for
colors, fonts etc."

In your example your are mixing the ITable* interface (that requires at
least one existing TreeColumn) and the non-column approach (no ITable*
stuff). You need to be consistent!

Three possible solutions:

a) Replace the ITableFontProvider by IFontProvider in your
ObservableMapLabelProvider implementation. This is the easiest fix
in your example.

b) Create at least one TreeColumn (in your example this will be exactly
one) and ensure that you *only* implement the getColumnText(Object,
int), getColumnImage(Object, int), and getFont(Object, int) methods.

c) I also assume that it should be possible to create TreeColumns and
combine them with column-specific CellLabelProviders (instead of a
single LabelProvider), but I have not tested this approach in your
example (But I do use and do prefer this approach all the time: It
makes me independent from column indices, plus makes providing column-
specific behaviour very simple).

Either of these choices should fix your problem.

HTH & Greetings from Bremen,

Daniel Krügler
icon14.gif  Re: Problems with Decorator [message #548733 is a reply to message #548673] Thu, 22 July 2010 18:43 Go to previous message
mho is currently offline mhoFriend
Messages: 11
Registered: July 2010
Junior Member
Hi Daniel,
thank you very much for all those hints! I tried only the first of the suggested solutions so far, but I can say: Yeah, that's it! Ok, it is essential that if I am able to read, then I should do it! Cool
Previous Topic:Default Font Size
Next Topic:Problem with Section.setText(...)
Goto Forum:
  


Current Time: Fri Apr 26 08:57:04 GMT 2024

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

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

Back to the top