~ubuntu-branches/ubuntu/raring/accessodf/raring

« back to all changes in this revision

Viewing changes to core/be/docarch/accessodf/FilterSorter.java

  • Committer: Package Import Robot
  • Author(s): Sebastian Humenda
  • Date: 2012-04-09 11:21:13 UTC
  • Revision ID: package-import@ubuntu.com-20120409112113-v0kmfdj1ks80xoj8
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
 *  AccessODF - Accessibility checker for OpenOffice.org and LibreOffice Writer.
 
3
 *
 
4
 *  Copyright (c) 2011 by DocArch <http://www.docarch.be>.
 
5
 *
 
6
 *  This program is free software: you can redistribute it and/or modify
 
7
 *  it under the terms of the GNU Lesser General Public License as
 
8
 *  published by the Free Software Foundation, either version 3 of the
 
9
 *  License, or (at your option) any later version.
 
10
 *
 
11
 *  This program is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 *  GNU Lesser General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Lesser General Public License
 
17
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
18
 */
 
19
 
 
20
package be.docarch.accessodf;
 
21
 
 
22
import java.util.ArrayList;
 
23
import java.util.TreeMap;
 
24
 
 
25
/**
 
26
 *
 
27
 * @author Bert Frees
 
28
 */
 
29
public class FilterSorter implements java.util.Comparator<Issue> {
 
30
 
 
31
    public static enum Property { NATURAL, CATEGORY, CHECKID, NAME }
 
32
    public static final Property NATURAL = Property.NATURAL;
 
33
    public static final Property CHECKID = Property.CHECKID;
 
34
    public static final Property NAME = Property.NAME;
 
35
 
 
36
    private TreeMap<Property, Boolean> upDown;
 
37
    private ArrayList<Property> priority;
 
38
 
 
39
 
 
40
    public FilterSorter() {
 
41
 
 
42
        upDown = new TreeMap<Property, Boolean>();
 
43
        upDown.put(NATURAL, true);
 
44
        upDown.put(NAME, true);
 
45
        upDown.put(CHECKID, true);
 
46
        priority = new ArrayList<Property>();
 
47
        priority.add(NATURAL);
 
48
        priority.add(NAME);
 
49
        priority.add(CHECKID);
 
50
 
 
51
    }
 
52
 
 
53
    public void setOrder(Property property,
 
54
                         boolean upDown) {
 
55
 
 
56
        if (this.upDown.containsKey(property)) {
 
57
            this.upDown.put(property, upDown);
 
58
        }
 
59
    }
 
60
 
 
61
    public void setOrderPriority(Property property,
 
62
                                 boolean highestLowest) {
 
63
 
 
64
        if (priority.contains(property)) {
 
65
            priority.remove(property);
 
66
            if (highestLowest) {
 
67
                priority.add(0, property);
 
68
            } else {
 
69
                priority.add(property);
 
70
            }
 
71
        }
 
72
    }
 
73
 
 
74
    public int compare(Issue entry1,
 
75
                       Issue entry2) {
 
76
 
 
77
        if (entry1 == null && entry2 == null) {
 
78
            return 0;
 
79
        } else if (entry1 == null) {
 
80
            return -1;
 
81
        } else if (entry2 == null) {
 
82
            return 1;
 
83
        }
 
84
 
 
85
        Property property;
 
86
        int compare = 0;
 
87
 
 
88
        for (int i=0; i<priority.size(); i++) {
 
89
            property = priority.get(i);
 
90
            switch (property) {
 
91
                case NAME:
 
92
                    compare = (entry1.getName().compareTo(entry2.getName()));
 
93
                    break;
 
94
                case CHECKID:
 
95
                    compare = (entry1.getCheck().getIdentifier().compareTo(entry2.getCheck().getIdentifier()));
 
96
                    break;
 
97
                case NATURAL:
 
98
                default:
 
99
                    compare = 0;
 
100
            }
 
101
            if (compare != 0) {
 
102
                if (upDown.get(property)) {
 
103
                    return compare;
 
104
                } else {
 
105
                    return -compare;
 
106
                }
 
107
            }
 
108
        }
 
109
 
 
110
        return compare;
 
111
    }
 
112
 
 
113
    public boolean accept(Issue entry) {
 
114
        return (entry != null);
 
115
    }
 
116
}