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
;
16 import org
.apache
.log4j
.Logger
;
17 import org
.eclipse
.core
.commands
.operations
.IUndoContext
;
18 import org
.eclipse
.core
.commands
.operations
.IUndoableOperation
;
19 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
20 import org
.eclipse
.swt
.dnd
.DND
;
21 import org
.eclipse
.swt
.dnd
.DropTarget
;
22 import org
.eclipse
.swt
.dnd
.DropTargetAdapter
;
23 import org
.eclipse
.swt
.dnd
.DropTargetEvent
;
24 import org
.eclipse
.swt
.dnd
.Transfer
;
25 import org
.eclipse
.swt
.widgets
.Composite
;
26 import org
.eclipse
.swt
.widgets
.Event
;
27 import org
.eclipse
.swt
.widgets
.Listener
;
28 import org
.eclipse
.ui
.IEditorInput
;
29 import org
.eclipse
.ui
.IEditorSite
;
30 import org
.eclipse
.ui
.PartInitException
;
32 import eu
.etaxonomy
.cdm
.model
.name
.HomotypicalGroup
;
33 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
34 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
35 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
36 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationship
;
37 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonRelationshipType
;
38 import eu
.etaxonomy
.taxeditor
.ITaxEditorConstants
;
39 import eu
.etaxonomy
.taxeditor
.actions
.WidgetTransfer
;
40 import eu
.etaxonomy
.taxeditor
.controller
.EditorController
;
41 import eu
.etaxonomy
.taxeditor
.controller
.GlobalController
;
42 import eu
.etaxonomy
.taxeditor
.editor
.AbstractTaxonEditor
;
43 import eu
.etaxonomy
.taxeditor
.editor
.FreeTextElementFactory
;
44 import eu
.etaxonomy
.taxeditor
.editor
.GroupedComposite
;
45 import eu
.etaxonomy
.taxeditor
.operations
.name
.ChangeHomotypicGroupOperation
;
46 import eu
.etaxonomy
.taxeditor
.operations
.name
.ChangeMisappliedNameToSynonymOperation
;
53 public class TaxonNameEditor
extends AbstractTaxonEditor
54 implements INameEditorCompositeRepository
{
55 private static final Logger logger
= Logger
.getLogger(TaxonNameEditor
.class);
58 * Shared listener that sets dirty state to true
59 * when any registered property changes
61 private PropertyChangeListener taxonChangeListener
= new PropertyChangeListener() {
62 public void propertyChange(PropertyChangeEvent event
) {
63 if (event
.getPropertyName().equals(ITaxEditorConstants
.PROPERTY_SHEET_CHANGE
)) {
64 firePropertyChange(PROP_DIRTY
);
70 public void doSave(IProgressMonitor monitor
) {}
73 public void doSaveAs() {}
76 public void init(IEditorSite site
, IEditorInput input
)
77 throws PartInitException
{
79 super.init(site
, input
);
81 Taxon taxon
= getTaxon();
82 EditorController
.addNameEditor(taxon
, this);
84 // Register listeners for any change in accepted name or set of relations
85 taxon
.getName().addPropertyChangeListener(taxonChangeListener
);
86 taxon
.addPropertyChangeListener(taxonChangeListener
);
90 public boolean isDirty() {
95 public boolean isSaveAsAllowed() {
100 protected void createManagedForm(Composite composite
) {
102 super.createManagedForm(composite
);
104 Taxon taxon
= getTaxon();
106 firstGroupedComposite
= (NameComposite
) FreeTextElementFactory
.getDefault().
107 createAcceptedTaxon(managedForm
, taxon
);
109 // Draw synonym and misapplied name elements
110 for (TaxonBase taxonBase
: new IterableSynonymyList(taxon
)) {
111 if (taxonBase
instanceof Synonym
) {
112 FreeTextElementFactory
.getDefault().createSynonym(taxon
, (Synonym
) taxonBase
);
114 FreeTextElementFactory
.getDefault().createMisappliedName(taxon
, (Taxon
) taxonBase
);
118 // Draw concept relation elements
119 Set
<TaxonRelationship
> taxonRelations
= taxon
.getTaxonRelations();
120 for (TaxonRelationship relationship
: taxonRelations
) {
122 if (relationship
.getType().equals(TaxonRelationshipType
.MISAPPLIED_NAME_FOR()) ||
123 relationship
.getType().equals(TaxonRelationshipType
.TAXONOMICALLY_INCLUDED_IN())) {
127 FreeTextElementFactory
.getDefault().createConcept(taxon
, relationship
);
130 // Listen for names being dragged outside of existing homotypic groups -
131 // user wants to create a new group
132 Transfer
[] types
= new Transfer
[] {WidgetTransfer
.getInstance()};
133 int operations
= DND
.DROP_MOVE
| DND
.DROP_COPY
| DND
.DROP_DEFAULT
;
134 DropTarget target
= new DropTarget(parent
, operations
);
135 target
.setTransfer(types
);
136 target
.addDropListener(new DropTargetAdapter() {
138 public void drop(DropTargetEvent event
) {
140 if (event
.data
instanceof GroupedComposite
) {
141 final GroupedComposite composite
= (GroupedComposite
) event
.data
;
142 if (composite
.getDragSource() != null) {
144 // Execute operations at end of drag event
145 composite
.getDragSource().addListener(DND
.DragEnd
, new Listener() {
147 public void handleEvent(Event event
) {
148 IUndoContext undoContext
= EditorController
.getUndoContext(getTaxon());
149 IUndoableOperation operation
= null;
151 // Synonym being dropped
152 if(composite
instanceof SynonymComposite
){
153 Synonym synonym
= ((SynonymComposite
)composite
).getSynonym();
154 operation
= new ChangeHomotypicGroupOperation
155 ("change type", undoContext
,
156 getTaxon(), synonym
, HomotypicalGroup
.NewInstance());
159 // Misapplied name being dropped
160 if(composite
instanceof MisappliedNameComposite
){
161 Taxon misapplication
= ((MisappliedNameComposite
)composite
).getMisappliedName();
162 operation
= new ChangeMisappliedNameToSynonymOperation
163 ("change misapplied name to synonym", undoContext
,
164 getTaxon(), misapplication
, HomotypicalGroup
.NewInstance());
167 // Execute operation if it's been init'ed
168 if (operation
== null) {
169 logger
.warn("User unsuccessfully tried to drop " + composite
.getClass());
171 GlobalController
.executeOperation(operation
);
188 public Composite
getAcceptedName() {
189 for(Composite c
: getAllComposites()){
190 if(c
instanceof AcceptedNameComposite
){
198 public Composite
getHomotypicGroup(HomotypicalGroup group
) {
199 for(Composite c
: getAllComposites()){
200 if(c
instanceof HomotypicalGroupComposite
){
202 ((HomotypicalGroupComposite
) c
).getGroup())) {
211 public Composite
getMisappliedGroup() {
212 for(Composite c
: getAllComposites()){
213 if(c
instanceof MisappliedGroupComposite
){
221 public Composite
getMisappliedName(Taxon misappliedName
) {
222 for(Composite c
: getAllComposites()){
223 if(c
instanceof MisappliedNameComposite
){
224 if (misappliedName
.equals(
225 ((MisappliedNameComposite
) c
).getMisappliedName())) {
234 public Composite
getSynonym(Synonym synonym
) {
235 for(Composite c
: getAllComposites()){
236 if(c
instanceof SynonymComposite
){
238 (((SynonymComposite
) c
).getSynonym())) {
247 public Composite
getConceptGroup() {
248 for(Composite c
: getAllComposites()){
249 if(c
instanceof ConceptGroupComposite
){
257 public Composite
getConcept(Taxon relatedConcept
) {
258 for(Composite c
: getAllComposites()){
259 if(c
instanceof ConceptComposite
){
260 if (relatedConcept
.equals
261 (((ConceptComposite
) c
).getRelatedTaxon())) {