Project

General

Profile

Download (7.46 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.l10n.Messages;
33
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
34
import eu.etaxonomy.taxeditor.store.CdmStore;
35

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

    
45
public class DefinedTermMenu extends CompoundContributionItem {
46

    
47

    
48
    @Override
49
    protected IContributionItem[] getContributionItems() {
50
        Collection<IContributionItem> items = new ArrayList<IContributionItem>();
51
        if(CdmStore.isActive()){
52
            MenuManager dtMenuManager =
53
                    new MenuManager(Messages.DefinedTermMenu_TERM_EDITOR,"eu.etaxonomy.taxeditor.store.definedTermEditorMenu"); //$NON-NLS-2$
54

    
55
            dtMenuManager.setVisible(true);
56

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

    
63
            MenuManager otherMenuManager =
64
                    new MenuManager(Messages.DefinedTermMenu_OTHERS,"eu.etaxonomy.taxeditor.store.term.other.menu"); //$NON-NLS-2$
65
            otherMenuManager.setVisible(true);
66
            dtMenuManager.add(otherMenuManager);
67
            dtMenuManager.add(new Separator());
68
            dtMenuManager.add(createFeatureTreeMenuItem());
69

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

    
91
	private IContributionItem addChildTermsToMenuManager(TermType termType) {
92

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

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

    
120
	}
121

    
122
	private CommandContributionItem createMenuItem(TermType termType) {
123

    
124
		Map<String, String> params = new HashMap<String, String>();
125
		params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid", //$NON-NLS-1$
126
				termType.getUuid().toString());
127

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

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

    
146
	}
147

    
148
	private CommandContributionItem createFeatureTreeMenuItem() {
149

    
150
		Map<String, String> params = new HashMap<String, String>();
151
		params.put("eu.etaxonomy.taxeditor.workbench.commandparameter.partName", //$NON-NLS-1$
152
				"eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor"); //$NON-NLS-1$
153

    
154
        CommandContributionItemParameter p = new CommandContributionItemParameter(
155
                PlatformUI.getWorkbench(),
156
                "eu.etaxonomy.taxeditor.featuretree.commandContributionItemParameter", //$NON-NLS-1$
157
                "eu.etaxonomy.taxeditor.command.openPart", //$NON-NLS-1$
158
                params,
159
                null,
160
                null,
161
                null,
162
                Messages.DefinedTermMenu_FEATURE_TREE,
163
                "", //$NON-NLS-1$
164
                "", //$NON-NLS-1$
165
                SWT.PUSH,
166
                "", //$NON-NLS-1$
167
                true);
168

    
169

    
170

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

    
174
	}
175

    
176
	private CommandContributionItem createDefaultMenuItem(TermType termType) {
177

    
178
	    Map<String, String> params = new HashMap<String, String>();
179
	    params.put("eu.etaxonomy.taxeditor.store.openDefinedTermEditor.termTypeUuid", //$NON-NLS-1$
180
	            termType.getUuid().toString());
181

    
182
	    CommandContributionItemParameter p = new CommandContributionItemParameter(
183
	            PlatformUI.getWorkbench(),
184
	            "", //$NON-NLS-1$
185
	            "eu.etaxonomy.taxeditor.store.openDefinedTermEditor", //$NON-NLS-1$
186
	            params,
187
	            null,
188
	            null,
189
	            null,
190
	            String.format(Messages.DefinedTermMenu_OTHER_S, termType.getMessage()),
191
	            "", //$NON-NLS-1$
192
	            "", //$NON-NLS-1$
193
	            SWT.PUSH,
194
	            "", //$NON-NLS-1$
195
	            true);
196

    
197

    
198

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

    
202
	}
203

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

    
218

    
219
}
(4-4/8)