cancel
Showing results for 
Search instead for 
Did you mean: 

TableSorter and filter class

Former Member
0 Kudos

Hey,

I am looking for the classes to use a tablesorter and filter on a web dynpro java table.

When I look in the tutorial it says I need to locate these classes in the Web Dynpro for Java Demo+Kit.

But the link for the Web Dynpro for Java Demo+Kit doesn't work anymore.

Could someone tell me where to find these classes or send them to me?

Thanks!

Accepted Solutions (1)

Accepted Solutions (1)

Sreejith1
Contributor
0 Kudos

Hi Niki,

I hope you knows how to do the rest. Else reply .

I pasted the code below.Because most of the time uploaded links will not work so it will be helpful for others also.

************* TableSorter start***************************

package myapp.comp.utils;

import java.sql.Date;

import java.text.Collator;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Comparator;

import java.util.HashMap;

import java.util.Iterator;

import java.util.Map;

import java.util.StringTokenizer;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByIndex;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByKey;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractInputField;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractTableColumn;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCaption;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCheckBox;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDLink;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDProgressIndicator;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDRadioButton;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableCellEditor;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumn;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumnGroup;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextEdit;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextView;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection;

import com.sap.tc.webdynpro.progmodel.api.IWDAction;

import com.sap.tc.webdynpro.progmodel.api.IWDCustomEvent;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;

import com.sap.tc.webdynpro.progmodel.api.IWDViewElement;

import com.sap.tc.webdynpro.services.sal.localization.api.WDResourceHandler;

/**

* Helper class that makes a Web Dynpro table UI element sortable (column-wise).

*/

public final class TableSorter {

          /**

           * @param table

           * @param sortAction

           * @param comparators

           */

          public TableSorter(IWDTable table, IWDAction sortAction, Map comparators) {

                    init(table, sortAction, comparators, null);

          }

          public TableSorter(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns) {

                    init(table, sortAction, comparators, sortableColumns);

          }

          /**

           * Initialisation stuff

           */

          private void init(IWDTable table, IWDAction sortAction, Map comparators, String[] sortableColumns){

                    this.table = table;

                    if(sortableColumns == null){

                              sortableCols = null;

                    }else{

                              sortableCols = new HashMap();

                              for (int i = 0; i < sortableColumns.length; i++) {

                                        sortableCols.put(sortableColumns[i], sortableColumns[i]);

                              }

                    }

                    // sanity checks

                    if (sortAction == null)

                              throw new IllegalArgumentException("Sort action must be given");

                    if (table == null)

                              throw new IllegalArgumentException("Table must be given");

                    if (table.bindingOfDataSource() == null)

                              throw new IllegalArgumentException(

                                        "Data source of table with id '" + table.getId() + "' must be bound");

                    // make the columns sortable

                    String dataSourcePrefix = table.bindingOfDataSource() + ".";

                    //TODO: remove the following line since this method is not longer available in later releases

                    //setComparatorsForColumns(dataSourcePrefix, table.iterateColumns(), comparators);

                    setComparatorsForColumns(dataSourcePrefix, table.iterateGroupedColumns(), comparators);

                    //set up the table properties

                    table.setOnSort(sortAction);

                    table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.COL, "selectedColumn");

