Project

General

Profile

Download (5.92 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.dialogs.MessageDialog;
16
import org.eclipse.jface.viewers.ISelection;
17
import org.eclipse.jface.viewers.IStructuredSelection;
18
import org.eclipse.swt.widgets.Display;
19
import org.eclipse.ui.PartInitException;
20
import org.eclipse.ui.handlers.HandlerUtil;
21

    
22
import eu.etaxonomy.cdm.api.service.IDescriptionService;
23
import eu.etaxonomy.cdm.model.common.CdmBase;
24
import eu.etaxonomy.cdm.model.description.DescriptionBase;
25
import eu.etaxonomy.cdm.model.description.TaxonDescription;
26
import eu.etaxonomy.cdm.model.taxon.Classification;
27
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
28
import eu.etaxonomy.taxeditor.editor.EditorUtil;
29
import eu.etaxonomy.taxeditor.editor.MultiPageTaxonEditor;
30
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
31
import eu.etaxonomy.taxeditor.editor.view.descriptive.operation.MoveDescriptionToOtherTaxonOperation;
32
import eu.etaxonomy.taxeditor.model.AbstractUtility;
33
import eu.etaxonomy.taxeditor.model.FeatureNodeContainer;
34
import eu.etaxonomy.taxeditor.model.MessagingUtils;
35
import eu.etaxonomy.taxeditor.operation.IPostOperationEnabled;
36
import eu.etaxonomy.taxeditor.session.ICdmEntitySessionEnabled;
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 MultiPageTaxonEditor 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
		editor = EditorUtil.getActiveMultiPageTaxonEditor();
66
		if (this.editor.isDirty()){
67
		    boolean proceed = MessageDialog.openQuestion(null,
68
	                   "Save changes", "You have made changes that must be saved before this query can be executed. Would you like to proceed?");
69
	        if (proceed) {
70
	            editor.doSave(EditorUtil.getMonitor());
71
	        } else {
72
	            return null;
73
	        }
74

    
75
		}
76

    
77

    
78
		if(selection instanceof IStructuredSelection){
79

    
80
			List<TaxonDescription> descriptions = new ArrayList<TaxonDescription>();
81

    
82
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
83

    
84
			for(Object element : structuredSelection.toArray()){
85
				UUID uuid = null;
86
				if (element instanceof FeatureNodeContainer){
87
					uuid = ((FeatureNodeContainer)element).getDescription().getUuid();
88
				}	else if (element instanceof DescriptionBase){
89
					uuid = ((DescriptionBase)element).getUuid();
90
				}
91

    
92
				if (uuid != null){
93
					descriptions.add((TaxonDescription) CdmStore.getService(IDescriptionService.class).load(uuid, null));
94
				}
95
			}
96
			if(descriptions.size() == 0){
97
				return null;
98
			}
99

    
100
			// Choose the target taxon
101
			List<UUID> excludeTaxa = new ArrayList<UUID>();
102

    
103

    
104
			excludeTaxa.add(descriptions.get(0).getTaxon().getUuid());
105

    
106
			//get current taxon node
107
			TaxonNode node = null;
108
			Classification classification = null;
109
			MultiPageTaxonEditor taxonEditor = EditorUtil.getActiveMultiPageTaxonEditor();
110
            if(taxonEditor!=null){
111
                node = ((TaxonEditorInput) taxonEditor.getEditorInput()).getTaxonNode();
112
                classification = node.getClassification();
113
            }
114
			TaxonNode newAcceptedTaxonNode = TaxonNodeSelectionDialog.select(HandlerUtil.getActiveShell(event),
115
					editor.getConversationHolder(),
116
					"Choose the accepted taxon",
117
					excludeTaxa,
118
					node,
119
					classification);
120

    
121
			if (newAcceptedTaxonNode == null) {
122
				return null;
123
			}
124

    
125
			newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
126

    
127
			try {
128
				for(TaxonDescription description : descriptions){
129
					operation = new MoveDescriptionToOtherTaxonOperation(event.getCommand().getName(),
130
											editor.getUndoContext(),
131
											description,
132
											newAcceptedTaxonNode,
133
											this,
134
											editor,
135
											(ICdmEntitySessionEnabled)editor.getEditorInput());
136
					AbstractUtility.executeOperation(operation);
137
				}
138

    
139
			} catch (NotDefinedException e) {
140
				logger.warn("Command name not set");
141
			}
142
		}
143

    
144
		return null;
145

    
146
	}
147

    
148
	/* (non-Javadoc)
149
	 * @see eu.etaxonomy.taxeditor.operations.IPostOperationEnabled#postOperation(eu.etaxonomy.cdm.model.common.CdmBase)
150
	 */
151
	/** {@inheritDoc} */
152
	@Override
153
    public boolean postOperation(CdmBase objectAffectedByOperation) {
154
		Display.getDefault().asyncExec(new Runnable(){
155

    
156
			@Override
157
            public void run() {
158
				AbstractUtility.close(editor);
159

    
160
				try {
161
					MultiPageTaxonEditor possibleOpenEditor = (MultiPageTaxonEditor) EditorUtil.findEditorByTaxonNodeUuid(newAcceptedTaxonNodeUuid);
162
					if(possibleOpenEditor != null){
163
						AbstractUtility.close(possibleOpenEditor);
164
					}
165
					EditorUtil.openTaxonNode(newAcceptedTaxonNodeUuid);
166
				} catch (PartInitException e) {
167
					MessagingUtils.error(this.getClass(), e);
168
					throw new RuntimeException(e);
169
				} catch (Exception e) {
170
					MessagingUtils.warningDialog("Could not create Taxon", this, e.getMessage());
171
				}
172
			}
173

    
174
		});
175

    
176

    
177
		return true;
178
	}
179

    
180
	/**
181
	 * <p>onComplete</p>
182
	 *
183
	 * @return a boolean.
184
	 */
185
	@Override
186
    public boolean onComplete() {
187
		// TODO Auto-generated method stub
188
		return false;
189
	}
190
}
(7-7/8)