Project

General

Profile

Download (5.79 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2009 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.definedterm;
11

    
12
import java.util.ArrayList;
13
import java.util.Collection;
14
import java.util.Collections;
15
import java.util.Comparator;
16
import java.util.EnumSet;
17
import java.util.HashMap;
18
import java.util.List;
19
import java.util.Map;
20
import java.util.Set;
21

    
22
import org.eclipse.jface.action.IContributionItem;
23
import org.eclipse.jface.action.MenuManager;
24
import org.eclipse.jface.action.Separator;
25
import org.eclipse.swt.SWT;
26
import org.eclipse.ui.PlatformUI;
27
import org.eclipse.ui.actions.CompoundContributionItem;
28
import org.eclipse.ui.menus.CommandContributionItem;
29
import org.eclipse.ui.menus.CommandContributionItemParameter;
30

    
31
import eu.etaxonomy.cdm.model.common.TermType;
32
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33

    
34
/**
35
 * Menu used in the store plugin xml to dynamically generate menu (sub-menu) contribution items
36
 * for term types which when clicked open the defined term editor for the chosen term type
37
 *
38
 * @author pplitzner
39
 * @date 21 Jul 2015
40
 *
41
 */
42

    
43
public class DefinedTermMenu extends CompoundContributionItem {
44

    
45

    
46
    @Override
47
    protected IContributionItem[] getContributionItems() {
48
        Collection<IContributionItem> items = new ArrayList<IContributionItem>();
49
	    MenuManager dtMenuManager =
50
	            new MenuManager("Term Editor","eu.etaxonomy.taxeditor.store.definedTermEditorMenu");
51

    
52
	    dtMenuManager.setVisible(true);
53

    
54
	    items.add(dtMenuManager);
55
	    //add Feature and NamedArea to top level
56
	    dtMenuManager.add(addChildTermsToMenuManager(TermType.Feature));
57
	    dtMenuManager.add(addChildTermsToMenuManager(TermType.NamedArea));
58
	    dtMenuManager.add(new Separator());
59

    
60
	    MenuManager otherMenuManager =
61
                new MenuManager("Others","eu.etaxonomy.taxeditor.store.term.other.menu");
62
	    otherMenuManager.setVisible(true);
63
	    dtMenuManager.add(otherMenuManager);
64

    
65
	    List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
66
	    Collections.sort(ttList,new SortByTermTypeMessage());
67
	    for (TermType tt : ttList)
68
	    {
69
	        //skip Feature and NamedArea as they have already been added to top level
70
	        if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
71
	            continue;
72
	        }
73
	        // if term type has a parent, do not add it
74
	        // it will be added in the recursive call
75
	        if(tt.getKindOf() == null) {
76
	            IContributionItem ici = addChildTermsToMenuManager(tt);
77
	            if(ici != null) {
78
	                otherMenuManager.add(ici);
79
	            }
80
	        }
81
	    }
82
	    return items.toArray(new IContributionItem[]{});
83
	}
84

    
85
	private IContributionItem addChildTermsToMenuManager(TermType termType) {
86

    
87
		//FIXME : need a better way to find out if a term type can be editable (ticket 3853)
88
		if(termType.getEmptyDefinedTermBase() != null) {
89
			Set<TermType> children = termType.getGeneralizationOf();
90
			// term type has no children, so create menu item
91
			if(children.isEmpty()) {
92
				return createMenuItem(termType);
93
			}
94
			// term type has children, so create sub menu
95
			MenuManager dtMenuManager =
96
					new MenuManager(termType.getMessage(PreferencesUtil.getGlobalLanguage()),"eu.etaxonomy.taxeditor.store." + termType.getKey() + "Menu");
97
			dtMenuManager.setVisible(true);
98
			dtMenuManager.add(createDefaultMenuItem(termType));
99

    
100
			Separator sep = new Separator();
101
			dtMenuManager.add(sep);
102
			// add child items to the sub menu
103
			for(TermType tt : children) {
104
				IContributionItem item = addChildTermsToMenuManager(tt);
105
				if(item != null) {
106
					dtMenuManager.add(item);
107
				}
108
			}
109
			return dtMenuManager;
110
		} else {
111
			return null;
112
		}
113

    
114
	}
115

    
116
	private CommandContributionItem createMenuItem(TermType termType) {
117

    
118
		Map<String, String> params = new HashMap<String, String>();
119
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
120
				termType.getUuid().toString());
121

    
122
        CommandContributionItemParameter p = new CommandContributionItemParameter(
123
                PlatformUI.getWorkbench(),
124
                "",
125
                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
126
                params,
127
                null,
128
                null,
129
                null,
130
                termType.getMessage(),
131
                "",
132
                "",
133
                SWT.PUSH,
134
                "",
135
                true);
136

    
137
        CommandContributionItem item = new CommandContributionItem(p);
138
        return item;
139

    
140
	}
141

    
142
	private CommandContributionItem createDefaultMenuItem(TermType termType) {
143

    
144
		Map<String, String> params = new HashMap<String, String>();
145
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
146
				termType.getUuid().toString());
147

    
148
        CommandContributionItemParameter p = new CommandContributionItemParameter(
149
                PlatformUI.getWorkbench(),
150
                "",
151
                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
152
                params,
153
                null,
154
                null,
155
                null,
156
                "Other " + termType.getMessage() + "s",
157
                "",
158
                "",
159
                SWT.PUSH,
160
                "",
161
                true);
162

    
163

    
164

    
165
        CommandContributionItem item = new CommandContributionItem(p);
166
        return item;
167

    
168
	}
169

    
170
	private class SortByTermTypeMessage implements Comparator<TermType> {
171
	    @Override
172
        public int compare(TermType t1, TermType t2) {
173
	        if (t1.equals(t2)){
174
	            return 0;
175
	        }
176
	        int result = t1.getMessage().compareTo(t2.getMessage());
177
	        if (result == 0){
178
	            return t1.compareTo(t2);
179
	        }
180
	        return result;
181
	    }
182
	}
183

    
184

    
185
}
(4-4/8)