                    table.mappingOfOnSort().addSourceMapping(IWDTable.IWDOnSort.DIRECTION, "sortDirection");

          }

          /**

           * Try to make the given columns sortable (recusivly, if necessary)

           */

          private void setComparatorsForColumns(String dataSourcePrefix, Iterator columnIterator, Map comparators){

                    int index = 0;

                    for (Iterator it = columnIterator; it.hasNext(); ++index) { // for every column: try to make it bindable

                              IWDAbstractTableColumn abstractColumn = (IWDAbstractTableColumn) it.next();

                              if(abstractColumn instanceof IWDTableColumn){

                                        IWDTableColumn column = (IWDTableColumn)abstractColumn;

                                        if(sortableCols == null || sortableCols.containsKey(column.getId())){

                                                  //try to make this column sortable

                                                  Comparator comparator = null;

                                                  if (comparators != null){

                                                            comparator = (Comparator)comparators.get(column.getId());

                                                  }

                                                  NodeElementByAttributeComparator elementComparator = null;

                                                  if (comparator instanceof NodeElementByAttributeComparator) {

                                                            // the easy one, attribute and ordering are given

                                                            elementComparator = (NodeElementByAttributeComparator)comparator;

                                                  } else { // attribute must be determined

                                                            String bindingOfPrimaryProperty = bindingOfPrimaryProperty(column.getTableCellEditor());

                                                            if (bindingOfPrimaryProperty == null || !bindingOfPrimaryProperty.startsWith(dataSourcePrefix)){

                                                                      //no attribute found or outside of data source

                                                                      column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

                                                                      continue;

                                                            }

                                                            String attributeName = bindingOfPrimaryProperty.substring(dataSourcePrefix.length());

                                                            Collection subnodes = new ArrayList();

                                                            if (attributeName.indexOf('.') >= 0){

                                                                      //attribute not immediately below data source

                                                                      String[] tokens = tokenize (attributeName, ".");

                                                                      for(int i=0; i<tokens.length-1; i++){

                                                                                subnodes.add(tokens[i]);

                                                                      }

                                                                      attributeName = tokens[tokens.length-1];

                                                            }

                                                            if(subnodes.size() == 0){

                                                                      elementComparator = new NodeElementByAttributeComparator(attributeName, comparator);

                                                            }else{

                                                                      elementComparator = new NodeElementByAttributeComparator(attributeName, comparator, subnodes);

                                                            }

                                                  }

                                                  // set up internal data structures

                                                  comparatorForColumn.put(column, elementComparator);

                                                  //set sort state

                                                  column.setSortState(WDTableColumnSortDirection.NONE);

                                        }else{

                                                  //column should not be sortable

                                                  column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

                                        }

                              }else if (abstractColumn instanceof IWDTableColumnGroup){

                                        //it's just a column group -> try to bind the columns of the column group

                                        IWDTableColumnGroup columnGroup = (IWDTableColumnGroup)abstractColumn;

                                        setComparatorsForColumns(dataSourcePrefix, columnGroup.iterateColumns(), comparators);

                              }

                    }

          }

          /**

           * Tokenizes the input string according to the given delimiters. The delimiters will be left out.

           * Example: tokenize("Hello_World", "_") results ["Hello", "World"]

           */

          private String[] tokenize (String input, String delim){

                    StringTokenizer tokenizer = new StringTokenizer(input, delim);

                    String[] tokens = new String[tokenizer.countTokens()];

                    int index = 0;

                    while(tokenizer.hasMoreTokens()){

                              tokens[index] = tokenizer.nextToken();

                              index++;

                    }

                    return tokens;

          }

          /**

           * This method must be called from the event handler of this table sorter's

           * sort action. It performs the actual sort operation.

           */

          public void sort(IWDCustomEvent wdEvent, IWDNode dataSource) {

                    // find the things we need

                    String columnId = wdEvent.getString("selectedColumn");

                    String direction = wdEvent.getString("sortDirection");

                    IWDTableColumn column = (IWDTableColumn) table.getView().getElement(columnId);

                    NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) comparatorForColumn.get(column);

                    if (elementComparator == null){

                              //not a sortable column

                              column.setSortState(WDTableColumnSortDirection.NOT_SORTABLE);

                              return;

                    }

                    // sorting

                    elementComparator.setSortDirection(WDTableColumnSortDirection.valueOf(direction));

                    dataSource.sortElements(elementComparator);

          }

          /**

           * Returns the binding of the given table cell editor's property that is

           * considered "primary" or <code>null</code> if no such binding exists or no

           * such property can be determined.

           */

          private static final String bindingOfPrimaryProperty(IWDTableCellEditor editor) {

                    return editor instanceof IWDViewElement ? bindingOfPrimaryProperty((IWDViewElement) editor) : null;

          }

          /**

           * Returns the binding of the given view element's property that is

           * considered "primary" or <code>null</code> if no such binding exists or no

           * such property can be determined.

           */

          private static final String bindingOfPrimaryProperty(IWDViewElement element) {

                    if (element instanceof IWDAbstractDropDownByIndex)

                              return ((IWDAbstractDropDownByIndex) element).bindingOfTexts();

                    if (element instanceof IWDAbstractDropDownByKey)

                              return ((IWDAbstractDropDownByKey) element).bindingOfSelectedKey();

                    if (element instanceof IWDAbstractInputField)

                              return ((IWDAbstractInputField) element).bindingOfValue();

                    if (element instanceof IWDCaption)

                              return ((IWDCaption) element).bindingOfText();

                    if (element instanceof IWDCheckBox)

                              return ((IWDCheckBox) element).bindingOfChecked();

                    if (element instanceof IWDLink)

                              return ((IWDLink) element).bindingOfText();

                    if (element instanceof IWDProgressIndicator)

                              return ((IWDProgressIndicator) element).bindingOfPercentValue();

                    if (element instanceof IWDRadioButton)

                              return ((IWDRadioButton) element).bindingOfSelectedKey();

                    if (element instanceof IWDTextEdit)

                              return ((IWDTextEdit) element).bindingOfValue();

                    if (element instanceof IWDTextView)

                              return ((IWDTextView) element).bindingOfText();

                    return null;

          }

          /**

           * Instance of a comparator according to the ordering imposed by the

           * implementation of <code>Comparable</code>.

           */

          private static final Comparator DEFAULT = new Comparator() {

                    /**

                     * Compares the given objects according to the ordering imposed by the first

                     * ones <code>compareTo(Object)</code> function. Furthermore, <code>null</code>

                     * is treated to be less than any object.

                     *

                     * @see java.lang.Comparable#compareTo(java.lang.Object)

                     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

                     */

                    public int compare(Object o1, Object o2) {

                              if (o1 == null && o2 == null)

                                        return 0;

                              if (o1 == null)

                                        return -1;

                              if (o2 == null)

                                        return +1;

                              if (o1 instanceof Boolean && o2 instanceof Boolean)

                                        return o1.toString().compareTo(o2.toString()); // false < true

                              if (o1 instanceof String && o2 instanceof String){

                                        //Use a Collator for sorting according to the given Locale

                                        Collator collate = Collator.getInstance(WDResourceHandler.getCurrentSessionLocale());

                                        return collate.compare(o1, o2);

                              }

                              return ((Comparable) o1).compareTo((Comparable) o2);

                    }

          };

          /**

           * Map of table column to comparator (<code>ReversableComparator</code>)

           * used for sorting that column (sortable columns only).

           */

          private Map comparatorForColumn = new HashMap();

          /**

           * The table to be sorted.

           */

          private IWDTable table = null;

          /**

           * Column-IDs of the columns, which should be sortable

           */

          private Map sortableCols = null;

          /**

           * Generic comparator that compares node elements by a given attribute with

           * the help of a given comparator.

           */

          public final class NodeElementByAttributeComparator implements Comparator {

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute according to the natural ordering of that attribute's

                     * type (which must implement <code>java.lang.Comparable</code>).

                     */

                    public NodeElementByAttributeComparator(String attributeName) {

                              this(attributeName, null, false, new ArrayList());

                    }

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute with the help of the given comparator. If no comparator

                     * is given, the natural ordering of that attribute's type is used.

                     */

                    public NodeElementByAttributeComparator(String attributeName, Comparator comparator) {

                              this(attributeName, comparator, false, new ArrayList());

                    }

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute either as objects (i.e. "in internal format") or as text

                     * (i.e. "in external format") as indicated. The ordering is the natural

                     * ordering of that attribute's type (which must implement

                     * <code>java.lang.Comparable</code>) in case objects are compared or the

                     * natural ordering of <code>java.lang.String</code> in case texts are compared.

                     */

                    public NodeElementByAttributeComparator(String attributeName, boolean compareAsText) {

                              this(attributeName, null, compareAsText, new ArrayList());

                    }

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute according to the natural ordering of that attribute's

                     * type (which must implement <code>java.lang.Comparable</code>). In addition it is possible

                     * to define the path to a child node with the <code>java.util.Collection</code> subnodes.

                     * (List of child node names in the correct order)

                     */

                    public NodeElementByAttributeComparator(String attributeName, Collection subnodes) {

                              this(attributeName, null, false, subnodes);

                    }

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute with the help of the given comparator. If no comparator

                     * is given, the natural ordering of that attribute's type is used. In addition it is possible

                     * to define the path to a child node with the <code>java.util.Collection</code> subnodes.

                     * (List of child node names in the correct order)

                     */

                    public NodeElementByAttributeComparator(String attributeName, Comparator comparator, Collection subnodes) {

                              this(attributeName, comparator, false, subnodes);

                    }

                    /**

                     * Creates a new comparator for the given attribute name that compares values

                     * of that attribute either as objects (i.e. "in internal format") or as text

                     * (i.e. "in external format") as indicated. The ordering is the natural

                     * ordering of that attribute's type (which must implement

                     * <code>java.lang.Comparable</code>) in case objects are compared or the

                     * natural ordering of <code>java.lang.String</code> in case texts are compared. In addition it is possible

                     * to define the path to a child node with the <code>java.util.Collection</code> subnodes.

                     * (List of child node names in the correct order)

                     */

                    public NodeElementByAttributeComparator(String attributeName, boolean compareAsText, Collection subnodes) {

                              this(attributeName, null, compareAsText, subnodes);

                    }

                    /**

                     * Internal constructor.

                     */

                    private NodeElementByAttributeComparator(

                              String attributeName,

                              Comparator comparator,

                              boolean compareAsText,

                              Collection subNodes) {

                              if (attributeName == null)

                                        throw new IllegalArgumentException("Attribute name must not be null");

                              if (comparator == null)

                                        comparator = DEFAULT;

                              this.attributeName = attributeName;

                              this.comparator = comparator;

                              this.compareAsText = compareAsText;

                              this.sortDirection = true;

                              this.subNodes = subNodes;

                    }

                    /**

                     * Sets the sort direction of this comparator to the given direction. The comparator sort in ascending order by default.

                     * @see com.sap.tc.webdynpro.clientserver.uielib.standard.api.WDTableColumnSortDirection

                     */

                    public void setSortDirection(WDTableColumnSortDirection direction){

                              if(direction.equals(WDTableColumnSortDirection.UP)){

                                        sortDirection = true;

                              }else if(direction.equals(WDTableColumnSortDirection.DOWN)){

                                        sortDirection = false;

                              }

                    }

                    /**

                     * Compares the given objects which must be instances of <code>IWDNodeElement</code>

                     * according to the values of the attribute given at construction time

                     * with the help of the comparator given at construction time.

                     *

                     * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)

                     * @see com.sap.tc.webdynpro.progmodel.api.IWDNodeElement

                     */

                    public int compare(Object o1, Object o2) {

                              IWDNodeElement element1 = (IWDNodeElement) o1;

                              IWDNodeElement element2 = (IWDNodeElement) o2;

                              if(subNodes.size() > 0){

                                        element1 = getSubNodeElement(element1, 0);

                                        element2 = getSubNodeElement(element2, 0);

                              }

                              Object attributeValue1 = null;

                              Object attributeValue2 = null;

                              if(element1 != null){

                                        attributeValue1 =

                                                  compareAsText

                                                            ? element1.getAttributeAsText(attributeName)

                                                            : element1.getAttributeValue(attributeName);

                              }

                              if(element2 != null){

                                        attributeValue2 =

                                                  compareAsText

                                                            ? element2.getAttributeAsText(attributeName)

                                                            : element2.getAttributeValue(attributeName);

                              }

                              if(sortDirection){

                                        return comparator.compare(attributeValue1, attributeValue2);

                              }else{

                                        return comparator.compare(attributeValue2, attributeValue1);

                              }

                    }

                    /**

                     * Determines recursivly the child node, which have an attribute with the given name.

                     * The path to this child node must be specified in the subnodes property of this comparator.

                     * Start this method with index = 0.

                     */

                    private IWDNodeElement getSubNodeElement(IWDNodeElement currentElement, int index){

                              if(currentElement == null || index >= subNodes.size()){

                                        //end of recursion

                                        return currentElement;

                              }else{

                                        return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getCurrentElement(), index+1);

                                        //return getSubNodeElement(currentElement.node().getChildNode((String)subNodes.toArray()[index], currentElement.index()).getElementAt(0), index+1);

                              }

                    }

                    /**

                     * Name of the attribute used for comparisons.

                     */

                    private final String attributeName;

                    /**

                     * Comparator used for comparing the attribute's values.

                     */

                    private final Comparator comparator;

                    /**

                     * Indicates whether attribute values are compared as text (as opposed to

                     * "as objects").

                     */

                    private final boolean compareAsText;

                    /**

                     * Sort direction (true = ascending order, false = descending order)

                     */

                    private boolean sortDirection;

                    /**

                     * List of child node names

                     * (Description of the path from the given context node to the specified attribute)

                     */

                    private Collection subNodes;

          }

}

