Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » [Databinding] Table cells to bean properties
[Databinding] Table cells to bean properties [message #538096] Fri, 04 June 2010 23:19
Mark Storer is currently offline Mark Storer
Messages: 46
Registered: May 2010
Location: Vista, CA
Member

I'm trying to make an editable table from an array of beans.

class myBean {
  /* lets assume it has two String properties, A & B, with property change support */
}

/**
 * Here's something spiffy I've been working on.  Consider it released under the Eclipse License if you want to use it.
 * It's actually pretty simple.
 */
public class BeanLabelProvider extends LabelProvider implements
            ITableLabelProvider {
        Class<?>           beanClass;
        PropertyDescriptor descriptors[];

        public BeanLabelProvider(Class<?> beanClass_) {
            beanClass = beanClass_;
            try {
                descriptors = Introspector.getBeanInfo(beanClass)
                        .getPropertyDescriptors();
            } catch (IntrospectionException ie) {
                throw new RuntimeException(ie);
            }
        }

        @Override
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }

        @Override
        public String getColumnText(Object element, int columnIndex) {
            assert element.getClass().equals(beanClass);
            if (columnIndex > descriptors.length) {
                return null;
            }

            try {
                PropertyDescriptor curProp = descriptors[columnIndex];
                if (curProp.getPropertyType().equals(String.class)) {
                    return (String) curProp.getReadMethod().invoke(element,
                            new Object[] {});
                } else {
                    return curProp.getReadMethod().invoke(element,
                            new Object[] {}).toString();
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    }
}

class MyDialog extends Dialog
  ArrayList<MyBean> beans = MagicBeans.getBeans();
  DataBindingContext dc = new DataBindingContext();
 
  ...

  @Override
  protected Control createContents(Composite parent_) {
    FormToolkit kit = GetFormToolkit();
    final Table beanTable = toolkit.createTable(parent_, SWT.SINGLE);
    beanTable.setHeaderVisible(true);
    beanTable.setLinesVisible(true);
    
    final TableViewer beanView = new TableViewer(beanTable);
    PropertyDescriptor descriptors[] = null;
    try {
      descriptors = Introspector.getBeanInfo(MyBean.class).getPropertyDescriptors();
    } catch (IntrospectionException e) {
      ...
      return null;
    }
    for (PropertyDescriptor desc : descriptors) {
      final TableColum col = new TableColumn(beanTable, SWT.RIGHT);
      col.setText(desc.getDisplayName();
      col.setToolTipText(desc.getShourDescription());

      /* I've tried messing around with TableViewerColumn and EditingSupport
         in here to no avail. */
    }

    tableView.setContentProvider(new ArrayContentProvider());
    tableView.setLabelProvider(new BeanLabelProvider(MyBean.class));
    tableView.setInput(beans);

     // I've also tried using TableViewerEditor.create(...) here, with a similar 
     //lack of success.
  }


In addition to being incapable of creating editors at all, much less ones capable of exchanging data with MyBean, I've run into a couple Other Oddities:
1) I have a very narrow third column on my table, with no label (at least that makes sense). ?!
2) I can only select the first column of my table.
3) Using SWT.FULL_SELECTION (as required for editors) breaks my table completely. The columns still appear (all three of them, grumble grumble), but the table has a single empty row. No labels.


PS: I'm running on Windows 7, with a 64-bit Intel.

EDIT: tableView.getCellEditors() also returns null.


--Mark Storer
Senior Software Engineer
Autonomy Cardiff

import legalese.disclaimer;
Disclaimer<Cardiff> disCard = null;

Google Code Search

[Updated on: Mon, 07 June 2010 17:21]

Report message to a moderator

Previous Topic:Slider & DataBinding
Next Topic:CheckBoxTableViewer
Goto Forum:
  


Current Time: Tue Sep 16 13:35:49 GMT 2014

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

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