editor now updatable via updateSite
[taxeditor.git] / taxeditor-editor / src / main / java / eu / etaxonomy / taxeditor / editor / name / NameComposite.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;
11
12 import java.util.Set;
13
14 import org.eclipse.jface.action.Action;
15 import org.eclipse.swt.graphics.Font;
16 import org.eclipse.swt.graphics.Image;
17 import org.eclipse.swt.widgets.Composite;
18 import org.eclipse.ui.views.properties.IPropertySource;
19
20 import eu.etaxonomy.cdm.model.common.CdmBase;
21 import eu.etaxonomy.cdm.model.name.BotanicalName;
22 import eu.etaxonomy.cdm.model.name.NameRelationship;
23 import eu.etaxonomy.cdm.model.name.NonViralName;
24 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25 import eu.etaxonomy.cdm.model.name.ZoologicalName;
26 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
27 import eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor;
28 import eu.etaxonomy.taxeditor.editor.EditorUtil;
29 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
30 import eu.etaxonomy.taxeditor.propertysheet.name.BotanicalNamePropertySource;
31 import eu.etaxonomy.taxeditor.propertysheet.name.NonViralNamePropertySource;
32 import eu.etaxonomy.taxeditor.propertysheet.name.ZoologicalNamePropertySource;
33 import eu.etaxonomy.taxeditor.store.model.ImageResources;
34 import eu.etaxonomy.taxeditor.store.model.NameRelationsUtil;
35 import eu.etaxonomy.taxeditor.store.model.NameUtil;
36 import eu.etaxonomy.taxeditor.store.model.Resources;
37 import eu.etaxonomy.taxeditor.store.operations.CreateSynonymInNewGroupOperation;
38
39 /**
40 * Formats an <code>GroupedComposite</code> to display <code>TaxonNameBase</code> elements
41 * in a <code>NameViewer</code>.
42 *
43 * @author p.ciardelli
44 * @created 02.06.2008
45 * @version 1.0
46 */
47 public abstract class NameComposite extends GroupedComposite{
48
49 /**
50 * ************ COMPOSITE TYPES ************
51 */
52 public String compositeType;
53 public static final String ACCEPTED_TAXON = "accepted_name_composite";
54 public static final String HOMOTYPIC_SYNONYM = "homotypic_name_composite";
55 public static final String HETEROTYPIC_SYNONYM = "heterotypic_name_composite";
56 public static final String MISAPPLIED_NAME = "misappliedname_name_composite";
57 public static final String CONCEPTRELATION = "concept_name_comcposite";
58
59 /**
60 * ************ INDENTATIONS ************
61 */
62 public static final int ACCEPTED_INDENT = 0;
63 public static final int SYNONYM_INDENT = 15;
64 public static final int MISAPPLIEDNAME_INDENT = 15;
65 public static final int CONCEPT_INDENT = 15;
66
67 /**
68 * ************ FONTS ************
69 */
70 public static final Font ACCEPTED_FONT = Resources
71 .getFont(Resources.ACCEPTED_TAXON_FONT);
72 public static final Font SYNONYM_FONT = Resources
73 .getFont(Resources.SYNONYM_FONT);
74 public static final Font MISAPPLIEDNAME_FONT = Resources
75 .getFont(Resources.MISAPPLIEDNAME_FONT);
76 public static final Font CONCEPT_FONT = Resources
77 .getFont(Resources.CONCEPT_FONT);
78
79 /**
80 * ************ ICONS ************
81 */
82 public static final Image ACCEPTED_ICON = ImageResources
83 .getImage(ImageResources.BLACK_SQUARE_ICON);
84 public static final Image HOMOTYPIC_SYNONYM_ICON = ImageResources.getImage(ImageResources.HOMOTYPIC_SYN_ICON);
85 public static final Image HOMOTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = ImageResources.getImage(
86 ImageResources.HOMOTYPIC_SYN_ORIGINAL_ICON);
87 public static final Image HETEROTYPIC_SYNONYM_ICON = ImageResources.getImage(ImageResources.HETEROTYPIC_SYN_ICON);
88 public static final Image HETEROTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = ImageResources.getImage(
89 ImageResources.HETEROTYPIC_SYN_ORIGINAL_ICON);
90 public static final Image MISAPPLIEDNAME_ICON = ImageResources.getImage(ImageResources.MISAPPLIED_NAME_ICON);
91 public static final Image CONCEPT_ICON = ImageResources.getImage(ImageResources.CONCEPT_ICON);
92 public static final Image AUTONYM_ICON = ImageResources
93 .getImage(ImageResources.AUTONYM_ICON);
94 public static final Image BASIONYM_ICON = ImageResources
95 .getImage(ImageResources.BASIONYM_ICON);
96 public static final Image MOVE = ImageResources.getImage(ImageResources.MOVE_ICON);
97
98 /**
99 * ************ TRANSFORMATIONS ************
100 */
101 public static final String ADD_GROUP_BASIONYM = "add_group_basionym";
102 public static final String REMOVE_GROUP_BASIONYM = "remove_group_basionym";
103
104 /**
105 * ************ MENU ACTIONS ************
106 */
107 public Action CHANGE_TAXON_TO_SYNONYM_ACTION;
108
109 private static final String EMPTY_NAME_PROMPT = "Click to add name";
110
111 /**
112 * Used to turn parser on and off.
113 *
114 * @see activateParser
115 * @see deactivateParser
116 */
117 private boolean isUseParser = false;
118
119 protected boolean isParsing;
120
121 /**
122 * The constructor for a DescriptionElementComposite. Takes a parent Composite on which to
123 * create itself, and an IManagedForm for Composite life cycle methods, i.e.
124 * drawing borders, creating other Composites, creating line wrap support,
125 * etc.
126 *
127 * @param parent
128 * @param managedForm
129 */
130 public NameComposite(AbstractTaxonEditor editor, Composite parent,
131 String compositeType, TaxonBase<?> taxonBase) {
132 super(editor, parent);
133
134 createTextViewer();
135 createBorderSupport();
136 createLineWrapSupport();
137 createLineBreakListener();
138
139 this.setMenu(editor.getMenu());
140
141 setDraggableControl(textViewer.getRulerControl());
142
143 createParser();
144 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
145 }
146
147 protected String getEmptyTextPrompt() {
148 return EMPTY_NAME_PROMPT;
149 }
150
151 /**
152 *
153 */
154 private void showNameRelations() {
155 TaxonNameBase<?, ?> name = getName();
156 if (name == null) {
157 return;
158 }
159 Set<NameRelationship> nameRelations = name.getNameRelations();
160 if (nameRelations.size() == 0) {
161 return;
162 }
163 for (NameRelationship nameRelation : nameRelations) {
164 String typeLabel = null;
165 TaxonNameBase<?, ?> relatedName = null;
166
167 boolean isZoological = name instanceof ZoologicalName ? true : false;
168
169 if (name.equals(nameRelation.getFromName())) {
170 typeLabel = NameRelationsUtil.getNameRelationTypeLabel(
171 nameRelation.getType(), isZoological);
172 relatedName = nameRelation.getToName();
173 } else {
174 typeLabel = NameRelationsUtil.getNameRelationInverseTypeLabel(
175 nameRelation.getType(), isZoological);
176 relatedName = nameRelation.getFromName();
177 }
178
179 setNonEditableInfo(typeLabel + " " + NameUtil.getDisplayName(relatedName));
180 }
181 }
182
183 protected void initTextViewer(TaxonBase<?> taxonBase) {
184
185 showNameRelations();
186
187 String text = NameUtil.getDisplayNameWithRef(taxonBase);
188
189 if (text.length() == 0) {
190 initEmptyText();
191 } else {
192 textViewer.getTextWidget().setText(text);
193 textViewer.setCursorToEOL();
194 }
195 calculateErrors();
196 activateParser();
197 }
198
199 protected void parse(String text) {
200
201 // Either composite is not yet fully built, or
202 // the property sheet is writing to it
203 if (!isUseParser) {
204 return;
205 }
206
207 // Let others know the parser is active
208 isParsing = true;
209
210 // Parse the name and paint the text field w any errors
211 if (getName() != null) {
212 // TODO change to HibernateProxyImpl.deproxy() once it is available
213 CdmParserController.parseFullReference(CdmBase.deproxy(getName(), NonViralName.class), text);
214 }
215
216 // Any entry of text means the taxon has been changed
217 setDirty(true);
218
219 // The parser is no longer active
220 isParsing = false;
221
222 // // Manually refresh the property sheet to reflect changes
223 setSelection();
224
225 // Show any errors in the name viewer
226 calculateErrors();
227 }
228
229 private void activateParser() {
230 isUseParser = true;
231 }
232
233 // FIXME we do have a naming conflict here
234 protected abstract TaxonNameBase<?, ?> getName();
235
236 protected void calculateErrors() {
237 textViewer.clearErrors();
238 textViewer.setShowParsingError(getName());
239 }
240
241 protected void handleSplitText(String text) {
242 // Create a synonym in a new homotypic group using text as name
243 EditorUtil.executeOperation(new CreateSynonymInNewGroupOperation
244 ("New Heterotypic Synonym", editor.getUndoContext(), editor.getTaxon(), text, editor));
245 }
246
247 protected IPropertySource getPropertySourceByName(TaxonNameBase<?, ?> name) {
248 if (name == null) {
249 return null;
250 }
251
252 if (name.getClass() == BotanicalName.class) {
253 return new BotanicalNamePropertySource((BotanicalName) name);
254 }
255 if (name.getClass() == ZoologicalName.class) {
256 return new ZoologicalNamePropertySource((ZoologicalName) name);
257 }
258 if (name instanceof NonViralName) {
259 return new NonViralNamePropertySource((NonViralName<?>) name);
260 }
261
262 return null;
263 }
264 }