⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 entrysorter.java

📁 基于Eclipse RCP开发的管理工具
💻 JAVA
字号:
/*   * Copyright 2006 Marcel Schoffelmeer  *  * Licensed under the Apache License, Version 2.0 (the "License");  * you may not use this file except in compliance with the License.  * You may obtain a copy of the License at  *  *    http://www.apache.org/licenses/LICENSE-2.0  *     * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS,  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package com.s10r.manager;import java.util.Date;import org.eclipse.jface.viewers.Viewer;import org.eclipse.jface.viewers.ViewerSorter;import org.eclipse.swt.SWT;import com.s10r.manager.model.ManagerItem;import com.s10r.manager.model.PasswordEntry;class MyComparator<T1 extends Comparable<T1>>{    public int compare(T1 lhs, T1 rhs)    {        if (lhs == null)        {            return -1;        }        else if (rhs == null)        {            return 1;        }        else        {            return lhs.compareTo(rhs);        }    }}public class EntrySorter extends ViewerSorter{    private int columnIndex = 0;    private int direction = SWT.DOWN;    private static final MyComparator<Date> DATE_COMP = new MyComparator<Date>();    private static final MyComparator<Long> LONG_COMP = new MyComparator<Long>();    private static final MyComparator<Integer> INT_COMP = new MyComparator<Integer>();    public EntrySorter(int index, int direction)    {        this.columnIndex = index;        this.direction = direction;    }    @Override    public int compare(Viewer arg0, Object arg1, Object arg2)    {        // TODO: investigate whether comparing functionality can        // be made more generic: this method will get more complicated        // as more types of objects are supported.        ManagerItem t1 = (ManagerItem) arg1;        ManagerItem t2 = (ManagerItem) arg2;        switch (columnIndex)        {            case 0:            {                return compareIgnoreCase(t1.getName(), t2.getName(), direction);            }            case 3:            {                return compareIgnoreCase(t1.getDescription(), t2                        .getDescription(), direction);            }            case 6:            {                return DATE_COMP.compare(t1.getLastUpdated(), t2                        .getLastUpdated())                        * getDirectionMultiplier(direction);            }            default:            {                // go the next option: comparing two password entries            }                // if comparing two password entries:                if (t1 instanceof PasswordEntry && t2 instanceof PasswordEntry)                {                    PasswordEntry pe1 = (PasswordEntry) t1;                    PasswordEntry pe2 = (PasswordEntry) t2;                    switch (columnIndex)                    {                        case 1:                        {                            return compareIgnoreCase(pe1.getId(), pe2.getId(),                                    direction);                        }                        case 2:                        {                            return compareIgnoreCase(pe1.getPassword(), pe2                                    .getPassword(), direction);                        }                        case 4:                        {                            return INT_COMP.compare(pe1.getUsageCnt(),                                    pe2.getUsageCnt())* getDirectionMultiplier(direction);                        }                        case 5:                        {                            return DATE_COMP.compare(pe1.getLastUsed(),                                    pe2.getLastUsed())* getDirectionMultiplier(direction);                        }                        case 7:                        {                            return LONG_COMP.compare(pe1.getPasswordAge(), pe2                                    .getPasswordAge())* getDirectionMultiplier(direction);                        }                        case 8:                        {                            return compareIgnoreCase(pe1.getUrl(),                                    pe2.getUrl(), direction);                        }                        default:                        {                            // go the next option: comparing two password                            // entries                        }                    }                }        }        return 0;    }    private int compareIgnoreCase(String t1, String t2, int direction)    {        if (t1 == null && t2 == null)        {            return 0;        }        else if (t1 == null)        {            return (getDirectionMultiplier(direction)) * -1;        }        else if (t2 == null)        {            return (getDirectionMultiplier(direction)) * 1;        }        else        {            return (getDirectionMultiplier(direction))                    * t1.compareToIgnoreCase(t2);        }    }    /**     * Used by compare methods to invert their result if the direction is SWT.UP     * instead of DOWN. Resulting in switching the sorting from ascending to     * descending.     *      * @param direction     *            SWT.UP or SWT.DOWN     * @return 1 is direction if SWT.UP, -1 if SWT.DOWN     */    private int getDirectionMultiplier(int direction)    {        return direction == SWT.DOWN ? 1 : -1;    }    public int getDirection()    {        return direction;    }    public int getColumnIndex()    {        return columnIndex;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -