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
|
import java.util.Set;
|
15
|
|
16
|
import org.apache.log4j.Logger;
|
17
|
import org.eclipse.core.commands.operations.IUndoableOperation;
|
18
|
import org.eclipse.core.runtime.IProgressMonitor;
|
19
|
import org.eclipse.swt.dnd.DND;
|
20
|
import org.eclipse.swt.dnd.DropTarget;
|
21
|
import org.eclipse.swt.dnd.DropTargetAdapter;
|
22
|
import org.eclipse.swt.dnd.DropTargetEvent;
|
23
|
import org.eclipse.swt.dnd.Transfer;
|
24
|
import org.eclipse.swt.widgets.Composite;
|
25
|
import org.eclipse.swt.widgets.Event;
|
26
|
import org.eclipse.swt.widgets.Listener;
|
27
|
import org.eclipse.ui.IEditorInput;
|
28
|
import org.eclipse.ui.IEditorSite;
|
29
|
import org.eclipse.ui.PartInitException;
|
30
|
|
31
|
import eu.etaxonomy.cdm.model.name.HomotypicalGroup;
|
32
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
33
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
34
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
35
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
36
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
37
|
import eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor;
|
38
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
39
|
import eu.etaxonomy.taxeditor.editor.FreeTextElementFactory;
|
40
|
import eu.etaxonomy.taxeditor.editor.GroupedComposite;
|
41
|
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
|
42
|
import eu.etaxonomy.taxeditor.editor.WidgetTransfer;
|
43
|
import eu.etaxonomy.taxeditor.store.model.Resources;
|
44
|
import eu.etaxonomy.taxeditor.store.operations.ChangeConceptToSynonymOperation;
|
45
|
import eu.etaxonomy.taxeditor.store.operations.ChangeHomotypicGroupOperation;
|
46
|
|
47
|
/**
|
48
|
* @author p.ciardelli
|
49
|
* @created 15.05.2008
|
50
|
* @version 1.0
|
51
|
*/
|
52
|
public class TaxonNameEditor extends AbstractTaxonEditor
|
53
|
implements INameEditorCompositeRepository {
|
54
|
private static final Logger logger = Logger.getLogger(TaxonNameEditor.class);
|
55
|
|
56
|
private static final String ID = "eu.etaxonomy.taxeditor.taxonNameEditor";
|
57
|
|
58
|
public TaxonNameEditor(MultiPageTaxonEditor editor){
|
59
|
super(editor);
|
60
|
}
|
61
|
|
62
|
/**
|
63
|
* Shared listener that sets dirty state to true
|
64
|
* when any registered property changes
|
65
|
*/
|
66
|
private PropertyChangeListener taxonChangeListener = new PropertyChangeListener() {
|
67
|
public void propertyChange(PropertyChangeEvent event) {
|
68
|
if (event.getPropertyName().equals(Resources.PROPERTY_SHEET_CHANGE)) {
|
69
|
firePropertyChange(PROP_DIRTY);
|
70
|
}
|
71
|
}
|
72
|
};
|
73
|
|
74
|
@Override
|
75
|
public void doSave(IProgressMonitor monitor) {}
|
76
|
|
77
|
@Override
|
78
|
public void doSaveAs() {}
|
79
|
|
80
|
@Override
|
81
|
public void init(IEditorSite site, IEditorInput input)
|
82
|
throws PartInitException {
|
83
|
|
84
|
super.init(site, input);
|
85
|
|
86
|
// Register listeners for any change in accepted name or set of relations
|
87
|
taxon.getName().addPropertyChangeListener(taxonChangeListener);
|
88
|
taxon.addPropertyChangeListener(taxonChangeListener);
|
89
|
}
|
90
|
|
91
|
@Override
|
92
|
public boolean isDirty() {
|
93
|
return false;
|
94
|
}
|
95
|
|
96
|
@Override
|
97
|
public boolean isSaveAsAllowed() {
|
98
|
return false;
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
protected void createManagedForm(Composite composite) {
|
103
|
|
104
|
super.createManagedForm(composite);
|
105
|
|
106
|
Taxon taxon = getTaxon();
|
107
|
|
108
|
firstGroupedComposite = (NameComposite) FreeTextElementFactory.getDefault().
|
109
|
createAcceptedTaxon(this);
|
110
|
|
111
|
// Draw synonym and misapplied name elements
|
112
|
for (TaxonBase<?> taxonBase : new IterableSynonymyList(taxon)) {
|
113
|
if (taxonBase instanceof Synonym) {
|
114
|
FreeTextElementFactory.getDefault().createSynonym(this, (Synonym) taxonBase);
|
115
|
} else {
|
116
|
FreeTextElementFactory.getDefault().createMisappliedName(this, (Taxon) taxonBase);
|
117
|
}
|
118
|
}
|
119
|
|
120
|
// Draw concept relation elements
|
121
|
Set<TaxonRelationship> taxonRelations = taxon.getTaxonRelations();
|
122
|
for (TaxonRelationship relationship : taxonRelations) {
|
123
|
|
124
|
if (relationship.getType().equals(TaxonRelationshipType.MISAPPLIED_NAME_FOR()) ||
|
125
|
relationship.getType().equals(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN())) {
|
126
|
continue;
|
127
|
}
|
128
|
|
129
|
FreeTextElementFactory.getDefault().createConcept(this, relationship);
|
130
|
}
|
131
|
|
132
|
// Listen for names being dragged outside of existing homotypic groups -
|
133
|
// user wants to create a new group
|
134
|
Transfer[] types = new Transfer[] {WidgetTransfer.getInstance()};
|
135
|
int operations = DND.DROP_MOVE | DND.DROP_COPY | DND.DROP_DEFAULT;
|
136
|
DropTarget target = new DropTarget(parent, operations);
|
137
|
target.setTransfer(types);
|
138
|
target.addDropListener(new DropTargetAdapter() {
|
139
|
|
140
|
public void drop(DropTargetEvent event) {
|
141
|
|
142
|
if (event.data instanceof GroupedComposite) {
|
143
|
final GroupedComposite composite = (GroupedComposite) event.data;
|
144
|
if (composite.getDragSource() != null) {
|
145
|
|
146
|
// Execute operations at end of drag event
|
147
|
composite.getDragSource().addListener(DND.DragEnd, new Listener() {
|
148
|
|
149
|
public void handleEvent(Event event) {
|
150
|
IUndoableOperation operation = null;
|
151
|
|
152
|
// Synonym being dropped
|
153
|
if(composite instanceof SynonymComposite){
|
154
|
Synonym synonym = ((SynonymComposite)composite).getSynonym();
|
155
|
operation = new ChangeHomotypicGroupOperation
|
156
|
("change type", getUndoContext(),
|
157
|
getTaxon(), synonym, HomotypicalGroup.NewInstance(), TaxonNameEditor.this);
|
158
|
}
|
159
|
|
160
|
// Misapplied name being dropped
|
161
|
if(composite instanceof MisappliedNameComposite){
|
162
|
Taxon misapplication = ((MisappliedNameComposite)composite).getMisappliedName();
|
163
|
operation = new ChangeConceptToSynonymOperation
|
164
|
("change misapplied name to synonym", getUndoContext(),
|
165
|
getTaxon(), misapplication, HomotypicalGroup.NewInstance(), TaxonNameEditor.this);
|
166
|
}
|
167
|
|
168
|
// Execute operation if it's been init'ed
|
169
|
if (operation == null) {
|
170
|
logger.warn("User unsuccessfully tried to drop " + composite.getClass());
|
171
|
} else {
|
172
|
EditorUtil.executeOperation(operation);
|
173
|
}
|
174
|
}
|
175
|
});
|
176
|
}
|
177
|
}
|
178
|
|
179
|
|
180
|
}
|
181
|
});
|
182
|
setFocus();
|
183
|
|
184
|
// Redraw composite
|
185
|
composite.layout();
|
186
|
}
|
187
|
|
188
|
|
189
|
public Composite getAcceptedName() {
|
190
|
for(Composite c : getAllComposites()){
|
191
|
if(c instanceof AcceptedNameComposite){
|
192
|
return c;
|
193
|
}
|
194
|
}
|
195
|
return null;
|
196
|
}
|
197
|
|
198
|
|
199
|
public Composite getHomotypicGroup(HomotypicalGroup group) {
|
200
|
for(Composite c : getAllComposites()){
|
201
|
if(c instanceof HomotypicalGroupComposite){
|
202
|
if (group.equals(
|
203
|
((HomotypicalGroupComposite) c).getGroup())) {
|
204
|
return c;
|
205
|
}
|
206
|
}
|
207
|
}
|
208
|
return null;
|
209
|
}
|
210
|
|
211
|
|
212
|
public Composite getMisappliedGroup() {
|
213
|
for(Composite c : getAllComposites()){
|
214
|
if(c instanceof MisappliedGroupComposite){
|
215
|
return c;
|
216
|
}
|
217
|
}
|
218
|
return null;
|
219
|
}
|
220
|
|
221
|
|
222
|
public Composite getMisappliedName(Taxon misappliedName) {
|
223
|
for(Composite c : getAllComposites()){
|
224
|
if(c instanceof MisappliedNameComposite){
|
225
|
if (misappliedName.equals(
|
226
|
((MisappliedNameComposite) c).getMisappliedName())) {
|
227
|
return c;
|
228
|
}
|
229
|
}
|
230
|
}
|
231
|
return null;
|
232
|
}
|
233
|
|
234
|
|
235
|
public Composite getSynonym(Synonym synonym) {
|
236
|
for(Composite c : getAllComposites()){
|
237
|
if(c instanceof SynonymComposite){
|
238
|
if (synonym.equals
|
239
|
(((SynonymComposite) c).getSynonym())) {
|
240
|
return c;
|
241
|
}
|
242
|
}
|
243
|
}
|
244
|
return null;
|
245
|
}
|
246
|
|
247
|
|
248
|
public Composite getConceptGroup() {
|
249
|
for(Composite c : getAllComposites()){
|
250
|
if(c instanceof ConceptGroupComposite){
|
251
|
return c;
|
252
|
}
|
253
|
}
|
254
|
return null;
|
255
|
}
|
256
|
|
257
|
|
258
|
public Composite getConcept(Taxon relatedConcept) {
|
259
|
for(Composite c : getAllComposites()){
|
260
|
if(c instanceof ConceptComposite){
|
261
|
if (relatedConcept.equals
|
262
|
(((ConceptComposite) c).getRelatedTaxon())) {
|
263
|
return c;
|
264
|
}
|
265
|
}
|
266
|
}
|
267
|
return null;
|
268
|
}
|
269
|
|
270
|
/* (non-Javadoc)
|
271
|
* @see eu.etaxonomy.taxeditor.editor.AbstractTaxonEditor#getID()
|
272
|
*/
|
273
|
@Override
|
274
|
public String getID() {
|
275
|
return TaxonNameEditor.ID;
|
276
|
}
|
277
|
}
|