How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory : 表 « SWT-JFace-Eclipse « Java

En
Java
1. 图形用户界面
2. 三维图形动画
3. 高级图形
4. 蚂蚁编译
5. Apache类库
6. 统计图
7. 
8. 集合数据结构
9. 数据类型
10. 数据库JDBC
11. 设计模式
12. 开发相关类
13. EJB3
14. 电子邮件
15. 事件
16. 文件输入输出
17. 游戏
18. 泛型
19. GWT
20. Hibernate
21. 本地化
22. J2EE平台
23. 基于J2ME
24. JDK-6
25. JNDI的LDAP
26. JPA
27. JSP技术
28. JSTL
29. 语言基础知识
30. 网络协议
31. PDF格式RTF格式
32. 映射
33. 常规表达式
34. 脚本
35. 安全
36. Servlets
37. Spring
38. Swing组件
39. 图形用户界面
40. SWT-JFace-Eclipse
41. 线程
42. 应用程序
43. Velocity
44. Web服务SOA
45. 可扩展标记语言
Java 教程
Java » SWT-JFace-Eclipse » 屏幕截图 
How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory
How to order 1000 elements in a swt column table with O(n log(n)) complexity! using Comparator and Array.sort() implemented in a TableColumn Listener Factory



/*
 *  This snippet show how to order 1000 elements in a swt column table 
 *  with O(n log(n)) complexity! using Comparator and Array.sort() 
 *  implemented in a TableColumn Listener Factory
 *  
 *  Comparator implemented: INT_COMPARATOR, STRING_COMPARATOR, DATE_COMPARATOR, DOUBLE_COMPARATOR, HOUR_COMPARATOR  
 *  Example: column.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.STRING_COMPARATOR));
 *  
 *  Gianluigi Davassi 
 *  Feel free to contact me: davassi (at) yahoo.it
 *  
 * */

/*  Copyright (C) 2004 by Gianluigi Davassi www.omega-dream.com
 
 This library is free software; you can redistribute it and/or
 modify it under the terms of the GNU Lesser General Public
 License as published by the Free Software Foundation; either
 version 2.1 of the License, or (at your option) any later version.

 This library is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 Lesser General Public License for more details.

 You should have received a copy of the GNU Lesser General Public
 License along with this library; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
 Author: Gianluigi Davassi
 davassi (at) yahoo.it
 www.omega-dream.com
 
 */


package org.omegadream;

