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