2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.ui
.section
.reference
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.List
;
17 import org
.eclipse
.swt
.SWT
;
18 import org
.eclipse
.swt
.widgets
.Display
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
22 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
23 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
25 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
26 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
27 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
28 import eu
.etaxonomy
.taxeditor
.ui
.combo
.EnumComboElement
;
29 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
30 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
31 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
32 import eu
.etaxonomy
.taxeditor
.ui
.element
.IErrorIntolerableElement
;
33 import eu
.etaxonomy
.taxeditor
.ui
.element
.IExceptionHandler
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.TimePeriodElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.ToggleableTextElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.UriWithLabelElement
;
40 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractIdentifiableEntityDetailElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
45 * ReferenceBaseDetailElement class.
49 * @created Mar 2, 2010
52 public class ReferenceDetailElement
extends AbstractIdentifiableEntityDetailElement
<Reference
>
53 implements IErrorIntolerableElement
, IExceptionHandler
{
55 private LabelElement warnForReferencedObjects
;
57 private ToggleableTextElement toggleableAbbrevCache
;
59 private TextWithLabelElement text_edition
;
60 private TextWithLabelElement text_editor
;
61 private TextWithLabelElement text_isbn
;
62 private TextWithLabelElement text_issn
;
63 private TextWithLabelElement text_organisation
;
64 private TextWithLabelElement text_pages
;
65 private TextWithLabelElement text_placePublished
;
66 private TextWithLabelElement text_publisher
;
67 private TextWithLabelElement text_referenceAbstract
;
68 private TextWithLabelElement text_series
;
69 // private TextWithLabelElement text_seriesPart;
70 private TextWithLabelElement text_volume
;
71 private TextWithLabelElement text_abbrevTitle
;
72 private TextWithLabelElement text_title
;
73 private UriWithLabelElement text_uri
;
74 private EntitySelectionElement
<AgentBase
> selection_authorTeam
;
75 private TimePeriodElement element_timePeriod
;
76 private EntitySelectionElement
<Reference
> selection_inReference
;
77 private EntitySelectionElement
<Institution
> selection_institution
;
78 private EnumComboElement
<ReferenceType
> combo_referenceType
;
80 private EntitySelectionElement
<Reference
> selection_inSeries
;
81 private boolean isWizard
;
85 * Constructor for ReferenceBaseDetailElement.
88 * @param cdmFormFactory
89 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
92 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
97 public ReferenceDetailElement(CdmFormFactory cdmFormFactory
,
98 ICdmFormElement formElement
, int style
, boolean isWizard
) {
99 super(cdmFormFactory
, formElement
);
100 this.isWizard
= isWizard
;
107 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
108 * #createControl(org.eclipse.swt.widgets.Composite, int)
112 protected void createControls(ICdmFormElement formElement
,
113 Reference entity
, int style
) {
114 ICdmFormElement o
= this.getParentElement();
115 Class clazz
=o
.getClass();
117 setWarnForReferencingObjects(formElement
);
121 toggleable_cache
= formFactory
.createToggleableTextField(formElement
,
122 "Reference Cache", entity
.getTitleCache(),
123 entity
.isProtectedTitleCache(), style
);
125 toggleableAbbrevCache
= formFactory
.createToggleableTextField(formElement
,
126 "Abbrev. Cache", entity
.getAbbrevTitleCache(),
127 entity
.isProtectedAbbrevTitleCache(), style
);
129 combo_referenceType
= formFactory
.createEnumComboElement(ReferenceType
.class,
132 combo_referenceType
.setSelection(entity
.getType());
135 text_title
= formFactory
.createTextWithLabelElement(formElement
,
136 "Title", entity
.getTitle(), style
);
138 text_abbrevTitle
= formFactory
.createTextWithLabelElement(formElement
,
139 "Nomenclatural Title", entity
.getAbbrevTitle(), style
);
142 selection_authorTeam
= formFactory
143 .createSelectionElement(AgentBase
.class,
144 getConversationHolder(), formElement
, "Author",
145 entity
.getAuthorship(),
146 EntitySelectionElement
.ALL
, style
);
148 ReferenceType referenceType
= entity
.getType();
150 if (referenceType
!= null) {
152 createInReferenceByType(this, referenceType
, entity
, SWT
.NULL
);
154 if (referenceType
.equals(ReferenceType
.Generic
)) {
155 createGenericControls(this, entity
, SWT
.NULL
);
156 createPublicationControls(this, referenceType
, entity
, SWT
.NULL
);
158 if (referenceType
.isPrintedUnit()) {
159 createPrintedUnitControls(this, referenceType
, entity
,
162 if (referenceType
.isPublication()) {
163 createPublicationControls(this, referenceType
, entity
,
166 if (referenceType
.isVolumeReference()) {
167 createVolumeControls(this, entity
, SWT
.NULL
);
169 if (referenceType
.isSection()) {
170 createSectionControls(this, entity
, SWT
.NULL
);
176 element_timePeriod
= formFactory
.createTimePeriodElement(formElement
,
177 "Date Published", entity
.getDatePublished(), style
);
179 createUriAndAbstract(this, entity
, SWT
.NULL
);
180 handleToggleableAbbrevTitleField();
181 handleToggleableCacheField();
188 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
193 protected void updateContent() {
194 if (getEntity() == null) {
195 setEntity(ReferenceFactory
.newGeneric());
198 super.updateContent();
200 // enable elements according to state of cache protection
201 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
202 // Object[]{text_cache}));
203 toggleable_cache
.setEnabled(getEntity().isProtectedTitleCache());
204 toggleableAbbrevCache
.setEnabled(getEntity().isProtectedAbbrevTitleCache());
205 setIrrelevantReferenceDetail(false);
206 setIrrelevantReferenceDetail(true);
210 * Creates an in reference for reference types that do hav this.
213 * @param referenceType
217 private void createInReferenceByType(ICdmFormElement element
,
218 ReferenceType referenceType
, Reference reference
, int style
) {
219 boolean createInReferenceWidget
= true;
220 String label
= "In Reference";
222 switch (referenceType
) {
231 label
= "Print Series";
239 createInReferenceWidget
= false;
242 if (createInReferenceWidget
) {
243 selection_inReference
= formFactory
244 .createSelectionElement(Reference
.class,
245 getConversationHolder(), element
, label
,
246 reference
.getInReference(),
247 EntitySelectionElement
.ALL
, style
);
252 * Creates additional controls to edit a generic reference
259 private void createGenericControls(ICdmFormElement element
,
260 Reference reference
, int style
) {
263 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
264 reference
.getEditor(), style
);
267 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
268 reference
.getSeriesPart(), style
);
271 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
272 reference
.getVolume(), style
);
275 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
276 reference
.getPages(), style
);
280 * Creates additional controls to edit a publication
287 private void createPublicationControls(ICdmFormElement element
,
288 ReferenceType referenceType
, Reference reference
, int style
) {
291 text_placePublished
= formFactory
.createTextWithLabelElement(element
,
292 "Place Published", reference
.getPlacePublished(), style
);
295 text_publisher
= formFactory
.createTextWithLabelElement(element
,
296 "Publisher", reference
.getPublisher(), style
);
298 if (referenceType
.equals(ReferenceType
.Journal
)) {
300 text_issn
= formFactory
.createTextWithLabelElement(element
, "ISSN",
301 reference
.getIssn(), style
);
304 if (referenceType
.equals(ReferenceType
.Report
)) {
306 selection_institution
= formFactory
307 .createSelectionElement(Institution
.class,
308 getConversationHolder(), element
, "Institution",
309 reference
.getInstitution(),
310 EntitySelectionElement
.ALL
, style
);
313 if (referenceType
.equals(ReferenceType
.Thesis
)) {
315 selection_institution
= formFactory
316 .createSelectionElement(Institution
.class,
317 getConversationHolder(), element
, "School",
318 reference
.getSchool(),
319 EntitySelectionElement
.ALL
, style
);
322 if (referenceType
.equals(ReferenceType
.Proceedings
)) {
324 text_organisation
= formFactory
.createTextWithLabelElement(element
,
325 "Organisation", reference
.getOrganization(), style
);
330 * Creates additional controls to edit a section
336 private void createSectionControls(ICdmFormElement element
,
337 Reference reference
, int style
) {
339 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
340 reference
.getPages(), style
);
344 * Creates additional controls to edit a volume
350 private void createVolumeControls(ICdmFormElement element
,
351 Reference reference
, int style
) {
353 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
354 reference
.getVolume(), style
);
356 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
357 reference
.getSeriesPart(), style
);
363 * @param referenceType
367 private void createPrintedUnitControls(ICdmFormElement element
,
368 ReferenceType referenceType
, Reference reference
, int style
) {
370 selection_inSeries
= formFactory
371 .createSelectionElement(Reference
.class,
372 getConversationHolder(), element
, "In Series",
373 (Reference
) reference
.getInSeries(),
374 EntitySelectionElement
.ALL
, style
);
377 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
378 reference
.getEditor(), style
);
381 text_seriesPart = formFactory.createTextWithLabelElement(element,
382 "Series", reference.getSeriesPart(), style);
384 if (referenceType
.equals(ReferenceType
.Book
)) {
386 text_edition
= formFactory
.createTextWithLabelElement(element
,
387 "Edition", reference
.getEdition(), style
);
390 text_isbn
= formFactory
.createTextWithLabelElement(element
, "ISBN",
391 reference
.getIsbn(), style
);
395 private void createUriAndAbstract(ICdmFormElement element
,
396 Reference reference
, int style
) {
398 text_uri
= formFactory
.createUriWithLabelElement(element
, "URI",
399 reference
.getUri(), style
);
401 text_referenceAbstract
= formFactory
.createMultiLineTextWithLabel(
402 element
, "Reference Abstract", 100, style
);
403 text_referenceAbstract
.setText(reference
.getReferenceAbstract());
408 public void handleEvent(Object eventSource
) {
409 if (eventSource
== combo_referenceType
) {
410 getEntity().setType(combo_referenceType
.getSelection());
412 } else if (eventSource
== element_timePeriod
) {
413 getEntity().setDatePublished(element_timePeriod
.getTimePeriod());
417 else if (eventSource
== selection_authorTeam
) {
418 getEntity().setAuthorship(
419 (TeamOrPersonBase
) selection_authorTeam
.getSelection());
420 } else if (eventSource
== selection_inReference
) {
421 getEntity().setInReference(selection_inReference
.getSelection());
422 } else if (eventSource
== selection_inSeries
) {
423 getEntity().setInReference(selection_inSeries
.getSelection());
424 } else if (eventSource
== selection_institution
) {
425 getEntity().setInstitution(selection_institution
.getSelection());
429 else if (eventSource
== toggleable_cache
) {
430 handleToggleableCacheField();
432 else if (eventSource
== toggleableAbbrevCache
) {
433 handleToggleableAbbrevTitleField();
434 } else if (eventSource
== text_edition
) {
435 getEntity().setEdition(text_edition
.getText());
436 } else if (eventSource
== text_editor
) {
437 getEntity().setEditor(text_editor
.getText());
438 } else if (eventSource
== text_isbn
) {
439 getEntity().setIsbn(text_isbn
.getText());
440 } else if (eventSource
== text_issn
) {
441 getEntity().setIssn(text_issn
.getText());
442 } else if (eventSource
== text_organisation
) {
443 getEntity().setOrganization(text_organisation
.getText());
444 } else if (eventSource
== text_pages
) {
445 getEntity().setPages(text_pages
.getText());
446 } else if (eventSource
== text_placePublished
) {
447 getEntity().setPlacePublished(text_placePublished
.getText());
448 } else if (eventSource
== text_publisher
) {
449 getEntity().setPublisher(text_publisher
.getText());
450 } else if (eventSource
== text_referenceAbstract
) {
451 getEntity().setReferenceAbstract(text_referenceAbstract
.getText());
452 } else if (eventSource
== text_series
) {
453 getEntity().setSeriesPart(text_series
.getText());
454 } else if (eventSource
== text_title
) {
455 getEntity().setTitle(text_title
.getText());
456 if (!toggleable_cache
.isEnabled()){
457 toggleable_cache
.setText(getEntity().generateTitle());
459 } else if (eventSource
== text_abbrevTitle
) {
460 getEntity().setAbbrevTitle(text_abbrevTitle
.getText());
461 if (!toggleableAbbrevCache
.isEnabled()){
462 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
464 } else if (eventSource
== text_uri
) {
465 getEntity().setUri(text_uri
.parseText());
466 } else if (eventSource
== text_volume
) {
467 getEntity().setVolume(text_volume
.getText());
470 if (eventSource
!= text_title
&& eventSource
!= text_abbrevTitle
) {
471 // set abbrev title cache to null to force new generation
472 // for the standard title cache this is done by a PropertyChangeListener registered
473 // in IdentifiableEntity.initListeners()
474 if(!getEntity().isProtectedAbbrevTitleCache()){
475 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
477 toggleable_cache
.setText(getEntity().getTitleCache());
478 toggleableAbbrevCache
.setText(getEntity().getAbbrevTitleCache());
481 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
486 public void updateToggleableCacheField() {
487 if (!getEntity().isProtectedTitleCache()) {
488 toggleable_cache
.setText(getEntity().generateTitle());
490 if (!getEntity().isProtectedAbbrevTitleCache()) {
491 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
495 protected void handleToggleableAbbrevTitleField() {
496 boolean pushedAbbrevState
= toggleableAbbrevCache
.getState();
497 getEntity().setAbbrevTitleCache(toggleableAbbrevCache
.getText(), pushedAbbrevState
);
498 setIrrelevantReferenceDetail(true);
499 updateToggleableCacheField();
506 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
507 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
511 public void handleException(CdmPropertyChangeEvent event
) {
512 exceptionOccurred(event
);
518 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
522 public void clearException() {
523 exceptionOccurred(null);
526 private final List
<IExceptionHandler
> exceptionHandlers
= new ArrayList
<IExceptionHandler
>();
532 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
533 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
537 public void addExceptionHandler(IExceptionHandler handler
) {
538 exceptionHandlers
.add(handler
);
545 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
546 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
550 public void removeExceptionHandler(IExceptionHandler handler
) {
551 exceptionHandlers
.remove(handler
);
558 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
559 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
563 public void exceptionOccurred(CdmPropertyChangeEvent event
) {
564 for (IExceptionHandler handler
: exceptionHandlers
) {
566 handler
.clearException();
568 handler
.handleException(event
);
574 protected void handleToggleableCacheField() {
576 boolean pushedState
= toggleable_cache
.getState();
577 getEntity().setTitleCache(toggleable_cache
.getText(), pushedState
);
578 setIrrelevantReferenceDetail(false);
579 updateToggleableCacheField();
582 protected void setIrrelevantReferenceDetail(boolean abbrev
){
584 List
<Object
> except
= new ArrayList();
585 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
}));
588 except
.add(text_title
);
589 pushedState
= toggleableAbbrevCache
.getState();
591 except
.add(text_abbrevTitle
);
592 pushedState
= toggleable_cache
.getState();
594 switch( getEntity().getType()){
596 except
.add(element_timePeriod
);
599 except
.remove(text_series
);
600 except
.remove(text_edition
);
605 setIrrelevant(pushedState
, except
);
608 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
609 if (getEntity().getId() != 0){
610 Integer referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(getEntity());
612 if (referencingObjectsCount
> 1){
613 warnForReferencedObjects
= formFactory
.createLabel(formElement
, CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
614 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
615 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 3));
617 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
622 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
623 if (warnForReferencedObjects
!= null){
624 warnForReferencedObjects
.setVisible(isVisible
);