Did some code cleanup. Removed obsolete and deprecated classes.
[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.apache.log4j.Logger;
15 import org.eclipse.swt.events.FocusAdapter;
16 import org.eclipse.swt.events.FocusEvent;
17 import org.eclipse.swt.events.ModifyEvent;
18 import org.eclipse.swt.events.ModifyListener;
19 import org.eclipse.swt.graphics.Font;
20 import org.eclipse.swt.graphics.Image;
21 import org.eclipse.swt.widgets.Composite;
22
23 import eu.etaxonomy.cdm.model.name.NameRelationship;
24 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
25 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
26 import eu.etaxonomy.taxeditor.editor.EditorUtil;
27 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
28 import eu.etaxonomy.taxeditor.labels.ILabelImageStrategy;
29 import eu.etaxonomy.taxeditor.labels.LabelImageProvider;
30 import eu.etaxonomy.taxeditor.model.ImageResources;
31 import eu.etaxonomy.taxeditor.model.NameHelper;
32 import eu.etaxonomy.taxeditor.model.Resources;
33 import eu.etaxonomy.taxeditor.operations.CreateSynonymInNewGroupOperation;
34 import eu.etaxonomy.taxeditor.parser.ParseHandler;
35
36 /**
37 * Formats an <code>GroupedComposite</code> to display <code>TaxonNameBase</code> elements
38 * in a <code>NameViewer</code>.
39 *
40 * @author p.ciardelli
41 * @created 02.06.2008
42 * @version 1.0
43 */
44 public abstract class NameComposite<T extends TaxonBase> extends GroupedComposite{
45 private static final Logger logger = Logger
46 .getLogger(NameComposite.class);
47 /**
48 * ************ COMPOSITE TYPES ************
49 */
50 public String compositeType;
51 public static final String ACCEPTED_TAXON = "accepted_name_composite";
52 public static final String HOMOTYPIC_SYNONYM = "homotypic_name_composite";
53 public static final String HETEROTYPIC_SYNONYM = "heterotypic_name_composite";
54 public static final String MISAPPLIED_NAME = "misappliedname_name_composite";
55 public static final String CONCEPTRELATION = "concept_name_comcposite";
56
57 /**
58 * ************ INDENTATIONS ************
59 */
60 public static final int ACCEPTED_INDENT = 0;
61 public static final int SYNONYM_INDENT = 15;
62 public static final int MISAPPLIEDNAME_INDENT = 15;
63 public static final int CONCEPT_INDENT = 15;
64
65 /**
66 * ************ FONTS ************
67 */
68 public static final Font ACCEPTED_FONT = Resources
69 .getFont(Resources.ACCEPTED_TAXON_FONT);
70 public static final Font SYNONYM_FONT = Resources
71 .getFont(Resources.SYNONYM_FONT);
72 public static final Font MISAPPLIEDNAME_FONT = Resources
73 .getFont(Resources.MISAPPLIEDNAME_FONT);
74 public static final Font CONCEPT_FONT = Resources
75 .getFont(Resources.CONCEPT_FONT);
76
77 /**
78 * ************ ICONS ************
79 */
80 public static final Image ACCEPTED_ICON = ImageResources
81 .getImage(ImageResources.BLACK_SQUARE_ICON);
82 public static final Image HOMOTYPIC_SYNONYM_ICON = ImageResources.getImage(ImageResources.HOMOTYPIC_SYN_ICON);
83 public static final Image HOMOTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = ImageResources.getImage(
84 ImageResources.HOMOTYPIC_SYN_ORIGINAL_ICON);
85 public static final Image HETEROTYPIC_SYNONYM_ICON = ImageResources.getImage(ImageResources.HETEROTYPIC_SYN_ICON);
86 public static final Image HETEROTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = ImageResources.getImage(
87 ImageResources.HETEROTYPIC_SYN_ORIGINAL_ICON);
88 public static final Image MISAPPLIEDNAME_ICON = ImageResources.getImage(ImageResources.MISAPPLIED_NAME_ICON);
89 public static final Image CONCEPT_ICON = ImageResources.getImage(ImageResources.CONCEPT_ICON);
90 public static final Image AUTONYM_ICON = ImageResources
91 .getImage(ImageResources.AUTONYM_ICON);
92 public static final Image MOVE = ImageResources.getImage(ImageResources.MOVE_ICON);
93
94 /**
95 * ************ TRANSFORMATIONS ************
96 */
97 public static final String ADD_GROUP_BASIONYM = "add_group_basionym";
98 public static final String REMOVE_GROUP_BASIONYM = "remove_group_basionym";
99
100
101
102 private static final String EMPTY_NAME_PROMPT = "Click to add name";
103
104 /**
105 * Used to turn parser on and off.
106 *
107 * @see activateParser
108 * @see deactivateParser
109 */
110 private boolean isUseParser = false;
111
112 protected ParseHandler parseHandler;
113
114 protected boolean isParsing;
115
116 protected TaxonNameBase parsedName;
117
118 /**
119 * Whether this NameComposite has unsaved changes
120 */
121 private boolean isDirty = false;
122
123 protected T taxonBase;
124 private NameCompositeFocusListener nameCompositeFocusListener;
125 private NameCompositeModifyListener nameCompositeModifyListener;
126
127 /**
128 * The constructor for a DescriptionElementComposite. Takes a parent Composite on which to
129 * create itself, and an IManagedForm for Composite life cycle methods, i.e.
130 * drawing borders, creating other Composites, creating line wrap support,
131 * etc.
132 *
133 * @param parent
134 * @param managedForm
135 */
136 public NameComposite(TaxonNameEditor editor, Composite parent, T taxonBase) {
137 super(editor, parent);
138
139 this.taxonBase = taxonBase;
140
141
142 createTextViewer();
143 // createBorderSupport();
144 createLineWrapSupport();
145 createLineBreakListener();
146
147 this.setMenu(editor.getMenu());
148
149 setDraggableControl(textViewer.getRulerControl());
150
151 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
152
153 parseHandler = ParseHandler.NewInstance(textViewer.getTextWidget(), taxonBase.getName());
154
155 initializeComposite();
156
157 createListener();
158
159
160 }
161
162 protected void createListener(){
163 nameCompositeModifyListener = new NameCompositeModifyListener();
164 nameCompositeFocusListener = new NameCompositeFocusListener();
165
166 addListener();
167 }
168
169 private void addListener(){
170 textViewer.getTextWidget().addModifyListener(nameCompositeModifyListener);
171 textViewer.getTextWidget().addFocusListener(nameCompositeFocusListener);
172 }
173
174 private void removeListener(){
175 textViewer.getTextWidget().removeModifyListener(nameCompositeModifyListener);
176 textViewer.getTextWidget().removeFocusListener(nameCompositeFocusListener);
177 }
178
179 /**
180 * Initialize the composite specific code
181 *
182 */
183 protected abstract void initializeComposite();
184
185 protected String getEmptyTextPrompt() {
186 return EMPTY_NAME_PROMPT;
187 }
188
189 /**
190 *
191 */
192 private void showNameRelations() {
193 TaxonNameBase<?, ?> name = getName();
194 if (name == null) {
195 return;
196 }
197
198 ILabelImageStrategy strategy = LabelImageProvider.getLabelStrategy(name);
199 LabelImageProvider labelProvider = new LabelImageProvider(strategy);
200
201 Set<NameRelationship> nameRelations = name.getNameRelations();
202 if (nameRelations.size() == 0) {
203 return;
204 }
205 for (NameRelationship nameRelation : nameRelations) {
206 String typeLabel = null;
207 TaxonNameBase<?, ?> relatedName = null;
208
209 if (name.equals(nameRelation.getFromName())) {
210 typeLabel = labelProvider.getNameRelationTypeLabel(
211 nameRelation.getType());
212 relatedName = nameRelation.getToName();
213 } else {
214 typeLabel = labelProvider.getNameRelationTypeInverseLabel(
215 nameRelation.getType());
216 relatedName = nameRelation.getFromName();
217 }
218
219 setNonEditableInfo(typeLabel + " " + NameHelper.getDisplayName(relatedName));
220 }
221 }
222
223 protected void initTextViewer(TaxonBase<?> taxonBase) {
224
225 showNameRelations();
226
227 String text = NameHelper.getDisplayNameWithRef(taxonBase);
228
229 if (text.length() == 0) {
230 initEmptyText();
231 } else {
232 textViewer.getTextWidget().setText(text);
233 textViewer.setCursorToEOL();
234 }
235 calculateErrors();
236 }
237
238 // FIXME we do have a naming conflict here
239 public abstract TaxonNameBase<?, ?> getName();
240
241 public abstract void setName(TaxonNameBase name);
242
243 synchronized protected void calculateErrors() {
244 textViewer.clearErrors();
245
246 textViewer.setShowParsingErrors(getName());
247 }
248
249 protected void handleSplitText(String text) {
250 // Create a synonym in a new homotypic group using text as name
251 TaxonNameBase synonymName = ParseHandler.quickParse(text);
252
253 EditorUtil.executeOperation(new CreateSynonymInNewGroupOperation
254 ("New Heterotypic Synonym", editor.getUndoContext(), editor.getTaxon(), synonymName, editor));
255 logger.debug("new heterotypic synonym created through entering of line break");
256 }
257
258 // protected IPropertySource getPropertySourceByName(TaxonNameBase<?, ?> name) {
259 // if (name == null) {
260 // return null;
261 // }
262 //
263 // if (name.getClass() == BotanicalName.class) {
264 // return new BotanicalNamePropertySource((BotanicalName) name);
265 // }
266 // if (name.getClass() == ZoologicalName.class) {
267 // return new ZoologicalNamePropertySource((ZoologicalName) name);
268 // }
269 // if (name instanceof NonViralName) {
270 // return new NonViralNamePropertySource((NonViralName<?>) name);
271 // }
272 //
273 // return null;
274 // }
275
276 /* (non-Javadoc)
277 * @see eu.etaxonomy.taxeditor.editor.GroupedComposite#refresh()
278 */
279 @Override
280 public void refresh() {
281 super.refresh();
282 // initTextViewer((TaxonBase) getData());
283
284 // FIXME this is copied from initTextViewer().
285
286 showNameRelations();
287
288 String text = NameHelper.getDisplayNameWithRef((TaxonBase) getData());
289
290 if (text.length() == 0) {
291 initEmptyText();
292 } else {
293 updateTextWidget(text);
294 // textViewer.setCursorToEOL();
295 }
296 }
297
298 private void updateTextWidget(String text){
299 removeListener();
300 textViewer.getTextWidget().setText(text);
301 parseHandler.parse();
302 addListener();
303 calculateErrors();
304 }
305
306 public boolean isDirty(){
307 return isDirty;
308 }
309
310 /* (non-Javadoc)
311 * @see eu.etaxonomy.taxeditor.editor.GroupedComposite#setDirty(boolean)
312 */
313 @Override
314 protected void setDirty(boolean isDirty) {
315 // TODO Auto-generated method stub
316 super.setDirty(isDirty);
317 // set the dirty state for this composite
318 this.isDirty = isDirty;
319 }
320
321 /**
322 * @return the taxonBase
323 */
324 public T getTaxonBase() {
325 return taxonBase;
326 }
327
328 /**
329 * @return the parsedName
330 */
331 public TaxonNameBase getParsedName() {
332 return parsedName;
333 }
334
335 /* (non-Javadoc)
336 * @see eu.etaxonomy.taxeditor.editor.GroupedComposite#dispose()
337 */
338 @Override
339 public void dispose() {
340 super.dispose();
341 this.removeFocusListener(nameCompositeFocusListener);
342 }
343
344 public void persistName(){
345 if(isDirty()){
346 logger.warn("Found dirty composite: " + this + ". Name: " + textViewer.getTextWidget().getText());
347 taxonBase.setName(parseHandler.parseAndResolveDuplicates());
348
349 getTaxonBase().setTitleCache((getTaxonBase().generateTitle()));
350
351 }
352 }
353
354 class NameCompositeModifyListener implements ModifyListener{
355
356 public void modifyText(ModifyEvent e) {
357 parseHandler.parse();
358
359 // getTaxonBase().setName(name);
360
361 setSelection();
362 // show errors resulting from parsing
363 calculateErrors();
364 // mark the composite dirty
365 setDirty(true);
366 }
367 }
368
369 class NameCompositeFocusListener extends FocusAdapter{
370
371 /* (non-Javadoc)
372 * @see org.eclipse.swt.events.FocusAdapter#focusLost(org.eclipse.swt.events.FocusEvent)
373 */
374 @Override
375 public void focusLost(FocusEvent e) {
376 super.focusLost(e);
377
378 persistName();
379
380 }
381 }
382
383
384
385
386 }