Project

General

Profile

Download (7.2 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.e4;
11

    
12
import java.util.ArrayList;
13
import java.util.Collections;
14
import java.util.Comparator;
15
import java.util.EnumSet;
16
import java.util.List;
17
import java.util.Set;
18

    
19
import org.eclipse.e4.ui.di.AboutToShow;
20
import org.eclipse.e4.ui.model.application.commands.MCommand;
21
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
22
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
24
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
25
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
26

    
27
import eu.etaxonomy.cdm.model.common.TermType;
28
import eu.etaxonomy.taxeditor.l10n.Messages;
29
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
30
import eu.etaxonomy.taxeditor.store.CdmStore;
31

    
32
/**
33
 *
34
 * Menu to dynamically generate menu (sub-menu) contribution items
35
 * for term types which when clicked open the defined term editor for the chosen term type
36
 *
37
 * @author pplitzner
38
 * @since Aug 23, 2017
39
 *
40
 */
41
public class DefinedTermMenuE4 {
42

    
43
    private static final String COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
44

    
45
    /** {@inheritDoc} */
46
    @AboutToShow
47
    public void aboutToShow(List<MMenuElement> items) {
48

    
49
        if(CdmStore.isActive()){
50

    
51
            //add Feature and NamedArea to top level
52
            addChildTermsToMenuManager(items, TermType.Feature);
53
            addChildTermsToMenuManager(items, TermType.NamedArea);
54

    
55
            MMenu otherMenu = MMenuFactory.INSTANCE.createMenu();
56
            otherMenu.setLabel(Messages.DefinedTermMenu_OTHERS);
57
            items.add(otherMenu);
58

    
59
            items.add(MMenuFactory.INSTANCE.createMenuSeparator());
60
//            createFeatureTreeMenuItem(menu);
61

    
62
            List<TermType> ttList = new ArrayList<TermType>(EnumSet.allOf(TermType.class));
63
            Collections.sort(ttList,new SortByTermTypeMessage());
64
            for (TermType tt : ttList)
65
            {
66
                //skip Feature and NamedArea as they have already been added to top level
67
                if(tt.equals(TermType.Feature) || tt.equals(TermType.NamedArea)){
68
                    continue;
69
                }
70
                // if term type has a parent, do not add it
71
                // it will be added in the recursive call
72
                if(tt.getKindOf() == null) {
73
                    addChildTermsToMenuManager(otherMenu.getChildren(), tt);
74
                }
75
            }
76
        }
77
	}
78

    
79
    private void addChildTermsToMenuManager(List<MMenuElement> items, TermType termType) {
80

    
81
        //FIXME : need a better way to find out if a term type can be editable (ticket 3853)
82
        if(termType.getEmptyDefinedTermBase() != null) {
83
            Set<TermType> children = termType.getGeneralizationOf();
84
            // term type has no children, so create menu item
85
            if(children.isEmpty()) {
86
                items.add(createMenuItem(termType));
87
            }
88
            else{
89
                MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
90
                subMenu.setLabel(termType.getMessage(PreferencesUtil.getGlobalLanguage()));
91
                items.add(subMenu);
92
                // term type has children, so create sub menu
93
                subMenu.getChildren().add(createDefaultMenuItem(termType));
94

    
95
                subMenu.getChildren().add(MMenuFactory.INSTANCE.createMenuSeparator());
96

    
97
                // add child items to the sub menu
98
                for(TermType tt : children) {
99
                    addChildTermsToMenuManager(subMenu.getChildren(), tt);
100
                }
101
            }
102
        }
103

    
104
    }
105

    
106
	private MHandledMenuItem createMenuItem(TermType termType) {
107

    
108
	    MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
109
        menuItem.setLabel(termType.getMessage());
110
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
111
        mCommand.setElementId(COMMAND_ID);
112

    
113
        //set params
114
        menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
115

    
116
        menuItem.setCommand(mCommand);
117
        return menuItem;
118
	}
119

    
120
	private void createFeatureTreeMenuItem() {
121

    
122
//	       String commandId = "eu.etaxonomy.taxeditor.workbench.commandparameter.partName";
123
//
124
//	       MDirectMenuItem directMenuItem = MMenuFactory.INSTANCE.createDirectMenuItem();
125
//	       directMenuItem.
126
//	        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
127
//	        menuItem.setLabel(Messages.DefinedTermMenu_FEATURE_TREE);
128
//	        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
129
//	        mCommand.setElementId(commandId);
130
//	        mCommand.getParameters().add(MCommandsFactory.INSTANCE.createCommandParameter().);
131
//
132
//	        MCommandParameter commandParameter = MCommandsFactory.INSTANCE.createCommandParameter();
133
//	        commandParameter.setName("eu.etaxonomy.taxeditor.workbench.commandparameter.partName");
134
//	        commandParameter.set
135
//
136
//	        //set params
137
//	        menuItem.getTransientData().put(commandId+"termTypeUuid", termType.getUuid());
138
//
139
//	        menuItem.setCommand(mCommand);
140
//	        menu.getChildren().add(menuItem);
141
//
142
//
143
//		Map<String, String> params = new HashMap<String, String>();
144
//		params.put("eu.etaxonomy.taxeditor.workbench.commandparameter.partName", //$NON-NLS-1$
145
//				"eu.etaxonomy.taxeditor.featuretree.e4.FeatureTreeEditor"); //$NON-NLS-1$
146
//
147
//        CommandContributionItemParameter p = new CommandContributionItemParameter(
148
//                PlatformUI.getWorkbench(),
149
//                "eu.etaxonomy.taxeditor.featuretree.commandContributionItemParameter", //$NON-NLS-1$
150
//                "eu.etaxonomy.taxeditor.command.openPart", //$NON-NLS-1$
151
//                params,
152
//                null,
153
//                null,
154
//                null,
155
//                Messages.DefinedTermMenu_FEATURE_TREE,
156
//                "", //$NON-NLS-1$
157
//                "", //$NON-NLS-1$
158
//                SWT.PUSH,
159
//                "", //$NON-NLS-1$
160
//                true);
161
//
162
//
163
//
164
//        CommandContributionItem item = new CommandContributionItem(p);
165
//        return item;
166

    
167
	}
168

    
169
	private MHandledMenuItem createDefaultMenuItem(TermType termType) {
170

    
171
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
172
        menuItem.setLabel(String.format(Messages.DefinedTermMenu_OTHER_S, termType.getMessage()));
173
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
174
        mCommand.setElementId(COMMAND_ID);
175

    
176
        //set params
177
        menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
178

    
179
        menuItem.setCommand(mCommand);
180

    
181
        return menuItem;
182
	}
183

    
184
	private class SortByTermTypeMessage implements Comparator<TermType> {
185
	    @Override
186
        public int compare(TermType t1, TermType t2) {
187
	        if (t1.equals(t2)){
188
	            return 0;
189
	        }
190
	        int result = t1.getMessage().compareTo(t2.getMessage());
191
	        if (result == 0){
192
	            return t1.compareTo(t2);
193
	        }
194
	        return result;
195
	    }
196
	}
197

    
198

    
199
}
(4-4/4)