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.handler;
|
11
|
|
12
|
import java.util.Set;
|
13
|
|
14
|
import org.apache.log4j.Logger;
|
15
|
import org.eclipse.core.commands.AbstractHandler;
|
16
|
import org.eclipse.core.commands.ExecutionEvent;
|
17
|
import org.eclipse.core.commands.ExecutionException;
|
18
|
import org.eclipse.jface.dialogs.MessageDialog;
|
19
|
import org.eclipse.ui.handlers.HandlerUtil;
|
20
|
|
21
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
22
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
23
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
24
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
|
25
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
26
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
27
|
import eu.etaxonomy.taxeditor.editor.FreeTextElementFactory;
|
28
|
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
|
29
|
import eu.etaxonomy.taxeditor.editor.name.IterableSynonymyList;
|
30
|
|
31
|
/**
|
32
|
* @author n.hoffmann
|
33
|
* @created 23.03.2009
|
34
|
* @version 1.0
|
35
|
*/
|
36
|
public class SaveTaxonHandler extends AbstractHandler {
|
37
|
private static final Logger logger = Logger
|
38
|
.getLogger(SaveTaxonHandler.class);
|
39
|
|
40
|
/* (non-Javadoc)
|
41
|
* @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
|
42
|
*/
|
43
|
public Object execute(ExecutionEvent event) throws ExecutionException {
|
44
|
|
45
|
MultiPageTaxonEditor editor = EditorUtil.getActiveEditor();
|
46
|
// check for non zero length of taxon name
|
47
|
if(isNameZeroLength(editor.getTaxon().getName())){
|
48
|
MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "No Name Specified", "An attempt was made to save a taxon with " +
|
49
|
"an empty name. Operation was cancelled.");
|
50
|
return null;
|
51
|
}
|
52
|
|
53
|
if (!areRelatedNamesNonZeroLength(editor.getTaxon())) {
|
54
|
MessageDialog.openWarning(HandlerUtil.getActiveShell(event), "Zero-Length Name", "One of this taxon's relationships " +
|
55
|
"has a zero-length name. Operation was cancelled.");
|
56
|
return null;
|
57
|
}
|
58
|
|
59
|
return EditorUtil.getActivePage().saveEditor(editor, false);
|
60
|
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* @param name
|
65
|
* @return
|
66
|
*/
|
67
|
private boolean isNameZeroLength(TaxonNameBase name) {
|
68
|
if (name == null) {
|
69
|
return true;
|
70
|
}
|
71
|
return (name.getFullTitleCache().length() == 0);
|
72
|
}
|
73
|
|
74
|
/**
|
75
|
* @param taxon
|
76
|
* @return
|
77
|
*/
|
78
|
private boolean areRelatedNamesNonZeroLength(Taxon taxon) {
|
79
|
for (TaxonBase taxonBase : new IterableSynonymyList(taxon)) {
|
80
|
if (isNameZeroLength(taxonBase.getName()) ) {
|
81
|
return false;
|
82
|
}
|
83
|
}
|
84
|
Set<TaxonRelationship> taxonRelations = taxon.getTaxonRelations();
|
85
|
for (TaxonRelationship relationship : taxonRelations) {
|
86
|
if (isNameZeroLength(relationship.getToTaxon().getName()) ||
|
87
|
isNameZeroLength(relationship.getFromTaxon().getName())) {
|
88
|
return false;
|
89
|
}
|
90
|
}
|
91
|
return true;
|
92
|
}
|
93
|
}
|