Project

General

Profile

Download (5.48 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
 *
3
 */
4
package eu.etaxonomy.taxeditor.editor.view.descriptive.handler;
5

    
6
import java.util.ArrayList;
7
import java.util.List;
8
import java.util.UUID;
9

    
10
import org.apache.log4j.Logger;
11
import org.eclipse.core.commands.AbstractHandler;
12
import org.eclipse.core.commands.ExecutionEvent;
13
import org.eclipse.core.commands.ExecutionException;
14
import org.eclipse.core.commands.common.NotDefinedException;
15
import org.eclipse.jface.viewers.ISelection;
16
import org.eclipse.jface.viewers.IStructuredSelection;
17
import org.eclipse.swt.widgets.Display;
18
import org.eclipse.ui.PartInitException;
19
import org.eclipse.ui.handlers.HandlerUtil;
20

    
21
import eu.etaxonomy.cdm.api.service.IDescriptionService;
22
import eu.etaxonomy.cdm.model.common.CdmBase;
23
import eu.etaxonomy.cdm.model.description.DescriptionBase;
24
import eu.etaxonomy.cdm.model.description.TaxonDescription;
25
import eu.etaxonomy.cdm.model.taxon.Classification;
26
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
27
import eu.etaxonomy.taxeditor.editor.EditorUtil;
28
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
29
import eu.etaxonomy.taxeditor.editor.Page;
30
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
31
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
32
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionToOtherTaxonOperation;
33
import eu.etaxonomy.taxeditor.model.AbstractUtility;
34
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
35
import eu.etaxonomy.taxeditor.model.MessagingUtils;
36
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
37
import eu.etaxonomy.taxeditor.store.CdmStore;
38
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
39

    
40
/**
41
 * <p>ChangeAcceptedTaxonToSynonymHandler class.</p>
42
 *
43
 * @author a.kohlbecker
44
 * @created Okt. 11, 2013
45
 * @version 1.0
46
 *
47
 */
48
public class MoveDescriptionToOtherTaxonHandler extends AbstractHandler
49
		implements IPostOperationEnabled {
50
	private static final Logger logger = Logger
51
			.getLogger(MoveDescriptionToOtherTaxonHandler.class);
52
	private MoveDescriptionToOtherTaxonOperation operation;
53

    
54
	private UUID newAcceptedTaxonNodeUuid;
55
	private TaxonNameEditor editor;
56

    
57
	/* (non-Javadoc)
58
	 * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
59
	 */
60
	/** {@inheritDoc} */
61
	@Override
62
    public Object execute(ExecutionEvent event) throws ExecutionException {
63

    
64
		ISelection selection = HandlerUtil.getCurrentSelection(event);
65

    
66
		if(selection instanceof IStructuredSelection){
67

    
68
			List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();
69

    
70
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
71

    
72
			for(Object element : structuredSelection.toArray()){
73
				UUID uuid = null;
74
				if (element instanceof FeatureNodeContainer){
75
					uuid = ((FeatureNodeContainer)element).getDescription().getUuid();
76
				}	else if (element instanceof DescriptionBase){
77
					uuid = ((DescriptionBase)element).getUuid();
78
				}
79

    
80
				if (uuid != null){
81
					descriptions.add((TaxonDescription) CdmStore.getService(IDescriptionService.class).load(uuid, null));
82
				}
83
			}
84
			if(descriptions.size() == 0){
85
				return null;
86
			}
87

    
88
			// Choose the target taxon
89
			List<UUID> excludeTaxa = new ArrayList<UUID>();
90
			editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
91
			excludeTaxa.add(descriptions.get(0).getTaxon().getUuid());
92

    
93
			//get current taxon node
94
			TaxonNode node = null;
95
			Classification classification = null;
96
			MultiPageTaxonEditor taxonEditor = EditorUtil.getActiveMultiPageTaxonEditor();
97
            if(taxonEditor!=null){
98
                node = ((TaxonEditorInput) taxonEditor.getEditorInput()).getTaxonNode();
99
                classification = node.getClassification();
100
            }
101
			TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
102
					editor.getConversationHolder(),
103
					"Choose the accepted taxon",
104
					excludeTaxa,
105
					node,
106
					classification);
107

    
108
			if (newAcceptedTaxonNode == null) {
109
				return null;
110
			}
111

    
112
			newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
113

    
114
			try {
115
				for(TaxonDescription description : descriptions){
116
					operation = new MoveDescriptionToOtherTaxonOperation(event.getCommand().getName(),
117
											editor.getUndoContext(), description, newAcceptedTaxonNode, this, editor);
118
					AbstractUtility.executeOperation(operation);
119
				}
120

    
121
			} catch (NotDefinedException e) {
122
				logger.warn("Command name not set");
123
			}
124
		}
125

    
126
		return null;
127

    
128
	}
129

    
130
	/* (non-Javadoc)
131
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
132
	 */
133
	/** {@inheritDoc} */
134
	@Override
135
    public boolean postOperation(CdmBase objectAffectedByOperation) {
136
		Display.getDefault().asyncExec(new Runnable(){
137

    
138
			@Override
139
            public void run() {
140
				AbstractUtility.close(editor.getMultiPageTaxonEditor());
141

    
142
				try {
143
					MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
144
					if(possibleOpenEditor != null){
145
						AbstractUtility.close(possibleOpenEditor);
146
					}
147
					EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
148
				} catch (PartInitException e) {
149
					MessagingUtils.error(this.getClass(), e);
150
					throw new RuntimeException(e);
151
				} catch (Exception e) {
152
					MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
153
				}
154
			}
155

    
156
		});
157

    
158

    
159
		return true;
160
	}
161

    
162
	/**
163
	 * <p>onComplete</p>
164
	 *
165
	 * @return a boolean.
166
	 */
167
	@Override
168
    public boolean onComplete() {
169
		// TODO Auto-generated method stub
170
		return false;
171
	}
172
}
(7-7/8)