Eclipse RCP and JFace: Problems with Images in Context menu and TreeViewer
- by Juri
I'm working on an Eclipse RCP application. Today I experienced some troubles when displaying images in the context menu. What I wanted to do is to add a column to my table containing images of stars for representing a user rating. On Windows, this causes some problems, since the star images are squeezed up on the left corner of the table cell instead of expanding on the whole cell, but I'll solve that somehow. In addition I have a context menu on the table, with an entry called "rate" where again the different stars from 1 to 5 (representing the rating level) are shown, such that the user can click on it for choosing different ratings. That works fine on Windows.
Now I switched to Linux (Ubuntu) to see how it works out there, and strangely, the stars in the table cell are layed out perfectly, while the stars on the context menu don't even show up.
On the context menu I'm using an action class where I'm setting the image descriptor for the star images:
public class RateAction extends Action {
private final int fRating;
private IStructuredSelection fSelection;
public RateAction(int rating, IStructuredSelection selection) {
super("", AS_CHECK_BOX);
fRating = rating;
fSelection = selection;
setImageDescriptor(createImageDescriptor());
}
/**
* Creates the correct ImageDescriptor depending on the given rating
* @return
*/
private ImageDescriptor createImageDescriptor() {
ImageDescriptor imgDescriptor = null;
switch (fRating) {
case 0:
return OwlUI.NEWS_STARON_0;
case 1:
return OwlUI.NEWS_STARON_1;
case 2:
return OwlUI.NEWS_STARON_2;
case 3:
return OwlUI.NEWS_STARON_3;
case 4:
return OwlUI.NEWS_STARON_4;
case 5:
return OwlUI.NEWS_STARON_5;
default:
break;
}
return imgDescriptor;
}
/*
* @see org.eclipse.jface.action.Action#getText()
*/
@Override
public String getText() {
//return no text, since the images of the stars will be displayed
return "";
}
...
}
Does somebody know why this strange behaviour appears?
Thanks a lot.
(For some strange reason, the images don't appear. Here are the direct URLs:
http://img187.imageshack.us/img187/4427/starsratingho4.png
http://img514.imageshack.us/img514/8673/contextmenuproblemgt1.png)
//Edit:
I did some tries and it seems as if the images just don't appear when using a Checkbox style for the context menu (see constructor of the RateAction). When I switched to a PushButton style, the images appeared, although not correctly scaled, but at least they were shown.