开发您的第一个 Eclipse RCP 应用程序(13/12)

来源:互联网 发布:网络信息安全管理制度 编辑:程序博客网 时间:2024/05/21 18:31
 

主-从关系的表

应用程序经常提供对象的汇总列表。选中一个列表后,详细信息就会显示在表单中。此类功能可以被编码到 JFace 数据绑定中,方法是使用一个集合小部件,例如 List 或 Table。然后可以将选中的值绑定到显示详细信息记录的目标表单上。

实现此功能的第一个步骤是创建另一个 Presentation Model 来保存要显示的表的列表。清单 19 显示了此功能的代码。这个 Presentation Model 还保存了一个 WritableValue 以保留表的选项状态。请再次注意,所有状态都被从 UI 表小部件中提取出来,并用 Presentation Model 来表示。



清单 19. TableForm 的 Presentation Model
                    public class TablePresentationModel extends PropertyChangeAware {    private List contacts;    private WritableValue selectedContact;    public TablePresentationModel(List contacts) {        this.contacts = contacts;        this.selectedContact = new WritableValue(Contact.class);        this.selectedContact.setValue(contacts.get(0));    }    public List getContacts() {        return contacts;    }    public void setContacts(List contacts) {        this.contacts = contacts;    }    public WritableValue getSelectedContactObservable() {        return selectedContact;    }    public void setSelectedContactObservable/    (WritableValue selectedContact) {        this.selectedContact = selectedContact;    }}

创建了 Presentation Model 之后,现在需要一个 UI。清单 20 显示了 TableForm 类的代码。



清单 20. TableForm 的实现
                    public class TableForm {        private TableViewer contactsTableViewer;        public TableForm(Composite c, TablePresentationModel presentationModel) {        createControls(c);        bindGUI(presentationModel);    }    private void bindGUI(TablePresentationModel presentationModel){        DataBindingContext ctx = createContext();        ctx.bind(new Property(this.contactsTableViewer, ViewersProperties.CONTENT), new TableModelDescription(new Property(presentationModel, "contacts", Contact.class, true), new String[] {"name", "spouse"}), null);        ctx.bind(new Property(this.contactsTableViewer, ViewersProperties.SINGLE_SELECTION), presentationModel.getSelectedContactObservable(), null);    }    private void createControls(Composite c) {        GridData gridData = new GridData(GridData.FILL_HORIZONTAL);        gridData.horizontalSpan = 2;        this.contactsTableViewer = new TableViewer(c, SWT.BORDER);        this.contactsTableViewer.getTable().setLayoutData(gridData);    }    public static DataBindingContext createContext() {        DataBindingContext context = new DataBindingContext();        context.addObservableFactory(new BeanObservableFactory(context, null,                new Class[] { Widget.class }));        context.addObservableFactory(new SWTObservableFactory());        context.addObservableFactory(new ViewersObservableFactory());        context.addBindSupportFactory(        new DefaultBindSupportFactory());        context.addBindingFactory(new DefaultBindingFactory());        context.addBindingFactory(new ViewersBindingFactory());        return context;    }}

类似于 ContactFormTableForm 也创建一个 Presentation Model 并将其内容绑定到 UI 上。在本例中,小部件是一张表。bindGUI() 方法中的第一行将列表中的 Contact 对象从 Presentation Model 连接到表上。此处并没有使用绑定时提供的简单的 Property 对象,而是使用了 TableModelDescription 对象。此对象允许传递一个字符串数组以表示要将 Contact 对象的哪些属性绑定到表中的列上。方法中的第二个绑定行将把表中的选定值绑定到在 Presentation Model 中创建的 WritableValue 选项保存程序上。最后,注意类定义末尾的 createContext() 方法将把 ViewerObservableFactoryViewersBindingFactory 添加到上下文中。没有这些工厂,上下文将不知道如何将数据与表绑定在一起。

这是一个很好的停止点来测试迄今为止的代码。用清单 21 中的代码修改示例运行程序。这段代码将创建一些示例联系人,并将这些信息传递给 Presentation Model,然后构建 TableForm。在运行程序上单击鼠标右键并以 SWT 应用程序来运行,将打开类似图 9 所示的窗口。



清单 21. 修改示例运行程序以尝试 TableForm
                    Contact contact = new Contact();List contacts = new ArrayList();contacts.add(new Contact("John Smith", "Jane Smith"));contacts.add(new Contact("John Smith2", "Jane Smith2"));contacts.add(new Contact("John Smith3", "Jane Smith3"));contacts.add(new Contact("John Smith4", "Jane Smith4"));TablePresentationModel tablePresentationModel = new TablePresentationModel(        contacts);TableForm tableForm = new TableForm(shell, tablePresentationModel);ContactPresentationModel presentationModel = new ContactPresentationModel(        contact);ContactForm contactForm = new ContactForm(shell, presentationModel);



图 9. 实现主-从关系的 UI 示例
实现主-从关系的 UI 示例 
原创粉丝点击