Project

General

Profile

Download (5.43 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2021 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.lang.reflect.Member;
12
import java.util.ArrayList;
13
import java.util.HashSet;
14
import java.util.Iterator;
15
import java.util.List;
16
import java.util.Set;
17
import java.util.UUID;
18

    
19
import org.eclipse.jface.viewers.ILabelProvider;
20
import org.eclipse.jface.viewers.LabelProvider;
21
import org.eclipse.swt.internal.win32.TCHITTESTINFO;
22
import org.eclipse.swt.widgets.Shell;
23

    
24
import eu.etaxonomy.cdm.api.service.IAgentService;
25
import eu.etaxonomy.cdm.api.service.dto.EntityDTOBase;
26
import eu.etaxonomy.cdm.api.service.dto.IdentifiedEntityDTO;
27
import eu.etaxonomy.cdm.model.agent.AgentBase;
28
import eu.etaxonomy.cdm.model.agent.Person;
29
import eu.etaxonomy.cdm.model.agent.Team;
30
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
32
import eu.etaxonomy.cdm.persistence.dto.TeamOrPersonUuidAndTitleCache;
33
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
34
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
35
import eu.etaxonomy.taxeditor.store.CdmStore;
36

    
37
/**
38
 * @author k.luther
39
 * @since Jul 1, 2021
40
 */
41
public class CollectorSelectionDialog extends AgentSelectionDialog<AgentBase<?>> {
42
	Team collectorTeam = null;
43

    
44
    /**
45
     * @param shell
46
     * @param title
47
     * @param multi
48
     * @param settings
49
     * @param agent
50
     * @param selectTeamMember
51
     */
52
    protected CollectorSelectionDialog(Shell shell, String title, boolean multi, String settings,
53
            AgentBase<?> agent, boolean selectTeamMember) {
54
        super(shell, title, multi, settings, agent, selectTeamMember);
55
        if (selectTeamMember && agent instanceof Team){
56
    		this.collectorTeam = (Team)agent;
57
    		
58
    	}
59

    
60
    }
61
    public static <S extends AgentBase> S select(Shell shell,
62
            S entity, boolean selectTeamMember) {
63
    	
64
        CollectorSelectionDialog dialog = new CollectorSelectionDialog(shell,
65
                "Choose Collector", false, CollectorSelectionDialog.class.getCanonicalName(), entity, selectTeamMember);
66
        return  (S) getSelectionFromDialog(dialog);
67
    }
68

    
69
    @Override
70
    protected ILabelProvider createListLabelProvider() {
71
        return new CollectorLabelProvider();
72
    }
73

    
74
    public class CollectorLabelProvider extends LabelProvider {
75
        @Override
76
        public String getText(Object element) {
77
            if (element == null) {
78
                return null;
79
            }
80
            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
81
            String title = "";
82
            String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
83
            String titleCache = uuidAndTitleCache.getTitleCache();
84
            String collectorTitleCache = null;
85
            if (element instanceof TeamOrPersonUuidAndTitleCache && ((TeamOrPersonUuidAndTitleCache)element).getCollectorTitleCache() != null){
86
                title = ((TeamOrPersonUuidAndTitleCache)element).getCollectorTitleCache();
87
            }
88
            if (!title.equals(titleCache)){
89
                title += " - " + titleCache;
90
            }
91
            if (abbrevTitleCache != null && !uuidAndTitleCache.getTitleCache().equals(abbrevTitleCache) && !abbrevTitleCache.equals(titleCache)){
92
                title += " - " + abbrevTitleCache;
93
            }
94
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
95
                title += " ["+uuidAndTitleCache.getId()+"]";
96
            }
97
            if (element instanceof EntityDTOBase){
98
                title += "(" + ((IdentifiedEntityDTO)element).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO)element).getIdentifier().getIdentifier() + ")";
99
            }
100

    
101
            return title;
102
        }
103

    
104

    
105
    }
106
    @Override
107
    protected void callService(String pattern) {
108
        Class clazz = AgentBase.class;
109
        if (selectTeamMember){
110
            clazz = Person.class;
111
        }
112
        Set<UUID> memberUuids = new HashSet<>();
113
        if (collectorTeam != null && selectTeamMember){
114
        	List<Person> teamMembers = this.collectorTeam.getTeamMembers();
115
        	
116
            teamMembers.forEach(member -> memberUuids.add(member.getUuid()));
117
        }
118
        
119
        model = CdmStore.getService(IAgentService.class).getUuidAndTitleCacheWithCollectorTitleCache(clazz, limitOfInitialElements, pattern);
120
        if (collectorTeam == null){
121
        	return;
122
        }
123
        //filter
124
        Iterator<UuidAndTitleCache<AgentBase<?>>> modelIterator = model.iterator();
125
        List<UuidAndTitleCache<AgentBase<?>>> tempModel = new ArrayList<>();
126
        while(modelIterator.hasNext()){
127
        	UuidAndTitleCache<AgentBase<?>> person = modelIterator.next();
128
        	if (memberUuids.contains(person.getUuid())){
129
        		tempModel.add(person);
130
        	}
131
        }
132
        model.clear();
133
        model.addAll(tempModel);
134
    }
135

    
136
    @Override
137
    protected String getTitle(AgentBase<?> cdmObject) {
138
		if(cdmObject == null){
139
			return "";
140
		}
141

    
142
		if (cdmObject instanceof TeamOrPersonBase) {
143
			return ((TeamOrPersonBase<?>) cdmObject).getCollectorTitleCache();
144
		}
145

    
146
		throw new IllegalArgumentException("Generic method only" +
147
				" supports cdmObject of type TeamOrPersonBase." +
148
				" Please implement specific method in subclass.");
149
	}
150
}
(9-9/46)