Project

General

Profile

Download (4.75 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.ui.section.description;
5

    
6
import java.util.ArrayList;
7
import java.util.Collection;
8
import java.util.Collections;
9
import java.util.Comparator;
10
import java.util.List;
11

    
12
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
13
import eu.etaxonomy.cdm.model.common.OriginalSourceType;
14
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
15
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
16
import eu.etaxonomy.cdm.model.reference.Reference;
17
import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
18
import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
19
import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
20

    
21
/**
22
 * <p>
23
 * DescriptionElementSourceSection class.
24
 * </p>
25
 *
26
 * @author n.hoffmann
27
 * @created Nov 17, 2009
28
 * @version 1.0
29
 */
30
public class DescriptionElementSourceSection extends
31
        AbstractEntityCollectionSection<DescriptionElementBase, DescriptionElementSource> {
32

    
33
    /**
34
     * <p>
35
     * Constructor for DescriptionElementSourceSection.
36
     * </p>
37
     *
38
     * @param parentElement
39
     *            a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
40
     *            object.
41
     * @param style
42
     *            a int.
43
     * @param cdmFormFactory
44
     *            a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
45
     *            object.
46
     * @param conversation
47
     *            a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
48
     *            object.
49
     */
50
    public DescriptionElementSourceSection(CdmFormFactory cdmFormFactory, ConversationHolder conversation,
51
            ICdmFormElement parentElement, int style) {
52
        super(cdmFormFactory, conversation, parentElement, "References", style);
53
    }
54

    
55
    /** {@inheritDoc} */
56
    @Override
57
    public DescriptionElementSource createNewElement() {
58
        return DescriptionElementSource.NewInstance(OriginalSourceType.PrimaryTaxonomicSource);
59
    }
60

    
61
    /** {@inheritDoc} */
62
    @Override
63
    public Collection<DescriptionElementSource> getCollection(DescriptionElementBase entity) {
64
        List<DescriptionElementSource> sources = new ArrayList<DescriptionElementSource>(entity.getSources());
65
        Collections.sort(sources, new Comparator<DescriptionElementSource>() {
66

    
67
            @Override
68
            public int compare(DescriptionElementSource o1, DescriptionElementSource o2) {
69
                int id1 = o1.getId();
70
                int id2 = o2.getId();
71
                OriginalSourceType type1 = o1.getType();
72
                OriginalSourceType type2 = o2.getType();
73
                Reference citation1 = o1.getCitation();
74
                Reference citation2 = o2.getCitation();
75

    
76
                // the newly created should always be on top
77
                if (id1 == 0) {
78
                    return -1;
79
                }
80
                if (id2 == 0) {
81
                    return 1;
82
                }
83

    
84
                // sort by type (Primary taxonomic > Primary Media > others
85
                // alphabetically by reference title cache)
86
                if (type1 != null && type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
87
                    return -1;
88
                }
89
                if (type1 != null && type1.equals(OriginalSourceType.PrimaryMediaSource)
90
                        && type2!=null && !type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
91
                    return -1;
92
                }
93
                if (type2 != null && type2.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
94
                    return 1;
95
                }
96
                if (type2 != null && type2.equals(OriginalSourceType.PrimaryMediaSource)
97
                        && type1!=null && !type1.equals(OriginalSourceType.PrimaryTaxonomicSource)) {
98
                    return 1;
99
                }
100

    
101
                //sort by citation title cache
102
                if(citation1==null){
103
                    return -1;
104
                }
105
                if(citation2==null){
106
                    return 1;
107
                }
108
                if(citation1!=null && citation2!=null){
109
                    return citation1.getTitleCache().compareTo(citation2.getTitleCache());
110
                }
111
                return 0;
112
            }
113
        });
114
        return sources;
115
    }
116

    
117
    /** {@inheritDoc} */
118
    @Override
119
    public String getEmptyString() {
120
        return "No references yet.";
121
    }
122

    
123
    /** {@inheritDoc} */
124
    @Override
125
    protected String getTooltipString() {
126
        return "Create a new reference";
127
    }
128

    
129
    /** {@inheritDoc} */
130
    @Override
131
    public void addElement(DescriptionElementSource element) {
132
        getEntity().addSource(element);
133
    }
134

    
135
    /** {@inheritDoc} */
136
    @Override
137
    public void removeElement(DescriptionElementSource element) {
138
        getEntity().removeSource(element);
139
    }
140
}
(9-9/24)