************* TableSorter end***************************

************* TableFilter start***************************

package myapp.comp.utils;

import java.math.BigDecimal;

import java.text.DateFormat;

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.ArrayList;

import java.util.Collection;

import java.util.Comparator;

import java.util.Enumeration;

import java.util.HashMap;

import java.util.Hashtable;

import java.util.Iterator;

import java.util.Locale;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByIndex;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractDropDownByKey;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractInputField;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDAbstractTableColumn;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCaption;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDCheckBox;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDLink;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDProgressIndicator;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDRadioButton;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTable;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableCellEditor;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumn;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTableColumnGroup;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextEdit;

import com.sap.tc.webdynpro.clientserver.uielib.standard.api.IWDTextView;

import com.sap.tc.webdynpro.progmodel.api.IWDAction;

import com.sap.tc.webdynpro.progmodel.api.IWDAttributeInfo;

import com.sap.tc.webdynpro.progmodel.api.IWDNode;

import com.sap.tc.webdynpro.progmodel.api.IWDNodeElement;

import com.sap.tc.webdynpro.progmodel.api.IWDViewElement;

import com.sap.tc.webdynpro.progmodel.api.WDCopyService;

import com.sap.tc.webdynpro.services.sal.localization.api.WDResourceHandler;

public final class TableFilter

