Merge branch 'release/4.8.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / container / MisapplicationContainer.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.editor.name.container;
11
12 import javax.persistence.GenerationType;
13
14 import org.apache.commons.lang.StringUtils;
15 import org.eclipse.swt.graphics.Font;
16
17 import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
18 import eu.etaxonomy.cdm.model.name.TaxonName;
19 import eu.etaxonomy.cdm.model.taxon.Taxon;
20 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
21 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
22 import eu.etaxonomy.taxeditor.editor.EditorUtil;
23 import eu.etaxonomy.taxeditor.editor.l10n.Messages;
24 import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
25 import eu.etaxonomy.taxeditor.model.NameHelper;
26 import eu.etaxonomy.taxeditor.preference.Resources;
27
28 /**
29 * <p>MisapplicationContainer class.</p>
30 *
31 * @author p.ciardelli
32 * @created 13.01.2009
33 * @version 1.0
34 */
35 public class MisapplicationContainer extends AbstractGroupedContainer<Taxon> {
36 TaxonRelationship misapplication;
37
38 /**
39 * <p>Constructor for MisapplicationContainer.</p>
40 *
41 * @param editor a {@link eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor} object.
42 * @param group a {@link eu.etaxonomy.taxeditor.editor.name.container.AbstractGroup} object.
43 * @param misappliedName a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
44 */
45 public MisapplicationContainer(AbstractGroup group, Taxon misappliedName) {
46 super(misappliedName);
47
48 }
49
50
51 /* (non-Javadoc)
52 * @see eu.etaxonomy.taxeditor.editor.name.NameComposite#initializeComposite()
53 */
54 /** {@inheritDoc} */
55 @Override
56 protected void initializeComposite() {
57 setIsDraggable(true);
58 setFont(getViewerFont());
59 TaxonNameEditor editor = getEditor();
60 Taxon taxon = editor.getTaxon();
61 for (TaxonRelationship rel: taxon.getTaxonRelations(getMisappliedName())){
62 if (rel.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR())){
63 misapplication = rel;
64 break;
65 }
66 }
67 showSec();
68
69 initTextViewer();
70 }
71
72 @Override
73 protected void updateIcon() {
74 setIcon(MISAPPLIEDNAME_ICON);
75 }
76
77 private void showSec() {
78 if (getMisappliedName() == null) {
79 return;
80 }
81 String title = "";
82 String author = "";
83 TaxonName misappliedNameName = HibernateProxyHelper.deproxy(getMisappliedName().getName(), TaxonName.class);
84 if (misappliedNameName.getAuthorshipCache() != null && !StringUtils.isBlank(misappliedNameName.getAuthorshipCache())&& getMisappliedName().getAppendedPhrase() == null){
85 author = ", non " + misappliedNameName.getAuthorshipCache();
86 }
87 if (misapplication.getCitation() == null) {
88 if (getMisappliedName().getAppendedPhrase() != null ){
89 title = getMisappliedName().getAppendedPhrase();
90 }else{
91 title = "auct."; //$NON-NLS-1$
92
93 }
94
95 } else {
96 String sec = " sensu "; //$NON-NLS-1$
97 if (getMisappliedName().getAppendedPhrase() != null ){
98 sec = getMisappliedName().getAppendedPhrase() + " "+sec; //$NON-NLS-1$
99 }
100 title += sec + misapplication.getCitation().getTitleCache();
101
102
103
104
105 }
106 title += author;
107 title = title.replace("&", "&&"); //$NON-NLS-1$ //$NON-NLS-2$
108 setNonEditableInfo(title, false);
109 }
110
111 /**
112 * <p>getMisappliedName</p>
113 *
114 * @return a {@link eu.etaxonomy.cdm.model.taxon.Taxon} object.
115 */
116 public Taxon getMisappliedName() {
117 return getTaxonBase();
118 }
119
120 /** {@inheritDoc} */
121 @Override
122 protected Font getViewerFont() {
123 return EditorUtil.getFont(Resources.MISAPPLIEDNAME_FONT);
124 }
125
126 /* (non-Javadoc)
127 * @see eu.etaxonomy.taxeditor.editor.name.container.AbstractGroupedContainer#showAnnotations()
128 */
129 @Override
130 public void showAnnotations() {
131 if(getData().getSec() == null){
132 getNameViewer().addAnnotation(
133 new EditorAnnotation(0, Messages.MisapplicationContainer_SEC_REF_REQUIRED));
134 }
135 super.showAnnotations();
136 }
137
138
139 /** {@inheritDoc} */
140 @Override
141 protected void updateNonEditableInfo() {
142 showSec();
143 }
144
145
146 /* (non-Javadoc)
147 * @see eu.etaxonomy.taxeditor.editor.name.AbstractGroupedContainer#updateIndent()
148 */
149 @Override
150 protected void updateIndent() {
151 setIndent(MISAPPLIEDNAME_INDENT);
152 }
153
154 /**
155 * <p>
156 * initTextViewer
157 * </p>
158 */
159 @Override
160 protected void initTextViewer() {
161
162 // showNameRelations();
163
164 updateIndent();
165
166 updateIcon();
167
168 String text = NameHelper.getDisplayNameCache(getData());
169
170 if (text.length() == 0) {
171 initEmptyText();
172 } else {
173 getNameViewer().setText(text);
174 placeCursor();
175 }
176 calculateAnnotations();
177 }
178
179 @Override
180 public void refresh() {
181 // showNameRelations();
182
183 String text = NameHelper.getDisplayNameCache(getData());
184
185 if (getNameViewer().getTextWidget() == null) {
186 // we might get here via dnd. Look slike it can be ignored
187 return;
188 }
189
190 if (text.length() == 0) {
191 initEmptyText();
192 } else if (!getNameViewer().getTextWidget().getText().equals(text)) {
193 removeListener();
194 getNameViewer().getTextWidget().setText(text);
195 addListener();
196 }
197
198 updateNonEditableInfo();
199
200 updateIcon();
201 // placeCursor();
202 updateIndent();
203
204 enableFreeText();
205 }
206
207 }