Project

General

Profile

Download (6.03 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);
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
    /**
70
     * <p>select</p>
71
     *
72
     * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
73
     * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
74
     * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
75
     * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
76
     */
77
    public static TeamOrPersonBase<?> select(Shell shell,
78
            TeamOrPersonBase<?> entity, boolean teamMemberSelection) {
79
        NomenclaturalAuthorSelectionDialog dialog = new NomenclaturalAuthorSelectionDialog(shell, //conversation,
80
                "Choose Agent", false, NomenclaturalAuthorSelectionDialog.class.getCanonicalName(), entity, teamMemberSelection);
81
        return getSelectionFromDialog(dialog);
82
    }
83

    
84
    @Override
85
    protected String getTitle(TeamOrPersonBase<?> cdmObject) {
86
        if(cdmObject == null){
87
            return "";
88
        }
89

    
90
        return ((INomenclaturalAuthor)cdmObject).getNomenclaturalTitleCache();
91
    }
92

    
93
    @Override
94
    protected ILabelProvider createListLabelProvider() {
95
        return new NomenclaturalLabelProvider();
96
    }
97

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

    
124
            return title;
125
        }
126
    }
127

    
128
	@Override
129
	protected Comparator<UuidAndTitleCache<TeamOrPersonBase<?>>> getItemsComparator() {
130
		return new Comparator<UuidAndTitleCache<TeamOrPersonBase<?>>>() {
131
			@Override
132
			public int compare(UuidAndTitleCache<TeamOrPersonBase<?>> entity1,
133
					UuidAndTitleCache<TeamOrPersonBase<?>> entity2) {
134

    
135
			    if (entity1.getUuid().equals(entity2.getUuid())){
136
			        return 0;
137
			    }
138
				Collator collator = Collator.getInstance();
139
				String compareString1 = "";
140
				if (entity1.getAbbrevTitleCache() != null){
141
					compareString1 = entity1.getAbbrevTitleCache();
142
				}
143
				if (entity1.getTitleCache() != null){
144
					compareString1 += entity1.getTitleCache();
145
				}
146

    
147
				String compareString2 = "";
148
				if (entity2.getAbbrevTitleCache() != null){
149
					compareString2 = entity2.getAbbrevTitleCache();
150
				}
151
				if (entity2.getTitleCache() != null){
152
					compareString2 += entity2.getTitleCache();
153
				}
154

    
155

    
156
				int result =collator.compare(compareString1, compareString2);
157
				if (result == 0){
158
				    result = entity1.getUuid().compareTo(entity2.getUuid());
159
				}
160
				return result;
161
			}
162
		};
163
	}
164

    
165

    
166
}
(25-25/46)