Merge branch 'hotfix/5.45.1'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / agent / TeamMemberSection.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.section.agent;
10
11 import java.util.Collection;
12 import java.util.Comparator;
13 import java.util.List;
14
15 import org.eclipse.swt.graphics.Color;
16
17 import eu.etaxonomy.cdm.model.agent.Person;
18 import eu.etaxonomy.cdm.model.agent.Team;
19 import eu.etaxonomy.taxeditor.ui.dialog.selection.CollectorSelectionDialog;
20 import eu.etaxonomy.taxeditor.ui.dialog.selection.NomenclaturalPersonAuthorSelectionDialog;
21 import eu.etaxonomy.taxeditor.ui.dialog.selection.PersonSelectionDialog;
22 import eu.etaxonomy.taxeditor.ui.element.CacheRelevance;
23 import eu.etaxonomy.taxeditor.ui.element.CacheRelevanceHelper;
24 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
25 import eu.etaxonomy.taxeditor.ui.element.ICacheRelevantFormElement;
26 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
27 import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
28 import eu.etaxonomy.taxeditor.ui.section.AbstractEntityCollectionSection;
29
30 /**
31 * @author n.hoffmann
32 * @created Apr 30, 2010
33 */
34 public class TeamMemberSection
35 extends AbstractEntityCollectionSection<Team, Person>
36 implements ICacheRelevantFormElement {
37
38 private boolean isNomenclaturalTeam = false;
39 private boolean isCollectorTeam = false;
40
41 private CacheRelevanceHelper cacheRelevanceHelper = new CacheRelevanceHelper();
42
43 public TeamMemberSection(CdmFormFactory cdmFormFactory, ICdmFormElement parentElement, int style, boolean isNomenclatural, boolean isCollectorTeam) {
44 super(cdmFormFactory, parentElement, null, style);
45 isNomenclaturalTeam = isNomenclatural;
46 this.isCollectorTeam = isCollectorTeam;
47 }
48
49 @Override
50 public String getTitleString() {
51 return "Team Members";
52 }
53
54 @Override
55 public void addElement(Person element) {
56 getEntity().addTeamMember(element);
57 }
58
59 @Override
60 public Person createNewElement() {
61 Person selection = null;
62 if (isNomenclaturalTeam){
63 selection = NomenclaturalPersonAuthorSelectionDialog.select(getShell(), null, true);
64 }else if(isCollectorTeam){
65 selection = CollectorSelectionDialog.select(getShell(), null, true);
66 }else{
67 selection = PersonSelectionDialog.select(getShell(), null);
68 }
69
70 return selection;
71 }
72
73 @Override
74 public String getEmptyString() {
75 return "No persons yet.";
76 }
77
78 @Override
79 protected String getTooltipString() {
80 return "Add a member to this team";
81 }
82
83 @Override
84 public void removeElement(Person element) {
85 getEntity().removeTeamMember(element);
86 }
87
88
89
90 @Override
91 public Comparator<Person> getComparator() {
92 return (p1, p2) -> {
93 if (p1 == p2){
94 return 0;
95 }else if(p1==null){
96 return -1;
97 }else if(p2==null){
98 return 1;
99 }
100
101 List<Person> teamMembers = getEntity().getTeamMembers();
102 int indexOfP1 = teamMembers.indexOf(p1);
103 int indexOfP2 = teamMembers.indexOf(p2);
104 if(indexOfP1==-1){
105 return 1;
106 }
107 if(indexOfP2==-1){
108 return -1;
109 }
110 return indexOfP1 - indexOfP2;
111 };
112 }
113
114 public boolean isNomenclaturalTeam() {
115 return isNomenclaturalTeam;
116 }
117
118 @Override
119 public Person addExisting() {
120 return null;
121 }
122
123 @Override
124 public boolean allowAddExisting() {
125 return false;
126 }
127
128 @Override
129 public void updateCacheRelevance() {
130 Color color = cacheRelevance().getColor();
131 setBackground(color);
132 }
133
134 @Override
135 public void addDependsOnCache(ToggleableTextElement toggleElement) {
136 cacheRelevanceHelper.addDependsOnCache(toggleElement);
137 }
138
139 @Override
140 public CacheRelevance cacheRelevance() {
141 return cacheRelevanceHelper.cacheRelevance();
142 }
143
144 @Override
145 public Collection<Person> getCollection(Team entity) {
146 return entity.getTeamMembers();
147
148 }
149
150 }