www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.dwt - a general tableViewer comparator

usage:

1.  the class to compare must implement the member get.(columnText) to get
value from column text
2.  string compare


module dwin.dwtx.ColumnSorter;

// Written in the D programming language.

/*******************************************************************************
        
        Copyright:      Copyright (c) 2008  (yidabu  g m a i l at com) All
rights reserved

        License:        BSD style: $(LICENSE)

        Version:        Initial release: May 2008

        Authors:        yidabu ( D Programming Language China :
http://www.d-programming-language-china.org/ )

*******************************************************************************/



version(DWTX):

import dwtx.jface.viewers.ColumnViewer;
import dwtx.jface.viewers.Viewer;
import dwtx.jface.viewers.ViewerComparator; 

import dwt.dwthelper.utils : compareToIgnoreCase ;

import dwt.DWT;    
import dwt.widgets.TableColumn;    
import dwt.widgets.Event;
import dwt.events.SelectionAdapter;
import dwt.events.SelectionEvent;

import dwin.core.Tuple : HasMember;
    

/**
    simple column sorter based on dwt jface

    Example:
    ---
        public abstract class BaseRecord
        {
            protected char[][char[]] instanceAA_;
            
            bool set(char[] key, char[] value)
            {
                if( (key in instanceAA) !is null )
                    instanceAA_[key] = value;
                else
                    return false;
                return true;
            }
            
            char[] get(char[] key)
            {
                auto p = key in instanceAA_;
                return (p !is null)? *p : null;
            }             
        }
        
        alias ColumnSorterT(BaseRecord) Sorter;
        
        TableViewer viewer;
        ...
        for (int i; i < 5; i++)
        {            
            auto column = new TableColumn(table, DWT.LEFT);
            column.setText( to!(char[])i) );
            new class(viewer, column) Sorter
            {
                this(TableViewer v, TableColumn t)
                {
                    super(v,t);
                }
            };                            
        }        
        
    ---

*/

class ColumnSorterT(T) : ViewerComparator 
{
        public static final int ASC = 1;

        public static final int NONE = 0;

        public static final int DESC = -1;

        private int direction = 0;

        private TableColumn column;
        private ColumnViewer viewer;
        private char[] columnText;

        public this(ColumnViewer viewer_, TableColumn column_) 
        {
            this.column = column_;
            this.viewer = viewer_;
            this.column.addSelectionListener
            (
                new class() SelectionAdapter 
                {
                    public void widgetSelected(SelectionEvent e) 
                    {
                        this.outer.widgetSelected(e);
                    }
                }
            );            
        }

        private void widgetSelected(SelectionEvent e)
        {
            this.columnText = e.widget.getNameText;
            if( viewer.getComparator() !is null ) 
            {
                if( viewer.getComparator() is this ) 
                {
                    int tdirection = direction;

                    if( tdirection is ASC ) 
                    {
                        setSorter(this, DESC);
                    } 
                    else if( tdirection is DESC ) 
                    {
                        setSorter(this, NONE);
                    }
                } 
                else 
                {
                    setSorter(this, ASC);
                }
            } 
            else 
            {
                setSorter(this, ASC);
            }
        }
        public void setSorter(ColumnSorterT sorter, int direction) 
        {
            if( direction is NONE ) 
            {
                column.getParent().setSortColumn(null);
                column.getParent().setSortDirection(DWT.NONE);
                viewer.setComparator(null);
            } 
            else 
            {
                column.getParent().setSortColumn(column);
                sorter.direction = direction;

                if( direction is ASC ) 
                {
                    column.getParent().setSortDirection(DWT.DOWN);
                } 
                else 
                {
                    column.getParent().setSortDirection(DWT.UP);
                }

                if( viewer.getComparator() is sorter ) 
                {
                    viewer.refresh();
                } 
                else 
                {
                    viewer.setComparator(sorter);
                }

            }
        }

        public int compare(Viewer viewer, Object e1, Object e2) 
        {
            return direction * doCompare(viewer, e1, e2);
        }

        protected int doCompare(Viewer viewer, Object e1, Object e2)
        {
            static assert(HasMember!(T, "get"));
            
            auto a = cast(T) e1;
            auto b = cast(T) e2;
            return a.get(columnText).compareToIgnoreCase(b.get(columnText));   
        
        }
    }




-- 
yidabu <yidabu.spam gmail.com>
http://www.dsource.org/projects/dwin

D ÓïÑÔ-ÖÐÎÄ(D Chinese):
http://www.d-programming-language-china.org/
http://bbs.d-programming-language-china.org/
http://dwin.d-programming-language-china.org/
http://scite4d.d-programming-language-china.org/
May 19 2008