1
|
/**
|
2
|
*
|
3
|
*/
|
4
|
package eu.etaxonomy.taxeditor.editor.view.concept.handler;
|
5
|
|
6
|
import java.util.ArrayList;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.List;
|
9
|
|
10
|
import org.eclipse.jface.action.ContributionItem;
|
11
|
import org.eclipse.swt.events.SelectionEvent;
|
12
|
import org.eclipse.swt.events.SelectionListener;
|
13
|
import org.eclipse.swt.widgets.Event;
|
14
|
import org.eclipse.swt.widgets.Menu;
|
15
|
import org.eclipse.swt.widgets.MenuItem;
|
16
|
import org.eclipse.ui.handlers.IHandlerService;
|
17
|
|
18
|
import eu.etaxonomy.cdm.model.common.Language;
|
19
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
20
|
import eu.etaxonomy.taxeditor.editor.EditorUtil;
|
21
|
import eu.etaxonomy.taxeditor.editor.internal.TaxeditorEditorPlugin;
|
22
|
import eu.etaxonomy.taxeditor.model.TaxonRelationshipTypeInverseContainer;
|
23
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
24
|
|
25
|
/**
|
26
|
* <p>Abstract AbstractDynamicConceptRelationMenu class.</p>
|
27
|
*
|
28
|
* @author n.hoffmann
|
29
|
* @created 17.04.2009
|
30
|
* @version 1.0
|
31
|
*/
|
32
|
public abstract class AbstractDynamicConceptRelationMenu extends ContributionItem {
|
33
|
|
34
|
private static List<TaxonRelationshipType> excludeRelationshipTypes = Arrays.asList(new TaxonRelationshipType[]{
|
35
|
TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN(),
|
36
|
TaxonRelationshipType.MISAPPLIED_NAME_FOR(),
|
37
|
TaxonRelationshipType.ALL_RELATIONSHIPS()
|
38
|
});
|
39
|
|
40
|
/*
|
41
|
* (non-Javadoc)
|
42
|
* @see org.eclipse.jface.action.ContributionItem#fill(org.eclipse.swt.widgets.Menu, int)
|
43
|
*/
|
44
|
/** {@inheritDoc} */
|
45
|
@Override
|
46
|
public void fill(Menu menu, int index){
|
47
|
final IHandlerService handlerService = (IHandlerService) TaxeditorEditorPlugin.getDefault().getWorkbench().getService(IHandlerService.class);
|
48
|
|
49
|
for(final TaxonRelationshipTypeInverseContainer container : getRelationshipTypesWithInverses()){
|
50
|
MenuItem menuItem = new MenuItem(menu, -1);
|
51
|
menuItem.setText(formatLabelText(container));
|
52
|
menuItem.setData(container);
|
53
|
menuItem.addSelectionListener(new SelectionListener(){
|
54
|
|
55
|
public void widgetDefaultSelected(SelectionEvent e) {}
|
56
|
|
57
|
public void widgetSelected(SelectionEvent ev) {
|
58
|
Event event = new Event();
|
59
|
event.data = container;
|
60
|
try {
|
61
|
handlerService.executeCommand(getCommandName(), event);
|
62
|
} catch (Exception e) {
|
63
|
EditorUtil.error(getClass(), "Error executing command", e);
|
64
|
}
|
65
|
}
|
66
|
});
|
67
|
}
|
68
|
}
|
69
|
|
70
|
private String formatLabelText(TaxonRelationshipTypeInverseContainer container){
|
71
|
Language language = Language.ENGLISH();
|
72
|
String formattedString = String.format("%s (%s)", container.getLabel(language), container.getAbbreviatedLabel(language));
|
73
|
|
74
|
return formattedString;
|
75
|
}
|
76
|
|
77
|
private List<TaxonRelationshipTypeInverseContainer> getRelationshipTypesWithInverses(){
|
78
|
List<TaxonRelationshipTypeInverseContainer> relationshipTypeInverseContainers = new ArrayList<TaxonRelationshipTypeInverseContainer>();
|
79
|
|
80
|
List<TaxonRelationshipType> relationshipTypes = CdmStore.getTermManager().getPreferredTaxonRelationshipTypes();
|
81
|
|
82
|
relationshipTypes.removeAll(excludeRelationshipTypes);
|
83
|
|
84
|
for (TaxonRelationshipType relationshipType : relationshipTypes){
|
85
|
if(!relationshipType.isSymmetric()){
|
86
|
TaxonRelationshipTypeInverseContainer inverseContainer = new TaxonRelationshipTypeInverseContainer(relationshipType, true);
|
87
|
relationshipTypeInverseContainers.add(inverseContainer);
|
88
|
}
|
89
|
TaxonRelationshipTypeInverseContainer container = new TaxonRelationshipTypeInverseContainer(relationshipType, false);
|
90
|
relationshipTypeInverseContainers.add(container);
|
91
|
}
|
92
|
|
93
|
return relationshipTypeInverseContainers;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* <p>getCommandName</p>
|
98
|
*
|
99
|
* @return a {@link java.lang.String} object.
|
100
|
*/
|
101
|
public abstract String getCommandName();
|
102
|
|
103
|
}
|