Refactoring selection elements so they can be configured
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / ui / section / name / AuthorshipDetailElement.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
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.
9 */
10
11 package eu.etaxonomy.taxeditor.ui.section.name;
12
13 import java.util.Arrays;
14
15 import eu.etaxonomy.cdm.model.agent.INomenclaturalAuthor;
16 import eu.etaxonomy.cdm.model.name.NonViralName;
17 import eu.etaxonomy.taxeditor.model.AuthorHelper;
18 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory;
19 import eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory.SelectionType;
20 import eu.etaxonomy.taxeditor.ui.forms.CdmPropertyChangeEvent;
21 import eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement;
22 import eu.etaxonomy.taxeditor.ui.forms.IEnableableFormElement;
23 import eu.etaxonomy.taxeditor.ui.forms.ToggleableTextElement;
24 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailElement;
25 import eu.etaxonomy.taxeditor.ui.section.AbstractCdmDetailSection;
26 import eu.etaxonomy.taxeditor.ui.selection.AbstractSelectionElement;
27 import eu.etaxonomy.taxeditor.ui.selection.NomenclaturalAuthorTeamSelectionElement;
28
29 /**
30 * <p>
31 * AuthorshipDetailElement class.
32 * </p>
33 *
34 * @author n.hoffmann
35 * @created Mar 4, 2010
36 * @version 1.0
37 */
38 public class AuthorshipDetailElement extends
39 AbstractCdmDetailElement<NonViralName> implements
40 IEnableableFormElement {
41
42 private ToggleableTextElement toggleable_cache;
43
44 private AbstractSelectionElement selection_exBasionymAuthorTeam;
45 private AbstractSelectionElement selection_basionymAuthorTeam;
46 private AbstractSelectionElement selection_exCombinationAuthorTeam;
47 private AbstractSelectionElement selection_combinationAuthorTeam;
48
49 /**
50 * <p>
51 * Constructor for AuthorshipDetailElement.
52 * </p>
53 *
54 * @param cdmFormFactory
55 * a {@link eu.etaxonomy.taxeditor.ui.forms.CdmFormFactory}
56 * object.
57 * @param formElement
58 * a {@link eu.etaxonomy.taxeditor.ui.forms.ICdmFormElement}
59 * object.
60 * @param style
61 * a int.
62 */
63 public AuthorshipDetailElement(CdmFormFactory cdmFormFactory,
64 ICdmFormElement formElement, int style) {
65 super(cdmFormFactory, formElement);
66 }
67
68 /** {@inheritDoc} */
69 @Override
70 protected void createControls(ICdmFormElement formElement,
71 NonViralName entity, int style) {
72 toggleable_cache = formFactory.createToggleableTextField(this,
73 "Authorship Cache", entity.getAuthorshipCache(),
74 entity.isProtectedAuthorshipCache(), style);
75
76 selection_combinationAuthorTeam = formFactory.createSelectionElement(
77 SelectionType.AUTHOR_TEAM, getConversationHolder(),
78 formElement, "Author",
79 AuthorHelper.getAuthor(entity.getCombinationAuthorTeam()),
80 NomenclaturalAuthorTeamSelectionElement.DEFAULT, style);
81 addElement(selection_combinationAuthorTeam);
82 selection_exCombinationAuthorTeam = formFactory.createSelectionElement(
83 SelectionType.AUTHOR_TEAM, getConversationHolder(),
84 formElement, "Ex Author",
85 AuthorHelper.getAuthor(entity.getExCombinationAuthorTeam()),
86 NomenclaturalAuthorTeamSelectionElement.DEFAULT, style);
87 addElement(selection_exCombinationAuthorTeam);
88 selection_basionymAuthorTeam = formFactory.createSelectionElement(
89 SelectionType.AUTHOR_TEAM, getConversationHolder(),
90 formElement, "Basionym Author",
91 AuthorHelper.getAuthor(entity.getBasionymAuthorTeam()),
92 NomenclaturalAuthorTeamSelectionElement.DEFAULT, style);
93 addElement(selection_basionymAuthorTeam);
94 selection_exBasionymAuthorTeam = formFactory.createSelectionElement(
95 SelectionType.AUTHOR_TEAM, getConversationHolder(),
96 formElement, "Ex Basionym Author",
97 AuthorHelper.getAuthor(entity.getExBasionymAuthorTeam()),
98 NomenclaturalAuthorTeamSelectionElement.DEFAULT, style);
99 addElement(selection_exBasionymAuthorTeam);
100 }
101
102 /** {@inheritDoc} */
103 @Override
104 public void updateContent() {
105 if (getEntity() == null) {
106 setEntity(NonViralName.NewInstance(null));
107 }
108 NonViralName nonViralName = getEntity();
109
110 super.updateContent();
111 toggleable_cache.setEnabled(getEntity().isProtectedAuthorshipCache());
112
113 if (this.isIrrelevant()) {
114 setIrrelevant(isIrrelevant());
115 } else {
116 setIrrelevant(toggleable_cache.getState(),
117 Arrays.asList(new Object[] { toggleable_cache }));
118 }
119 }
120
121 /** {@inheritDoc} */
122 @Override
123 public void handleEvent(Object eventSource) {
124 if (eventSource == toggleable_cache) {
125 getEntity().setAuthorshipCache(toggleable_cache.getText(),
126 toggleable_cache.getState());
127 if (!isIrrelevant())
128 setIrrelevant(toggleable_cache.getState(),
129 Arrays.asList(new Object[] { toggleable_cache }));
130 } else if (eventSource == selection_combinationAuthorTeam) {
131 getEntity().setCombinationAuthorTeam(
132 (INomenclaturalAuthor) selection_combinationAuthorTeam
133 .getSelection());
134 } else if (eventSource == selection_exCombinationAuthorTeam) {
135 getEntity().setExCombinationAuthorTeam(
136 (INomenclaturalAuthor) selection_exCombinationAuthorTeam
137 .getSelection());
138 } else if (eventSource == selection_basionymAuthorTeam) {
139 getEntity().setBasionymAuthorTeam(
140 (INomenclaturalAuthor) selection_basionymAuthorTeam
141 .getSelection());
142 } else if (eventSource == selection_exBasionymAuthorTeam) {
143 getEntity().setExBasionymAuthorTeam(
144 (INomenclaturalAuthor) selection_exBasionymAuthorTeam
145 .getSelection());
146 }
147
148 if (eventSource != toggleable_cache) {
149 toggleable_cache.setText(getEntity().getAuthorshipCache());
150 }
151
152 // we have to notify the parent if this is embedded in the nonviral name
153 // section
154 // maybe we can handle this a little bit more elegant
155 if (getParentElement() instanceof AbstractCdmDetailSection)
156 firePropertyChangeEvent(new CdmPropertyChangeEvent(
157 getParentElement(), null));
158 }
159 }