Project

General

Profile

« Previous | Next » 

Revision 953c844d

Added by Alex Theys almost 12 years ago

AT: committing changes to the TaxEditor Post second round of code review

View differences:

eu.etaxonomy.taxeditor.editor/src/main/java/eu/etaxonomy/taxeditor/editor/view/uses/handler/CreateUseRecordHandler.java
1
package eu.etaxonomy.taxeditor.editor.view.uses.handler;
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
 */
2 9

  
10
package eu.etaxonomy.taxeditor.editor.view.uses.handler;
3 11

  
4 12
import java.util.UUID;
5 13

  
6
import org.eclipse.core.commands.AbstractHandler;
7 14
import org.eclipse.core.commands.ExecutionEvent;
8
import org.eclipse.core.commands.ExecutionException;
9
import org.eclipse.core.commands.common.NotDefinedException;
10
import org.eclipse.jface.viewers.ISelection;
11
import org.eclipse.jface.viewers.IStructuredSelection;
12
import org.eclipse.jface.viewers.ITreeSelection;
13
import org.eclipse.jface.viewers.TreePath;
14
import org.eclipse.ui.IEditorInput;
15
import org.eclipse.ui.IEditorPart;
16
import org.eclipse.ui.IWorkbenchPart;
17
import org.eclipse.ui.forms.editor.FormEditor;
18
import org.eclipse.ui.handlers.HandlerUtil;
19 15

  
20 16
import eu.etaxonomy.cdm.api.service.ITermService;
21 17
import eu.etaxonomy.cdm.model.description.Feature;
22 18
import eu.etaxonomy.cdm.model.description.TaxonDescription;
23 19
import eu.etaxonomy.cdm.model.taxon.Taxon;
24 20
import eu.etaxonomy.taxeditor.editor.EditorUtil;
25
import eu.etaxonomy.taxeditor.editor.TaxonEditorInput;
26 21
import eu.etaxonomy.taxeditor.editor.view.descriptive.handler.CreateDescriptionElementHandler;
27 22
import eu.etaxonomy.taxeditor.editor.view.uses.operation.CreateUseRecordOperation;
28 23
import eu.etaxonomy.taxeditor.operation.AbstractPostOperation;
......
30 25
import eu.etaxonomy.taxeditor.store.CdmStore;
31 26

  
32 27
/**
33
 * The context manager mediates context start/stop and workbench shutdowns to all registered listeners.
34
 *
28
 * CreateUseRecordHandler Class
35 29
 * @author a.theys	
36 30
 * @created mar 13, 2012
37 31
 * @version 1.0
38 32
 */
39 33
public class CreateUseRecordHandler extends CreateDescriptionElementHandler {
34
	/** {@inheritDoc} */
40 35
	@Override
41
	public Object execute(ExecutionEvent event) throws ExecutionException {
42
		IWorkbenchPart part = HandlerUtil.getActivePart(event);
43
		IPostOperationEnabled postOperationEnabled = (part instanceof IPostOperationEnabled) ? (IPostOperationEnabled) part
44
				: null;
45

  
46
		IEditorPart editor = HandlerUtil.getActiveEditor(event);
47
		if (editor instanceof FormEditor) {
48
			editor = ((FormEditor) editor).getActiveEditor();
49
		}
50
		IEditorInput input = editor.getEditorInput();
51
		if (input instanceof TaxonEditorInput) {
52
			Taxon taxon = ((TaxonEditorInput) input).getTaxon();
53

  
54
			TaxonDescription description = null;
55

  
56
			ISelection selection = HandlerUtil.getActiveMenuSelection(event);
57
			if (selection instanceof ITreeSelection) {
58
				TreePath[] paths = ((ITreeSelection) selection).getPaths();
59
				Object firstSegment = paths[0].getFirstSegment();
60
				if (firstSegment instanceof TaxonDescription) {
61
					description = (TaxonDescription) firstSegment;
62
				}
63
			}else if (selection instanceof IStructuredSelection) {
64
				Object selectedElement = ((IStructuredSelection) selection)
65
						.getFirstElement();
66
				if (selectedElement instanceof TaxonDescription){
67
					description = (TaxonDescription) selectedElement;
68
				}
69
			} 
70

  
71
			if (description != null) {
72
				AbstractPostOperation operation = null;
73
				try {
74
					//Use Record Feature retrieval below
75
					//Feature useRecordFeature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
76
					//The code below retrieves the feature "Uses" as a work around
77
					//Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("ba773d5b-ab01-47fb-94cf-d241fcdd02ff"));
78
					Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
79
					feature.setSupportsCategoricalData(true);
80
					operation = new CreateUseRecordOperation(event
81
							.getCommand().getName(),
82
							EditorUtil.getUndoContext(), taxon,
83
							description, feature, postOperationEnabled);
84
					
85
					EditorUtil.executeOperation(operation);
86
				} catch (NotDefinedException e) {
87
					EditorUtil.warn(getClass(), "Command name not set");
88
				}
89
			} else {
90
				EditorUtil.error(getClass(), new IllegalArgumentException("Could not determine the taxon description"));
91
				return null;
92
			}
93
		}
94
		return null;
95

  
36
	protected AbstractPostOperation operationCreationInstance(String label, ExecutionEvent event, Taxon taxon, TaxonDescription description, IPostOperationEnabled postOperationEnabled) {
37
		//Use Record Feature retrieval below
38
		Feature feature = (Feature) CdmStore.getService(ITermService.class).find(UUID.fromString("8125a59d-b4d5-4485-89ea-67306297b599"));
39
		feature.setSupportsCategoricalData(true);
40
		return new CreateUseRecordOperation(label,
41
					EditorUtil.getUndoContext(), taxon,
42
					description, feature, postOperationEnabled);
96 43
	}
44
	
45
	
46
	
97 47
}

Also available in: Unified diff