Wednesday, February 26, 2014

Adding a list editor in eclipse preference. Extend ListEditor

Adding a list editor in Eclipse preferences implies to extend ListEditor.
This  post concern the build of a ListEditor dealing with filter definition relying on regular expression.


The beginning,  create a FilterDialog

This dialog aims to get a filter entry. This dialog should return a String containing a regular expression. (I don't verify that the content typed is OK but I will)


the FilterDialog source code :

import org.eclipse.jface.dialogs.TitleAreaDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class FilterDialog extends TitleAreaDialog {

  private Text filterValue;

  private String filter;

  public FilterDialog(Shell parentShell) {
    super(parentShell);
  }

  @Override
  public void create() {
    super.create();
    setTitle("Add filter to keep tests");
  }

  @Override
  protected Control createDialogArea(Composite parent) {
    Composite area = (Composite) super.createDialogArea(parent);
    Composite container = new Composite(area, SWT.NONE);
    container.setLayoutData(new GridData(GridData.FILL_BOTH));
    GridLayout layout = new GridLayout(2, false);
    container.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    container.setLayout(layout);
    createFilter(container);
    return area;
  }

  private void createFilter(Composite container) {
    Label lbtFirstName = new Label(container, SWT.NONE);
    lbtFirstName.setText("Filter as regular expression");

    GridData dataFirstName = new GridData();
    dataFirstName.grabExcessHorizontalSpace = true;
    dataFirstName.horizontalAlignment = GridData.FILL;

    filterValue = new Text(container, SWT.BORDER);
    filterValue.setLayoutData(dataFirstName);
  }

  @Override
  protected boolean isResizable() {
    return true;
  }
 
  private void saveInput() {
    filter = filterValue.getText();
  }

  @Override
  protected void okPressed() {
    saveInput();
    super.okPressed();
  }

  public String getFilter() {
    return filter;
  }
 
} 

Extend ListEditor


the source code :
import java.io.File;

import org.eclipse.jface.preference.ListEditor;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Composite;


public class FilterTestEditor extends ListEditor {

    protected FilterTestEditor() {
    }

    public FilterTestEditor(String name, String labelText, Composite parent) {
        init(name, labelText);
        createControl(parent);
    }

    protected String createList(String[] items) {
        StringBuffer path = new StringBuffer("");//$NON-NLS-1$
        for (int i = 0; i < items.length; i++) {
            path.append(items[i]);
            path.append(File.pathSeparator);
        }
        return path.toString();
    }


    protected String getNewInputObject() {
     String filter=null;
        FilterDialog dialog = new FilterDialog(getShell());
        dialog.create();
       
        if (dialog.open() == Window.OK) {
           System.out.println(dialog.getFilter());
           filter = dialog.getFilter();
         } 
        return filter;
    }
    
    protected String[] parseString(String stringList) {
  StringTokenizer sto = new StringTokenizer(stringList, File.pathSeparator
                + "\n\r");
        ArrayList<String> v = new ArrayList<String>();
        while (st.hasMoreElements()) {
            v.add((String) sto.nextElement());
        }
        return (String[]) v.toArray(new String[v.size()]);
    }
}

and finally in the préférence page add

addField(new FilterTestEditor(KEY_FILTER_TEST, "Test filtering", getFieldEditorParent()));

1 comment:

  1. Easy to understand, great example, thanks

    ReplyDelete