Project

General

Profile

Download (6.16 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 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
	@Override
92
	protected T getPersistentObject(UUID cdmUuid) {
93
		return (T)CdmStore.getService(IAgentService.class).load(cdmUuid);
94
	}
95

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

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

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

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

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

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

    
168
            return titleCache;
169
        }
170
    }
171
}
(4-4/46)