Merge branch 'hotfix/5.43.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / dialog / selection / AgentSelectionDialog.java
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.IdentifiedEntityDTO;
21 import eu.etaxonomy.cdm.model.agent.AgentBase;
22 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
23 import eu.etaxonomy.cdm.model.agent.Person;
24 import eu.etaxonomy.cdm.model.metadata.PreferencePredicate;
25 import eu.etaxonomy.cdm.persistence.dto.EntityDTOBase;
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 entity a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
53 * @return a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
54 */
55 public static <S extends AgentBase> S select(Shell shell,
56 S entity, boolean selectTeamMember) {
57 AgentSelectionDialog<S> dialog = new AgentSelectionDialog<>(shell,
58 "Choose Agent", false, AgentSelectionDialog.class.getCanonicalName(), entity, selectTeamMember);
59 return getSelectionFromDialog(dialog);
60 }
61
62 /**
63 * <p>Constructor for FilteredAgentSelectionDialog.</p>
64 *
65 * @param shell a {@link org.eclipse.swt.widgets.Shell} object.
66 * @param title a {@link java.lang.String} object.
67 * @param agent a {@link eu.etaxonomy.cdm.model.agent.AgentBase} object.
68 * @param multi a boolean.
69 * @param settings a {@link java.lang.String} object.
70 */
71 protected AgentSelectionDialog(Shell shell,
72 String title, boolean multi, String settings, T agent, boolean selectTeamMember) {
73 super(shell, title, multi, settings, agent);
74 this.selectTeamMember = selectTeamMember;
75 }
76
77 public class DetailsLabelProvider extends LabelProvider {
78 @Override
79 public String getText(Object element) {
80 T agent = getCdmObjectByUuid(((UuidAndTitleCache<AgentBase>) element).getUuid());
81 if (agent instanceof INomenclaturalAuthor) {
82 return "Nomenclatural title: '" + ((INomenclaturalAuthor) agent).getNomenclaturalTitleCache() + "'";
83 } else {
84 return "'" + agent.getTitleCache() + "' is not a nomenclatural author.";
85 }
86 }
87 }
88
89 @Override
90 protected T getPersistentObject(UUID cdmUuid) {
91 return (T)CdmStore.getService(IAgentService.class).load(cdmUuid);
92 }
93
94 @Override
95 protected AbstractNewEntityWizard getNewEntityWizard(String parameter) {
96 if(TEAM.equals(parameter)){
97 return new NewTeamWizard();
98 }
99 else if(PERSON.equals(parameter)){
100 return new NewPersonWizard();
101 }
102 else{
103 throw new IllegalArgumentException("Could not determine the desired wizard.");
104 }
105 }
106
107 @Override
108 protected String[] getNewWizardText() {
109 String[] result;
110 if (selectTeamMember){
111 result = new String[1];
112 result[0] = PERSON;
113 }else{
114 result = new String[2];
115 result[0] = PERSON;
116 result[1] = TEAM;
117 }
118 return result;
119 }
120
121 @Override
122 protected SelectionListener getNewWizardButtonSelectionListener() {
123 return super.getNewWizardButtonSelectionListener();
124 }
125
126 @Override
127 protected void callService(String pattern) {
128 Class<T> clazz = (Class<T>)AgentBase.class;
129 if (selectTeamMember){
130 clazz = (Class<T>)Person.class;
131 }
132
133 model = (List)CdmStore.getService(IAgentService.class).getTeamOrPersonUuidAndTitleCache(clazz, limitOfInitialElements, pattern);
134 }
135 @Override
136 protected ILabelProvider createListLabelProvider() {
137 return new AgentLabelProvider();
138 }
139
140 public class AgentLabelProvider extends LabelProvider {
141 @Override
142 public String getText(Object element) {
143 if (element == null) {
144 return null;
145 }
146 UuidAndTitleCache<?> uuidAndTitleCache = (UuidAndTitleCache<?>) element;
147 String titleCache = uuidAndTitleCache.getTitleCache()!= null? uuidAndTitleCache.getTitleCache(): "" ;
148 String abbrevTitleCache = uuidAndTitleCache.getAbbrevTitleCache()!= null? uuidAndTitleCache.getAbbrevTitleCache(): "";
149 String collectorTitleCache = null;
150 if (element instanceof TeamOrPersonUuidAndTitleCache){
151 collectorTitleCache = ((TeamOrPersonUuidAndTitleCache)element).getCollectorTitleCache();
152 }
153 if (!titleCache.equals(abbrevTitleCache) && abbrevTitleCache != null){
154 titleCache += " - " + abbrevTitleCache;
155 }
156 if (collectorTitleCache != null && !titleCache.equals(collectorTitleCache) && !abbrevTitleCache.equals(collectorTitleCache)){
157 titleCache += " - " + collectorTitleCache;
158 }
159 if(PreferencesUtil.getBooleanValue(PreferencePredicate.ShowIdInSelectionDialog.getKey())){
160 titleCache += " ["+uuidAndTitleCache.getId()+"]";
161 }
162 if (element instanceof EntityDTOBase){
163 titleCache += "(" + ((IdentifiedEntityDTO)element).getIdentifier().getTypeLabel() +": " + ((IdentifiedEntityDTO)element).getIdentifier().getIdentifier() + ")";
164 }
165
166 return titleCache;
167 }
168 }
169 }