Project

General

Profile

Download (6.26 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.l10n.Messages;
39
import eu.etaxonomy.taxeditor.editor.name.TaxonNameEditor;
40
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionElementsOperation;
41
import eu.etaxonomy.taxeditor.model.AbstractUtility;
42
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
43
import eu.etaxonomy.taxeditor.model.MessagingUtils;
44
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
45
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
46
import eu.etaxonomy.taxeditor.store.CdmStore;
47
import eu.etaxonomy.taxeditor.ui.dialog.selection.TaxonNodeSelectionDialog;
48

    
49

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

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

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

    
68
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
69

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

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

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

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

    
103
			    }
104
			}
105
			TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
106
					editor.getConversationHolder(),
107
					Messages.MoveDescriptionElementsHandler_CHOOSE_ACC_TAXON,
108
					excludeTaxa,
109
					null, classification
110
					);
111
			if (newAcceptedTaxonNode != null){
112
    			Taxon targetTaxon = newAcceptedTaxonNode.getTaxon();
113

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

    
120
    			String moveMessage = String.format(Messages.MoveDescriptionElementsHandler_ELEMENTS_MOVED, EditorUtil.getActiveMultiPageTaxonEditor().getTaxon());
121

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

    
127
    				AbstractUtility.executeOperation(operation);
128

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

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

    
137
		return null;
138
	}
139

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

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

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

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

    
166
		});
167

    
168

    
169
		return true;
170
	}
171

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

    
177
}
(6-6/8)