I had some time today to once more work a bit on UFaceKit and it’s viewer implementation and I think I added a feature which makes sense to show of.
I was working on the rewrite of an old application we wrote 3 years ago to use the Highlevel-Databinding-API I’m developing as part of the UFaceKit-Project. One of the common problems I had in my RCP-Application was that SWT-Combo-Widgets are not friendly if you have many entries because native keyboard selection is not supported on by default.
Inspired by a proposed component for JFace I wrote such a Viewer-Implementation from scratch which can be used like this:
/** * @param args */ public static void main(String[] args) { final Display d = new Display(); Shell shell = new Shell(d); shell.setLayout(new GridLayout()); Text text = new Text(shell,SWT.BORDER); text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); final Collection<Person> persons = new ArrayList<Person>(); persons.add(new Person(1, "Tom")); persons.add(new Person(2, "Udo")); persons.add(new Person(3, "Bernd")); persons.add(new Person(4, "Tom")); persons.add(new Person(5, "Thomas")); final ProposalViewer<Person, Collection<Person>> viewer = new ProposalViewer<Person, Collection<Person>>(text); viewer.setLabelConverter(new LabelConverter<Person>() { @Override public String getText(Person element) { return element.name + " ( ID-"+element.id+")"; } }); viewer.setContentProvider( new CollectionContentProvider<Person>() ); viewer.setInput(persons); viewer.addSelectionChangedListener( new ISelectionChangedListener<Person>() { public void selectionChanged( SelectionChangedEvent<Person> event) { if( event.getSelection().isEmpty() ) { System.out.println("<Empty>"); } else { for( Person p : event.getSelection() ) { System.out.println(p.name); } } } }); Button b = new Button(shell,SWT.PUSH); b.setText("Selection"); b.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { viewer.setSelection( new StructuredSelection<Person>( persons.iterator().next() ) ); } }); shell.open(); while( ! shell.isDisposed() ) { if( ! d.readAndDispatch() ) { d.sleep(); } }
There are some interesting things to notice about the Viewers and their implementation:
- Java5 Language Features like Generics, for-loop above Selection
- A generic interface definition making it widget-toolkit agnostic (I’ve already implemented some Swing, Qt, SWT, GWT, SmartGWT-Viewers)
- Automatic Databinding support because it works on the Interface-Definition
How nice this new SWT-Proposal-Viewer integrates itself into my application take a look at this small screencast:
Hi Tom,
this looks very nice!
Is there a place I can download this viewer?
Thanks,
Markus
You need to check it out from our Subversion repository:
* https://dev.eclipse.org/svnroot/eclipse/org.eclipse.ufacekit/develop/eclipse/virtual-structure/proper/org.eclipse.ufacekit.ui.jface.viewers
* https://dev.eclipse.org/svnroot/eclipse/org.eclipse.ufacekit/develop/eclipse/virtual-structure/proper/org.eclipse.ufacekit.ui.viewers
This reminds me of the NakedObjects Framework, where you define business objects and add a few annotations to indicate properties and actions and the framework automatically makes the objects available for the users to manipulate. See http://www.nakedobjects.org/home/no_for_java_intro.shtml