Project

General

Profile

Download (7.06 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
import eu.etaxonomy.taxeditor.store.CdmStore;
34

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

    
44
public class DefinedTermMenu extends CompoundContributionItem {
45

    
46

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

    
54
            dtMenuManager.setVisible(true);
55

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

    
62
            MenuManager otherMenuManager =
63
                    new MenuManager("Others","eu.etaxonomy.taxeditor.store.term.other.menu");
64
            otherMenuManager.setVisible(true);
65
            dtMenuManager.add(otherMenuManager);
66
            //FIXME E4: This should be removed during e4 migration. dynamic menu should be declared in model fragment
67
            dtMenuManager.add(createFeatureTreeMenuItem());
68

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

    
90
	private IContributionItem addChildTermsToMenuManager(TermType termType) {
91

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

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

    
119
	}
120

    
121
	private CommandContributionItem createMenuItem(TermType termType) {
122

    
123
		Map<String, String> params = new HashMap<String, String>();
124
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
125
				termType.getUuid().toString());
126

    
127
        CommandContributionItemParameter p = new CommandContributionItemParameter(
128
                PlatformUI.getWorkbench(),
129
                "",
130
                "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
131
                params,
132
                null,
133
                null,
134
                null,
135
                termType.getMessage(),
136
                "",
137
                "",
138
                SWT.PUSH,
139
                "",
140
                true);
141

    
142
        CommandContributionItem item = new CommandContributionItem(p);
143
        return item;
144

    
145
	}
146

    
147
	private CommandContributionItem createFeatureTreeMenuItem() {
148

    
149
		Map<String, String> params = new HashMap<String, String>();
150
		params.put("eu.etaxonomy.taxeditor.workbench.commandparameter.partName",
151
				"eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor");
152

    
153
        CommandContributionItemParameter p = new CommandContributionItemParameter(
154
                PlatformUI.getWorkbench(),
155
                "eu.etaxonomy.taxeditor.featuretree.commandContributionItemParameter",
156
                "eu.etaxonomy.taxeditor.command.openPart",
157
                params,
158
                null,
159
                null,
160
                null,
161
                "Feature Tree",
162
                "",
163
                "",
164
                SWT.PUSH,
165
                "",
166
                true);
167

    
168

    
169

    
170
        CommandContributionItem item = new CommandContributionItem(p);
171
        return item;
172

    
173
	}
174

    
175
	private CommandContributionItem createDefaultMenuItem(TermType termType) {
176

    
177
	    Map<String, String> params = new HashMap<String, String>();
178
	    params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid",
179
	            termType.getUuid().toString());
180

    
181
	    CommandContributionItemParameter p = new CommandContributionItemParameter(
182
	            PlatformUI.getWorkbench(),
183
	            "",
184
	            "eu.etaxonomy.taxeditor.store.openDefinedTermEditor",
185
	            params,
186
	            null,
187
	            null,
188
	            null,
189
	            "Other " + termType.getMessage() + "s",
190
	            "",
191
	            "",
192
	            SWT.PUSH,
193
	            "",
194
	            true);
195

    
196

    
197

    
198
	    CommandContributionItem item = new CommandContributionItem(p);
199
	    return item;
200

    
201
	}
202

    
203
	private class SortByTermTypeMessage implements Comparator<TermType> {
204
	    @Override
205
        public int compare(TermType t1, TermType t2) {
206
	        if (t1.equals(t2)){
207
	            return 0;
208
	        }
209
	        int result = t1.getMessage().compareTo(t2.getMessage());
210
	        if (result == 0){
211
	            return t1.compareTo(t2);
212
	        }
213
	        return result;
214
	    }
215
	}
216

    
217

    
218
}
(4-4/8)