wiki:AddingFilterSection

In order to facilitate code sharing between ms plugins and the glycanarray plugin which both requires the use of a GUI component for filter selection, I've created a class (FilterTableSetup) in package org.grits.toolbox.util.structure.glycan.gui

It has a few public methods:

  • one for the creation of the filter table and the controls required to add/delete filters from the table - createFilterTableSection
  • one to get all the filters added into the table by the user. Returns a filtersettings object containing the appropriate filters to be applied on the glycan objects - getFilterSetting (returns null if the table is empty)
  • one to reset the filters in the table - resetFilters
  • one to handle a special case when you click on a button outside the table while your cursor is still in the cell editing mode - stopEditing()
  • one method to register a listener - addFilterChangedListener
  • one method to get the list of filters (as read from the xml file) - getFilterList

Usage is shown in the following code (from GlycanArray plugin - uk.ac.imperial.glycosciences.glycanarray.editors.pages.GlycanArrayMasterTableView):

filterTableSetup = new FilterTableSetup();
try {
     filterTableSetup.setFilterFile(FileUtils.getFilterPath());
     filterTableSetup.setCustomFilterFile(FileUtils.getCustomFilterPath());
     filterTableSetup.createFilterTableSection(filterOptionsComposite);
     filterTableSetup.addFilterChangedListener(this);
} catch (Exception e1) {
     MessageDialog.openError(parent.getShell(), "Error", "Cannot create filter section");
     logger.error("Cannot create filter section", e1);
}

....


Button filterNow = new Button (buttonGroup, SWT.PUSH);
filterNow.setText("Apply Filter");
filterNow.addSelectionListener(new SelectionAdapter() {

        @Override
	public void widgetSelected(SelectionEvent e) {
	    logger.debug("Filter selected");
	    filterTableSetup.stopEditing();
	    if (filterDirty) {
		showAllRows();
		filterDirty = false;
	    }
	    applyFilter();
	    logger.debug("Filter finished");
	}
});
		
Button resetFilter = new Button (buttonGroup, SWT.PUSH);
resetFilter.setText ("Reset filters");
resetFilter.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
	logger.debug("Resetting filters");
	// clear all selections
	filterTableSetup.resetFilters();
	GlycanArrayMasterTable table = (GlycanArrayMasterTable) getTableBase().getNatTable();
	table.resetFilters();
        filterDirty = false;
        logger.debug("Filters reset");
    }
});

First portion in the above code sample shows the creation of the filter table section. You have to make sure to set the full path for the filters.xml file and your plugin's custom filter file (if any) before calling createFilterTableSection.

FilterSetting object returned from FilterTableSetup class (see filterButton selection listener code above) has a single Filter. You need to set this Filter into a GlycanFilterVisitor (in org.grits.toolbox.util.structure.glycan.filter.om) object then call evaluate with your Sugar object (obtained by parsing the sequence) to see if the given glycan passes the filter.

Example usage:

GlycanFilterVisitor myFilterVisitor = new GlycanFilterVisitor();
myFilterVisitor.setFilter (settings.getFilter());

try {
     if (myFilterVisitor.evaluate(mySugar)) {
         ...
     }
} catch (GlycanFilterException e) {
     ...
}

If you would like to be informed about the changes in the filter selections (editing a filter or removing a filter), you should implement FilterChangedListener and add your listener to the FilterTableSetup as in the following example:

package org.grits.toolbox.util.structure.glycan.gui;

public interface FilterChangedListener {
	void filterChanged();
}

public class GlycanArrayMasterTableView extends EntryEditorPart implements FilterChangedListener{
...
   Boolean filterDirty = false;
...

   @Override
   public void filterChanged() {
	this.filterDirty = true;
   }

In your view/editor when you initialize the filtertablesetup do the following:

       filterTableSetup.addFilterChangedListener(this);
Last modified 6 years ago Last modified on 05/31/2016 08:45:52 PM