{

          public TableFilter( IWDTable table, IWDAction filterAction, IWDNode sourceNode, Hashtable hashicons )

          {

                    init(table, filterAction, sourceNode, hashicons);

          }

          private void init( IWDTable table, IWDAction filterAction, IWDNode sourceNode, Hashtable hashicons )

          {

                    this.table = table;

                    this.sourceNode = sourceNode;

                    if (sourceNode == null) throw new IllegalArgumentException("SourceNode must be given");

                    if (filterAction == null) throw new IllegalArgumentException("Filter action must be given");

                    if (table == null) throw new IllegalArgumentException("Table must be given");

                    if (table.bindingOfDataSource() == null) throw new IllegalArgumentException("Data source of table with id '" + table.getId() + "' must be bound");

                    if (hashicons != null)

                    {

                              this.hashIcon = new Hashtable();

                              this.hashIcon = hashicons;

                    }

                    String dataSourcePrefix = table.bindingOfDataSource() + ".";

                    //works on columns

                    //setFiltersForColumns(dataSourcePrefix, table.iterateColumns(), sourceNode);

                    setFiltersForColumns(dataSourcePrefix, table.iterateGroupedColumns(), sourceNode);

                    table.setOnFilter(filterAction);

                    filterAction.setEnabled(true);

                    this.sourceNode = sourceNode;

          }

          private void setFiltersForColumns( String dataSourcePrefix, Iterator columnIterator, IWDNode sourceNode )