import java.text.Collator;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Date;
import java.util.Locale;
import java.util.Random;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class TableSortListenerFactory {

    private static final int MAX_ELEMENTS = 1000;
    private Display display;
    private Shell shell;
    
    private Table table;
    private TableColumn intColumn,stringColumn,dateColumn,doubleColumn,hourColumn;  
    
    private Random ran = new Random();
    
    public TableSortListenerFactory() {
        display = new Display();
        shell = new Shell(display);

        shell.setSize(640480);
        
        shell.setText("A Table Sorter Example. Click on column header to order it!");
        shell.setLayout(new FillLayout());

        table = new Table(shell, SWT.HIDE_SELECTION|SWT.SINGLE|SWT.FULL_SELECTION|SWT.BORDER);
        table.setHeaderVisible(true);
        table.setLinesVisible(true);

        intColumn = new TableColumn(table, SWT.CENTER);
        intColumn.setText("Number Column");
        intColumn.setWidth(120);
        intColumn.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.INT_COMPARATOR));
        
        stringColumn = new TableColumn(table, SWT.CENTER);
        stringColumn.setText("String Column");
        stringColumn.setWidth(120);
        stringColumn.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.STRING_COMPARATOR));
        
        dateColumn = new TableColumn(table, SWT.CENTER);
        dateColumn.setText("Date Column");
        dateColumn.setWidth(120);
        dateColumn.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.DATE_COMPARATOR));
        
        doubleColumn = new TableColumn(table, SWT.CENTER);
        doubleColumn.setText("Double Column");
        doubleColumn.setWidth(120);
        doubleColumn.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.DOUBLE_COMPARATOR));
        
        hourColumn = new TableColumn(table, SWT.CENTER);
        hourColumn.setText("Hour Column");
        hourColumn.setWidth(120);
        hourColumn.addListener(SWT.Selection, SortListenerFactory.getListener(SortListenerFactory.HOUR_COMPARATOR));
     
        // let's populate the table with random data!
        for (int i = 0; i< MAX_ELEMENTS ; i++)
        {
            String int_value = String.valueOf(ran.nextInt(1000000));
            
            String string_value = randomstring(4,16);
            
            String date_value = "" + ran.nextInt(31"/" + ran.nextInt(12"/" + ran.nextInt(2000)// dd-mm-aaaa
            
            String double_value = String.valueOf(ran.nextDouble());
            
            String hour_value = "" + ran.nextInt(24":" + ran.nextInt(60":" + ran.nextInt(60)// hh:mm:SS
            
            // ok now store it in the table
            TableItem item = new TableItem(table, SWT.NONE);
            item.setText(new String[] { int_value, string_value, date_value, double_value, hour_value });
        }
      
        shell.open();
        while (!shell.isDisposed()) {
          if (!display.readAndDispatch())
            display.sleep();
        }
        display.dispose();
    }
    
    
    public static void main(String[] args)
    {
        new TableSortListenerFactory();
    }
    
    private int rand(int lo, int hi)
    {
            int n = hi - lo + 1;
            int i = ran.nextInt() % n;
            if (i < 0)
                    i = -i;
            return lo + i;
    }

    private String randomstring(int lo, int hi)
    {
            int n = rand(lo, hi);
            byte b[] new byte[n];
            for (int i = 0; i < n; i++)
                    b[i(byte)rand('a''z');
            return new String(b, 0);
    }
    
}

// this is the ListenerFactory implementation

class SortListenerFactory implements Listener
{
    private Comparator currentComparator = null;
    
    private Collator col = Collator.getInstance(Locale.getDefault());
    
    public static final int INT_COMPARATOR    = 0;
    public static final int STRING_COMPARATOR = 1;
    public static final int DATE_COMPARATOR   = 2;
    public static final int DOUBLE_COMPARATOR = 3;
    public static final int HOUR_COMPARATOR   = 4;
    
    private SortListenerFactory(int _comp)
    {
        switch (_comp
        {
        case INT_COMPARATOR:
            currentComparator = intComparator;
            break;

        case STRING_COMPARATOR:
            currentComparator = strComparator;
            break;

        case DATE_COMPARATOR:
            currentComparator = dateComparator;
            break;
            
        case DOUBLE_COMPARATOR:
            currentComparator = doubleComparator;
            break;
            
        case HOUR_COMPARATOR:
            currentComparator = hourComparator;
            break;

        default:
            currentComparator = strComparator;
        }
    }
    
    public static Listener getListener(int _comp)
    {
        return new SortListenerFactory(_comp);
    }
    
    private int colIndex = 0;
    private int updown   = 1;
          
    // Integer Comparator
    private Comparator intComparator = new Comparator()
    {
        public int compare(Object arg0, Object arg1) {

            TableItem t1 = (TableItem)arg0;
            TableItem t2 = (TableItem)arg1;

            int v1 = Integer.parseInt(t1.getText(colIndex));
            int v2 = Integer.parseInt(t2.getText(colIndex));

            if (v1<v2return 1*updown;
            if (v1>v2return -1*updown;

            return 0;
        }    
    };
         
    // String Comparator
    private Comparator strComparator = new Comparator()
    {
        public int compare(Object arg0, Object arg1) {

            TableItem t1 = (TableItem)arg0;
            TableItem t2 = (TableItem)arg1;

            String v1 = (t1.getText(colIndex));
            String v2 = (t2.getText(colIndex));

            return (col.compare(v1,v2))*updown;
        }    
    };
    
    // Double Comparator
    private Comparator doubleComparator = new Comparator()
    {
        public int compare(Object arg0, Object arg1) {
            
            TableItem t1 = (TableItem)arg0;
            TableItem t2 = (TableItem)arg1;
            
            double v1 = Double.parseDouble(t1.getText(colIndex));
            double v2 = Double.parseDouble(t2.getText(colIndex));
            
            if (v1<v2return 1*updown;
            if (v1>v2return -1*updown;
            
            return 0;
        }    
    };
    
    // Hour Comparator (hh:mm:ss)
    private Comparator hourComparator = new Comparator()
    {
        public int compare(Object arg0, Object arg1) {
            
            TableItem t1 = (TableItem)arg0;
            TableItem t2 = (TableItem)arg1;
            
            String v1 = (t1.getText(colIndex)).trim();
            String v2 = (t2.getText(colIndex)).trim();
            
            DateFormat df = new SimpleDateFormat("hh:mm:ss");
           
            Date d1 = null; Date d2 = null;
            
            try {                
                d1 = df.parse(v1);
            catch (ParseException e
            
                System.out.println("[WARNING] v1 " + v1);
                try d1 = df.parse("01:01:01")catch (ParseException e1) {}
            }
            
            try {               
                d2 = df.parse(v2);
            catch (ParseException e
            
                System.out.println("[WARNING] v2 " + v2);
                try d2 = df.parse("01:01:01")catch (ParseException e1) {}
            }

            if (d1.equals(d2))
                return 0;

            return updown*(d1.before(d2: -1);
        }    
    };
    
    private Comparator dateComparator = new Comparator()
    {
        public int compare(Object arg0, Object arg1
        {    
            TableItem t1 = (TableItem)arg0;
            TableItem t2 = (TableItem)arg1;
            
            String v1 = (t1.getText(colIndex)).trim();
            String v2 = (t2.getText(colIndex)).trim();

            v1.replaceAll("-""/");
            v2.replaceAll("-""/");
            
            DateFormat df_europe = new SimpleDateFormat("dd/MM/yyyy");
            DateFormat df_usa = new SimpleDateFormat("yyyy/MM/dd");

            DateFormat df = df_europe;
            
            Date d1 = null; Date d2 = null;
            
            try {
                d1 = df.parse(v1);
            catch (ParseException e
            
               //System.out.println("[WARNING] v1 " + v1);
                d1 = new Date("01/01/1900");
            }
            
            try {               
                d2 = df.parse(v2);
            catch (ParseException e
            
                d2 = new Date("01/01/1900");
            }

            if (d1.equals(d2))
                return 0;

            return updown*(d1.before(d2: -1);
        }    
    };
        
    public void handleEvent(Event e) {
        
        updown = (updown == ? -1);
        
        TableColumn currentColumn = (TableColumn)e.widget;
        Table table = currentColumn.getParent();
    
        colIndex = searchColumnIndex(currentColumn);
        
        table.setRedraw(false);
        
        TableItem[] items = table.getItems();
       
        Arrays.sort(items,currentComparator);
        
        table.setItemCount(items.length);
        
        for (int i = 0;i<items.length;i++)
        {   
            TableItem item = new TableItem(table,SWT.NONE,i);
            item.setText(getData(items[i]));
            items[i].dispose();
        }
        
        table.setRedraw(true);     
    }
    
    private String[] getData(TableItem t)
    {
        Table table = t.getParent();
        
        int colCount = table.getColumnCount();
        String [] s = new String[colCount];
        
        for (int i = 0;i<colCount;i++)
            s[i= t.getText(i);
                
        return s;
        
    }
    
    private int searchColumnIndex(TableColumn currentColumn)
    {
        Table table = currentColumn.getParent();
        
        int in = 0;
        
        for (int i = 0;i<table.getColumnCount();i++)
            if (table.getColumn(i== currentColumn)
                in = i;
        
        return in;
    }
}

           
       
Related examples in the same category
1. Print KTable (SWT Table)Example Print KTable (SWT Table)Example
2. The source of a custom table class for Java SWT applicationsThe source of a custom table class for Java SWT applications
3. SWT表编辑器
4. SWT表简单的文件浏览器SWT表简单的文件浏览器
5. 错误追踪JFace错误追踪JFace
6. 错误追踪错误追踪
7. 文件浏览器演示文件浏览器演示
8. TableEditor演示TableEditor演示
9. 文件浏览器文件浏览器
10. 文件浏览器JFace
11. 错误报告错误报告
12. 显示ASCII码显示ASCII码
13. SWT.VIRTUAL风格SWT.VIRTUAL风格
14. 显示表显示表
15. 表排序表排序
16. 演示TableCursor演示TableCursor
17. 演示TableTree演示TableTree
18. 演示TableEditor演示TableEditor
19. Shows the extensions on the system and their associated programsShows the extensions on the system and their associated programs
20. 表实例3表实例3
21. 表实例2表实例2
22. 表实例表实例
23. 演示TableViewers演示TableViewers
24. 演示CheckboxTableViewer演示CheckboxTableViewer
25. 演示CellEditors演示CellEditors
26. SWT表简单演示SWT表简单演示
27. 在SWT表创建虚提示在SWT表创建虚提示
28. SWT表,进度条单元格SWT表,进度条单元格
29. 在一个SWT表编辑单元格(原地,花式)在一个SWT表编辑单元格(原地,花式)
30. SWT表项目编辑文字(原地)SWT表项目编辑文字(原地)
31. 浏览SWT表单元格,使用箭头键浏览SWT表单元格,使用箭头键
32. 更新SWT表项目文本更新SWT表项目文本
33. 排序表列SWT排序表列SWT
34. 选择行指数(选择和滚动)在SWT表选择行指数(选择和滚动)在SWT表
35. 滚动SWT表(设置顶端指数)滚动SWT表(设置顶端指数)
36. 调整列,SWT表调整调整列,SWT表调整
37. 删除选定的项目,在一个SWT表删除选定的项目,在一个SWT表
38. 打印选定的项目,在一个SWT表打印选定的项目,在一个SWT表
39. 添加任意控件,在SWT表添加任意控件,在SWT表
40. 重新排序重新排序
41. 插入一个SWT表列(索引)插入一个SWT表列(索引)
42. 插入一个SWT表项目(索引)插入一个SWT表项目(索引)
43. Find a SWT table cell from mouse down (works for any table style)Find a SWT table cell from mouse down (works for any table style)
44. Find a table cell from mouse down (SWT.FULL_SELECTION)Find a table cell from mouse down (SWT.FULL_SELECTION)
45. Detect a selection or check event in a table (SWT.CHECK)Detect a selection or check event in a table (SWT.CHECK)
46. 创建一个SWT项目表1,000,000创建一个SWT项目表1,000,000
47. 创建一个SWT表(栏,标题,线)创建一个SWT表(栏,标题,线)
48. 创建一个SWT表(无柱,无标题)创建一个SWT表(无柱,无标题)
49. SWT表彩色单元格和行SWT表彩色单元格和行
50. Create a virtual SWT table and add 1000 entries to it every 500 msCreate a virtual SWT table and add 1000 entries to it every 500 ms
51. 拖放数据类型取决于目标项目表拖放数据类型取决于目标项目表
52. 创建一个表(懒惰)创建一个表(懒惰)
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.