2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.editor
.name
;
12 import java
.beans
.PropertyChangeEvent
;
13 import java
.beans
.PropertyChangeListener
;
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
;
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
;
57 * Formats an <code>GroupedComposite</code> to display <code>TaxonNameBase</code> elements
58 * in a <code>NameViewer</code>.
64 public abstract class NameComposite
extends GroupedComposite
{
65 private static final Logger logger
= Logger
.getLogger(NameComposite
.class);
68 * ************ COMPOSITE TYPES ************
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";
78 * ************ INDENTATIONS ************
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;
86 * ************ FONTS ************
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
);
98 * ************ ICONS ************
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
);
124 * ************ TRANSFORMATIONS ************
126 public static final String ADD_GROUP_BASIONYM
= "add_group_basionym";
127 public static final String REMOVE_GROUP_BASIONYM
= "remove_group_basionym";
130 * ************ MENU ACTIONS ************
132 public Action CHANGE_TAXON_TO_SYNONYM_ACTION
;
134 private static final String EMPTY_NAME_PROMPT
= "Click to add name";
137 * Used to turn parser on and off.
139 * @see activateParser
140 * @see deactivateParser
142 private boolean isUseParser
= false;
144 protected boolean isParsing
;
147 * The constructor for a DescriptionElementComposite. Takes a parent Composite on which to
148 * create itself, and an IManagedForm for Composite life cycle methods, i.e.
149 * drawing borders, creating other Composites, creating line wrap support,
155 public NameComposite(Composite parent
, IManagedForm managedForm
,
156 String compositeType
, TaxonBase taxonBase
) {
157 super(parent
, managedForm
);
160 createBorderSupport();
161 createLineWrapSupport();
163 setDraggableControl(textViewer
.getRulerControl());
166 createEmptyViewerPrompt(EMPTY_NAME_PROMPT
);
169 // NOTE: placing this after setFocus() inexplicably solved a strange bug where if the first action
170 // during a session was to open a new taxon, the empty name prompt didn't work
171 createNameListener(taxonBase
.getName());
174 protected String
getEmptyTextPrompt() {
175 return EMPTY_NAME_PROMPT
;
179 * Listens for changes to this name's <code>fullTitleCache</code>.
183 private void createNameListener(final TaxonNameBase name
) {
189 final PropertyChangeListener listener
= new PropertyChangeListener() {
190 public void propertyChange(PropertyChangeEvent evt
) {
196 if (EditorController
.isSaving()) {
201 ((NameViewer
) getTextViewer()).setText(name
.getFullTitleCache());
206 // TODO clean this part up
207 name
.addPropertyChangeListener("fullTitleCache", listener
);
208 name
.addPropertyChangeListener("nomenclaturalMicroReference", listener
);
210 // name.addPropertyChangeListener(ITaxEditorConstants.REFRESH_NAMEVIEWER, listener);
212 StrictReferenceBase reference
= (StrictReferenceBase
) name
.getNomenclaturalReference();
213 if (reference
!= null) {
214 reference
.addPropertyChangeListener("titleCache", listener
);
218 protected void initNameViewer(TaxonBase taxonBase
) {
219 String text
= CdmUtil
.getDisplayNameWithRef(taxonBase
);
220 if (text
.length() == 0) {
223 getTextViewer().getTextWidget().setText(text
);
225 if (getTextViewer() instanceof NameViewer
) {
227 ((NameViewer
) getTextViewer()).setCursorToEOL();
229 TaxonNameBase name
= taxonBase
.getName();
238 private void createParser() {
239 ((NameViewer
) getTextViewer()).addParseListener(new ParseListener() {
242 public void parse(String text
) {
244 // Either composite is not yet fully built, or
245 // the property sheet is writing to it
250 // Let others know the parser is active
253 // Parse the name and paint the text field w any errors
254 if (getName() != null) {
255 CdmParserController
.parseFullReference((NonViralName
) getName(), text
);
258 // Any entry of text means the taxon has been changed
261 // The parser is no longer active
264 // Manually refresh the property sheet to reflect changes
267 // Show any errors in the name viewer
273 public void activateParser() {
277 public void deactivateParser() {
281 protected abstract TaxonNameBase
getName();
283 protected void calculateErrors() {
284 textViewer
.clearErrors();
285 textViewer
.setShowParsingError(getName());
288 private NameViewer
createTextViewer() {
290 textViewer
= new NameViewer(this);
292 StyledText styledText
= textViewer
.getTextWidget();
293 // textViewer.setLineBreakListener(new LineBreakListener() {
295 setTextViewer(textViewer
);
297 LineBreakListener lineBreakListener
= new LineBreakListener() {
299 public void handleSplitText(String text
) {
301 // Create a synonym in a new homotypic group using text as name
302 IUndoContext undoContext
= EditorController
.getUndoContext(taxon
);
303 IUndoableOperation operation
= new CreateSynonymInNewGroupOperation
304 ("new heterotypic synonym", undoContext
, taxon
, text
); //$NON-NLS-1$
305 GlobalController
.executeOperation(operation
);
309 styledText
.addVerifyListener(lineBreakListener
);
310 styledText
.addKeyListener(lineBreakListener
);
312 styledText
.addFocusListener(new FocusAdapter() {
313 public void focusGained(FocusEvent e
) {
318 // createLineWrapSupport(textViewer);
323 private Label nonEditableInfo
;
325 public void setText(String text
) {
326 Assert
.isNotNull(getTextViewer(),
327 "Cannot set text for a TextViewer that has not yet been initialized.");
328 Assert
.isNotNull(getTextViewer().getDocument(),
329 "Cannot set text for a TextViewer whose Document has not yet been initialized.");
330 getTextViewer().getDocument().set(text
);
333 public void setNonEditableInfo(String info
) {
334 if (nonEditableInfo
== null) {
335 nonEditableInfo
= new Label(this, SWT
.WRAP
);
336 nonEditableInfo
.setLayoutData(new TableWrapData(TableWrapData
.FILL_GRAB
, TableWrapData
.TOP
));
338 info
= nonEditableInfo
.getText() + ", " + info
;
340 nonEditableInfo
.setText(info
.toUpperCase());
343 public void createBorderSupport() {
344 super.createBorderSupport();
345 if (textViewer
!= null) {
346 borderDecorator
.setBorderedComposite(textViewer
.getTextWidget());
350 protected IPropertySource
getPropertySourceByName(TaxonNameBase name
) {
355 if (name
.getClass() == BotanicalName
.class) {
356 return new BotanicalNamePropertySource((BotanicalName
) name
);
358 if (name
.getClass() == ZoologicalName
.class) {
359 return new ZoologicalNamePropertySource((ZoologicalName
) name
);
361 if (name
instanceof NonViralName
) {
362 return new NonViralNamePropertySource((NonViralName
) name
);
368 protected ContextMenu
createContextMenu() {
369 if (textViewer
!= null) {
370 ContextMenu contextMenu
= new ContextMenu(textViewer
.getRulerControl());
371 textViewer
.getTextWidget().setMenu(contextMenu
.getMenu());
374 logger
.warn("Can't create menu because textViewer has not been initalized.");