33825936fe7ff0a7b41942641b3d711538b36318
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / propertysheet / name / NomenclaturalAuthorshipPropertySource.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.propertysheet.name;
12
13 import java.util.ArrayList;
14 import java.util.List;
15 import java.util.Vector;
16
17 import org.eclipse.ui.views.properties.ComboBoxPropertyDescriptor;
18 import org.eclipse.ui.views.properties.IPropertyDescriptor;
19 import org.eclipse.ui.views.properties.IPropertySource;
20 import org.eclipse.ui.views.properties.PropertyDescriptor;
21 import org.eclipse.ui.views.properties.TextPropertyDescriptor;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
25 import eu.etaxonomy.cdm.model.agent.Team;
26 import eu.etaxonomy.cdm.model.name.NonViralName;
27
28 /**
29 * @author p.ciardelli
30 * @created 18.05.2009
31 * @version 1.0
32 */
33 public class NomenclaturalAuthorshipPropertySource implements IPropertySource {
34
35 // Property unique keys
36 public static final String P_ID_EDITABLECACHE = "P_ID_EDITABLECACHE";
37 public static final String P_ID_PROTECT_CACHE = "P_ID_PROTECT_CACHE";
38 public static final String P_ID_AUTHORSHIPCACHE = "P_ID_AUTHORSHIPCACHE";
39 public static final String P_ID_EXAUTHORSHIPCACHE = "P_ID_EXAUTHORSHIPCACHE";
40 public static final String P_ID_BASAUTHORSHIPCACHE = "P_ID_BASAUTHORSHIPCACHE";
41 public static final String P_ID_EXBASAUTHORSHIPCACHE = "P_ID_EXBASAUTHORSHIPCACHE";
42
43 // Property display keys
44 public static final String P_EDITABLECACHE = "Editable Cache";
45 public static final String P_PROTECT_CACHE = "Protect Cache from overwriting?";
46 public static final String P_AUTHORSHIPCACHE = "Authorship";
47 public static final String P_EXAUTHORSHIPCACHE = "Ex-Authorship";
48 public static final String P_BASAUTHORSHIPCACHE = "Basionym Authorship";
49 public static final String P_EXBASAUTHORSHIPCACHE = "Ex-Basionym Authorship";
50
51 private static final int CACHE_NOT_PROTECTED = 0;
52 private static final int CACHE_PROTECTED = 1;
53
54 private NonViralName name;
55
56 public NomenclaturalAuthorshipPropertySource(NonViralName name) {
57 this.name = name;
58
59 initDescriptors();
60 }
61
62 protected void initDescriptors() {
63
64 List<String> displayFields = new ArrayList<String>();
65
66 // Cache fields
67 displayFields.add(P_ID_EDITABLECACHE);
68 displayFields.add(P_ID_PROTECT_CACHE);
69
70 displayFields.add(P_ID_AUTHORSHIPCACHE);
71 displayFields.add(P_ID_EXAUTHORSHIPCACHE);
72 displayFields.add(P_ID_BASAUTHORSHIPCACHE);
73 displayFields.add(P_ID_EXBASAUTHORSHIPCACHE);
74
75
76 for (String field : displayFields) {
77 addDescriptor(field);
78 }
79 }
80
81 protected Vector<PropertyDescriptor> descriptors = new Vector<PropertyDescriptor>();
82
83 protected void addDescriptor(String id) {
84
85 // Editable cache
86 if (id.equals(P_ID_EDITABLECACHE)) {
87 descriptors.addElement(
88 new TextPropertyDescriptor(P_ID_EDITABLECACHE, P_EDITABLECACHE));
89 }
90
91 // Protect cache?
92 if (id.equals(P_ID_PROTECT_CACHE)) {
93 descriptors.addElement(
94 new ComboBoxPropertyDescriptor(P_ID_PROTECT_CACHE, P_PROTECT_CACHE,
95 new String[] {"no", "yes"}));
96 }
97
98 boolean isProtectedCache = name.isProtectedAuthorshipCache();
99
100 // Authorship cache
101 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
102 descriptors.addElement(
103 isProtectedCache ?
104 new PropertyDescriptor(P_ID_AUTHORSHIPCACHE, P_AUTHORSHIPCACHE) :
105 new TextPropertyDescriptor(P_ID_AUTHORSHIPCACHE, P_AUTHORSHIPCACHE));
106 }
107
108 // Ex-Authorship cache
109 if (id.equals(P_ID_EXAUTHORSHIPCACHE)) {
110 descriptors.addElement(
111 isProtectedCache ?
112 new PropertyDescriptor(P_ID_EXAUTHORSHIPCACHE, P_EXAUTHORSHIPCACHE) :
113 new TextPropertyDescriptor(P_ID_EXAUTHORSHIPCACHE, P_EXAUTHORSHIPCACHE));
114 }
115
116 // Basionym Authorship cache
117 if (id.equals(P_ID_BASAUTHORSHIPCACHE)) {
118 descriptors.addElement(
119 isProtectedCache ?
120 new PropertyDescriptor(P_ID_BASAUTHORSHIPCACHE, P_BASAUTHORSHIPCACHE) :
121 new TextPropertyDescriptor(P_ID_BASAUTHORSHIPCACHE, P_BASAUTHORSHIPCACHE));
122 }
123
124 // Ex-Basionym Authorship cache
125 if (id.equals(P_ID_EXBASAUTHORSHIPCACHE)) {
126 descriptors.addElement(
127 isProtectedCache ?
128 new PropertyDescriptor(P_ID_EXBASAUTHORSHIPCACHE, P_EXBASAUTHORSHIPCACHE) :
129 new TextPropertyDescriptor(P_ID_EXBASAUTHORSHIPCACHE, P_EXBASAUTHORSHIPCACHE));
130 }
131 }
132
133 /* (non-Javadoc)
134 * @see org.eclipse.ui.views.properties.IPropertySource#getEditableValue()
135 */
136 public Object getEditableValue() {
137 return CdmUtils.Nz(name.getAuthorshipCache());
138 }
139
140 /* (non-Javadoc)
141 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyDescriptors()
142 */
143 public IPropertyDescriptor[] getPropertyDescriptors() {
144 return (IPropertyDescriptor[]) descriptors.toArray(
145 new IPropertyDescriptor[descriptors.size()]);
146 }
147
148 /* (non-Javadoc)
149 * @see org.eclipse.ui.views.properties.IPropertySource#getPropertyValue(java.lang.Object)
150 */
151 public Object getPropertyValue(Object id) {
152
153 // Editable cache
154 if (id.equals(P_ID_EDITABLECACHE)) {
155 return CdmUtils.Nz(name.getAuthorshipCache());
156 }
157
158 // Protect cache?
159 if (id.equals(P_ID_PROTECT_CACHE)) {
160 return name.isProtectedAuthorshipCache() ? CACHE_PROTECTED : CACHE_NOT_PROTECTED;
161 }
162
163 // Authorship cache
164 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
165 if (name.getCombinationAuthorTeam() != null) {
166 return CdmUtils.Nz(name.getCombinationAuthorTeam().getNomenclaturalTitle());
167 } else {
168 return "";
169 }
170 }
171
172 // Ex-Authorship cache
173 if (id.equals(P_ID_EXAUTHORSHIPCACHE)) {
174 if (name.getExCombinationAuthorTeam() != null) {
175 return CdmUtils.Nz(name.getExCombinationAuthorTeam().getNomenclaturalTitle());
176 } else {
177 return "";
178 }
179 }
180
181 // Basionym Authorship cache
182 if (id.equals(P_ID_BASAUTHORSHIPCACHE)) {
183 if (name.getBasionymAuthorTeam() != null) {
184 return CdmUtils.Nz(name.getBasionymAuthorTeam().getNomenclaturalTitle());
185 } else {
186 return "";
187 }
188 }
189
190 // Ex-Basionym Authorship cache
191 if (id.equals(P_ID_EXBASAUTHORSHIPCACHE)) {
192 if (name.getExBasionymAuthorTeam() != null) {
193 return CdmUtils.Nz(name.getExBasionymAuthorTeam().getNomenclaturalTitle());
194 } else {
195 return "";
196 }
197 }
198
199 return null;
200 }
201
202 /* (non-Javadoc)
203 * @see org.eclipse.ui.views.properties.IPropertySource#isPropertySet(java.lang.Object)
204 */
205 public boolean isPropertySet(Object id) {
206 return false;
207 }
208
209 /* (non-Javadoc)
210 * @see org.eclipse.ui.views.properties.IPropertySource#resetPropertyValue(java.lang.Object)
211 */
212 public void resetPropertyValue(Object id) {}
213
214 /* (non-Javadoc)
215 * @see org.eclipse.ui.views.properties.IPropertySource#setPropertyValue(java.lang.Object, java.lang.Object)
216 */
217 public void setPropertyValue(Object id, Object value) {
218
219 // Editable cache
220 if (id.equals(P_ID_EDITABLECACHE)) {
221 name.setAuthorshipCache((String) value);
222 name.generateAuthorship();
223 }
224
225 // Protect cache?
226 if (id.equals(P_ID_PROTECT_CACHE)) {
227 name.setProtectedAuthorshipCache(
228 ((Integer) value).intValue() == CACHE_PROTECTED);
229 }
230
231 // Authorship cache
232 if (id.equals(P_ID_AUTHORSHIPCACHE)) {
233 if (name.getCombinationAuthorTeam() == null) {
234 name.setCombinationAuthorTeam(createNewTeam((String) value));
235 } else {
236 name.getCombinationAuthorTeam().setNomenclaturalTitle((String) value);
237 }
238 }
239
240 // Ex-Authorship cache
241 if (id.equals(P_ID_EXAUTHORSHIPCACHE)) {
242 if (name.getExCombinationAuthorTeam() == null) {
243 name.setExCombinationAuthorTeam(createNewTeam((String) value));
244 } else {
245 name.getExCombinationAuthorTeam().setNomenclaturalTitle((String) value);
246 }
247 }
248
249 // Basionym Authorship cache
250 if (id.equals(P_ID_BASAUTHORSHIPCACHE)) {
251 if (name.getBasionymAuthorTeam() == null) {
252 name.setBasionymAuthorTeam(createNewTeam((String) value));
253 } else {
254 name.getBasionymAuthorTeam().setNomenclaturalTitle((String) value);
255 }
256 }
257
258 // Ex-Basionym Authorship cache
259 if (id.equals(P_ID_EXBASAUTHORSHIPCACHE)) {
260 if (name.getExBasionymAuthorTeam() == null) {
261 name.setExBasionymAuthorTeam(createNewTeam((String) value));
262 } else {
263 name.getExBasionymAuthorTeam().setNomenclaturalTitle((String) value);
264 }
265 }
266 }
267
268 /**
269 * @param value
270 * @return
271 */
272 private INomenclaturalAuthor createNewTeam(String teamCache) {
273 Team team = Team.NewInstance();
274 team.setNomenclaturalTitle(teamCache);
275 return team;
276 }
277 }