b06934cee379257323c946611f94ae01bcdde875
[taxeditor.git] / eclipseprojects / eu.etaxonomy.taxeditor / src / 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.beans.PropertyChangeEvent;
13 import java.beans.PropertyChangeListener;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.commands.operations.IUndoContext;
17 import org.eclipse.core.commands.operations.IUndoableOperation;
18 import org.eclipse.core.runtime.Assert;
19 import org.eclipse.jface.action.Action;
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.StyledText;
22 import org.eclipse.swt.events.FocusAdapter;
23 import org.eclipse.swt.events.FocusEvent;
24 import org.eclipse.swt.events.KeyAdapter;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.graphics.Color;
27 import org.eclipse.swt.graphics.Font;
28 import org.eclipse.swt.graphics.Image;
29 import org.eclipse.swt.widgets.Composite;
30 import org.eclipse.swt.widgets.Label;
31 import org.eclipse.ui.forms.IManagedForm;
32 import org.eclipse.ui.forms.widgets.TableWrapData;
33 import org.eclipse.ui.forms.widgets.TableWrapLayout;
34 import org.eclipse.ui.views.properties.IPropertySource;
35
36 import eu.etaxonomy.cdm.model.name.BotanicalName;
37 import eu.etaxonomy.cdm.model.name.NonViralName;
38 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
39 import eu.etaxonomy.cdm.model.name.ZoologicalName;
40 import eu.etaxonomy.cdm.model.reference.StrictReferenceBase;
41 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42 import eu.etaxonomy.taxeditor.ITaxEditorConstants;
43 import eu.etaxonomy.taxeditor.TaxEditorPlugin;
44 import eu.etaxonomy.taxeditor.controller.EditorController;
45 import eu.etaxonomy.taxeditor.controller.GlobalController;
46 import eu.etaxonomy.taxeditor.editor.ContextMenu;
47 import eu.etaxonomy.taxeditor.editor.GroupedComposite;
48 import eu.etaxonomy.taxeditor.editor.LineBreakListener;
49 import eu.etaxonomy.taxeditor.editor.ParseListener;
50 import eu.etaxonomy.taxeditor.model.CdmUtil;
51 import eu.etaxonomy.taxeditor.operations.name.CreateSynonymInNewGroupOperation;
52 import eu.etaxonomy.taxeditor.propertysheet.name.BotanicalNamePropertySource;
53 import eu.etaxonomy.taxeditor.propertysheet.name.NonViralNamePropertySource;
54 import eu.etaxonomy.taxeditor.propertysheet.name.ZoologicalNamePropertySource;
55
56 /**
57 * Formats an <code>GroupedComposite</code> to display <code>TaxonNameBase</code> elements
58 * in a <code>NameViewer</code>.
59 *
60 * @author p.ciardelli
61 * @created 02.06.2008
62 * @version 1.0
63 */
64 public abstract class NameComposite extends GroupedComposite {
65 private static final Logger logger = Logger.getLogger(NameComposite.class);
66
67 /**
68 * ************ COMPOSITE TYPES ************
69 */
70 public String compositeType;
71 public static final String ACCEPTED_TAXON = "accepted_name_composite";
72 public static final String HOMOTYPIC_SYNONYM = "homotypic_name_composite";
73 public static final String HETEROTYPIC_SYNONYM = "heterotypic_name_composite";
74 public static final String MISAPPLIED_NAME = "misappliedname_name_composite";
75 public static final String CONCEPTRELATION = "concept_name_comcposite";
76
77 /**
78 * ************ INDENTATIONS ************
79 */
80 public static final int ACCEPTED_INDENT = 0;
81 public static final int SYNONYM_INDENT = 15;
82 public static final int MISAPPLIEDNAME_INDENT = 15;
83 public static final int CONCEPT_INDENT = 15;
84
85 /**
86 * ************ FONTS ************
87 */
88 public static final Font ACCEPTED_FONT = TaxEditorPlugin.getDefault()
89 .getFont(ITaxEditorConstants.ACCEPTED_TAXON_FONT);
90 public static final Font SYNONYM_FONT = TaxEditorPlugin.getDefault()
91 .getFont(ITaxEditorConstants.SYNONYM_FONT);
92 public static final Font MISAPPLIEDNAME_FONT = TaxEditorPlugin.getDefault()
93 .getFont(ITaxEditorConstants.MISAPPLIEDNAME_FONT);
94 public static final Font CONCEPT_FONT = TaxEditorPlugin.getDefault()
95 .getFont(ITaxEditorConstants.CONCEPT_FONT);
96
97 /**
98 * ************ ICONS ************
99 */
100 public static final Image ACCEPTED_ICON = TaxEditorPlugin.getDefault()
101 .getImage(ITaxEditorConstants.BLACK_SQUARE_ICON);
102 public static final Image HOMOTYPIC_SYNONYM_ICON = TaxEditorPlugin
103 .getDefault().getImage(ITaxEditorConstants.HOMOTYPIC_SYN_ICON);
104 public static final Image HOMOTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = TaxEditorPlugin
105 .getDefault().getImage(
106 ITaxEditorConstants.HOMOTYPIC_SYN_ORIGINAL_ICON);
107 public static final Image HETEROTYPIC_SYNONYM_ICON = TaxEditorPlugin
108 .getDefault().getImage(ITaxEditorConstants.HETEROTYPIC_SYN_ICON);
109 public static final Image HETEROTYPIC_SYNONYM_ORIGINAL_COMBINATION_ICON = TaxEditorPlugin
110 .getDefault().getImage(
111 ITaxEditorConstants.HETEROTYPIC_SYN_ORIGINAL_ICON);
112 public static final Image MISAPPLIEDNAME_ICON = TaxEditorPlugin
113 .getDefault().getImage(ITaxEditorConstants.MISAPPLIED_NAME_ICON);
114 public static final Image CONCEPT_ICON = TaxEditorPlugin
115 .getDefault().getImage(ITaxEditorConstants.CONCEPT_ICON);
116 public static final Image AUTONYM_ICON = TaxEditorPlugin.getDefault()
117 .getImage(ITaxEditorConstants.AUTONYM_ICON);
118 public static final Image BASIONYM_ICON = TaxEditorPlugin.getDefault()
119 .getImage(ITaxEditorConstants.BASIONYM_ICON);
120 public static final Image MOVE = TaxEditorPlugin.getDefault().getImage(
121 ITaxEditorConstants.MOVE_ICON);
122
123 /**
124 * ************ TRANSFORMATIONS ************
125 */
126 public static final String ADD_GROUP_BASIONYM = "add_group_basionym";
127 public static final String REMOVE_GROUP_BASIONYM = "remove_group_basionym";
128
129 /**
130 * ************ MENU ACTIONS ************
131 */
132 public Action CHANGE_TAXON_TO_SYNONYM_ACTION;
133
134 private static final String EMPTY_NAME_PROMPT = "Click to add name";
135
136 /**
137 * Used to turn parser on and off.
138 *
139 * @see activateParser
140 * @see deactivateParser
141 */
142 private boolean isUseParser = false;
143
144 protected boolean isParsing;
145
146 protected NameViewer nameViewer;
147
148 /**
149 * The constructor for a DescriptionElementComposite. Takes a parent Composite on which to
150 * create itself, and an IManagedForm for Composite life cycle methods, i.e.
151 * drawing borders, creating other Composites, creating line wrap support,
152 * etc.
153 *
154 * @param parent
155 * @param managedForm
156 */
157 public NameComposite(Composite parent, IManagedForm managedForm,
158 String compositeType, TaxonBase taxonBase) {
159 super(parent, managedForm);
160
161 createNameViewer();
162 createBorderSupport();
163 createLineWrapSupport();
164
165 setDraggableControl(nameViewer.getRulerControl());
166
167 // setData(taxonBase);
168
169 createParser();
170 createEmptyViewerPrompt(EMPTY_NAME_PROMPT);
171 setFocus();
172
173 // NOTE: placing this after setFocus() inexplicably solved a strange bug where if the first action
174 // during a session was to open a new taxon, the empty name prompt didn't work
175 createNameListener(taxonBase.getName());
176 }
177
178 protected void createContent() {
179 setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB));
180 TableWrapLayout layout = new TableWrapLayout();
181 layout.leftMargin = 0;
182 layout.topMargin = 0;
183 layout.bottomMargin = 0;
184 layout.verticalSpacing = 0;
185 setLayout(layout);
186
187 Color groupBackgroundColor = TaxEditorPlugin.getDefault().
188 getColor(ITaxEditorConstants.GROUP_GRAY_BKG_COLOR);
189 setBackground(groupBackgroundColor);
190 }
191
192 protected String getEmptyTextPrompt() {
193 return EMPTY_NAME_PROMPT;
194 }
195
196 /**
197 * Listens for changes to this name's <code>fullTitleCache</code>.
198 *
199 * @param data
200 */
201 private void createNameListener(final TaxonNameBase name) {
202
203 if (name == null) {
204 return;
205 }
206
207 final PropertyChangeListener listener = new PropertyChangeListener() {
208 public void propertyChange(PropertyChangeEvent evt) {
209
210 if (isParsing) {
211 return;
212 }
213
214 if (EditorController.isSaving()) {
215 return;
216 }
217
218 deactivateParser();
219 ((NameViewer) getTextViewer()).setText(name.getFullTitleCache());
220 activateParser();
221 }
222 };
223
224 // TODO clean this part up
225 name.addPropertyChangeListener("fullTitleCache", listener);
226 name.addPropertyChangeListener("nomenclaturalMicroReference", listener);
227
228 // name.addPropertyChangeListener(ITaxEditorConstants.REFRESH_NAMEVIEWER, listener);
229
230 StrictReferenceBase reference = (StrictReferenceBase) name.getNomenclaturalReference();
231 if (reference != null) {
232 reference.addPropertyChangeListener("titleCache", listener);
233 }
234 }
235
236 protected void initNameViewer(TaxonBase taxonBase) {
237 String text = CdmUtil.getDisplayNameWithRef(taxonBase);
238 if (text.length() == 0) {
239 initEmptyText();
240 } else {
241 getTextViewer().getTextWidget().setText(text);
242
243 if (getTextViewer() instanceof NameViewer) {
244
245 ((NameViewer) getTextViewer()).setCursorToEOL();
246
247 TaxonNameBase name = taxonBase.getName();
248 if (name != null) {
249
250 // boolean hasProblem = name.getHasProblem();
251 // ((NameViewer) getTextViewer()).setShowError(name);
252
253 calculateErrors();
254 }
255 }
256 }
257 activateParser();
258 }
259
260 private void createParser() {
261 ((NameViewer) getTextViewer()).addParseListener(new ParseListener() {
262
263 @Override
264 public void parse(String text) {
265
266 // Either composite is not yet fully built, or
267 // the property sheet is writing to it
268 if (!isUseParser) {
269 return;
270 }
271
272 // Let others know the parser is active
273 isParsing = true;
274
275 // Get the name from the composite's data field
276 // TaxonBase taxonBase = (TaxonBase) getData();
277 // NonViralName nonViralName = (NonViralName) taxonBase.getName();
278
279 // Parse the name and paint the text field w any errors
280 if (getName() != null) {
281 CdmParserController.parseFullReference((NonViralName) getName(), text);
282 // ((NameViewer) getTextViewer()).setShowError(nonViralName);
283 }
284
285 // Any entry of text means the taxon has been changed
286 setDirty(true);
287
288 // The parser is no longer active
289 isParsing = false;
290
291 // Manually refresh the property sheet to reflect changes
292 setSelection();
293
294 // Show any errors in the name viewer
295 calculateErrors();
296 }
297 });
298 }
299
300 public void activateParser() {
301 isUseParser = true;
302 }
303
304 public void deactivateParser() {
305 isUseParser = false;
306 }
307
308 protected abstract TaxonNameBase getName();
309
310 protected void calculateErrors() {
311
312 nameViewer.clearErrors();
313
314 nameViewer.setShowParsingError(getName());
315
316 }
317
318 private NameViewer createNameViewer() {
319
320 nameViewer = new NameViewer(this);
321
322 StyledText styledText = nameViewer.getTextWidget();
323 // nameViewer.setLineBreakListener(new LineBreakListener() {
324
325 setTextViewer(nameViewer);
326
327 nameViewer.addLineBreakListener(new LineBreakListener() {
328
329 @Override
330 public void handleSplitText(String text) {
331
332 // Create a synonym in a new homotypic group using text as name
333 IUndoContext undoContext = EditorController.getUndoContext(taxon);
334 // IUndoableOperation operation = new CreateSynonymInNewGroupOperation
335 setOperation(new CreateSynonymInNewGroupOperation
336 ("new heterotypic synonym", undoContext, taxon, text)); //$NON-NLS-1$
337
338 // GlobalController.executeOperation(operation);
339
340 // getTextViewer().removeLineBreakListener(this);
341 }
342
343 });
344
345 styledText.addKeyListener(new KeyAdapter(){
346
347 @Override
348 public void keyReleased(KeyEvent e) {
349 if (operation != null) {
350 GlobalController.executeOperation(operation);
351 operation = null;
352 }
353 }
354
355 });
356
357
358 // Following collides with
359 // NameViewer line 100: ruler.getControl().moveAbove(getTextWidget());
360
361 // styledText.addFocusListener(new FocusAdapter() {
362 // public void focusGained(FocusEvent e) {
363 // setFocus();
364 // }
365 // });
366
367 // createLineWrapSupport(nameViewer);
368
369
370 return nameViewer;
371 }
372
373 IUndoableOperation operation;
374
375 private Label nonEditableInfo;
376
377 protected void setOperation(IUndoableOperation operation) {
378 this.operation = operation;
379 }
380
381 public void setText(String text) {
382 Assert.isNotNull(getTextViewer(),
383 "Cannot set text for a TextViewer that has not yet been initialized.");
384 Assert.isNotNull(getTextViewer().getDocument(),
385 "Cannot set text for a TextViewer whose Document has not yet been initialized.");
386 getTextViewer().getDocument().set(text);
387 }
388
389 public void setNonEditableInfo(String info) {
390 if (nonEditableInfo == null) {
391 nonEditableInfo = new Label(this, SWT.WRAP);
392 nonEditableInfo.setLayoutData(new TableWrapData(TableWrapData.FILL_GRAB, TableWrapData.TOP));
393 } else {
394 info = nonEditableInfo.getText() + ", " + info;
395 }
396 nonEditableInfo.setText(info.toUpperCase());
397 }
398
399 public NameViewer getTextViewer() {
400 return this.nameViewer;
401 }
402
403 public void setTextViewer(NameViewer textViewer) {
404 this.nameViewer = textViewer;
405 }
406
407 public void createBorderSupport() {
408 super.createBorderSupport();
409 if (nameViewer != null) {
410 borderDecorator.setBorderedComposite(nameViewer.getTextWidget());
411 }
412 }
413
414 public void setIcon(Image icon) {
415 // super.setIcon(icon);
416 if (nameViewer != null) {
417 nameViewer.setIcon(icon);
418 }
419 }
420
421 protected IPropertySource getPropertySourceByName(TaxonNameBase name) {
422 if (name == null) {
423 return null;
424 }
425
426 if (name.getClass() == BotanicalName.class) {
427 return new BotanicalNamePropertySource((BotanicalName) name);
428 }
429 if (name.getClass() == ZoologicalName.class) {
430 return new ZoologicalNamePropertySource((ZoologicalName) name);
431 }
432 if (name instanceof NonViralName) {
433 return new NonViralNamePropertySource((NonViralName) name);
434 }
435
436 return null;
437 }
438
439 protected ContextMenu createContextMenu() {
440 if (nameViewer != null) {
441 ContextMenu contextMenu = new ContextMenu(nameViewer.getRulerControl());
442 nameViewer.getTextWidget().setMenu(contextMenu.getMenu());
443 return contextMenu;
444 } else {
445 logger.warn("Can't create menu because nameViewer has not been initalized.");
446 return null;
447 }
448 }
449 }