Merge branch 'release/5.19.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / reference / ReferenceDetailElement.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.reference;
10
11 import java.util.ArrayList;
12 import java.util.Arrays;
13 import java.util.List;
14
15 import org.eclipse.swt.SWT;
16 import org.joda.time.DateTime;
17
18 import eu.etaxonomy.cdm.model.agent.Institution;
19 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
20 import eu.etaxonomy.cdm.model.reference.Reference;
21 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
22 import eu.etaxonomy.cdm.model.reference.ReferenceType;
23 import eu.etaxonomy.taxeditor.store.StoreUtil;
24 import eu.etaxonomy.taxeditor.ui.combo.EnumComboElement;
25 import eu.etaxonomy.taxeditor.ui.element.CacheRelevance;
26 import eu.etaxonomy.taxeditor.ui.element.CdmFormFactory;
27 import eu.etaxonomy.taxeditor.ui.element.CdmPropertyChangeEvent;
28 import eu.etaxonomy.taxeditor.ui.element.DoiWithLabelElement;
29 import eu.etaxonomy.taxeditor.ui.element.ICdmFormElement;
30 import eu.etaxonomy.taxeditor.ui.element.IErrorIntolerableElement;
31 import eu.etaxonomy.taxeditor.ui.element.IExceptionHandler;
32 import eu.etaxonomy.taxeditor.ui.element.LsidWithExceptionLabelElement;
33 import eu.etaxonomy.taxeditor.ui.element.TextWithLabelElement;
34 import eu.etaxonomy.taxeditor.ui.element.ToggleableTextElement;
35 import eu.etaxonomy.taxeditor.ui.element.UriWithLabelElement;
36 import eu.etaxonomy.taxeditor.ui.element.VerbatimTimePeriodElement;
37 import eu.etaxonomy.taxeditor.ui.mvc.element.DateElement;
38 import eu.etaxonomy.taxeditor.ui.section.AbstractIdentifiableEntityDetailElement;
39 import eu.etaxonomy.taxeditor.ui.selection.EntitySelectionElement;
40
41 /**
42 * ReferenceBaseDetailElement class.
43 *
44 * @author n.hoffmann
45 * @created Mar 2, 2010
46 */
47 public class ReferenceDetailElement extends AbstractIdentifiableEntityDetailElement<Reference>
48 implements IErrorIntolerableElement, IExceptionHandler {
49
50 private ToggleableTextElement toggleableAbbrevCache;
51
52 private TextWithLabelElement text_edition;
53 private TextWithLabelElement text_editor;
54 private TextWithLabelElement text_isbn;
55 private TextWithLabelElement text_issn;
56 private TextWithLabelElement text_organisation;
57 private TextWithLabelElement text_pages;
58 private TextWithLabelElement text_placePublished;
59 private TextWithLabelElement text_publisher;
60 private TextWithLabelElement text_referenceAbstract;
61 private TextWithLabelElement text_series;
62 // private TextWithLabelElement text_seriesPart;
63 private TextWithLabelElement text_volume;
64 private TextWithLabelElement text_abbrevTitle;
65 private TextWithLabelElement text_title;
66 private DoiWithLabelElement text_doi;
67 private UriWithLabelElement text_uri;
68 private LsidWithExceptionLabelElement text_lsid;
69 private DateElement text_accessed;
70 private EntitySelectionElement<TeamOrPersonBase> selection_authorTeam;
71 private VerbatimTimePeriodElement element_timePeriod;
72 private EntitySelectionElement<Reference> selection_inReference;
73 private EntitySelectionElement<Institution> selection_institution;
74 private EnumComboElement<ReferenceType> combo_referenceType;
75
76 private EntitySelectionElement<Reference> selection_inSeries;
77
78 private void registerCacheElements() {
79 registerCacheRelevance(text_edition, toggleable_cache, toggleableAbbrevCache);
80 registerCacheRelevance(text_pages, toggleable_cache);
81 registerCacheRelevance(text_series, toggleable_cache, toggleableAbbrevCache);
82 registerCacheRelevance(text_volume, toggleable_cache, toggleableAbbrevCache);
83 registerCacheRelevance(text_abbrevTitle, toggleableAbbrevCache);
84 registerCacheRelevance(text_title, toggleable_cache);
85 registerCacheRelevance(text_accessed, toggleable_cache, toggleableAbbrevCache);
86 registerCacheRelevance(selection_authorTeam, toggleable_cache, toggleableAbbrevCache);
87 registerCacheRelevance(element_timePeriod, toggleable_cache, toggleableAbbrevCache);
88 registerCacheRelevance(selection_inReference, toggleable_cache, toggleableAbbrevCache);
89 registerCacheRelevance(combo_referenceType, toggleable_cache, toggleableAbbrevCache);
90 }
91
92 /**
93 * Constructor for ReferenceBaseDetailElement.
94 */
95 public ReferenceDetailElement(CdmFormFactory cdmFormFactory,
96 ICdmFormElement formElement, int style) {
97 super(cdmFormFactory, formElement);
98 }
99
100 @Override
101 protected void createControls(ICdmFormElement formElement,
102 Reference entity, int style) {
103 setWarnForReferencingObjects(formElement);
104 toggleable_cache = formFactory.createToggleableTextField(formElement,
105 "Reference Cache", entity.getTitleCache(),
106 entity.isProtectedTitleCache(), style);
107
108 toggleableAbbrevCache = formFactory.createToggleableTextField(formElement,
109 "Nomencl. Cache", entity.getAbbrevTitleCache(),
110 entity.isProtectedAbbrevTitleCache(), CacheRelevance.CACHE2, style);
111
112 combo_referenceType = formFactory.createEnumComboElement(ReferenceType.class,
113 formElement, style);
114
115 combo_referenceType.setSelection(entity.getType());
116
117 // title
118 text_title = formFactory.createMultiLineTextWithLabel(formElement,
119 "Title", 60, style);
120 text_title.setText(entity.getTitle());
121 // abbreviated title
122 text_abbrevTitle = formFactory.createTextWithLabelElement(formElement,
123 "Nomencl. Title", entity.getAbbrevTitle(), style);
124 ReferenceType referenceType = entity.getType();
125 // author team
126 if(referenceType != null && !referenceType.equals(ReferenceType.Journal) && !referenceType.equals(ReferenceType.PrintSeries) ) {
127 selection_authorTeam = formFactory
128 .createSelectionElement(TeamOrPersonBase.class,
129 formElement, "Author",
130 entity.getAuthorship(),
131 EntitySelectionElement.ALL, style);
132 }
133
134 if (referenceType != null) {
135
136 createInReferenceByType(this, referenceType, entity, SWT.NULL);
137
138 if (referenceType.equals(ReferenceType.Generic)) {
139 createGenericControls(this, entity, SWT.NULL);
140 createPublicationControls(this, referenceType, entity, SWT.NULL);
141
142 } else {
143 if (referenceType.isPrintedUnit()) {
144 createPrintedUnitControls(this, referenceType, entity,
145 SWT.NULL);
146 }
147 // if (referenceType.equals(ReferenceType.Proceedings)) {
148 // text_isbn = formFactory.createTextWithLabelElement(this, "ISBN",
149 // entity.getIsbn(), style);
150 // }
151 if (referenceType.isPublication()) {
152 createPublicationControls(this, referenceType, entity,
153 SWT.NULL);
154 }
155 if (referenceType.isVolumeReference()) {
156 createVolumeControls(this, entity, SWT.NULL);
157 }
158 if (referenceType.isSection()) {
159 createSectionControls(this, entity, SWT.NULL);
160 }
161 }
162 }
163
164 // date published
165 if(referenceType != null && !referenceType.equals(ReferenceType.Journal) && !referenceType.equals(ReferenceType.PrintSeries)){
166 element_timePeriod = formFactory.createVerbatimTimePeriodElement(formElement,
167 "Date Published", entity.getDatePublished(), style);
168 }
169
170 createUri(this, entity, SWT.NULL);
171 if(referenceType != null && (!referenceType.equals(ReferenceType.Journal) && !referenceType.equals(ReferenceType.PrintSeries) && !referenceType.equals(ReferenceType.PersonalCommunication))) {
172 text_doi = formFactory.createDoiWithLabelElement(formElement, "DOI", entity.getDoi(), style);
173 }
174
175 createLsid(this, entity, SWT.NULL);
176 createAbstract(this, entity, SWT.NULL);
177
178 registerCacheElements();
179
180 handleToggleableAbbrevTitleField();
181 handleToggleableCacheField();
182
183 }
184
185 private void createWebPageControls(ReferenceDetailElement referenceDetailElement, Reference reference, int style) {
186 DateTime accessed = reference.getAccessed();
187
188 text_accessed = formFactory.createDateElement(referenceDetailElement, "Accessed", accessed, style, false);
189 }
190
191 @Override
192 protected void updateContent() {
193 if (getEntity() == null) {
194 setEntity(ReferenceFactory.newGeneric());
195 }
196
197 super.updateContent();
198
199 // enable elements according to state of cache protection
200 toggleable_cache.setEnabled(getEntity().isProtectedTitleCache());
201 toggleableAbbrevCache.setEnabled(getEntity().isProtectedAbbrevTitleCache());
202 }
203
204 /**
205 * Creates an in reference for reference types that do hav this.
206 */
207 private void createInReferenceByType(ICdmFormElement element,
208 ReferenceType referenceType, Reference reference, int style) {
209
210 String label;
211
212 switch (referenceType) {
213 case Book:
214 case Proceedings:
215 label = "In Series";
216 break;
217 case BookSection:
218 label = "Book";
219 break;
220 case InProceedings:
221 label = "Proceedings";
222 break;
223 case Article:
224 label = "Journal";
225 break;
226 default:
227 label = "In Reference";
228 }
229
230 boolean createInReferenceWidget = !ReferenceType.inReferenceContraints(referenceType).isEmpty();
231
232 if (createInReferenceWidget) {
233 selection_inReference = formFactory
234 .createSelectionElement(Reference.class,
235 element, label,
236 reference.getInReference(),
237 EntitySelectionElement.ALL, style);
238 }
239 }
240
241 /**
242 * Creates additional controls to edit a generic reference
243 */
244 private void createGenericControls(ICdmFormElement element,
245 Reference reference, int style) {
246
247 // editor
248 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
249 reference.getEditor(), style);
250
251 // series
252 text_series = formFactory.createTextWithLabelElement(element, "Series",
253 reference.getSeriesPart(), style);
254
255 // volume
256 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
257 reference.getVolume(), style);
258
259 // pages
260 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
261 reference.getPages(), style);
262 }
263
264 /**
265 * Creates additional controls to edit a publication
266 */
267 private void createPublicationControls(ICdmFormElement element,
268 ReferenceType referenceType, Reference reference, int style) {
269
270 // placePublished
271 text_placePublished = formFactory.createTextWithLabelElement(element,
272 "Place Published", reference.getPlacePublished(), style);
273
274 // publisher
275 text_publisher = formFactory.createTextWithLabelElement(element,
276 "Publisher", reference.getPublisher(), style);
277
278 if (referenceType.equals(ReferenceType.Journal)) {
279 // issn
280 text_issn = formFactory.createTextWithLabelElement(element, "ISSN",
281 reference.getIssn(), style);
282 }
283
284 if (referenceType.equals(ReferenceType.Report)) {
285 // institution
286 selection_institution = formFactory
287 .createSelectionElement(Institution.class,//getConversationHolder(),
288 element, "Institution",
289 reference.getInstitution(),
290 EntitySelectionElement.ALL, style);
291 }
292
293 if (referenceType.equals(ReferenceType.Thesis)) {
294 // school
295 selection_institution = formFactory
296 .createSelectionElement(Institution.class,
297 element, "School",
298 reference.getSchool(),
299 EntitySelectionElement.ALL, style);
300 }
301
302 if (referenceType.equals(ReferenceType.Proceedings)) {
303 // organization
304 text_organisation = formFactory.createTextWithLabelElement(element,
305 "Organisation", reference.getOrganization(), style);
306 }
307 }
308
309 /**
310 * Creates additional controls to edit a section
311 */
312 private void createSectionControls(ICdmFormElement element,
313 Reference reference, int style) {
314 // pages
315 text_pages = formFactory.createTextWithLabelElement(element, "Pages",
316 reference.getPages(), style);
317 }
318
319 /**
320 * Creates additional controls to edit a volume
321 */
322 private void createVolumeControls(ICdmFormElement element,
323 Reference reference, int style) {
324
325 text_volume = formFactory.createTextWithLabelElement(element, "Volume",
326 reference.getVolume(), style);
327
328 text_series = formFactory.createTextWithLabelElement(element, "Series",
329 reference.getSeriesPart(), style);
330 }
331
332 private void createPrintedUnitControls(ICdmFormElement element,
333 ReferenceType referenceType, Reference reference, int style) {
334 // in series
335 //handled in generic in-Reference part
336
337 // editor
338 text_editor = formFactory.createTextWithLabelElement(element, "Editor",
339 reference.getEditor(), style);
340
341 /* series part
342 text_seriesPart = formFactory.createTextWithLabelElement(element,
343 "Series", reference.getSeriesPart(), style);
344 */
345 if (referenceType.equals(ReferenceType.Book)) {
346 // edition
347 text_edition = formFactory.createTextWithLabelElement(element,
348 "Edition", reference.getEdition(), style);
349 }
350
351 // isbn
352 text_isbn = formFactory.createTextWithLabelElement(element, "ISBN",
353 reference.getIsbn(), style);
354 }
355
356 private void createAbstract(ICdmFormElement element,
357 Reference reference, int style) {
358
359 text_referenceAbstract = formFactory.createMultiLineTextWithLabel(
360 element, "Reference Abstract", 100, style);
361 text_referenceAbstract.setText(reference.getReferenceAbstract());
362 }
363
364 private void createLsid(ICdmFormElement element,
365 Reference reference, int style) {
366 text_lsid = formFactory.createLsidWithExceptionLabelElement(element, "LSID",
367 reference.getLsid(), style);
368 }
369
370 private void createUri(ICdmFormElement element,
371 Reference reference, int style) {
372
373 text_uri = formFactory.createUriWithLabelElement(element, "URI",
374 reference.getUri(), style);
375 if (reference.getType().equals(ReferenceType.WebPage)){
376 createWebPageControls(this, reference, style);
377 }
378 }
379
380 @Override
381 public void handleEvent(Object eventSource) {
382 if (eventSource == combo_referenceType) {
383 getEntity().setType(combo_referenceType.getSelection());
384 updateContent();
385 //this is needed for the "new" wizards in order to redraw correctly
386 StoreUtil.reflowParentScrolledForm(getLayoutComposite(), true);
387 } else if (eventSource == element_timePeriod) {
388 getEntity().setDatePublished(element_timePeriod.getTimePeriod());
389 clearException();
390 }
391 // selections
392 else if (eventSource == selection_authorTeam) {
393 getEntity().setAuthorship(
394 selection_authorTeam.getEntity());
395 } else if (eventSource == selection_inReference) {
396 getEntity().setInReference(selection_inReference.getSelection());
397 } else if (eventSource == selection_inSeries) {
398 getEntity().setInReference(selection_inSeries.getSelection());
399 } else if (eventSource == selection_institution) {
400 getEntity().setInstitution(selection_institution.getSelection());
401 }
402
403 // textfields
404 else if (eventSource == toggleable_cache) {
405 handleToggleableCacheField();
406 }
407 else if (eventSource == toggleableAbbrevCache) {
408 handleToggleableAbbrevTitleField();
409 } else if (eventSource == text_edition) {
410 getEntity().setEdition(text_edition.getText());
411 } else if (eventSource == text_editor) {
412 getEntity().setEditor(text_editor.getText());
413 } else if (eventSource == text_isbn) {
414 getEntity().setIsbn(text_isbn.getText());
415 } else if (eventSource == text_issn) {
416 getEntity().setIssn(text_issn.getText());
417 } else if (eventSource == text_organisation) {
418 getEntity().setOrganization(text_organisation.getText());
419 } else if (eventSource == text_pages) {
420 getEntity().setPages(text_pages.getText());
421 } else if (eventSource == text_placePublished) {
422 getEntity().setPlacePublished(text_placePublished.getText());
423 } else if (eventSource == text_publisher) {
424 getEntity().setPublisher(text_publisher.getText());
425 } else if (eventSource == text_referenceAbstract) {
426 getEntity().setReferenceAbstract(text_referenceAbstract.getText());
427 } else if (eventSource == text_series) {
428 getEntity().setSeriesPart(text_series.getText());
429 } else if (eventSource == text_title ) {
430 getEntity().setTitle(text_title.getText());
431 if (!toggleable_cache.isEnabled()){
432 toggleable_cache.setText(getEntity().generateTitle());
433 }
434 } else if (eventSource == text_abbrevTitle ) {
435 getEntity().setAbbrevTitle(text_abbrevTitle.getText());
436 if (!toggleableAbbrevCache.isEnabled()){
437 toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
438 }
439 } else if (eventSource == text_uri) {
440 getEntity().setUri(text_uri.parseText());
441 } else if (eventSource == text_lsid) {
442 getEntity().setLsid(text_lsid.parseText());
443 } else if (eventSource == text_volume) {
444 getEntity().setVolume(text_volume.getText());
445 }else if (eventSource == text_doi) {
446 getEntity().setDoi(text_doi.parseText());
447 }else if (eventSource == text_accessed.getController()) {
448 getEntity().setAccessed(text_accessed.getController().getDateTime());
449 }
450
451 if (eventSource != text_title && eventSource != text_abbrevTitle) {
452 // set abbrev title cache to null to force new generation
453 // for the standard title cache this is done by a PropertyChangeListener registered
454 // in IdentifiableEntity.initListeners()
455 if(!getEntity().isProtectedAbbrevTitleCache()){
456 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
457 }
458 if(!getEntity().isProtectedTitleCache()){
459 getEntity().setTitleCache(null, getEntity().isProtectedTitleCache());
460 }
461
462 toggleable_cache.setText(getEntity().getTitleCache());
463 toggleableAbbrevCache.setText(getEntity().getAbbrevTitleCache());
464 }
465
466 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
467 }
468
469 @Override
470 public void updateToggleableCacheField() {
471 if (!getEntity().isProtectedTitleCache()) {
472 toggleable_cache.setText(getEntity().generateTitle());
473 }
474 if (!getEntity().isProtectedAbbrevTitleCache()) {
475 toggleableAbbrevCache.setText(getEntity().generateAbbrevTitle());
476 }
477 }
478
479 protected void handleToggleableAbbrevTitleField() {
480 getEntity().setAbbrevTitleCache(
481 toggleableAbbrevCache.getText(), toggleableAbbrevCache.getState());
482 updateToggleableCacheField();
483 updateCacheRelevance();
484 }
485
486 @Override
487 public void handleException(CdmPropertyChangeEvent event) {
488 exceptionOccurred(event);
489 }
490
491 @Override
492 public void clearException() {
493 exceptionOccurred(null);
494 }
495
496 private final List<IExceptionHandler> exceptionHandlers = new ArrayList<>();
497
498 @Override
499 public void addExceptionHandler(IExceptionHandler handler) {
500 exceptionHandlers.add(handler);
501 }
502
503 @Override
504 public void removeExceptionHandler(IExceptionHandler handler) {
505 exceptionHandlers.remove(handler);
506 }
507
508 @Override
509 public void exceptionOccurred(CdmPropertyChangeEvent event) {
510 for (IExceptionHandler handler : exceptionHandlers) {
511 if (event == null) {
512 handler.clearException();
513 } else {
514 handler.handleException(event);
515 }
516 }
517 }
518
519 @Override
520 protected void handleToggleableCacheField() {
521 getEntity().setTitleCache(toggleable_cache.getText(), toggleable_cache.getState());
522 updateToggleableCacheField();
523 updateCacheRelevance();
524 }
525
526 protected void setIrrelevantReferenceDetail(boolean abbrev){
527
528 List<Object> except = new ArrayList<>();
529
530 except.addAll( Arrays.asList(new Object[] { toggleable_cache, toggleableAbbrevCache, text_editor, text_isbn, text_issn, text_organisation, text_pages, text_placePublished, text_publisher, text_referenceAbstract, text_uri, selection_institution}));
531
532 if (abbrev){
533 except.add(text_title);
534 } else{
535 except.add(text_abbrevTitle);
536 }
537
538 switch( getEntity().getType()){
539 case Journal:
540 except.add(element_timePeriod);
541 break;
542 case Book:
543 except.remove(text_series);
544 except.remove(text_edition);
545 break;
546 default:
547 break;
548 }
549
550 updateCacheRelevance();
551 }
552 }