          {

                    attributeSource = new Hashtable();

                    for (Iterator iter = sourceNode.getNodeInfo().iterateAttributes(); iter.hasNext();)

                    {

                              IWDAttributeInfo attribInfo = (IWDAttributeInfo) iter.next();

                              String attribName = attribInfo.getName();

                              attributeSource.put(attribName, attribInfo.getSimpleType().getBuiltInType());

                    }

                    int index = 0;

                    for (Iterator it = columnIterator; it.hasNext(); ++index)

                    { // for every column: try to make it bindable

                              IWDAbstractTableColumn abstractColumn = (IWDAbstractTableColumn) it.next();

                              if (abstractColumn instanceof IWDTableColumn)

                              {

                                        IWDTableColumn column = (IWDTableColumn) abstractColumn;

                                        String columnId = column.getId();

                                        Comparator comparator = null;

                                        NodeElementByAttributeComparator elementComparator = null;

                                        String bindingOfPrimaryProperty = bindingOfPrimaryProperty(column.getTableCellEditor());

                                        String attributeName = null;

                                        if (bindingOfPrimaryProperty == null || !bindingOfPrimaryProperty.startsWith(dataSourcePrefix))

                                        {

                                                  if (hashIcon == null)

                                                  {

                                                            continue;

                                                  }

                                                  else

                                                  {

                                                            if (!hashIcon.containsKey(columnId))

                                                            {

                                                                      continue;

                                                            }

                                                            else

                                                            {

                                                                      attributeName = hashIcon.get(columnId).toString();

                                                            }

                                                  }

                                        }

                                        else

                                        {

                                                  attributeName = bindingOfPrimaryProperty.substring(dataSourcePrefix.length());

                                                  if ( hashIcon != null )

                                                  {

                                                            if (hashIcon.containsKey(columnId))

                                                            {

                                                                      attributeName = hashIcon.get(columnId).toString();

                                                            }

                                                  }

                                        }

                                        String attributeInfo = null;

                                        if (attributeSource.containsKey(attributeName))

                                        {

                                                  attributeInfo = (String) attributeSource.get(attributeName);

                                        }

                                        elementComparator = new NodeElementByAttributeComparator(attributeInfo, column, comparator);

                                        filterForColumn.put(attributeName, elementComparator);

                              }

                              else if (abstractColumn instanceof IWDTableColumnGroup)

                              {

                                        IWDTableColumnGroup columnGroup = (IWDTableColumnGroup) abstractColumn;

                                        setFiltersForColumns(dataSourcePrefix, columnGroup.iterateColumns(), sourceNode);

                              }

                    }

          }

          public void filter( IWDNode allDataNode, IWDNode targetNode )

          {

                    if (allDataNode != null)

                    {

                              this.sourceNode = allDataNode;

                    }

                    HashMap FilterValuesByAttribute = prepareFilterValuesByAttribute();

                    ArrayList ar = new ArrayList();

                    for (int i = 0; i < sourceNode.size(); i++)

                    {

                              IWDNodeElement el = sourceNode.getElementAt(i);

                              boolean addelement = true;

                              for (Enumeration e = this.attributeSource.keys(); e.hasMoreElements();)

                              {

                                        String attributeName = e.nextElement().toString();

                                        Object o1 = el.getAttributeValue(attributeName);

                                        if ( attributeName.equalsIgnoreCase("DATE") )

                                        {

                                                  //o1 =(java.sql.Date) el.getAttributeValue(attributeName);

                                        }

                                        if (FilterValuesByAttribute.containsKey(attributeName) && filterForColumn.containsKey(attributeName))

                                        {

                                                  NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) filterForColumn.get(attributeName);

                                                  HashMap valuehash = new HashMap();

                                                  valuehash = (HashMap) FilterValuesByAttribute.get(attributeName);

                                                  String attributetype = elementComparator.attributeInfo;

                                                  Comparator comparator = elementComparator.comparator;

                                                  if (valuehash.size() == 0)

                                                  {

                                                            elementComparator.column.setIsFiltered(false);

                                                  }

                                                  else

                                                  {

                                                            elementComparator.column.setIsFiltered(true);

                                                  }

                                                  addelement = this.evaluateFilteredAttributeValue(valuehash, attributetype, comparator, o1);

                                                  if (!addelement) break;

                                        }

                                        if (!addelement) break;

                              }

                              if (!addelement) continue;

                              IWDNodeElement targetElement = targetNode.createElement();

                              WDCopyService.copyCorresponding(el, targetElement);

                              ar.add(targetElement);

                    }

