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
.jface
.wizard
.WizardPage
;
19 import org
.eclipse
.swt
.SWT
;
20 import org
.eclipse
.swt
.widgets
.Display
;
21 import org
.eclipse
.swt
.widgets
.Label
;
22 import org
.eclipse
.swt
.graphics
.Color
;
23 import org
.eclipse
.swt
.graphics
.FontData
;
25 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
26 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
27 import eu
.etaxonomy
.cdm
.model
.agent
.Institution
;
28 import eu
.etaxonomy
.cdm
.model
.agent
.TeamOrPersonBase
;
29 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
30 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
31 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceType
;
32 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
33 import eu
.etaxonomy
.taxeditor
.ui
.combo
.EnumComboElement
;
34 import eu
.etaxonomy
.taxeditor
.ui
.element
.AbstractCdmEntityWizardPage
;
35 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmFormFactory
;
36 import eu
.etaxonomy
.taxeditor
.ui
.element
.CdmPropertyChangeEvent
;
37 import eu
.etaxonomy
.taxeditor
.ui
.element
.ICdmFormElement
;
38 import eu
.etaxonomy
.taxeditor
.ui
.element
.IErrorIntolerableElement
;
39 import eu
.etaxonomy
.taxeditor
.ui
.element
.IExceptionHandler
;
40 import eu
.etaxonomy
.taxeditor
.ui
.element
.LabelElement
;
41 import eu
.etaxonomy
.taxeditor
.ui
.element
.LayoutConstants
;
42 import eu
.etaxonomy
.taxeditor
.ui
.element
.TextWithLabelElement
;
43 import eu
.etaxonomy
.taxeditor
.ui
.element
.TimePeriodElement
;
44 import eu
.etaxonomy
.taxeditor
.ui
.element
.ToggleableTextElement
;
45 import eu
.etaxonomy
.taxeditor
.ui
.element
.UriWithLabelElement
;
46 import eu
.etaxonomy
.taxeditor
.ui
.section
.AbstractIdentifiableEntityDetailElement
;
47 import eu
.etaxonomy
.taxeditor
.ui
.selection
.EntitySelectionElement
;
51 * ReferenceBaseDetailElement class.
55 * @created Mar 2, 2010
58 public class ReferenceDetailElement
extends AbstractIdentifiableEntityDetailElement
<Reference
>
59 implements IErrorIntolerableElement
, IExceptionHandler
{
61 private LabelElement warnForReferencedObjects
;
63 private ToggleableTextElement toggleableAbbrevCache
;
65 private TextWithLabelElement text_edition
;
66 private TextWithLabelElement text_editor
;
67 private TextWithLabelElement text_isbn
;
68 private TextWithLabelElement text_issn
;
69 private TextWithLabelElement text_organisation
;
70 private TextWithLabelElement text_pages
;
71 private TextWithLabelElement text_placePublished
;
72 private TextWithLabelElement text_publisher
;
73 private TextWithLabelElement text_referenceAbstract
;
74 private TextWithLabelElement text_series
;
75 // private TextWithLabelElement text_seriesPart;
76 private TextWithLabelElement text_volume
;
77 private TextWithLabelElement text_abbrevTitle
;
78 private TextWithLabelElement text_title
;
79 private UriWithLabelElement text_uri
;
80 private EntitySelectionElement
<AgentBase
> selection_authorTeam
;
81 private TimePeriodElement element_timePeriod
;
82 private EntitySelectionElement
<Reference
> selection_inReference
;
83 private EntitySelectionElement
<Institution
> selection_institution
;
84 private EnumComboElement
<ReferenceType
> combo_referenceType
;
86 private EntitySelectionElement
<Reference
> selection_inSeries
;
90 * Constructor for ReferenceBaseDetailElement.
93 * @param cdmFormFactory
94 * a {@link eu.etaxonomy.taxeditor.ui.element.CdmFormFactory}
97 * a {@link eu.etaxonomy.taxeditor.ui.element.ICdmFormElement}
102 public ReferenceDetailElement(CdmFormFactory cdmFormFactory
,
103 ICdmFormElement formElement
, int style
) {
104 super(cdmFormFactory
, formElement
);
111 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
112 * #createControl(org.eclipse.swt.widgets.Composite, int)
116 protected void createControls(ICdmFormElement formElement
,
117 Reference entity
, int style
) {
118 ICdmFormElement o
= this.getParentElement();
119 Class clazz
=o
.getClass();
120 setWarnForReferencingObjects(formElement
);
123 toggleable_cache
= formFactory
.createToggleableTextField(formElement
,
124 "Reference Cache", entity
.getTitleCache(),
125 entity
.isProtectedTitleCache(), style
);
127 toggleableAbbrevCache
= formFactory
.createToggleableTextField(formElement
,
128 "Abbrev. Cache", entity
.getAbbrevTitleCache(),
129 entity
.isProtectedAbbrevTitleCache(), style
);
131 combo_referenceType
= formFactory
.createEnumComboElement(ReferenceType
.class,
134 combo_referenceType
.setSelection(entity
.getType());
137 text_title
= formFactory
.createTextWithLabelElement(formElement
,
138 "Title", entity
.getTitle(), style
);
140 text_abbrevTitle
= formFactory
.createTextWithLabelElement(formElement
,
141 "Nomenclatural Title", entity
.getAbbrevTitle(), style
);
144 selection_authorTeam
= formFactory
145 .createSelectionElement(AgentBase
.class,
146 getConversationHolder(), formElement
, "Author",
147 entity
.getAuthorship(),
148 EntitySelectionElement
.ALL
, style
);
150 ReferenceType referenceType
= entity
.getType();
152 if (referenceType
!= null) {
154 createInReferenceByType(this, referenceType
, entity
, SWT
.NULL
);
156 if (referenceType
.equals(ReferenceType
.Generic
)) {
157 createGenericControls(this, entity
, SWT
.NULL
);
158 createPublicationControls(this, referenceType
, entity
, SWT
.NULL
);
160 if (referenceType
.isPrintedUnit()) {
161 createPrintedUnitControls(this, referenceType
, entity
,
164 if (referenceType
.isPublication()) {
165 createPublicationControls(this, referenceType
, entity
,
168 if (referenceType
.isVolumeReference()) {
169 createVolumeControls(this, entity
, SWT
.NULL
);
171 if (referenceType
.isSection()) {
172 createSectionControls(this, entity
, SWT
.NULL
);
178 element_timePeriod
= formFactory
.createTimePeriodElement(formElement
,
179 "Date Published", entity
.getDatePublished(), style
);
181 createUriAndAbstract(this, entity
, SWT
.NULL
);
182 handleToggleableAbbrevTitleField();
183 handleToggleableCacheField();
190 * eu.etaxonomy.taxeditor.forms.section.cdmdetail.AbstractCdmDetailComposite
195 protected void updateContent() {
196 if (getEntity() == null) {
197 setEntity(ReferenceFactory
.newGeneric());
200 super.updateContent();
202 // enable elements according to state of cache protection
203 // setEnabled(getEntity().isProtectedTitleCache(), Arrays.asList(new
204 // Object[]{text_cache}));
205 toggleable_cache
.setEnabled(getEntity().isProtectedTitleCache());
206 toggleableAbbrevCache
.setEnabled(getEntity().isProtectedAbbrevTitleCache());
207 setIrrelevantReferenceDetail(false);
208 setIrrelevantReferenceDetail(true);
212 * Creates an in reference for reference types that do hav this.
215 * @param referenceType
219 private void createInReferenceByType(ICdmFormElement element
,
220 ReferenceType referenceType
, Reference reference
, int style
) {
221 boolean createInReferenceWidget
= true;
222 String label
= "In Reference";
224 switch (referenceType
) {
233 label
= "Print Series";
241 createInReferenceWidget
= false;
244 if (createInReferenceWidget
) {
245 selection_inReference
= formFactory
246 .createSelectionElement(Reference
.class,
247 getConversationHolder(), element
, label
,
248 reference
.getInReference(),
249 EntitySelectionElement
.ALL
, style
);
254 * Creates additional controls to edit a generic reference
261 private void createGenericControls(ICdmFormElement element
,
262 Reference reference
, int style
) {
265 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
266 reference
.getEditor(), style
);
269 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
270 reference
.getSeriesPart(), style
);
273 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
274 reference
.getVolume(), style
);
277 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
278 reference
.getPages(), style
);
282 * Creates additional controls to edit a publication
289 private void createPublicationControls(ICdmFormElement element
,
290 ReferenceType referenceType
, Reference reference
, int style
) {
293 text_placePublished
= formFactory
.createTextWithLabelElement(element
,
294 "Place Published", reference
.getPlacePublished(), style
);
297 text_publisher
= formFactory
.createTextWithLabelElement(element
,
298 "Publisher", reference
.getPublisher(), style
);
300 if (referenceType
.equals(ReferenceType
.Journal
)) {
302 text_issn
= formFactory
.createTextWithLabelElement(element
, "ISSN",
303 reference
.getIssn(), style
);
306 if (referenceType
.equals(ReferenceType
.Report
)) {
308 selection_institution
= formFactory
309 .createSelectionElement(Institution
.class,
310 getConversationHolder(), element
, "Institution",
311 reference
.getInstitution(),
312 EntitySelectionElement
.ALL
, style
);
315 if (referenceType
.equals(ReferenceType
.Thesis
)) {
317 selection_institution
= formFactory
318 .createSelectionElement(Institution
.class,
319 getConversationHolder(), element
, "School",
320 reference
.getSchool(),
321 EntitySelectionElement
.ALL
, style
);
324 if (referenceType
.equals(ReferenceType
.Proceedings
)) {
326 text_organisation
= formFactory
.createTextWithLabelElement(element
,
327 "Organisation", reference
.getOrganization(), style
);
332 * Creates additional controls to edit a section
338 private void createSectionControls(ICdmFormElement element
,
339 Reference reference
, int style
) {
341 text_pages
= formFactory
.createTextWithLabelElement(element
, "Pages",
342 reference
.getPages(), style
);
346 * Creates additional controls to edit a volume
352 private void createVolumeControls(ICdmFormElement element
,
353 Reference reference
, int style
) {
355 text_volume
= formFactory
.createTextWithLabelElement(element
, "Volume",
356 reference
.getVolume(), style
);
358 text_series
= formFactory
.createTextWithLabelElement(element
, "Series",
359 reference
.getSeriesPart(), style
);
365 * @param referenceType
369 private void createPrintedUnitControls(ICdmFormElement element
,
370 ReferenceType referenceType
, Reference reference
, int style
) {
372 selection_inSeries
= formFactory
373 .createSelectionElement(Reference
.class,
374 getConversationHolder(), element
, "In Series",
375 (Reference
) reference
.getInSeries(),
376 EntitySelectionElement
.ALL
, style
);
379 text_editor
= formFactory
.createTextWithLabelElement(element
, "Editor",
380 reference
.getEditor(), style
);
383 text_seriesPart = formFactory.createTextWithLabelElement(element,
384 "Series", reference.getSeriesPart(), style);
386 if (referenceType
.equals(ReferenceType
.Book
)) {
388 text_edition
= formFactory
.createTextWithLabelElement(element
,
389 "Edition", reference
.getEdition(), style
);
392 text_isbn
= formFactory
.createTextWithLabelElement(element
, "ISBN",
393 reference
.getIsbn(), style
);
397 private void createUriAndAbstract(ICdmFormElement element
,
398 Reference reference
, int style
) {
400 text_uri
= formFactory
.createUriWithLabelElement(element
, "URI",
401 reference
.getUri(), style
);
403 text_referenceAbstract
= formFactory
.createMultiLineTextWithLabel(
404 element
, "Reference Abstract", 100, style
);
405 text_referenceAbstract
.setText(reference
.getReferenceAbstract());
410 public void handleEvent(Object eventSource
) {
411 if (eventSource
== combo_referenceType
) {
412 getEntity().setType(combo_referenceType
.getSelection());
414 } else if (eventSource
== element_timePeriod
) {
415 getEntity().setDatePublished(element_timePeriod
.getTimePeriod());
419 else if (eventSource
== selection_authorTeam
) {
420 getEntity().setAuthorship(
421 (TeamOrPersonBase
) selection_authorTeam
.getSelection());
422 } else if (eventSource
== selection_inReference
) {
423 getEntity().setInReference(selection_inReference
.getSelection());
424 } else if (eventSource
== selection_inSeries
) {
425 getEntity().setInReference(selection_inSeries
.getSelection());
426 } else if (eventSource
== selection_institution
) {
427 getEntity().setInstitution(selection_institution
.getSelection());
431 else if (eventSource
== toggleable_cache
) {
432 handleToggleableCacheField();
434 else if (eventSource
== toggleableAbbrevCache
) {
435 handleToggleableAbbrevTitleField();
436 } else if (eventSource
== text_edition
) {
437 getEntity().setEdition(text_edition
.getText());
438 } else if (eventSource
== text_editor
) {
439 getEntity().setEditor(text_editor
.getText());
440 } else if (eventSource
== text_isbn
) {
441 getEntity().setIsbn(text_isbn
.getText());
442 } else if (eventSource
== text_issn
) {
443 getEntity().setIssn(text_issn
.getText());
444 } else if (eventSource
== text_organisation
) {
445 getEntity().setOrganization(text_organisation
.getText());
446 } else if (eventSource
== text_pages
) {
447 getEntity().setPages(text_pages
.getText());
448 } else if (eventSource
== text_placePublished
) {
449 getEntity().setPlacePublished(text_placePublished
.getText());
450 } else if (eventSource
== text_publisher
) {
451 getEntity().setPublisher(text_publisher
.getText());
452 } else if (eventSource
== text_referenceAbstract
) {
453 getEntity().setReferenceAbstract(text_referenceAbstract
.getText());
454 } else if (eventSource
== text_series
) {
455 getEntity().setSeriesPart(text_series
.getText());
456 } else if (eventSource
== text_title
) {
457 getEntity().setTitle(text_title
.getText());
458 if (!toggleable_cache
.isEnabled()){
459 toggleable_cache
.setText(getEntity().generateTitle());
461 } else if (eventSource
== text_abbrevTitle
) {
462 getEntity().setAbbrevTitle(text_abbrevTitle
.getText());
463 if (!toggleableAbbrevCache
.isEnabled()){
464 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
466 } else if (eventSource
== text_uri
) {
467 getEntity().setUri(text_uri
.parseText());
468 } else if (eventSource
== text_volume
) {
469 getEntity().setVolume(text_volume
.getText());
472 if (eventSource
!= text_title
&& eventSource
!= text_abbrevTitle
) {
473 // set abbrev title cache to null to force new generation
474 // for the standard title cache this is done by a PropertyChangeListener registered
475 // in IdentifiableEntity.initListeners()
476 if(!getEntity().isProtectedAbbrevTitleCache()){
477 getEntity().setAbbrevTitleCache(null, getEntity().isProtectedAbbrevTitleCache());
479 toggleable_cache
.setText(getEntity().getTitleCache());
480 toggleableAbbrevCache
.setText(getEntity().getAbbrevTitleCache());
483 firePropertyChangeEvent(new CdmPropertyChangeEvent(this, null));
488 public void updateToggleableCacheField() {
489 if (!getEntity().isProtectedTitleCache()) {
490 toggleable_cache
.setText(getEntity().generateTitle());
492 if (!getEntity().isProtectedAbbrevTitleCache()) {
493 toggleableAbbrevCache
.setText(getEntity().generateAbbrevTitle());
497 protected void handleToggleableAbbrevTitleField() {
498 boolean pushedAbbrevState
= toggleableAbbrevCache
.getState();
499 getEntity().setAbbrevTitleCache(toggleableAbbrevCache
.getText(), pushedAbbrevState
);
500 setIrrelevantReferenceDetail(true);
501 updateToggleableCacheField();
508 * eu.etaxonomy.taxeditor.section.AbstractCdmDetailElement#handleException
509 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
513 public void handleException(CdmPropertyChangeEvent event
) {
514 exceptionOccurred(event
);
520 * @see eu.etaxonomy.taxeditor.forms.IExceptionHandler#clearException()
524 public void clearException() {
525 exceptionOccurred(null);
528 private final List
<IExceptionHandler
> exceptionHandlers
= new ArrayList
<IExceptionHandler
>();
534 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#addExceptionHandler
535 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
539 public void addExceptionHandler(IExceptionHandler handler
) {
540 exceptionHandlers
.add(handler
);
547 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#removeExceptionHandler
548 * (eu.etaxonomy.taxeditor.forms.IExceptionHandler)
552 public void removeExceptionHandler(IExceptionHandler handler
) {
553 exceptionHandlers
.remove(handler
);
560 * eu.etaxonomy.taxeditor.forms.IErrorIntolerableElement#exceptionOccurred
561 * (eu.etaxonomy.taxeditor.forms.CdmPropertyChangeEvent)
565 public void exceptionOccurred(CdmPropertyChangeEvent event
) {
566 for (IExceptionHandler handler
: exceptionHandlers
) {
568 handler
.clearException();
570 handler
.handleException(event
);
576 protected void handleToggleableCacheField() {
578 boolean pushedState
= toggleable_cache
.getState();
579 getEntity().setTitleCache(toggleable_cache
.getText(), pushedState
);
580 setIrrelevantReferenceDetail(false);
581 updateToggleableCacheField();
584 protected void setIrrelevantReferenceDetail(boolean abbrev
){
586 List
<Object
> except
= new ArrayList();
587 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
}));
590 except
.add(text_title
);
591 pushedState
= toggleableAbbrevCache
.getState();
593 except
.add(text_abbrevTitle
);
594 pushedState
= toggleable_cache
.getState();
596 switch( getEntity().getType()){
598 except
.add(element_timePeriod
);
601 except
.remove(text_series
);
602 except
.remove(text_edition
);
607 setIrrelevant(pushedState
, except
);
610 public void setWarnForReferencingObjects(ICdmFormElement formElement
){
611 Integer referencingObjectsCount
= CdmStore
.getCommonService().getReferencingObjectsCount(getEntity());
612 if (referencingObjectsCount
> 1){
613 if (warnForReferencedObjects
== null){
614 warnForReferencedObjects
= formFactory
.createLabel(formElement
, CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
615 warnForReferencedObjects
.setBackground(Display
.getCurrent().getSystemColor(SWT
.COLOR_RED
));
616 warnForReferencedObjects
.setLayout(LayoutConstants
.FILL(2, 2));
617 warnForReferencedObjects
.setForeground(Display
.getCurrent().getSystemColor(
621 warnForReferencedObjects
.setText(CdmUtils
.Nz("The reference is referenced by " + referencingObjectsCount
+ " objects, if you change it, it is changed for all these objects"));
623 warnForReferencedObjects
.setVisible(false);
627 public void setWarnForReferencingObjectsVisible(boolean isVisible
){
628 if (warnForReferencedObjects
!= null){
629 warnForReferencedObjects
.setVisible(isVisible
);