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
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
24 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
27 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
28 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
29 import eu
.etaxonomy
.taxeditor
.ui
.combo
.EnumComboElement
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.IErrorIntolerableElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.IExceptionHandler
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.TimePeriodElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.ToggleableTextElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.UriWithLabelElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractIdentifiableEntityDetailElement
;
42 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
46 * ReferenceBaseDetailElement class.
50 * @created Mar 2, 2010
53 public class ReferenceDetailElement
extends AbstractIdentifiableEntityDetailElement
<Reference
>
54 implements IErrorIntolerableElement
, IExceptionHandler
{
56 private LabelElement warnForReferencedObjects
;
58 private ToggleableTextElement toggleableAbbrevCache
;
60 private TextWithLabelElement text_edition
;
61 private TextWithLabelElement text_editor
;
62 private TextWithLabelElement text_isbn
;
63 private TextWithLabelElement text_issn
;
64 private TextWithLabelElement text_organisation
;
65 private TextWithLabelElement text_pages
;
66 private TextWithLabelElement text_placePublished
;
67 private TextWithLabelElement text_publisher
;
68 private TextWithLabelElement text_referenceAbstract
;
69 private TextWithLabelElement text_series
;
70 // private TextWithLabelElement text_seriesPart;
71 private TextWithLabelElement text_volume
;
72 private TextWithLabelElement text_abbrevTitle
;
73 private TextWithLabelElement text_title
;
74 private UriWithLabelElement text_uri
;
75 private EntitySelectionElement
<AgentBase
> selection_authorTeam
;
76 private TimePeriodElement element_timePeriod
;
77 private EntitySelectionElement
<Reference
> selection_inReference
;
78 private EntitySelectionElement
<Institution
> selection_institution
;
79 private EnumComboElement
<ReferenceType
> combo_referenceType
;
81 private EntitySelectionElement
<Reference
> selection_inSeries
;
82 private boolean isWizard
;
86 * Constructor for ReferenceBaseDetailElement.
89 * @param cdmFormFactory
90 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
93 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
98 public ReferenceDetailElement(CdmFormFactory cdmFormFactory
,
99 ICdmFormElement formElement
, int style
, boolean isWizard
) {
100 super(cdmFormFactory
, formElement
);
101 this.isWizard
= isWizard
;
108 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
109 * #createControl(org.eclipse.swt.widgets.Composite, int)
113 protected void createControls(ICdmFormElement formElement
,
114 Reference entity
, int style
) {
115 ICdmFormElement o
= this.getParentElement();
116 Class clazz
=o
.getClass();
118 setWarnForReferencingObjects(formElement
);
122 toggleable_cache
= formFactory
.createToggleableTextField(formElement
,
123 "Reference Cache", entity
.getTitleCache(),
124 entity
.isProtectedTitleCache(), style
);
126 toggleableAbbrevCache
= formFactory
.createToggleableTextField(formElement
,
127 "Abbrev. Cache", entity
.getAbbrevTitleCache(),
128 entity
.isProtectedAbbrevTitleCache(), style
);
130 combo_referenceType
= formFactory
.createEnumComboElement(ReferenceType
.class,
133 combo_referenceType
.setSelection(entity
.getType());
136 text_title
= formFactory
.createTextWithLabelElement(formElement
,
137 "Title", entity
.getTitle(), style
);
139 text_abbrevTitle
= formFactory
.createTextWithLabelElement(formElement
,
140 "Nomenclatural Title", entity
.getAbbrevTitle(), style
);
143 selection_authorTeam
= formFactory
144 .createSelectionElement(AgentBase
.class,
145 getConversationHolder(), formElement
, "Author",
146 entity
.getAuthorship(),
147 EntitySelectionElement
.ALL
, style
);
149 ReferenceType referenceType
= entity
.getType();
151 if (referenceType
!= null) {
153 createInReferenceByType(this, referenceType
, entity
, SWT
.NULL
);
155 if (referenceType
.equals(ReferenceType
.Generic
)) {
156 createGenericControls(this, entity
, SWT
.NULL
);
157 createPublicationControls(this, referenceType
, entity
, SWT
.NULL
);
159 if (referenceType
.isPrintedUnit()) {
160 createPrintedUnitControls(this, referenceType
, entity
,
163 if (referenceType
.isPublication()) {
164 createPublicationControls(this, referenceType
, entity
,
167 if (referenceType
.isVolumeReference()) {
168 createVolumeControls(this, entity
, SWT
.NULL
);
170 if (referenceType
.isSection()) {
171 createSectionControls(this, entity
, SWT
.NULL
);
177 element_timePeriod
= formFactory
.createTimePeriodElement(formElement
,
178 "Date Published", entity
.getDatePublished(), style
);
180 createUriAndAbstract(this, entity
, SWT
.NULL
);
181 handleToggleableAbbrevTitleField();
182 handleToggleableCacheField();
189 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
194 protected void updateContent() {
195 if (getEntity() == null) {
196 setEntity(ReferenceFactory
.newGeneric());
199 super.updateContent();
201 // enable elements according to state of cache protection
202 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
203 // Object[]{text_cache}));
204 toggleable_cache
.setEnabled(getEntity().isProtectedTitleCache());
205 toggleableAbbrevCache
.setEnabled(getEntity().isProtectedAbbrevTitleCache());
206 setIrrelevantReferenceDetail(false);
207 setIrrelevantReferenceDetail(true);
211 * Creates an in reference for reference types that do hav this.
214 * @param referenceType
218 private void createInReferenceByType(ICdmFormElement element
,
219 ReferenceType referenceType
, Reference reference
, int style
) {
220 boolean createInReferenceWidget
= true;
221 String label
= "In Reference";
223 switch (referenceType
) {
232 label
= "Print Series";
240 createInReferenceWidget
= false;
243 if (createInReferenceWidget
) {
244 selection_inReference
= formFactory
245 .createSelectionElement(Reference
.class,
246 getConversationHolder(), element
, label
,
247 reference
.getInReference(),
248 EntitySelectionElement
.ALL
, style
);
253 * Creates additional controls to edit a generic reference
260 private void createGenericControls(ICdmFormElement element
,
261 Reference reference
, int style
) {
264 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
265 reference
.getEditor(), style
);
268 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
269 reference
.getSeriesPart(), style
);
272 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
273 reference
.getVolume(), style
);
276 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
277 reference
.getPages(), style
);
281 * Creates additional controls to edit a publication
288 private void createPublicationControls(ICdmFormElement element
,
289 ReferenceType referenceType
, Reference reference
, int style
) {
292 text_placePublished
= formFactory
.createTextWithLabelElement(element
,
293 "Place Published", reference
.getPlacePublished(), style
);
296 text_publisher
= formFactory
.createTextWithLabelElement(element
,
297 "Publisher", reference
.getPublisher(), style
);
299 if (referenceType
.equals(ReferenceType
.Journal
)) {
301 text_issn
= formFactory
.createTextWithLabelElement(element
, "ISSN",
302 reference
.getIssn(), style
);
305 if (referenceType
.equals(ReferenceType
.Report
)) {
307 selection_institution
= formFactory
308 .createSelectionElement(Institution
.class,
309 getConversationHolder(), element
, "Institution",
310 reference
.getInstitution(),
311 EntitySelectionElement
.ALL
, style
);
314 if (referenceType
.equals(ReferenceType
.Thesis
)) {
316 selection_institution
= formFactory
317 .createSelectionElement(Institution
.class,
318 getConversationHolder(), element
, "School",
319 reference
.getSchool(),
320 EntitySelectionElement
.ALL
, style
);
323 if (referenceType
.equals(ReferenceType
.Proceedings
)) {
325 text_organisation
= formFactory
.createTextWithLabelElement(element
,
326 "Organisation", reference
.getOrganization(), style
);
331 * Creates additional controls to edit a section
337 private void createSectionControls(ICdmFormElement element
,
338 Reference reference
, int style
) {
340 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
341 reference
.getPages(), style
);
345 * Creates additional controls to edit a volume
351 private void createVolumeControls(ICdmFormElement element
,
352 Reference reference
, int style
) {
354 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
355 reference
.getVolume(), style
);
357 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
358 reference
.getSeriesPart(), style
);
364 * @param referenceType
368 private void createPrintedUnitControls(ICdmFormElement element
,
369 ReferenceType referenceType
, Reference reference
, int style
) {
371 selection_inSeries
= formFactory
372 .createSelectionElement(Reference
.class,
373 getConversationHolder(), element
, "In Series",
374 (Reference
) reference
.getInSeries(),
375 EntitySelectionElement
.ALL
, style
);
378 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
379 reference
.getEditor(), style
);
382 text_seriesPart = formFactory.createTextWithLabelElement(element,
383 "Series", reference.getSeriesPart(), style);
385 if (referenceType
.equals(ReferenceType
.Book
)) {
387 text_edition
= formFactory
.createTextWithLabelElement(element
,
388 "Edition", reference
.getEdition(), style
);
391 text_isbn
= formFactory
.createTextWithLabelElement(element
, "ISBN",
392 reference
.getIsbn(), style
);
396 private void createUriAndAbstract(ICdmFormElement element
,
397 Reference reference
, int style
) {
399 text_uri
= formFactory
.createUriWithLabelElement(element
, "URI",
400 reference
.getUri(), style
);
402 text_referenceAbstract
= formFactory
.createMultiLineTextWithLabel(
403 element
, "Reference Abstract", 100, style
);
404 text_referenceAbstract
.setText(reference
.getReferenceAbstract());
409 public void handleEvent(Object eventSource
) {
410 if (eventSource
== combo_referenceType
) {
411 getEntity().setType(combo_referenceType
.getSelection());
413 } else if (eventSource
== element_timePeriod
) {
414 getEntity().setDatePublished(element_timePeriod
.getTimePeriod());
418 else if (eventSource
== selection_authorTeam
) {
419 getEntity().setAuthorship(
420 (TeamOrPersonBase
) selection_authorTeam
.getSelection());
421 } else if (eventSource
== selection_inReference
) {
422 getEntity().setInReference(selection_inReference
.getSelection());
423 } else if (eventSource
== selection_inSeries
) {
424 getEntity().setInReference(selection_inSeries
.getSelection());
425 } else if (eventSource
== selection_institution
) {
426 getEntity().setInstitution(selection_institution
.getSelection());
430 else if (eventSource
== toggleable_cache
) {
431 handleToggleableCacheField();
433 else if (eventSource
== toggleableAbbrevCache
) {
434 handleToggleableAbbrevTitleField();
435 } else if (eventSource
== text_edition
) {
436 getEntity().setEdition(text_edition
.getText());
437 } else if (eventSource
== text_editor
) {
438 getEntity().setEditor(text_editor
.getText());
439 } else if (eventSource
== text_isbn
) {
440 getEntity().setIsbn(text_isbn
.getText());
441 } else if (eventSource
== text_issn
) {
442 getEntity().setIssn(text_issn
.getText());
443 } else if (eventSource
== text_organisation
) {
444 getEntity().setOrganization(text_organisation
.getText());
445 } else if (eventSource
== text_pages
) {
446 getEntity().setPages(text_pages
.getText());
447 } else if (eventSource
== text_placePublished
) {
448 getEntity().setPlacePublished(text_placePublished
.getText());
449 } else if (eventSource
== text_publisher
) {
450 getEntity().setPublisher(text_publisher
.getText());
451 } else if (eventSource
== text_referenceAbstract
) {
452 getEntity().setReferenceAbstract(text_referenceAbstract
.getText());
453 } else if (eventSource
== text_series
) {
454 getEntity().setSeriesPart(text_series
.getText());
455 } else if (eventSource
== text_title
) {
456 getEntity().setTitle(text_title
.getText());
457 if (!toggleable_cache
.isEnabled()){
458 toggleable_cache
.setText(getEntity().generateTitle());
460 } else if (eventSource
== text_abbrevTitle
) {
461 getEntity().setAbbrevTitle(text_abbrevTitle
.getText());
462 if (!toggleableAbbrevCache
.isEnabled()){
463 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
465 } else if (eventSource
== text_uri
) {
466 getEntity().setUri(text_uri
.parseText());
467 } else if (eventSource
== text_volume
) {
468 getEntity().setVolume(text_volume
.getText());
471 if (eventSource
!= text_title
&& eventSource
!= text_abbrevTitle
) {
472 // set abbrev title cache to null to force new generation
473 // for the standard title cache this is done by a PropertyChangeListener registered
474 // in IdentifiableEntity.initListeners()
475 if(!getEntity().isProtectedAbbrevTitleCache()){
476 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
478 toggleable_cache
.setText(getEntity().getTitleCache());
479 toggleableAbbrevCache
.setText(getEntity().getAbbrevTitleCache());
482 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
487 public void updateToggleableCacheField() {
488 if (!getEntity().isProtectedTitleCache()) {
489 toggleable_cache
.setText(getEntity().generateTitle());
491 if (!getEntity().isProtectedAbbrevTitleCache()) {
492 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
496 protected void handleToggleableAbbrevTitleField() {
497 boolean pushedAbbrevState
= toggleableAbbrevCache
.getState();
498 getEntity().setAbbrevTitleCache(toggleableAbbrevCache
.getText(), pushedAbbrevState
);
499 setIrrelevantReferenceDetail(true);
500 updateToggleableCacheField();
507 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
508 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
512 public void handleException(CdmPropertyChangeEvent event
) {
513 exceptionOccurred(event
);
519 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
523 public void clearException() {
524 exceptionOccurred(null);
527 private final List
<IExceptionHandler
> exceptionHandlers
= new ArrayList
<IExceptionHandler
>();
533 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
534 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
538 public void addExceptionHandler(IExceptionHandler handler
) {
539 exceptionHandlers
.add(handler
);
546 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
547 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
551 public void removeExceptionHandler(IExceptionHandler handler
) {
552 exceptionHandlers
.remove(handler
);
559 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
560 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
564 public void exceptionOccurred(CdmPropertyChangeEvent event
) {
565 for (IExceptionHandler handler
: exceptionHandlers
) {
567 handler
.clearException();
569 handler
.handleException(event
);
575 protected void handleToggleableCacheField() {
577 boolean pushedState
= toggleable_cache
.getState();
578 getEntity().setTitleCache(toggleable_cache
.getText(), pushedState
);
579 setIrrelevantReferenceDetail(false);
580 updateToggleableCacheField();
583 protected void setIrrelevantReferenceDetail(boolean abbrev
){
585 List
<Object
> except
= new ArrayList();
586 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
}));
589 except
.add(text_title
);
590 pushedState
= toggleableAbbrevCache
.getState();
592 except
.add(text_abbrevTitle
);
593 pushedState
= toggleable_cache
.getState();
595 switch( getEntity().getType()){
597 except
.add(element_timePeriod
);
600 except
.remove(text_series
);
601 except
.remove(text_edition
);
606 setIrrelevant(pushedState
, except
);
609 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
610 if (getEntity().getId() != 0){
611 Integer referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(getEntity());
613 if (referencingObjectsCount
> 1){
614 if (warnForReferencedObjects
== null){
615 warnForReferencedObjects
= formFactory
.createLabel(formElement
, CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
616 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
617 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 2));
618 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(
622 warnForReferencedObjects
.setText(CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
624 warnForReferencedObjects
.setVisible(false);
629 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
630 if (warnForReferencedObjects
!= null){
631 warnForReferencedObjects
.setVisible(isVisible
);