                    targetNode.bind(ar);

          }

          public void deleteAllDataNodeElement( IWDNode sourceNode, IWDNode targetNode, String attributeRowId, ArrayList idsToDelete )

          {

                    for (int j = sourceNode.size() - 1; j >= 0; --j)

                    {

                              IWDNodeElement els = sourceNode.getElementAt(j);

                              String elsid = els.getAttributeAsText(attributeRowId);

                              if (!idsToDelete.contains(elsid)) continue;

                              sourceNode.removeElement(els);

                    }

                    for (int i = targetNode.size() - 1; i >= 0; --i)

                    {

                              IWDNodeElement elt = targetNode.getElementAt(i);

                              String eltid = elt.getAttributeAsText(attributeRowId);

                              if (!idsToDelete.contains(eltid)) continue;

                              targetNode.removeElement(elt);

                    }

          }

          /*Update all data node if the table node has been updated

           * if a row has been added in the table node,

           * the all data node can also be updated by using the using the flag addRowOnFly

           */

          public void updateAllDataNodeElement( IWDNode sourceNode, IWDNode targetNode, String attributeRowId, boolean addRowOnFly )

          {

                    HashMap hashs = new HashMap();

                    HashMap hasht = new HashMap();

                    HashMap hashup = new HashMap();

                    IWDNodeElement elt;

                    IWDNodeElement els;

                    for (int j = sourceNode.size() - 1; j >= 0; --j)

                    {

                              els = sourceNode.getElementAt(j);

                              String elsid = els.getAttributeAsText(attributeRowId);

                              hashs.put(elsid, els);

                    }

                    for (int i = 0; i < targetNode.size(); i++)

                    {

                              elt = targetNode.getElementAt(i);

                              String rowid = elt.getAttributeAsText(attributeRowId);

                              boolean isInSource = hashs.containsKey(rowid);

                              if (isInSource && !elt.isChangedByClient())

                              {

                                        continue;

                              }

                              if (!isInSource)

                              {

                                        els = sourceNode.createElement();

                                        WDCopyService.copyCorresponding(elt, els);

                                        sourceNode.addElement(els);

                              }

                              else

                              {

                                        els = (IWDNodeElement) hashs.get(rowid);

                                        WDCopyService.copyCorresponding(elt, els);

                              }

                    }

          }

          /*find if an column has a valid filter

           * and if so parse the filter and put in a hash table

           */

          private HashMap prepareFilterValuesByAttribute( )

          {

                    HashMap hashmap = new HashMap();

                    for (Enumeration e = this.attributeSource.keys(); e.hasMoreElements();)

                    {

                              String attributeName = e.nextElement().toString();

                              if (filterForColumn.containsKey(attributeName))

                              {

                                        NodeElementByAttributeComparator elementComparator = (NodeElementByAttributeComparator) filterForColumn.get(attributeName);

                                        IWDTableColumn column = elementComparator.column;

                                        column.setIsFiltered(false);

                                        String filtervalue = column.getFilterValue();

                                        if (filtervalue == null || filtervalue.trim().length() == 0)

                                        {

                                                  continue;

                                        }

                                        String attributetype = elementComparator.attributeInfo;

                                        HashMap valuehash = new HashMap();

                                        valuehash = this.detectFilterSign(filtervalue);

                                        hashmap.put(attributeName, valuehash);

                              }

                    }

                    return hashmap;

          }

          /* generate a filter value into the right object */

          private Object generateObject( String filtervalue, String attributetype, String sign )

          {

                    Object o2 = filtervalue;

                    try

                    {

                              if (filtervalue == null)

                              {

                                        return o2;

                              }

                              if (attributetype.equalsIgnoreCase("decimal"))

                              {

                                        o2 = new BigDecimal(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("double"))

                              {

                                        o2 = new Double((filtervalue));

                              }

                              if (attributetype.equalsIgnoreCase("long"))

                              {

                                        o2 = new Long(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("float"))

                              {

                                        o2 = new Float(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("short"))

                              {

                                        o2 = new Short(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("integer"))

                              {

                                        o2 = new Integer(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("long"))

                              {

                                        o2 = new Long(filtervalue);

                              }

                              if (attributetype.equalsIgnoreCase("date"))

                              {

                                        try

                                        {

                                                  /*java.util.Date dtTmp = new java.util.Date(filtervalue);

                                                  SimpleDateFormat template = new SimpleDateFormat("yyyy-MM-dd");

                                                  o2 = Date.valueOf(template.format(dtTmp));

                                                  */

                                                  final Locale locale = WDResourceHandler.getCurrentSessionLocale();

                                                  DateFormat formater = DateFormat.getDateInstance(DateFormat.SHORT, locale);

                                                  o2 = new java.sql.Date(formater.parse(filtervalue).getTime());

                                        }

                                        catch (Exception ex)

                                        {

                                                  o2 = filtervalue;

                                        }

                              }

                              if (attributetype.equalsIgnoreCase("time"))

                              {

                                        try

                                        {

                                                  SimpleDateFormat timeformater = new SimpleDateFormat("hh:mm:ss", WDResourceHandler.getCurrentSessionLocale());

                                                  o2 = timeformater.parse(filtervalue);

                                        }

                                        catch (ParseException ex)

                                        {

                                                  o2 = filtervalue;

                                        }

                              }

                              if (attributetype.equalsIgnoreCase("boolean"))

                              {

                                        if (sign.equalsIgnoreCase(NE))

                                        {

                                                  o2 = new Boolean(true);

                                        }

                                        else

                                        {

                                                  o2 = new Boolean(true);

                                        }

                              }

                              return o2;

                    }

                    catch (Exception ex)

                    {

                              return filtervalue;

                    }

          }

          /*compare object according the filter */

          private boolean evaluateFilteredAttributeValue( HashMap filter, String attributetype, Comparator comparator, Object o1 )

          {

                    boolean accept = true;

                    try

                    {

                              if (filter == null || filter.size() == 0)

                              {

                                        return accept;

                              }

                              if (attributetype == null || attributetype.trim().length() == 0)

                              {

                                        return accept;

                              }

                              Object o2 = filter.get("value1");

                              if (filter.get("sign") == null)

                              {

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, null);

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (!attributetype.equalsIgnoreCase("string"))

                                        {

                                                  if (compared == 0)

                                                  {

                                                            return accept;

                                                  }

                                                  else

                                                  {

                                                            return (!accept);

                                                  }

                                        }

                                        else

                                        {

                                                  if (compared >= 0)

                                                  {

                                                            return accept;

                                                  }

                                                  else

                                                  {

                                                            return (!accept);

                                                  }

                                        }

                              }

                              String sign = filter.get("sign").toString();

                              String place = filter.get("place").toString();

                              if (sign.equalsIgnoreCase(EQ) && place.equalsIgnoreCase("S"))

                              {

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, sign);

                                        }

                                        else

                                        {

                                                  if (attributetype.equalsIgnoreCase("boolean"))

                                                  {

                                                            o2 = generateObject("", attributetype, sign);

                                                  }

                                                  if (attributetype.equalsIgnoreCase("string") && o1 != null && o1.toString().trim().length() == 0)

                                                  {

                                                            o2 = generateObject("", attributetype, sign);

                                                  }

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (!attributetype.equalsIgnoreCase("string"))

                                        {

                                                  if (compared == 0)

                                                  {

                                                            return accept;

                                                  }

                                                  else

                                                  {

                                                            return (!accept);

                                                  }

                                        }

                                        else

                                        {

                                                  if (compared >= 0)

                                                  {

                                                            return accept;

                                                  }

                                                  else

                                                  {

                                                            return (!accept);

                                                  }

                                        }

                              }

                              if (sign.equalsIgnoreCase(NE) && place.equalsIgnoreCase("S"))

                              {

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, sign);

                                        }

                                        else

                                        {

                                                  if (attributetype.equalsIgnoreCase("boolean"))

                                                  {

                                                            o2 = generateObject("", attributetype, sign);

                                                  }

                                                  if (attributetype.equalsIgnoreCase("string") && o1 != null && o1.toString().trim().length() == 0)

                                                  {

                                                            o2 = generateObject("", attributetype, sign);

                                                  }

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (!attributetype.equalsIgnoreCase("string"))

                                        {

                                                  if (compared == 0)

                                                  {

                                                            return !accept;

                                                  }

                                                  else

                                                  {

                                                            return (accept);

                                                  }

                                        }

                                        else

                                        {

                                                  if (compared >= 0)

                                                  {

                                                            return !accept;

                                                  }

                                                  else

                                                  {

                                                            return (accept);

                                                  }

                                        }

                              }

                              if (sign.equalsIgnoreCase(R) && place.equalsIgnoreCase("S"))

                              {

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, sign);

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (compared > 0)

                                        {

                                                  return !accept;

                                        }

                                        else

                                        {

                                                  return (accept);

                                        }

                              }

                              if (sign.equalsIgnoreCase(R) && place.equalsIgnoreCase("E"))

                              {

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, sign);

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (compared >= 0)

                                        {

                                                  return accept;

                                        }

                                        else

                                        {

                                                  return (!accept);

                                        }

                              }

                              Object o3 = filter.get("value2").toString();

                              if (sign.equalsIgnoreCase(R) && place.equalsIgnoreCase("M"))

                              {

                                        boolean accept1 = true;

                                        boolean accept2 = true;

                                        if (o2 != null)

                                        {

                                                  String value1 = filter.get("value1").toString();

                                                  o2 = generateObject(value1, attributetype, sign);

                                        }

                                        if (o3 != null)

                                        {

                                                  String value1 = filter.get("value2").toString();

                                                  o3 = generateObject(value1, attributetype, sign);

                                        }

                                        int compared = comparator.compare(o1, o2);

                                        if (compared >= 0)

                                        {

                                                  accept1 = accept;

                                        }

                                        else

                                        {

                                                  accept1 = !accept;

                                        }

                                        int compared2 = comparator.compare(o1, o3);

                                        if (compared2 > 0)

                                        {

                                                  accept2 = !accept;

                                        }

                                        else

                                        {

                                                  accept2 = accept;

                                        }

                                        if (accept1 && accept2)

                                        {

                                                  return accept;

                                        }

                                        else

                                        {

                                                  return !accept;

                                        }

                              }

                              return accept;

                    }

                    catch (Exception ex)

                    {

                              return accept;

                    }

          }

          /* parse the filter */

          private HashMap detectFilterSign( String value )

          {

                    HashMap ar = new HashMap();

                    String val1 = null;

                    String val2 = null;

                    String sign = null;

                    String place = null;

                    if (null == value)

                    {

                              ar.put("value1", value);

                              ar.put("sign", null);

                              return ar;

                    }

                    if (!value.startsWith(EQ) & !value.startsWith(NE) & value.indexOf(R) < 0)

                    {

                              ar.put("value1", value.trim());

                              ar.put("sign", null);

                              return ar;

                    }

                    if (value.startsWith(EQ))

                    {

                              if (value.trim().length() > 1)

                              {

                                        val1 = value.substring(1);

                              }

                              else

                              {

                                        val1 = "";

                                        val1 = null;

                              }

                              sign = EQ;

                              place = "S";

                              ar.put("value1", val1);

                              ar.put("sign", EQ);

                              ar.put("place", place);

                              return ar;

                    }

                    if (value.startsWith(NE))

                    {

                              if (value.trim().length() > 1)

                              {

                                        val1 = value.substring(1);

                              }

                              else

                              {

                                        val1 = "";

                                        val1 = null;

                              }

                              sign = NE;

                              place = "S";

                              ar.put("value1", val1);

                              ar.put("sign", NE);

                              ar.put("place", place);

                              return ar;

                    }

                    if (value.startsWith(R))

                    {

                              if (value.trim().length() > 1)

                              {

                                        val1 = value.substring(1);

                              }

                              else

                              {

                                        val1 = "";

                                        val1 = null;

                              }

                              sign = R;

                              place = "S";

                              ar.put("value1", val1);

                              ar.put("sign", R);

                              ar.put("place", place);

                              return ar;

                    }

                    if (value.endsWith(R))

                    {

                              if (value.trim().length() > 1)

                              {

                                        val1 = value.substring(0, value.length() - 1);

                              }

                              else

                              {

                                        val1 = "";

                                        val1 = null;

                              }

                              sign = R;

                              place = "E";

                              ar.put("value1", val1);

                              ar.put("sign", R);

                              ar.put("place", place);

                              return ar;

                    }

                    if (value.indexOf(R) > 0)

                    {

                              sign = R;

                              place = "M";

                              val1 = value.substring(0, value.indexOf(R));

                              val2 = value.substring(value.indexOf(R) + 1);

                              ar.put("value1", val1);

                              ar.put("value2", val2);

                              ar.put("sign", R);

                              ar.put("place", place);

                              return ar;

                    }

                    if (ar.size() == 0)

                    {

                              ar.put("value1", value);

                    }

                    return ar;

          }

          private static final String bindingOfPrimaryProperty( IWDTableCellEditor editor )

          {

                    return editor instanceof IWDViewElement ? bindingOfPrimaryProperty((IWDViewElement) editor) : null;

          }

          private static final String bindingOfPrimaryProperty( IWDViewElement element )

          {

                    if (element instanceof IWDAbstractDropDownByIndex) return ((IWDAbstractDropDownByIndex) element).bindingOfTexts();

                    if (element instanceof IWDAbstractDropDownByKey)           return ((IWDAbstractDropDownByKey) element).bindingOfSelectedKey();

                    if (element instanceof IWDAbstractInputField)                     return ((IWDAbstractInputField) element).bindingOfValue();

                    if (element instanceof IWDCaption)                                                             return ((IWDCaption) element).bindingOfText();

                    if (element instanceof IWDCheckBox)                                                   return ((IWDCheckBox) element).bindingOfChecked();

                    if (element instanceof IWDLink)                                                                       return ((IWDLink) element).bindingOfText();

                    if (element instanceof IWDProgressIndicator)                     return ((IWDProgressIndicator) element).bindingOfPercentValue();

                    if (element instanceof IWDRadioButton)                                         return ((IWDRadioButton) element).bindingOfSelectedKey();

                    if (element instanceof IWDTextEdit)                                                   return ((IWDTextEdit) element).bindingOfValue();

                    if (element instanceof IWDTextView)                                                   return ((IWDTextView) element).bindingOfText();

                    return null;

          }

          private static final Comparator DEFAULT = new Comparator()

          {

                    public int compare( Object o1, Object o2 )

                    {

                              if (o1 == null && o2 == null) { return 0; }

                              if (o1 == null) { return -1; }

                              if (o2 == null) { return -1; }

                              if (o1 instanceof Boolean && o2 instanceof Boolean)

                              {

                                        return o1.toString().compareTo(o2.toString());

                              }

                              if ( o1 instanceof java.util.Date )

                              {

                                        java.util.Date dummy = (java.util.Date) o1 ;

                                        o1 = new java.sql.Date( dummy.getTime() );

                              }

                              if ( o2 instanceof java.util.Date )

                              {

                                        java.util.Date dummy2 = (java.util.Date) o2 ;

                                        o2 = new java.sql.Date( dummy2.getTime() );

                              }

                              if ( o1 instanceof java.sql.Date && o2 instanceof java.sql.Date )

                              {

                                        return ((java.sql.Date) o1).compareTo((java.sql.Date) o2);

                              }

                              if (o1 instanceof String && o2 instanceof String)

                              {

                                        return o1.toString().toUpperCase(WDResourceHandler.getCurrentSessionLocale()).indexOf(o2.toString().toUpperCase(WDResourceHandler.getCurrentSessionLocale()));

                              }

                              return ((Comparable) o1).compareTo((Comparable) o2);

                    }

          };

          private Hashtable filterForColumn = new Hashtable();

          private IWDTable table = null;

          private Hashtable attributeSource = null;

          private Hashtable sourceNodeHash = null;

          private Hashtable hashIcon = null;

          private IWDNode sourceNode = null;

          private IWDNode targetNode = null;

          String EQ = "=";

          String NE = "#";

          String R = "~";

          public final class NodeElementByAttributeComparator

          {

                    public NodeElementByAttributeComparator( String attributeInfo, IWDTableColumn column, Comparator comparator )

                    {

                              this(attributeInfo, column, comparator, false, new ArrayList());

                    }

                    private NodeElementByAttributeComparator( String attributeInfo, IWDTableColumn column, Comparator comparator, boolean compareAsText, Collection subNodes )

                    {

                              if (attributeInfo == null) throw new IllegalArgumentException("attributeInfo must not be null");

                              if (column == null) throw new IllegalArgumentException("Column must not be null");

                              if (comparator == null) comparator = DEFAULT;

                              this.attributeInfo = attributeInfo;

                              this.column = column;

                              this.comparator = comparator;

                              this.compareAsText = compareAsText;

                              this.subNodes = subNodes;

                    }

                    private final Comparator comparator;

                    private final boolean compareAsText;

                    private IWDTableColumn column;

                    private Collection subNodes;

                    private String attributeInfo;

          }

}

************* TableFilter end***************************

Answers (0)