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