Home » Eclipse Projects » Eclipse Platform » Does jface databinding support treeviewer?
| | | | | | | | | |
Re: Does jface databinding support treeviewer? [message #328562 is a reply to message #328549] |
Tue, 27 May 2008 16:42 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Hao Missing name![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=d95776%40yahoo.com) Messages: 115 Registered: July 2009 |
Senior Member |
|
|
Here's the code.
package com.discover.plugin.phonebook.treeviewer.v34;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import org.eclipse.core.databinding.DataBindingContext;
import org.eclipse.core.databinding.UpdateValueStrategy;
import org.eclipse.core.databinding.beans.BeansObservables;
import org.eclipse.core.databinding.observable.IObservable;
import org.eclipse.core.databinding.observable.Realm;
import org.eclipse.core.databinding.observable.map.IObservableMap;
import
org.eclipse.core.databinding.observable.masterdetail.IObserv ableFactory;
import org.eclipse.core.databinding.observable.value.IObservableVal ue;
import org.eclipse.core.internal.databinding.beans.JavaBeanObservab leMap;
import
org.eclipse.core.internal.databinding.observable.EmptyObserv ableList;
import org.eclipse.core.runtime.IStatus;
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.ViewersObservables;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
/**
*
* @author hchen
* The code is not a good solution
* Always get error: org.eclipse.core.databinding.BindingException: Could
not find property with name email in class class
com.discover.plugin.model.PersonGroup
*/
public class PhoneBookUsingTreeViewerEx {
class AbstractModelObject {
private final PropertyChangeSupport propertyChangeSupport = new
PropertyChangeSupport(
this);
public void addPropertyChangeListener(PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(listener);
}
public void addPropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.addPropertyChangeListener(propertyName , listener);
}
public void removePropertyChangeListener(PropertyChangeListener
listener) {
propertyChangeSupport.removePropertyChangeListener(listener) ;
}
public void removePropertyChangeListener(String propertyName,
PropertyChangeListener listener) {
propertyChangeSupport.removePropertyChangeListener(propertyN ame,
listener);
}
protected void firePropertyChange(String propertyName, Object oldValue,
Object newValue) {
propertyChangeSupport.firePropertyChange(propertyName, oldValue,
newValue);
}
}
class PersonPhone extends AbstractModelObject{
private String m_name = "";
private String m_email = "";
private String m_phone = "";
private String m_mobilePhone1 = "";
private String m_mobilePhone2 = "";
public PersonPhone() {
}
public PersonPhone(String name, String email, String phone, String
phone1,
String phone2) {
m_name = name;
m_email = email;
m_phone = phone;
m_mobilePhone1 = phone1;
m_mobilePhone2 = phone2;
}
public String getName() {
return m_name;
}
public void setName(String name) {
String oldValue = m_name;
m_name = name;
firePropertyChange("name", oldValue, m_name);
}
public String getEmail() {
return m_email;
}
public void setEmail(String email) {
String oldValue = m_email;
m_email = email;
firePropertyChange("email", oldValue, m_email);
}
public String getPhone() {
return m_phone;
}
public void setPhone(String phone) {
String oldValue = m_phone;
m_phone = phone;
firePropertyChange("phone", oldValue, m_phone);
}
public String getMobilePhone1() {
return m_mobilePhone1;
}
public void setMobilePhone1(String phone1) {
String oldValue = m_mobilePhone1;
m_mobilePhone1 = phone1;
firePropertyChange("mobilePhone1", oldValue, m_mobilePhone1);
}
public String getMobilePhone2() {
return m_mobilePhone2;
}
public void setMobilePhone2(String phone2) {
String oldValue = m_mobilePhone2;
m_mobilePhone2 = phone2;
firePropertyChange("mobilePhone2", oldValue, m_mobilePhone2);
}
@Override
public String toString() {
StringBuilder strBuf = new StringBuilder();
strBuf.append("name=");
strBuf.append(m_name);
strBuf.append(";");
strBuf.append("email=");
strBuf.append(m_email);
strBuf.append(";");
strBuf.append("phone=");
strBuf.append(m_phone);
strBuf.append(";");
strBuf.append("mobilePhone1=");
strBuf.append(m_mobilePhone1);
strBuf.append(";");
strBuf.append("mobilePhone2=");
strBuf.append(m_mobilePhone2);
return strBuf.toString();
}
}
class PersonPhoneGroup extends AbstractModelObject{
private final List m_persons = new ArrayList();
private String m_name;
public PersonPhoneGroup() {
}
public PersonPhoneGroup(String name) {
m_name = name;
}
public String getName() {
return m_name;
}
public void setName(String name) {
String oldValue = m_name;
m_name = name;
firePropertyChange("name", oldValue, m_name);
}
public void addPersonPhone(PersonPhone person) {
m_persons.add(person);
firePropertyChange("persons", null, m_persons);
}
public void removePersonPhone(PersonPhone person) {
m_persons.remove(person);
firePropertyChange("persons", null, m_persons);
}
public List getPersons() {
return m_persons;
}
public String getEmail() {
return "";
}
public void setEmail(String email) {
}
public String getPhone() {
return "";
}
public void setPhone(String phone) {
}
public String getMobilePhone1() {
return "";
}
public void setMobilePhone1(String phone1) {
}
public String getMobilePhone2() {
return "";
}
public void setMobilePhone2(String phone2) {
}
@Override
public String toString() {
String RL = System.getProperty("line.separator");
StringBuilder strBuf = new StringBuilder();
strBuf.append("Person Group - ");
strBuf.append(m_name);
strBuf.append(RL);
for(Iterator it= m_persons.iterator(); it.hasNext(); ){
strBuf.append(it.next());
strBuf.append(RL);
}
return strBuf.toString();
}
}
class PersonPhoneGroups extends AbstractModelObject {
private List m_groups;
public PersonPhoneGroups(){
m_groups = new ArrayList();
}
public void addGroup(PersonPhoneGroup group) {
m_groups.add(group);
firePropertyChange("groups", null, m_groups);
}
public void removeGroup(PersonPhoneGroup group) {
m_groups.remove(group);
firePropertyChange("groups", null, m_groups);
}
public List getGroups() {
return m_groups;
}
@Override
public String toString() {
String RL = System.getProperty("line.separator");
StringBuilder strBuf = new StringBuilder();
strBuf.append("Person Groups");
strBuf.append(RL);
for(Iterator it= m_groups.iterator(); it.hasNext(); ){
strBuf.append(it.next());
strBuf.append(RL);
}
return strBuf.toString();
}
public String getName() {
return "";
}
public void setName(String name) {
}
public String getEmail() {
return "";
}
public void setEmail(String email) {
}
public String getPhone() {
return "";
}
public void setPhone(String phone) {
}
public String getMobilePhone1() {
return "";
}
public void setMobilePhone1(String phone1) {
}
public String getMobilePhone2() {
return "";
}
public void setMobilePhone2(String phone2) {
}
}
public class PersonPhoneObservableFactory implements IObservableFactory{
public PersonPhoneObservableFactory() {
super();
}
public IObservable createObservable(Object target) {
if( target instanceof PersonPhoneGroups ){
return BeansObservables.observeList(Realm.getDefault(), target,
"groups", PersonPhoneGroup.class);
}else if( target instanceof PersonPhoneGroup ){
return BeansObservables.observeList(Realm.getDefault(), target,
"persons", PersonPhone.class);
}else{
return new EmptyObservableList(Realm.getDefault(), null);
}
}
}
private DataBindingContext m_bindingContext;
private TreeColumn newColumnTreeColumn_4;
private TreeColumn newColumnTreeColumn_3;
private TreeColumn newColumnTreeColumn_2;
private TreeColumn newColumnTreeColumn_1;
private TreeColumn newColumnTreeColumn;
private Tree tree;
private TreeViewer m_personViewer;
private PersonPhoneGroups m_groups = new PersonPhoneGroups();
private Text m_mobile2Text;
private Text m_mobile1Text;
private Text m_phoneText;
private Text m_emailText;
private Text m_nameText;
protected Shell shell;
class TreeLabelProvider implements ITableLabelProvider {
public String getColumnText(Object element, int columnIndex) {
if (columnIndex == 0) {
if (element instanceof PersonPhoneGroup) {
return ((PersonPhoneGroup)element).getName();
} else if (element instanceof PersonPhone) {
return ((PersonPhone)element).getName();
}
} else if (element instanceof PersonPhone) {
PersonPhone person = (PersonPhone) element;
switch (columnIndex) {
case 1:
return person.getEmail();
case 2:
return person.getPhone();
case 3:
return person.getMobilePhone1();
case 4:
return person.getMobilePhone2();
default:
return "";
}
}
return "";
}
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
}
private void setDefaultValues() {
PersonPhoneGroup group1 = new PersonPhoneGroup("Developer Team");
m_groups.addGroup(group1);
//
PersonPhoneGroup group2 = new PersonPhoneGroup("Management Team");
m_groups.addGroup(group2);
//
PersonPhoneGroup group3 = new PersonPhoneGroup("Support Team");
m_groups.addGroup(group3);
group1.addPersonPhone(new PersonPhone("Konstantin Scheglov",
"kosta@nospam.com",
"1234567890", "", ""));
group1.addPersonPhone(new PersonPhone("Alexander Mitin",
"mitin@nospam.com", "",
"0987654321", ""));
group1.addPersonPhone(new PersonPhone("Alexander Lobas",
"lobas@nospam.com", "",
"", "111-222-333-00"));
group2.addPersonPhone(new PersonPhone("Mike Taylor",
"taylor@instantiations.com",
"503-598-4900", "", ""));
group2.addPersonPhone(new PersonPhone("Eric Clayberg",
"clayberg@instantiations.com", "+1 (503) 598-4900", "", ""));
group2.addPersonPhone(new PersonPhone("Dan Rubel",
"dan@instantiations.com",
"503-598-4900", "", ""));
group3.addPersonPhone(new PersonPhone("Gina Nebling",
"support@instantiations.com", "800-808-3737", "", ""));
}
public static void main(String[] args) {
Display display = new Display();
Realm.runWithDefault(SWTObservables.getRealm(display), new Runnable() {
public void run() {
try {
PhoneBookUsingTreeViewerEx window = new PhoneBookUsingTreeViewerEx();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Open the window
*/
public void open() {
final Display display = Display.getDefault();
setDefaultValues();
createContents();
initDataBindings0();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch())
display.sleep();
}
}
/**
* Create contents of the window
*/
protected void createContents() {
shell = new Shell();
shell.setLayout(new FillLayout());
shell.setSize(789, 517);
shell.setText("Phone Book");
final SashForm sashForm = new SashForm(shell, SWT.VERTICAL);
m_personViewer = new TreeViewer(sashForm, SWT.FULL_SELECTION);
//m_personViewer.setLabelProvider(new TreeLabelProvider());
tree = m_personViewer.getTree();
tree.setLinesVisible(true);
tree.setHeaderVisible(true);
/**
newColumnTreeColumn = new TreeColumn(tree, SWT.NONE);
newColumnTreeColumn.setWidth(148);
newColumnTreeColumn.setText("Name");
newColumnTreeColumn_1 = new TreeColumn(tree, SWT.NONE);
newColumnTreeColumn_1.setWidth(168);
newColumnTreeColumn_1.setText("E-mail");
newColumnTreeColumn_2 = new TreeColumn(tree, SWT.NONE);
newColumnTreeColumn_2.setWidth(119);
newColumnTreeColumn_2.setText("Phone");
newColumnTreeColumn_3 = new TreeColumn(tree, SWT.NONE);
newColumnTreeColumn_3.setWidth(100);
newColumnTreeColumn_3.setText("Mobile Phone 1");
newColumnTreeColumn_4 = new TreeColumn(tree, SWT.NONE);
newColumnTreeColumn_4.setWidth(100);
newColumnTreeColumn_4.setText("Mobile Phone 2");
**/
final Composite detailComposite = new Composite(sashForm, SWT.BORDER);
final GridLayout gridLayout_2 = new GridLayout();
gridLayout_2.numColumns = 2;
detailComposite.setLayout(gridLayout_2);
final Label descriptionLabel = new Label(detailComposite, SWT.NONE);
descriptionLabel.setText("Description:");
new Label(detailComposite, SWT.NONE);
final Label label = new Label(detailComposite, SWT.NONE);
label.setText("Name:");
m_nameText = new Text(detailComposite, SWT.BORDER);
final GridData gd_m_nameText = new GridData(SWT.FILL, SWT.CENTER, true,
false);
m_nameText.setLayoutData(gd_m_nameText);
final Label emailLabel = new Label(detailComposite, SWT.NONE);
emailLabel.setText("E-mail:");
m_emailText = new Text(detailComposite, SWT.BORDER);
final GridData gd_m_emailText = new GridData(SWT.FILL, SWT.CENTER,
true, false);
m_emailText.setLayoutData(gd_m_emailText);
final Label phoneLabel = new Label(detailComposite, SWT.NONE);
phoneLabel.setText("Phone:");
m_phoneText = new Text(detailComposite, SWT.BORDER);
final GridData gd_m_phoneText = new GridData(SWT.FILL, SWT.CENTER,
true, false);
m_phoneText.setLayoutData(gd_m_phoneText);
final Label mobilePhone1Label = new Label(detailComposite, SWT.NONE);
mobilePhone1Label.setText("Mobile Phone 1:");
m_mobile1Text = new Text(detailComposite, SWT.BORDER);
final GridData gd_m_mobile1Text = new GridData(SWT.FILL, SWT.CENTER,
true, false);
m_mobile1Text.setLayoutData(gd_m_mobile1Text);
final Label mobilePhone2Label = new Label(detailComposite, SWT.NONE);
mobilePhone2Label.setText("Mobile Phone 2:");
m_mobile2Text = new Text(detailComposite, SWT.BORDER);
final GridData gd_m_mobile2Text = new GridData(SWT.FILL, SWT.CENTER,
true, false);
m_mobile2Text.setLayoutData(gd_m_mobile2Text);
sashForm.setWeights(new int[] {218, 262 });
m_bindingContext = initDataBindings();
//m_bindingContext = initDataBindings();
//
}
class PersonViewerUpdateValueStrategy extends UpdateValueStrategy {
protected IStatus doSet(IObservableValue observableValue, Object value) {
Realm.getDefault().asyncExec(new Runnable() {
public void run() {
m_personViewer.refresh();
}
});
return super.doSet(observableValue, value);
}
}
protected DataBindingContext initDataBindings() {
ObservableListTreeContentProvider contentProvider =
new ObservableListTreeContentProvider((IObservableFactory)new
PersonPhoneObservableFactory(), null);
m_personViewer.setContentProvider(contentProvider);
IObservableMap[] personViewerLabelProviderMaps00 =
BeansObservables.observeMaps(contentProvider.getKnownElement s(),
PersonPhoneGroups.class, new String[]{"name", "email", "phone",
"mobilePhone1", "mobilePhone2"});
IObservableMap[] personViewerLabelProviderMaps0 =
BeansObservables.observeMaps(contentProvider.getKnownElement s(),
PersonPhoneGroup.class, new String[]{"name", "email", "phone",
"mobilePhone1", "mobilePhone2"});
IObservableMap[] personViewerLabelProviderMaps1 =
BeansObservables.observeMaps(contentProvider.getKnownElement s(),
PersonPhone.class, new String[]{"name", "email", "phone", "mobilePhone1",
"mobilePhone2"});
IObservableMap[] personViewerLabelProviderMaps = new
IObservableMap[personViewerLabelProviderMaps00.length+person ViewerLabelProviderMaps0.length+personViewerLabelProviderMap s1.length];
int count=0;
for(int i=0; i<personViewerLabelProviderMaps00.length; i++){
personViewerLabelProviderMaps[count++] =
personViewerLabelProviderMaps00[i];
}
for(int i=0; i<personViewerLabelProviderMaps0.length; i++){
personViewerLabelProviderMaps[count++] =
personViewerLabelProviderMaps0[i];
}
for(int i=0; i<personViewerLabelProviderMaps1.length; i++){
personViewerLabelProviderMaps[count++] =
personViewerLabelProviderMaps1[i];
}
/**
int mapLength = personViewerLabelProviderMaps.length;
System.out.println(mapLength);
for(int i=0; i<mapLength; i++){
IObservableMap map = personViewerLabelProviderMaps[i];
Set mapKeySet = map.keySet();
Iterator it = mapKeySet.iterator();
boolean go = it.hasNext();
while(go){
Object mapKey = it.next();
Object value = map.get(mapKey);
System.out.println(mapKey+","+value);
go = it.hasNext();
}
if( map instanceof JavaBeanObservableMap ){
JavaBeanObservableMap javaBmap = (JavaBeanObservableMap)map;
}
**/
m_personViewer.setLabelProvider(new
ObservableMapLabelProvider(personViewerLabelProviderMaps));
/**
IObservableMap map =
BeansObservables.observeMap(contentProvider.getKnownElements (),
PersonPhoneGroup.class, "name");
Set mapKeySet = map.keySet();
Iterator it = mapKeySet.iterator();
boolean go = it.hasNext();
while(go){
Object mapKey = it.next();
Object value = map.get(mapKey);
System.out.println(mapKey+","+value);
go = it.hasNext();
}
m_personViewer.setLabelProvider(new ObservableMapLabelProvider(map));
**/
//IObservableMap[] m_personViewerLabelProviderMaps =
BeansObservables.observeMaps(contentProvider.getKnownElement s(),
PersonPhoneGroup.class, new String[]{"persons"});
//m_personViewer.setLabelProvider(new
ObservableMapLabelProvider(m_personViewerLabelProviderMaps)) ;
m_personViewer.setInput(m_groups);
//
DataBindingContext bindingContext = new DataBindingContext();
//
//
return bindingContext;
}
public Image getColumnImage(Object element, int columnIndex) {
// TODO Auto-generated method stub
return null;
}
public void addListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
public void dispose() {
// TODO Auto-generated method stub
}
public boolean isLabelProperty(Object element, String property) {
// TODO Auto-generated method stub
return false;
}
public void removeListener(ILabelProviderListener listener) {
// TODO Auto-generated method stub
}
protected DataBindingContext initDataBindings0() {
IObservableValue m_mobile2TextTextObserveWidget =
SWTObservables.observeText(m_mobile2Text, SWT.Modify);
IObservableValue m_mobile1TextTextObserveWidget =
SWTObservables.observeText(m_mobile1Text, SWT.Modify);
IObservableValue m_personViewerSelectionObserveSelection =
ViewersObservables.observeSingleSelection(m_personViewer);
IObservableValue m_personViewerSelectionObserveSelection_1 =
ViewersObservables.observeSingleSelection(m_personViewer);
IObservableValue m_emailTextTextObserveWidget =
SWTObservables.observeText(m_emailText, SWT.Modify);
IObservableValue m_nameTextTextObserveWidget =
SWTObservables.observeText(m_nameText, SWT.Modify);
IObservableValue m_personViewerSelectionObserveSelection_1_1 =
ViewersObservables.observeSingleSelection(m_personViewer);
IObservableValue m_phoneTextTextObserveWidget =
SWTObservables.observeText(m_phoneText, SWT.Modify);
IObservableValue m_personViewerSelectionObserveSelection_1_1_1 =
ViewersObservables.observeSingleSelection(m_personViewer);
IObservableValue m_personViewerSelectionObserveSelection_1_1_1_1 =
ViewersObservables.observeSingleSelection(m_personViewer);
IObservableValue m_personViewerMobilePhone2ObserveDetailValue =
BeansObservables.observeDetailValue(Realm.getDefault(),
m_personViewerSelectionObserveSelection_1, "mobilePhone2",
java.lang.String.class);
IObservableValue m_personViewerMobilePhone1ObserveDetailValue =
BeansObservables.observeDetailValue(Realm.getDefault(),
m_personViewerSelectionObserveSelection_1_1, "mobilePhone1",
java.lang.String.class);
IObservableValue m_personViewerEmailObserveDetailValue =
BeansObservables.observeDetailValue(Realm.getDefault(),
m_personViewerSelectionObserveSelection_1_1_1, "email",
java.lang.String.class);
IObservableValue m_personViewerNameObserveDetailValue =
BeansObservables.observeDetailValue(Realm.getDefault(),
m_personViewerSelectionObserveSelection_1_1_1_1, "name",
java.lang.String.class);
IObservableValue m_personViewerPhoneObserveDetailValue =
BeansObservables.observeDetailValue(Realm.getDefault(),
m_personViewerSelectionObserveSelection, "phone", java.lang.String.class);
//
//
//DataBindingContext bindingContext = new DataBindingContext();
DataBindingContext bindingContext = m_bindingContext;
//
bindingContext.bindValue(m_personViewerNameObserveDetailValu e,
m_nameTextTextObserveWidget, null, new PersonViewerUpdateValueStrategy());
bindingContext.bindValue(m_personViewerEmailObserveDetailVal ue,
m_emailTextTextObserveWidget, null, new PersonViewerUpdateValueStrategy());
bindingContext.bindValue(m_personViewerPhoneObserveDetailVal ue,
m_phoneTextTextObserveWidget, null, new PersonViewerUpdateValueStrategy());
bindingContext.bindValue(m_personViewerMobilePhone1ObserveDe tailValue,
m_mobile1TextTextObserveWidget, null, new
PersonViewerUpdateValueStrategy());
bindingContext.bindValue(m_personViewerMobilePhone2ObserveDe tailValue,
m_mobile2TextTextObserveWidget, null, new
PersonViewerUpdateValueStrategy());
//
return bindingContext;
}
}
|
|
|
Re: Does jface databinding support treeviewer? [message #328567 is a reply to message #328502] |
Tue, 27 May 2008 18:08 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Matthew Hall![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=qualidafial%40gmail.com) Messages: 368 Registered: July 2009 |
Senior Member |
|
|
hao wrote:
> HI,
>
> I already did that but still get error. Please see the source code on my
> post of treeviewer databinding error. I think the problem is that the
> label provider is not generated correctly. Do you have any idea on how
> to create the label provider?
>
> thanks!
I see the problem. In the code that sets up the label providers, you
are using BeansObservables.observeMaps() and specifying
PersonPhone.class as the bean class. However PersonPhoneGroup instances
are not instanceof PersonPhone. Bean properties are not duck-typed so
there has to be common superclass for them.
You can subclass ListeningLabelProvider to accommodate viewers where
elements do not have properties in common. I suggest writing your own
subclass of ListeningLabelProvider which delegates to another label
provider based on the runtime type of the element:
class PersonGroupLabelProvider extends ListeningLabelProvider
implements ITableLabelProvider {
boolean initialized;
IObservableSet personPhoneKnownElements;
IObservableSet personGroupKnownElements;
ObservableMapLabelProvider personPhoneLabelProvider;
ObservableMapLabelProvider personPhoneGroupLabelProvider;
PersonGroupLabelProvider(IObservableSet knownElements) {
super(knownElements);
// Cannot init in the constructor because superconstructor calls
// addListenerTo. That aspect should probably be changed...
}
private void init() {
if (!initialized) {
personPhoneKnownElements = new WritableSet();
IObservableMap[] maps = BeansObservables.observeMaps(
personPhoneKnownElements,
PersonPhone.class,
new String[] { "name", "email", "phone", "mobilePhone1",
"mobilePhone2" } );
personPhoneLabelProvider = new ObservableMapLabelProvider(maps)
personPhoneGroupKnownElements = new WritableSet();
maps = BeansObservables.observeMaps(
personPhoneGroupKnownElements,
PersonPhoneGroup,
new String[] { "name" } );
personPhoneGroupLabelProvider =
new ObservableMapLabelProvider(maps);
initialized = true;
}
}
protected void addListenerTo(Object element) {
init();
if (element instanceof PersonPhone)
personPhoneKnownElements.add(element);
if (element instanceof PersonPhoneGroup)
personPhoneGroupKnownElements.add(element);
}
protected void removeListenerFrom(Object element) {
init();
if (element instanceof PersonPhone)
personPhoneKnownElements.remove(element);
if (element instanceof PersonPhoneGroup)
personPhoneGroupKnownElements.remove(element);
}
public void addListener(ILabelProviderListener listener) {
init();
personPhoneLabelProvider.addListener(listener);
personPhoneGroupLabelProvider.addListener(listener);
}
public void removeListener(ILabelProviderListener listener) {
init();
personPhoneLabelProvider.removeListener(listener);
personPhoneGroupLabelProvider.removeListener(listener);
}
// Implement the ITableLabelProvider methods so they delegate
// to personPhoneLabelProvider if the element is an instance of
// PersonPhone, or to personPhoneGroupLabelProvider if the element is
// an instanceof PersonPhoneGroup.
}
I also noticed that PersonPhoneGroup has several of the PersonPhone
properties, but they are not used. With the above changes you could
eliminate those.
Or you could just go the easy route and extract a common interface
between the two classes, then change the call to
BeansObservables.observeMaps() so it passes the extracted interface
instead of PersonPhone.class:
interface IPersonPhone {
public void setName(String name);
public String getName();
public void setEmail(String email);
// etc
}
Let us know what you decide to do, and how it goes.
Matthew
|
|
| | |
Re: Does jface databinding support treeviewer? [message #328582 is a reply to message #328567] |
Tue, 27 May 2008 21:43 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Hao Missing name![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=d95776%40yahoo.com) Messages: 115 Registered: July 2009 |
Senior Member |
|
|
Hi,
I have some questions regarding PersonGroupLabelProvider class
implmentations:
1. For its constructor - PersonGroupLabelProvider(IObservableSet
knownElements), what's the input - knownElements and how could I set it
up? can it be a null. It looks like the class does not use it at all.
2. What does the following call of method BeansObservables.observeMaps()
do to the argument - personPhoneKnownElements? I am trying to understand
why we need to pass a new instance of WritableSet(). I guess it should be
some kind of wrapper of the business model object - PersonPhoneGroups
clas. Actually, it is not. Why?
personPhoneKnownElements = new WritableSet();
IObservableMap[] maps = BeansObservables.observeMaps(
personPhoneKnownElements,
PersonPhone.class,
new String[] { "name", "email", "phone", "mobilePhone1",
"mobilePhone2" }
);
3. I have already implemented ITableLableProvider. Why do I need to
implement PersonGroupLabelProvider instead, just use my original
ITableLableProvider?
Using PersonGroupLabelProvider works for this test case. I just try
convince myself that's the way I must do.
Thanks a lot for your help.
|
|
|
Re: Does jface databinding support treeviewer? [message #328588 is a reply to message #328582] |
Tue, 27 May 2008 23:58 ![Go to previous message Go to previous message](theme/Solstice/images/up.png) ![Go to next message Go to next message](theme/Solstice/images/down.png) |
Matthew Hall![Friend of Eclipse Friend](/donate/web-api/friends_decorator.php?email=qualidafial%40gmail.com) Messages: 368 Registered: July 2009 |
Senior Member |
|
|
hao wrote:
> Hi,
>
> I have some questions regarding PersonGroupLabelProvider class
> implmentations:
>
> 1. For its constructor - PersonGroupLabelProvider(IObservableSet
> knownElements), what's the input - knownElements and how could I set it
> up? can it be a null. It looks like the class does not use it at all.
The input is contentProvider.getKnownElements(), just like with
ObservableMapLabelProvider
> 2. What does the following call of method BeansObservables.observeMaps()
> do to the argument - personPhoneKnownElements? I am trying to understand
> why we need to pass a new instance of WritableSet(). I guess it should
> be some kind of wrapper of the business model object - PersonPhoneGroups
> clas. Actually, it is not. Why? personPhoneKnownElements = new
> WritableSet();
> IObservableMap[] maps = BeansObservables.observeMaps(
> personPhoneKnownElements,
> PersonPhone.class,
> new String[] { "name", "email", "phone", "mobilePhone1",
> "mobilePhone2" } );
personPhoneKnownElements becomes the keySet of the maps in the returned
IObservableMap array. In each map, the value associated with each key
is the value of the named property.
Thus, given a set of users:
{ dave = ["Dave", "dave@example.com", "123-456-7890", null, null],
george = ["George", "george@nonexistent.org", "800-555-5555", null,
null] }
maps[0] == { dave : "Dave", george : "George" }
maps[1] == { dave : "dave@example.com", george : "george@nonexistent.org" }
etc
Thus, when you add or remove an element from the known elements set,
this automatically adds/removes the entry with that key from the
associated IObservableMaps.
The problem in your case is that you have two different types of objects
in your tree: PersonPhone and PersonPhoneGroup. However
BeansObservables.observeMaps(knownElementsSet, PersonPhone.class,
propertyArray) expects knownElementsSet to only contain instances of
PersonPhone. The original known elements set violates this constraint
by containing both types.
The solution I advocated separates the original known elements set into
two separate sets: personPhoneKnownElements (for PersonPhone instances)
and personPhoneGroupKnownElements (for PersonPhoneGroup instances).
These observable sets only contain instances of the correct types, so
they can be safely passed to BeansObservables.observeMaps().
The final part of the solution lies in the behavior of
ListeneingLabelProvider. ListeningLabelProvider calls the
addListenerTo(element) method for every element in the set at the time
of construction. It also registers an ISetChangeListener on the set,
and calls addListenerTo(element) and removeListenerFrom(element)
whenever elements are added to / removed from the set. We implement
these methods in PersonGroupLabelProvider and use instanceof to
determine which observable set the belong to.
> 3. I have already implemented ITableLableProvider. Why do I need to
> implement PersonGroupLabelProvider instead, just use my original
> ITableLableProvider?
>
> Using PersonGroupLabelProvider works for this test case. I just try
> convince myself that's the way I must do.
You need to implement ITableLabelProvider so that the tree viewer will
recognize that your label provider supports columns. It should not be
very involved, you just need to delegate each call to
personPhoneLabelProvider or personPhoneGroupLabelProvider depending on
the runtime type of the element.
Hope this helps.
Matthew
|
|
| | | | | | | | | | |
Goto Forum:
Current Time: Thu Aug 15 05:41:00 GMT 2024
Powered by FUDForum. Page generated in 0.06323 seconds
|