Project

General

Profile

Download (5.68 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2016 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.taxeditor.ui.dialog.selection;
10

    
11
import java.text.Collator;
12
import java.util.Comparator;
13
import java.util.List;
14

    
15
import org.eclipse.jface.viewers.ILabelProvider;
16
import org.eclipse.jface.viewers.LabelProvider;
17
import org.eclipse.swt.widgets.Shell;
18

    
19
import eu.etaxonomy.cdm.api.service.IAgentService;
20
import eu.etaxonomy.cdm.api.service.dto.EntityDTOBase;
21
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
22
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
23
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
24
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
25
import eu.etaxonomy.cdm.persistence.dto.TeamOrPersonUuidAndTitleCache;
26
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
27
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
28
import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
29
import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
30
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
31
import eu.etaxonomy.taxeditor.store.CdmStore;
32

    
33
/**
34
 * @author k.luther
35
 * @date 25.05.2016
36
 */
37
public class NomenclaturalAuthorSelectionDialog extends TeamOrPersonBaseSelectionDialog {
38

    
39
    protected NomenclaturalAuthorSelectionDialog(Shell shell,
40
            String title,
41
            boolean multi, String settings, TeamOrPersonBase<?> agent, boolean teamMemberSelection) {
42
        super(shell, title, multi, settings, agent, teamMemberSelection);
43

    
44
    }
45

    
46
    @Override
47
    protected void callService(String pattern) {
48
        model = (List)CdmStore.getService(IAgentService.class).getUuidAndAbbrevTitleCache(TeamOrPersonBase.class, limitOfInitialElements, pattern);
49
    }
50

    
51
    @Override
52
    protected AbstractNewEntityWizard<? extends TeamOrPersonBase<?>> getNewEntityWizard(String parameter) {
53
        if(TEAM.equals(parameter)){
54
            return new NewTeamWizard(true, false);
55
        }
56
        else if(PERSON.equals(parameter)){
57
            return new NewPersonWizard();
58
        }
59
        else{
60
            throw new IllegalArgumentException("Could not determine the desired wizard.");
61
        }
62
    }
63

    
64
//    @Override
65
//	protected ILabelProvider createListLabelProvider() {
66
//		return new FilteredCdmResourceAbbrevLabelProvider();
67
//	}
68

    
69
    public static TeamOrPersonBase<?> select(Shell shell,
70
            TeamOrPersonBase<?> entity, boolean teamMemberSelection) {
71
        NomenclaturalAuthorSelectionDialog dialog = new NomenclaturalAuthorSelectionDialog(shell, //conversation,
72
                "Choose Agent", false, NomenclaturalAuthorSelectionDialog.class.getCanonicalName(), entity, teamMemberSelection);
73
        return getSelectionFromDialog(dialog);
74
    }
75

    
76
    @Override
77
    protected String getTitle(TeamOrPersonBase<?> cdmObject) {
78
        if(cdmObject == null){
79
            return "";
80
        }
81

    
82
        return ((INomenclaturalAuthor)cdmObject).getNomenclaturalTitleCache();
83
    }
84

    
85
    @Override
86
    protected ILabelProvider createListLabelProvider() {
87
        return new NomenclaturalLabelProvider();
88
    }
89

    
90
    public class NomenclaturalLabelProvider extends LabelProvider {
91
        @Override
92
        public String getText(Object element) {
93
            if (element == null) {
94
                return null;
95
            }
96
            UuidAndTitleCache<?> uuidAndTitleCache = (UuidAndTitleCache<?>) element;
97
            String title = uuidAndTitleCache.getAbbrevTitleCache();
98
            String titleCache = uuidAndTitleCache.getTitleCache();
99
            String collectorTitleCache = null;
100
            if (element instanceof TeamOrPersonUuidAndTitleCache){
101
                collectorTitleCache = ((TeamOrPersonUuidAndTitleCache)element).getCollectorTitleCache();
102
            }
103
            if (!titleCache.equals(title)){
104
                title += " - " + titleCache;
105
            }
106
            if (collectorTitleCache != null && !uuidAndTitleCache.getTitleCache().equals(collectorTitleCache) && !titleCache.equals(collectorTitleCache)){
107
                title += " - " + collectorTitleCache;
108
            }
109
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
110
                title += " ["+uuidAndTitleCache.getId()+"]";
111
            }
112
            if (element instanceof EntityDTOBase){
113
                title += "(" + ((IdentifiedEntityDTO)element).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO)element).getIdentifier().getIdentifier() + ")";
114
            }
115

    
116
            return title;
117
        }
118
    }
119

    
120
	@Override
121
	protected Comparator<UuidAndTitleCache<TeamOrPersonBase<?>>> getItemsComparator() {
122
		return new Comparator<UuidAndTitleCache<TeamOrPersonBase<?>>>() {
123
			@Override
124
			public int compare(UuidAndTitleCache<TeamOrPersonBase<?>> entity1,
125
					UuidAndTitleCache<TeamOrPersonBase<?>> entity2) {
126

    
127
			    if (entity1.getUuid().equals(entity2.getUuid())){
128
			        return 0;
129
			    }
130
				Collator collator = Collator.getInstance();
131
				String compareString1 = "";
132
				if (entity1.getAbbrevTitleCache() != null){
133
					compareString1 = entity1.getAbbrevTitleCache();
134
				}
135
				if (entity1.getTitleCache() != null){
136
					compareString1 += entity1.getTitleCache();
137
				}
138

    
139
				String compareString2 = "";
140
				if (entity2.getAbbrevTitleCache() != null){
141
					compareString2 = entity2.getAbbrevTitleCache();
142
				}
143
				if (entity2.getTitleCache() != null){
144
					compareString2 += entity2.getTitleCache();
145
				}
146

    
147

    
148
				int result =collator.compare(compareString1, compareString2);
149
				if (result == 0){
150
				    result = entity1.getUuid().compareTo(entity2.getUuid());
151
				}
152
				return result;
153
			}
154
		};
155
	}
156
}
(25-25/46)