Project

General

Profile

Download (5.65 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.session.ICdmEntitySessionEnabled;
38
import eu.etaxonomy.taxeditor.store.CdmStore;
39
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
40

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

    
55
	private UUID newAcceptedTaxonNodeUuid;
56
	private TaxonNameEditor editor;
57

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

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

    
67
		if(selection instanceof IStructuredSelection){
68

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

    
71
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
72

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

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

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

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

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

    
113
			newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
114

    
115
			try {
116
				for(TaxonDescription description : descriptions){
117
					operation = new MoveDescriptionToOtherTaxonOperation(event.getCommand().getName(),
118
											editor.getUndoContext(),
119
											description,
120
											newAcceptedTaxonNode,
121
											this,
122
											editor,
123
											(ICdmEntitySessionEnabled)editor.getEditorInput());
124
					AbstractUtility.executeOperation(operation);
125
				}
126

    
127
			} catch (NotDefinedException e) {
128
				logger.warn("Command name not set");
129
			}
130
		}
131

    
132
		return null;
133

    
134
	}
135

    
136
	/* (non-Javadoc)
137
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
138
	 */
139
	/** {@inheritDoc} */
140
	@Override
141
    public boolean postOperation(CdmBase objectAffectedByOperation) {
142
		Display.getDefault().asyncExec(new Runnable(){
143

    
144
			@Override
145
            public void run() {
146
				AbstractUtility.close(editor.getMultiPageTaxonEditor());
147

    
148
				try {
149
					MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
150
					if(possibleOpenEditor != null){
151
						AbstractUtility.close(possibleOpenEditor);
152
					}
153
					EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
154
				} catch (PartInitException e) {
155
					MessagingUtils.error(this.getClass(), e);
156
					throw new RuntimeException(e);
157
				} catch (Exception e) {
158
					MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
159
				}
160
			}
161

    
162
		});
163

    
164

    
165
		return true;
166
	}
167

    
168
	/**
169
	 * <p>onComplete</p>
170
	 *
171
	 * @return a boolean.
172
	 */
173
	@Override
174
    public boolean onComplete() {
175
		// TODO Auto-generated method stub
176
		return false;
177
	}
178
}
(7-7/8)