|
[databinding] TreeViewer [message #331039 is a reply to message #331021] |
Thu, 21 August 2008 19:40 |
Eclipse User |
|
|
|
Originally posted by: krzysztof.daniel.gmail.com
Hi there,
I am trying to learn databinding and I have run into following problem:
I have a tree displayed in a TreeViewer (with databinding):
Account
+- Folder
Now, Folder has messages, and I would like to show only the number of
messages in the Folder:
Account
+- Folder (4)
Then I populate messages in the background job and the Folder label is
*not* updated.
I use the code found somewhere in examples
(TreeStructureAdvisor/IObservableFactory).
How can I update the label of Folder when its observered message list
changes?
TIA
Chris
|
|
|
|
Re: [databinding] TreeViewer [message #331067 is a reply to message #331042] |
Fri, 22 August 2008 19:12 |
Eclipse User |
|
|
|
Originally posted by: krzysztof.daniel.gmail.com
Thank you, thank you, thank you!
I was able to achieve the goal but I had to modify slightly your code,
basically because I had to support different objects (attached below).
The only sad thing is that you have access only to ViewerLabel and you
cannot use styled labels... any idea? should I open RFE?
Regards,
Chris
public class ServersLabelProvider extends ListeningLabelProvider {
public ServersLabelProvider(IObservableSet itemsThatNeedLabels) {
super(itemsThatNeedLabels);
}
public String prepareText(Object element) {
if (element instanceof ServerManager) {
return "Mail accounts";
}
if (element instanceof Server) {
return ((Server) element).getName();
}
if (element instanceof Folder) {
return ((Folder) element).getName() + " "
+ ((Folder) element).getMessages().size();
}
return null;
}
Adapter adapter;
@Override
protected void addListenerTo(Object next) {
if (adapter == null) {
if (next instanceof Folder) {
adapter = new Adapter() {
@Override
public Notifier getTarget() {
return null;
}
@Override
public boolean isAdapterForType(Object type) {
return true;
}
@Override
public void notifyChanged(final Notification notification) {
Display.getDefault().syncExec(new Runnable(){
@Override
public void run() {
fireChangeEvent(Collections.singleton(notification
.getNotifier()));
}
});
}
@Override
public void setTarget(Notifier newTarget) {
}
};
((Folder) next).eAdapters().add(adapter);
}
}
}
@Override
protected void removeListenerFrom(Object next) {
if (adapter != null && next instanceof Folder) {
((Folder) next).eAdapters().remove(adapter);
}
}
public void updateLabel(ViewerLabel label, Object element) {
label.setText(prepareText(element));
}
}
On Thu, 2008-08-21 at 15:18 -0700, Matthew Hall wrote:
> You need two things: the Folder object must fire some sort of change
> event, and you need an observable capable of receiving those event
> dispatches.
>
> For your situation, I recommend extending ListeningLabelProvider. You
> will have to implement the addListenerTo() and removeListenerFrom()
> methods, and you will likely want to reimplement the updateLabel()
> method as well.
>
> class FolderLabelProvider extends ListeningLabelProvider {
> public FolderLabelProvider(IObservableSet knownElements) {
> super(knownElements);
> }
>
> // Must lazy init because addListenerTo is called by superconstructor
> PropertyChangeListener listener;
>
> protected void addListenerTo(Object o) {
> if (listener == null) {
> listener = new PropertyChangeListener() {
> public void propertyChanged(PropertyChangeEvent e) {
> // If properties may be changed in another thread, you may
> // have to wrap this method call in a Realm.asyncExec()
> fireChangeEvent(Collections.singleton(e.getSource()));
> }
> }
> }
> ((Folder)o).addPropertyChangeListener(listener);
> }
>
> protected void removeListenerFrom(Object o) {
> if (listener != null) {
> ((Folder)o).removePropertyChangeListener(listener);
> }
> }
>
> public void updateLabel(ViewerLabel label, Object element) {
> Folder folder = (Folder) element;
> String text = folder.getLabel();
> if (folder.getUnreadItemCount() > 0)
> text = text + " ("+folder.getUnreadItemCount()+")";
> label.setText(text);
> }
> }
>
> Matthew
>
> > Hi there,
> >
> > I am trying to learn databinding and I have run into following problem:
> > I have a tree displayed in a TreeViewer (with databinding):
> >
> > Account
> > +- Folder
> >
> > Now, Folder has messages, and I would like to show only the number of
> > messages in the Folder:
> > Account
> > +- Folder (4)
> >
> > Then I populate messages in the background job and the Folder label is
> > *not* updated.
> >
> > I use the code found somewhere in examples
> > (TreeStructureAdvisor/IObservableFactory).
> >
> > How can I update the label of Folder when its observered message list
> > changes?
> >
> > TIA
> > Chris
> >
|
|
|
|
Powered by
FUDForum. Page generated in 0.02735 seconds