Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Rich Client Platform (RCP) » Decorator Problem(How ro enable/disable decorators?)
Decorator Problem [message #665175] Wed, 13 April 2011 14:46 Go to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 49
Registered: March 2010
Member
Hi all,
I'm trying to create a Decorator that changes the background in an alerts table
depending on the severity of the Alert and I would like to be able to enable/disable
the decorator at runtime. So I have declare the following extension point:
   <extension
         point="org.eclipse.ui.decorators">
      <decorator
            class="net.morcate.console.alerts.views.AlertsDecorator"
            id="net.morcate.console.alerts.decorator1"
            label="BackgroundDecorator"
            lightweight="false"
            state="true">
         <enablement>
            <objectClass
                  name="net.morcate.bos.alerts.Alert">
            </objectClass>
         </enablement>
      </decorator>

I have implemented the decorator AlertsDecorator
public class AlertsDecorator extends LabelProvider implements ILabelDecorator,
		IColorDecorator {
...
}

and the label provider as:
public class AlertsLabelProvider extends ColumnLabelProvider implements IStyledLabelProvider{
...
}

In my view I have the following:
private void makeColumns(TableColumnLayout layout, ColumnSortAdapter columnSortAdapter) {
    TableViewerColumn col=null; 
    ...
       // for each column in the table:
       IDecoratorManager  decoratorManager= getSite().getWorkbenchWindow().getWorkbench().getDecoratorManager();
       decorator = decoratorManager.getLabelDecorator("net.morcate.console.alerts.decorator1");

       DecoratingStyledCellLabelProvider  decoratingLabelProvider = new DecoratingStyledCellLabelProvider(alertsLabelProvider, decorator, null);
       col.setLabelProvider(decoratingLabelProvider);

In a preference page I have a toggle to enable/disable the decorator:
	@Override
	public boolean performOk() {
		try {
			this.saveValues(this.getPreferenceStore());
			PlatformUI.getWorkbench().getDecoratorManager().setEnabled("net.morcate.console.alerts.decorator1", this.useBackgroundHighLighting);
		return true;
	}

The problem is that this does not change the rendering in the table. If I close
and reopen the view, the table is displayed as expected from the state of the
decorator. So I guess that I have to ask the table viewer to redraw itself but I
don't kown how. Any help?

Thank you very much.

Joaquin Morcate
Re: Decorator Problem [message #665225 is a reply to message #665175] Wed, 13 April 2011 19:14 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin GereaFriend
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

I had a similar problem. My solution is bellow.

In my view I have a property change listener (from org.eclipse.jface.util) that triggers the refresh of the table:
IPropertyChangeListener preferenceListener = new IPropertyChangeListener() {
    /*
     * @see IPropertyChangeListener.propertyChange()
     */
    public void propertyChange(PropertyChangeEvent event) {
      if (event.getProperty().equals(IPreferenceConstants.P_BOOK_DISPLAY_MODE)) {
        tableViewer.refresh(true);
      }
      if (event.getProperty().equals(
          IPreferenceConstants.P_CHECK_COLLECTION_STATUS)) {
        tableViewer.refresh(true);
      }
    }
  };


In the init() method of the view I add the listener to the preference store:
BooksActivator.getDefault().getPreferenceStore().addPropertyChangeListener(
        preferenceListener);


And finally in the dispose method I remove the listner from the preference store:
BooksActivator.getDefault().getPreferenceStore()
        .removePropertyChangeListener(preferenceListener);


Maybe there is a nicer solution to this, but I couldn't find it.

HTH
Catalin


Time is what you make of it.
Re: Decorator Problem [message #665323 is a reply to message #665225] Thu, 14 April 2011 10:32 Go to previous messageGo to next message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 49
Registered: March 2010
Member
Hi Catalin,

Thank you very much for your support. Unfortunately I didn't work for me.

I've been debuging how the viewer update the table viewer and I have found that the ViewerColumn class has a method called refresh(ViewerCell vc) that call the update(cell) in the label provider (in my case is an instance of DecoratingStyledCellLabelProvider. This is the place where the cell rendering is evaluated. This is the code to set the background color:
if (this.decorator instance of IColorDecorator) {
    Color color=((IColorDecorator)this.decorator).
        decorateBackground(element);
    if (color!=null)
        return color;
}
return super.getBackground(element);

Nowhere is the state of the decorator, enabled or disabled, checked; or at least I cannot find it.

Furthermore, there is something wrong in the way that I create the label provider:
private void makeColumns(TableColumnLayout layout, ColumnSortAdapter columnSortAdapter) {
    TableViewerColumn col=null; 
    ...
       // for each column col in the table:
       IDecoratorManager  decoratorManager= getSite().getWorkbenchWindow().getWorkbench().getDecoratorManager();
       decorator = decoratorManager.getLabelDecorator("net.morcate.console.alerts.decorator1");

       DecoratingStyledCellLabelProvider  decoratingLabelProvider = new DecoratingStyledCellLabelProvider(alertsLabelProvider, decorator, null);
       col.setLabelProvider(decoratingLabelProvider);
     ...
}

If the decorator is disabled, the decorator returned by the decoratorManager will be "null" and that is what will be stored in the decorator field member in my instance of DecoratingStyledCellLabelProvider. So, it doesn't matter if I enable/disable the decorator afterwards, the label provider will have a null decorator for ever.

The only solution that I can see is, reusing your suggestion, is to re-instance the label provider in the property change listener. Instead of just calling view.refresh(true), to do the following:

    public void propertyChange(PropertyChangeEvent event) {
      if (event.getProperty().equals(IPreferenceConstants.P_BOOK_DISPLAY_MODE)) {
         IDecoratorManager  decoratorManager= getSite().getWorkbenchWindow().getWorkbench().getDecoratorManager();
         decorator = decoratorManager.getLabelDecorator("net.morcate.console.alerts.decorator1");
         for (each column col in the table) {
       	     DecoratingStyledCellLabelProvider  decoratingLabelProvider = new DecoratingStyledCellLabelProvider(alertsLabelProvider, decorator, null);
             col.setLabelProvider(decoratingLabelProvider);
	 }
      }
      if (event.getProperty().equals(
          IPreferenceConstants.P_CHECK_COLLECTION_STATUS)) {
        //same thing.
      }


But then I can see the convenience of using decorators.

Maybe my approach is complete wrong and I am trying to use decorators in the way they are supposed to be used. I could be following a dead road. Any ideas?

Joaquin Morcate
Re: Decorator Problem [message #665370 is a reply to message #665323] Thu, 14 April 2011 13:13 Go to previous messageGo to next message
Catalin Gerea is currently offline Catalin GereaFriend
Messages: 89
Registered: July 2009
Location: Bucharest, Romania
Member

Just a some remarks:
1.
 if (event.getProperty().equals(IPreferenceConstants.P_BOOK_DISPLAY_MODE)) {
should be replaced with name of your property from your preference store.
2. Unfortunately my code did not use Decorators as label provider so it's not exactly similar to your situation (I used the property from the preference store in the LabelProvider without depending on another object state (like the Alert in your case)). Further more my experience with decorators is still limited to a small project with very simple scenario.
3. Maybe you need to issue
PlatformUI.getWorkbench().getDecoratorManager().update(id_of_your_decorator); 
in the propertyChange() method to trigger the change (although I guess that you should have a ILabelProviderListener in your code or your LabelProvider should override isLabelProperty() method).

I think that the correct question in your case is: how to trigger the change the label provider of a table at runtime.

In case that you have solved this problem in a different way just let us know how.


Time is what you make of it.
Re: Decorator Problem [message #666005 is a reply to message #665370] Mon, 18 April 2011 15:12 Go to previous message
Joaquin Morcate is currently offline Joaquin MorcateFriend
Messages: 49
Registered: March 2010
Member
Hi Catalin,

Sorry for the delay in my answer.

I have tried both of your suggestions (calling update on the decorator manager and overriding the isLabelProperty) and none of the worked. The only solution that I've been able to find is the one that I mentioned in my previous answer, adding a viewer.refresh() call (and changing the name of the properties of course): I have to create new label providers!

Anyhow, I am a bit confused about how the workbench deals with decorators. My idea was to chain different decorators so the user can decide how he or she what the alerts to be highlighted. Unfortunately, DecoratingStyledCellLabelProvider does not support this. For example, you can code the following:

DecoratingLabelProvider dlp = new DecoratingLabelProvider(
                                                                  new DecoratingLabelProvider(aLabProv, deco1),
                                                                  deco2);

because DecoratingLabelProvider implements ILabelProvider and has a contructor that takes as arguments ILabelProvider and ILabelDecorator. But this does not apply to DecoratingStyledCellLabelProvider.

I will let you know if I can find a better solution for this problem.

Thank you very much for your help

Joaquin
Previous Topic:Multiple applications bundled in a single product / DS conflicts
Next Topic:Display URL object into eclipse.swt.Browser
Goto Forum:
  


Current Time: Sat Nov 22 09:12:41 GMT 2014

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

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