Project

General

Profile

Download (6.18 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.util.List;
12
import java.util.UUID;
13

    
14
import org.eclipse.jface.viewers.ILabelProvider;
15
import org.eclipse.jface.viewers.LabelProvider;
16
import org.eclipse.swt.events.SelectionListener;
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.AgentBase;
23
import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
24
import eu.etaxonomy.cdm.model.agent.Person;
25
import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
26
import eu.etaxonomy.cdm.persistence.dto.TeamOrPersonUuidAndTitleCache;
27
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
28
import eu.etaxonomy.taxeditor.newWizard.AbstractNewEntityWizard;
29
import eu.etaxonomy.taxeditor.newWizard.NewPersonWizard;
30
import eu.etaxonomy.taxeditor.newWizard.NewTeamWizard;
31
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * FilteredAgentSelectionDialog class.
36
 *
37
 * @author n.hoffmann
38
 * @created Sep 10, 2009
39
 */
40
public class AgentSelectionDialog<T extends AgentBase>
41
        extends AbstractFilteredCdmResourceSelectionDialog<T> {
42

    
43
	protected static final String PERSON = "New Person";
44
	protected static final String TEAM = "New Team";
45

    
46
	protected static boolean selectTeamMember;
47

    
48
	/**
49
	 * select
50
	 *
51
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
52
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
53
	 * @param entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
54
	 * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
55
	 */
56
	public static <S extends AgentBase> S select(Shell shell,
57
	        S entity, boolean selectTeamMember) {
58
		AgentSelectionDialog<S> dialog = new AgentSelectionDialog<>(shell,
59
				"Choose Agent", false, AgentSelectionDialog.class.getCanonicalName(), entity, selectTeamMember);
60
		return getSelectionFromDialog(dialog);
61
	}
62

    
63
	/**
64
	 * <p>Constructor for FilteredAgentSelectionDialog.</p>
65
	 *
66
	 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
67
	 * @param title a {@link java.lang.String} object.
68
	 * @param agent a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
69
	 * @param conversation a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
70
	 * @param multi a boolean.
71
	 * @param settings a {@link java.lang.String} object.
72
	 */
73
	protected AgentSelectionDialog(Shell shell,
74
	        String title, boolean multi, String settings, T agent, boolean selectTeamMember) {
75
		super(shell, title, multi, settings, agent);
76
		this.selectTeamMember = selectTeamMember;
77
	}
78

    
79
	public class DetailsLabelProvider extends LabelProvider {
80
		@Override
81
        public String getText(Object element) {
82
		    T agent = getCdmObjectByUuid(((UuidAndTitleCache<AgentBase>) element).getUuid());
83
			if (agent instanceof INomenclaturalAuthor) {
84
				return "Nomenclatural title: '" + ((INomenclaturalAuthor) agent).getNomenclaturalTitleCache() + "'";
85
			} else {
86
				return "'" + agent.getTitleCache() + "' is not a nomenclatural author.";
87
			}
88
		}
89
	}
90

    
91

    
92

    
93
	@Override
94
	protected T getPersistentObject(UUID cdmUuid) {
95
		return (T)CdmStore.getService(IAgentService.class).load(cdmUuid);
96
	}
97

    
98
	@Override
99
	protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
100
		if(TEAM.equals(parameter)){
101
			return new NewTeamWizard();
102
		}
103
		else if(PERSON.equals(parameter)){
104
			return new NewPersonWizard();
105
		}
106
		else{
107
			throw new IllegalArgumentException("Could not determine the desired wizard.");
108
		}
109
	}
110

    
111
	@Override
112
	protected String[] getNewWizardText() {
113
	    String[] result;
114
		if (this.selectTeamMember){
115
		    result = new String[1];
116
		    result[0] = PERSON;
117
		}else{
118
		    result = new String[2];
119
            result[0] = PERSON;
120
            result[1] = TEAM;
121
		}
122
		return result;
123
	}
124

    
125
	@Override
126
	protected SelectionListener getNewWizardButtonSelectionListener() {
127
		return super.getNewWizardButtonSelectionListener();
128
	}
129

    
130
    @Override
131
    protected void callService(String pattern) {
132
        Class<T> clazz = (Class)AgentBase.class;
133
        if (selectTeamMember){
134
            clazz = (Class)Person.class;
135
        }
136

    
137
        model = (List)CdmStore.getService(IAgentService.class).getTeamOrPersonUuidAndTitleCache(clazz, limitOfInitialElements, pattern);
138
    }
139
    @Override
140
    protected ILabelProvider createListLabelProvider() {
141
        return new CollectorLabelProvider();
142
    }
143

    
144
    public class CollectorLabelProvider extends LabelProvider {
145
        @Override
146
        public String getText(Object element) {
147
            if (element == null) {
148
                return null;
149
            }
150
            UuidAndTitleCache uuidAndTitleCache = (UuidAndTitleCache) element;
151
            String titleCache = uuidAndTitleCache.getTitleCache();
152
            String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache();
153
            String collectorTitleCache = null;
154
            if (element instanceof TeamOrPersonUuidAndTitleCache){
155
                collectorTitleCache = ((TeamOrPersonUuidAndTitleCache)element).getCollectorTitleCache();
156
            }
157
            if (!titleCache.equals(abbrevTitleCache)){
158
                titleCache += " - " + abbrevTitleCache;
159
            }
160
            if (collectorTitleCache != null && !uuidAndTitleCache.getTitleCache().equals(collectorTitleCache) && !abbrevTitleCache.equals(collectorTitleCache)){
161
                titleCache += " - " + collectorTitleCache;
162
            }
163
            if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
164
                titleCache += " ["+uuidAndTitleCache.getId()+"]";
165
            }
166
            if (element instanceof EntityDTOBase){
167
                titleCache += "(" + ((IdentifiedEntityDTO)element).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO)element).getIdentifier().getIdentifier() + ")";
168
            }
169

    
170
            return titleCache;
171
        }
172

    
173

    
174
    }
175

    
176

    
177
}
(4-4/46)