Blog

UFaceKit and Java5-Viewers

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: Video and for the readers of blog aggregators here as an image Screenshot

published on
Previous
QxWT a wrapper for Qooxdoo-JS