3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
11 package eu
.etaxonomy
.taxeditor
.ui
.section
.reference
;
14 import java
.util
.ArrayList
;
15 import java
.util
.Arrays
;
16 import java
.util
.List
;
18 import org
.eclipse
.swt
.SWT
;
19 import org
.eclipse
.swt
.widgets
.Display
;
20 import org
.eclipse
.swt
.widgets
.Label
;
21 import org
.eclipse
.swt
.graphics
.Color
;
22 import org
.eclipse
.swt
.graphics
.FontData
;
24 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
25 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
28 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
31 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
32 import eu
.etaxonomy
.taxeditor
.ui
.combo
.EnumComboElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.IErrorIntolerableElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.IExceptionHandler
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.TimePeriodElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.ToggleableTextElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.UriWithLabelElement
;
44 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractIdentifiableEntityDetailElement
;
45 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
49 * ReferenceBaseDetailElement class.
53 * @created Mar 2, 2010
56 public class ReferenceDetailElement
extends AbstractIdentifiableEntityDetailElement
<Reference
>
57 implements IErrorIntolerableElement
, IExceptionHandler
{
59 private LabelElement warnForReferencedObjects
;
61 private ToggleableTextElement toggleableAbbrevCache
;
63 private TextWithLabelElement text_edition
;
64 private TextWithLabelElement text_editor
;
65 private TextWithLabelElement text_isbn
;
66 private TextWithLabelElement text_issn
;
67 private TextWithLabelElement text_organisation
;
68 private TextWithLabelElement text_pages
;
69 private TextWithLabelElement text_placePublished
;
70 private TextWithLabelElement text_publisher
;
71 private TextWithLabelElement text_referenceAbstract
;
72 private TextWithLabelElement text_series
;
73 // private TextWithLabelElement text_seriesPart;
74 private TextWithLabelElement text_volume
;
75 private TextWithLabelElement text_abbrevTitle
;
76 private TextWithLabelElement text_title
;
77 private UriWithLabelElement text_uri
;
78 private EntitySelectionElement
<AgentBase
> selection_authorTeam
;
79 private TimePeriodElement element_timePeriod
;
80 private EntitySelectionElement
<Reference
> selection_inReference
;
81 private EntitySelectionElement
<Institution
> selection_institution
;
82 private EnumComboElement
<ReferenceType
> combo_referenceType
;
84 private EntitySelectionElement
<Reference
> selection_inSeries
;
88 * Constructor for ReferenceBaseDetailElement.
91 * @param cdmFormFactory
92 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
95 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
100 public ReferenceDetailElement(CdmFormFactory cdmFormFactory
,
101 ICdmFormElement formElement
, int style
) {
102 super(cdmFormFactory
, formElement
);
109 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
110 * #createControl(org.eclipse.swt.widgets.Composite, int)
114 protected void createControls(ICdmFormElement formElement
,
115 Reference entity
, int style
) {
116 setWarnForReferencingObjects(formElement
);
118 toggleable_cache
= formFactory
.createToggleableTextField(formElement
,
119 "Reference Cache", entity
.getTitleCache(),
120 entity
.isProtectedTitleCache(), style
);
122 toggleableAbbrevCache
= formFactory
.createToggleableTextField(formElement
,
123 "Abbrev. Cache", entity
.getAbbrevTitleCache(),
124 entity
.isProtectedAbbrevTitleCache(), style
);
126 combo_referenceType
= formFactory
.createEnumComboElement(ReferenceType
.class,
129 combo_referenceType
.setSelection(entity
.getType());
132 text_title
= formFactory
.createTextWithLabelElement(formElement
,
133 "Title", entity
.getTitle(), style
);
135 text_abbrevTitle
= formFactory
.createTextWithLabelElement(formElement
,
136 "Nomenclatural Title", entity
.getAbbrevTitle(), style
);
139 selection_authorTeam
= formFactory
140 .createSelectionElement(AgentBase
.class,
141 getConversationHolder(), formElement
, "Author",
142 entity
.getAuthorship(),
143 EntitySelectionElement
.ALL
, style
);
145 ReferenceType referenceType
= entity
.getType();
147 if (referenceType
!= null) {
149 createInReferenceByType(this, referenceType
, entity
, SWT
.NULL
);
151 if (referenceType
.equals(ReferenceType
.Generic
)) {
152 createGenericControls(this, entity
, SWT
.NULL
);
153 createPublicationControls(this, referenceType
, entity
, SWT
.NULL
);
155 if (referenceType
.isPrintedUnit()) {
156 createPrintedUnitControls(this, referenceType
, entity
,
159 if (referenceType
.isPublication()) {
160 createPublicationControls(this, referenceType
, entity
,
163 if (referenceType
.isVolumeReference()) {
164 createVolumeControls(this, entity
, SWT
.NULL
);
166 if (referenceType
.isSection()) {
167 createSectionControls(this, entity
, SWT
.NULL
);
173 element_timePeriod
= formFactory
.createTimePeriodElement(formElement
,
174 "Date Published", entity
.getDatePublished(), style
);
176 createUriAndAbstract(this, entity
, SWT
.NULL
);
177 handleToggleableAbbrevTitleField();
178 handleToggleableCacheField();
185 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
190 protected void updateContent() {
191 if (getEntity() == null) {
192 setEntity(ReferenceFactory
.newGeneric());
195 super.updateContent();
197 // enable elements according to state of cache protection
198 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
199 // Object[]{text_cache}));
200 toggleable_cache
.setEnabled(getEntity().isProtectedTitleCache());
201 toggleableAbbrevCache
.setEnabled(getEntity().isProtectedAbbrevTitleCache());
202 setIrrelevantReferenceDetail(false);
203 setIrrelevantReferenceDetail(true);
207 * Creates an in reference for reference types that do hav this.
210 * @param referenceType
214 private void createInReferenceByType(ICdmFormElement element
,
215 ReferenceType referenceType
, Reference reference
, int style
) {
216 boolean createInReferenceWidget
= true;
217 String label
= "In Reference";
219 switch (referenceType
) {
228 label
= "Print Series";
236 createInReferenceWidget
= false;
239 if (createInReferenceWidget
) {
240 selection_inReference
= formFactory
241 .createSelectionElement(Reference
.class,
242 getConversationHolder(), element
, label
,
243 reference
.getInReference(),
244 EntitySelectionElement
.ALL
, style
);
249 * Creates additional controls to edit a generic reference
256 private void createGenericControls(ICdmFormElement element
,
257 Reference reference
, int style
) {
260 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
261 reference
.getEditor(), style
);
264 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
265 reference
.getSeriesPart(), style
);
268 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
269 reference
.getVolume(), style
);
272 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
273 reference
.getPages(), style
);
277 * Creates additional controls to edit a publication
284 private void createPublicationControls(ICdmFormElement element
,
285 ReferenceType referenceType
, Reference reference
, int style
) {
288 text_placePublished
= formFactory
.createTextWithLabelElement(element
,
289 "Place Published", reference
.getPlacePublished(), style
);
292 text_publisher
= formFactory
.createTextWithLabelElement(element
,
293 "Publisher", reference
.getPublisher(), style
);
295 if (referenceType
.equals(ReferenceType
.Journal
)) {
297 text_issn
= formFactory
.createTextWithLabelElement(element
, "ISSN",
298 reference
.getIssn(), style
);
301 if (referenceType
.equals(ReferenceType
.Report
)) {
303 selection_institution
= formFactory
304 .createSelectionElement(Institution
.class,
305 getConversationHolder(), element
, "Institution",
306 reference
.getInstitution(),
307 EntitySelectionElement
.ALL
, style
);
310 if (referenceType
.equals(ReferenceType
.Thesis
)) {
312 selection_institution
= formFactory
313 .createSelectionElement(Institution
.class,
314 getConversationHolder(), element
, "School",
315 reference
.getSchool(),
316 EntitySelectionElement
.ALL
, style
);
319 if (referenceType
.equals(ReferenceType
.Proceedings
)) {
321 text_organisation
= formFactory
.createTextWithLabelElement(element
,
322 "Organisation", reference
.getOrganization(), style
);
327 * Creates additional controls to edit a section
333 private void createSectionControls(ICdmFormElement element
,
334 Reference reference
, int style
) {
336 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
337 reference
.getPages(), style
);
341 * Creates additional controls to edit a volume
347 private void createVolumeControls(ICdmFormElement element
,
348 Reference reference
, int style
) {
350 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
351 reference
.getVolume(), style
);
353 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
354 reference
.getSeriesPart(), style
);
360 * @param referenceType
364 private void createPrintedUnitControls(ICdmFormElement element
,
365 ReferenceType referenceType
, Reference reference
, int style
) {
367 selection_inSeries
= formFactory
368 .createSelectionElement(Reference
.class,
369 getConversationHolder(), element
, "In Series",
370 (Reference
) reference
.getInSeries(),
371 EntitySelectionElement
.ALL
, style
);
374 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
375 reference
.getEditor(), style
);
378 text_seriesPart = formFactory.createTextWithLabelElement(element,
379 "Series", reference.getSeriesPart(), style);
381 if (referenceType
.equals(ReferenceType
.Book
)) {
383 text_edition
= formFactory
.createTextWithLabelElement(element
,
384 "Edition", reference
.getEdition(), style
);
387 text_isbn
= formFactory
.createTextWithLabelElement(element
, "ISBN",
388 reference
.getIsbn(), style
);
392 private void createUriAndAbstract(ICdmFormElement element
,
393 Reference reference
, int style
) {
395 text_uri
= formFactory
.createUriWithLabelElement(element
, "URI",
396 reference
.getUri(), style
);
398 text_referenceAbstract
= formFactory
.createMultiLineTextWithLabel(
399 element
, "Reference Abstract", 100, style
);
400 text_referenceAbstract
.setText(reference
.getReferenceAbstract());
405 public void handleEvent(Object eventSource
) {
406 if (eventSource
== combo_referenceType
) {
407 getEntity().setType(combo_referenceType
.getSelection());
409 } else if (eventSource
== element_timePeriod
) {
410 getEntity().setDatePublished(element_timePeriod
.getTimePeriod());
414 else if (eventSource
== selection_authorTeam
) {
415 getEntity().setAuthorship(
416 (TeamOrPersonBase
) selection_authorTeam
.getSelection());
417 } else if (eventSource
== selection_inReference
) {
418 getEntity().setInReference(selection_inReference
.getSelection());
419 } else if (eventSource
== selection_inSeries
) {
420 getEntity().setInReference(selection_inSeries
.getSelection());
421 } else if (eventSource
== selection_institution
) {
422 getEntity().setInstitution(selection_institution
.getSelection());
426 else if (eventSource
== toggleable_cache
) {
427 handleToggleableCacheField();
429 else if (eventSource
== toggleableAbbrevCache
) {
430 handleToggleableAbbrevTitleField();
431 } else if (eventSource
== text_edition
) {
432 getEntity().setEdition(text_edition
.getText());
433 } else if (eventSource
== text_editor
) {
434 getEntity().setEditor(text_editor
.getText());
435 } else if (eventSource
== text_isbn
) {
436 getEntity().setIsbn(text_isbn
.getText());
437 } else if (eventSource
== text_issn
) {
438 getEntity().setIssn(text_issn
.getText());
439 } else if (eventSource
== text_organisation
) {
440 getEntity().setOrganization(text_organisation
.getText());
441 } else if (eventSource
== text_pages
) {
442 getEntity().setPages(text_pages
.getText());
443 } else if (eventSource
== text_placePublished
) {
444 getEntity().setPlacePublished(text_placePublished
.getText());
445 } else if (eventSource
== text_publisher
) {
446 getEntity().setPublisher(text_publisher
.getText());
447 } else if (eventSource
== text_referenceAbstract
) {
448 getEntity().setReferenceAbstract(text_referenceAbstract
.getText());
449 } else if (eventSource
== text_series
) {
450 getEntity().setSeriesPart(text_series
.getText());
451 } else if (eventSource
== text_title
) {
452 getEntity().setTitle(text_title
.getText());
453 if (!toggleable_cache
.isEnabled()){
454 toggleable_cache
.setText(getEntity().generateTitle());
456 } else if (eventSource
== text_abbrevTitle
) {
457 getEntity().setAbbrevTitle(text_abbrevTitle
.getText());
458 if (!toggleableAbbrevCache
.isEnabled()){
459 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
461 } else if (eventSource
== text_uri
) {
462 getEntity().setUri(text_uri
.parseText());
463 } else if (eventSource
== text_volume
) {
464 getEntity().setVolume(text_volume
.getText());
467 if (eventSource
!= text_title
&& eventSource
!= text_abbrevTitle
) {
468 // set abbrev title cache to null to force new generation
469 // for the standard title cache this is done by a PropertyChangeListener registered
470 // in IdentifiableEntity.initListeners()
471 if(!getEntity().isProtectedAbbrevTitleCache()){
472 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
474 toggleable_cache
.setText(getEntity().getTitleCache());
475 toggleableAbbrevCache
.setText(getEntity().getAbbrevTitleCache());
478 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
483 public void updateToggleableCacheField() {
484 if (!getEntity().isProtectedTitleCache()) {
485 toggleable_cache
.setText(getEntity().generateTitle());
487 if (!getEntity().isProtectedAbbrevTitleCache()) {
488 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
492 protected void handleToggleableAbbrevTitleField() {
493 boolean pushedAbbrevState
= toggleableAbbrevCache
.getState();
494 getEntity().setAbbrevTitleCache(toggleableAbbrevCache
.getText(), pushedAbbrevState
);
495 setIrrelevantReferenceDetail(true);
496 updateToggleableCacheField();
503 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
504 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
508 public void handleException(CdmPropertyChangeEvent event
) {
509 exceptionOccurred(event
);
515 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
519 public void clearException() {
520 exceptionOccurred(null);
523 private final List
<IExceptionHandler
> exceptionHandlers
= new ArrayList
<IExceptionHandler
>();
529 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
530 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
534 public void addExceptionHandler(IExceptionHandler handler
) {
535 exceptionHandlers
.add(handler
);
542 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
543 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
547 public void removeExceptionHandler(IExceptionHandler handler
) {
548 exceptionHandlers
.remove(handler
);
555 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
556 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
560 public void exceptionOccurred(CdmPropertyChangeEvent event
) {
561 for (IExceptionHandler handler
: exceptionHandlers
) {
563 handler
.clearException();
565 handler
.handleException(event
);
571 protected void handleToggleableCacheField() {
573 boolean pushedState
= toggleable_cache
.getState();
574 getEntity().setTitleCache(toggleable_cache
.getText(), pushedState
);
575 setIrrelevantReferenceDetail(false);
576 updateToggleableCacheField();
579 protected void setIrrelevantReferenceDetail(boolean abbrev
){
581 List
<Object
> except
= new ArrayList();
582 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
}));
585 except
.add(text_title
);
586 pushedState
= toggleableAbbrevCache
.getState();
588 except
.add(text_abbrevTitle
);
589 pushedState
= toggleable_cache
.getState();
591 switch( getEntity().getType()){
593 except
.add(element_timePeriod
);
596 except
.remove(text_series
);
597 except
.remove(text_edition
);
602 setIrrelevant(pushedState
, except
);
605 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
606 Integer referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(getEntity());
607 if (referencingObjectsCount
> 1){
608 if (warnForReferencedObjects
== null){
609 warnForReferencedObjects
= formFactory
.createLabel(formElement
, CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
610 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
611 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 2));
612 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(
616 warnForReferencedObjects
.setText(CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));