Project

General

Profile

Download (6.13 KB) Statistics
| Branch: | Tag: | Revision:
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.view.descriptive.handler;
11

    
12
import java.util.ArrayList;
13
import java.util.List;
14
import java.util.UUID;
15

    
16
import org.eclipse.core.commands.AbstractHandler;
17
import org.eclipse.core.commands.ExecutionEvent;
18
import org.eclipse.core.commands.ExecutionException;
19
import org.eclipse.core.commands.common.NotDefinedException;
20
import org.eclipse.jface.viewers.ISelection;
21
import org.eclipse.jface.viewers.IStructuredSelection;
22
import org.eclipse.swt.widgets.Display;
23
import org.eclipse.ui.PartInitException;
24
import org.eclipse.ui.handlers.HandlerUtil;
25

    
26
import eu.etaxonomy.cdm.api.service.IDescriptionService;
27
import eu.etaxonomy.cdm.hibernate.HibernateProxyHelper;
28
import eu.etaxonomy.cdm.model.common.CdmBase;
29
import eu.etaxonomy.cdm.model.description.DescriptionBase;
30
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
31
import eu.etaxonomy.cdm.model.description.TaxonDescription;
32
import eu.etaxonomy.cdm.model.taxon.Classification;
33
import eu.etaxonomy.cdm.model.taxon.Taxon;
34
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
35
import eu.etaxonomy.taxeditor.editor.EditorUtil;
36
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
37
import eu.etaxonomy.taxeditor.editor.Page;
38
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
39
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
40
import eu.etaxonomy.taxeditor.model.AbstractUtility;
41
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
42
import eu.etaxonomy.taxeditor.model.MessagingUtils;
43
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
44
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
45
import eu.etaxonomy.taxeditor.store.CdmStore;
46
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
47

    
48

    
49
/**
50
 * @author n.hoffmann
51
 * @created Feb 8, 2011
52
 * @version 1.0
53
 */
54
public class MoveDescriptionElementsHandler extends AbstractHandler implements IPostOperationEnabled{
55
	private UUID newAcceptedTaxonNodeUuid;
56
	private TaxonNameEditor editor;
57

    
58
	@Override
59
	public Object execute(ExecutionEvent event) throws ExecutionException {
60

    
61
//		ConversationHolder conversation = CdmStore.createConversation();
62
		editor = (TaxonNameEditor) EditorUtil.getActiveEditorPage(Page.NAME);
63
		ISelection selection = HandlerUtil.getCurrentSelection(event);
64
		Taxon actualTaxon= null;
65
		if(selection instanceof IStructuredSelection){
66

    
67
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
68

    
69
			List<DescriptionElementBase> elements = new ArrayList<DescriptionElementBase>();
70

    
71
			for(Object element : structuredSelection.toArray()){
72
				if(element instanceof DescriptionElementBase){
73
					UUID uuid = ((DescriptionElementBase) element).getUuid();
74

    
75
					elements.add(CdmStore.getService(IDescriptionService.class).loadDescriptionElement(uuid, null));
76
				} else if(element instanceof FeatureNodeContainer){
77
					for(DescriptionElementBase de : ((FeatureNodeContainer)element).getDescriptionElements()){
78
						elements.add(
79
								CdmStore.getService(IDescriptionService.class).loadDescriptionElement(de.getUuid(), null)
80
							);
81
					}
82
				}
83
			}
84

    
85
			if(elements.size() == 0){
86
				return null;
87
			}
88
			DescriptionBase description = elements.get(0).getInDescription();
89
			List<UUID> excludeTaxa = new ArrayList<UUID>();
90
			if (description!=null && description.isInstanceOf(TaxonDescription.class)){
91
				TaxonDescription taxonDescription = HibernateProxyHelper.deproxy(description, TaxonDescription.class);
92
				actualTaxon = taxonDescription.getTaxon();
93
				excludeTaxa.add(actualTaxon.getUuid());
94
			}
95
			Classification classification = null;
96
			TaxonNode actualNode = null;
97
			if (actualTaxon != null){
98
			    if (!actualTaxon.getTaxonNodes().isEmpty() && actualTaxon.getTaxonNodes().size() ==1){
99
			        actualNode = actualTaxon.getTaxonNodes().iterator().next();
100
			        classification = actualNode.getClassification();
101

    
102
			    }
103
			}
104
			TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
105
					editor.getConversationHolder(),
106
					"Choose the accepted taxon",
107
					excludeTaxa,
108
					null, classification
109
					);
110
			if (newAcceptedTaxonNode != null){
111
    			Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
112

    
113
    			if(targetTaxon == null){
114
    				// canceled
115
    				return null;
116
    			}
117
    			newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
118

    
119
    			String moveMessage = String.format("Elements moved from %s", EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
120

    
121
    			try {
122
    				AbstractPostOperation operation = new MoveDescriptionElementsOperation(
123
    						event.getCommand().getName(), EditorUtil.getUndoContext(),
124
    						targetTaxon.getUuid(), moveMessage, elements, false, this);
125

    
126
    				AbstractUtility.executeOperation(operation);
127

    
128
    				//CdmStore.getService(ITaxonService.class).saveOrUpdate(targetTaxon);
129

    
130
    			} catch (NotDefinedException e) {
131
    				MessagingUtils.error(getClass(), e);
132
    			}
133
			}
134
		}
135

    
136
		return null;
137
	}
138

    
139
	/** {@inheritDoc} */
140
	@Override
141
    public boolean postOperation(CdmBase objectAffectedByOperation) {
142

    
143
		editor.getConversationHolder().bind();
144
		editor.getConversationHolder().commit(true);
145
		Display.getDefault().asyncExec(new Runnable(){
146

    
147
            @Override
148
            public void run() {
149
				//AbstractUtility.close(editor.getMultiPageTaxonEditor());
150

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

    
165
		});
166

    
167

    
168
		return true;
169
	}
170

    
171
	@Override
172
	public boolean onComplete() {
173
		return false;
174
	}
175

    
176
}
(6-6/8)