Project

General

Profile

Download (6.05 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.l10n.Messages;
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 MultiPageTaxonEditor 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
		editor = EditorUtil.getActiveMultiPageTaxonEditor();
67
		if (this.editor.isDirty()){
68
		    boolean proceed = MessageDialog.openQuestion(null,
69
	                   Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES, Messages.MoveDescriptionToOtherTaxonHandler_SAVE_CHANGES_MESSAGE);
70
	        if (proceed) {
71
	            editor.doSave(EditorUtil.getMonitor());
72
	        } else {
73
	            return null;
74
	        }
75

    
76
		}
77

    
78

    
79
		if(selection instanceof IStructuredSelection){
80

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

    
83
			IStructuredSelection structuredSelection = (IStructuredSelection) selection;
84

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

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

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

    
104

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

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

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

    
126
			newAcceptedTaxonNodeUuid = newAcceptedTaxonNode.getUuid();
127

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

    
140
			} catch (NotDefinedException e) {
141
				logger.warn("Command name not set"); //$NON-NLS-1$
142
			}
143
		}
144

    
145
		return null;
146

    
147
	}
148

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

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

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

    
175
		});
176

    
177

    
178
		return true;
179
	}
180

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