SIDEBAR
»
S
I
D
E
B
A
R
«
TokenViewer – Basics
January 29th, 2009 by Eugene Ostroukhov

I created a new viewer that I think may be really useful. I’m writing this post to provide a high-level description. You can use it as follows:


final TokenViewer tokenViewer = new TokenViewer(root);

tokenViewer.getControl().setLayoutData(

new GridData(GridData.FILL_HORIZONTAL));

tokenViewer.setContentProvider(new BaseWorkbenchContentProvider());

tokenViewer.setLabelProvider(new WorkbenchLabelProvider());

tokenViewer.setInput(workspaceRoot);

The result is a simple text field with following services:

  1. Content assist.
  2. Object-oriented IStructuredSelection support – if the user enters proper string the selection event is thrown. If user sets selection – string will appear in the text entry.
  3. Easy and familiar programming model – I guess any JFace/Eclipse plugin developer wrote dozens of those label/content providers.

 

TokenViewer working with workspace

TokenViewer working with workspace

This simple API works for the case when text in the text field is the same as a label in the content assist pop-up. But once you want to have different text (i.e. SSN entry may show employee name in the pop-up) you will need something more specific for this task and that is why I defined special content provider (I still hope it is easier to use then regular JFace content assist API):


public interface ITokenContentProvider extends IStructuredContentProvider {

Object fromString(Object parent, String value);

String toString(Object object);

Object[] getChildren(Object object, String prefix);

}
  • fromString method will return a child of the given parent node that corresponds to a specific string. It may return null.
  • toString will convert given object to its string representation.
  • getChildren tries to find all the elements that correspond to given string.

If the viewer is provided with IStructuredContentProvider (or ITreeContentProvider) it will wrap it with a custom adapter so you can use your existing code.

The label provider is used to decorate the content assist pop-up. I also have a slightly extended label provider interface:


public interface IDescriptionProvider extends ILabelProvider {

public String getDescription(Object element);

}

This optional extension provides the way to provide extended object descriptions that are shown along the content assist pop-up:

This is also a tokenviewer

This is also a tokenviewer

Note – red squiggles are automatically shown when there is no object that corresponds to a text.

What I’m going to implement next is:

  1. Multiple selection support when token list is separated with some delimeter.
  2. Rich text (HTML?) support both for content assist pop-up and description pop-up.

2 Responses  
  • Prakash writes:
    January 29th, 20097:44 amat

    Eugene,
    The token viewer with workspace contents is interesting. I like this!

    I would like to try it. Where do I get the code? If you could attach it to the bug it would reach a wider audience I guess.

    • zzhou writes:
      January 29th, 200910:52 amat

      I will cleanup the code through the weekend and then will publish it in a bug.


Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

»  Substance:WordPress   »  Style:Ahren Ahimsa