Project

General

Profile

Download (7.41 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 javax.inject.Named;
20

    
21
import org.eclipse.e4.ui.di.AboutToShow;
22
import org.eclipse.e4.ui.model.application.commands.MCommand;
23
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
24
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
25
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
26
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
27
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
28
import org.eclipse.e4.ui.services.IServiceConstants;
29

    
30
import eu.etaxonomy.cdm.model.common.TermType;
31
import eu.etaxonomy.taxeditor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
33
import eu.etaxonomy.taxeditor.store.CdmStore;
34

    
35
/**
36
 *
37
 * Menu 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
 * @since Aug 23, 2017
42
 *
43
 */
44
public class DefinedTermMenuE4 {
45

    
46
    private static final String COMMAND_ID = "eu.etaxonomy.taxeditor.store.openDefinedTermEditor";
47

    
48
    /** {@inheritDoc} */
49
    @AboutToShow
50
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION) Object selection) {
51

    
52
        if(CdmStore.isActive()){
53

    
54
            //add Feature and NamedArea to top level
55
            addChildTermsToMenuManager(items, TermType.Feature);
56
            addChildTermsToMenuManager(items, TermType.NamedArea);
57
            items.add(MMenuFactory.INSTANCE.createMenuSeparator());
58

    
59
            MMenu otherMenu = MMenuFactory.INSTANCE.createMenu();
60
            otherMenu.setLabel(Messages.DefinedTermMenu_OTHERS);
61
            items.add(otherMenu);
62

    
63
            items.add(MMenuFactory.INSTANCE.createMenuSeparator());
64
//            createFeatureTreeMenuItem(menu);
65

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

    
83
    private void addChildTermsToMenuManager(List<MMenuElement> items, TermType termType) {
84

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

    
99
                subMenu.getChildren().add(MMenuFactory.INSTANCE.createMenuSeparator());
100

    
101
                // add child items to the sub menu
102
                for(TermType tt : children) {
103
                    addChildTermsToMenuManager(subMenu.getChildren(), tt);
104
                }
105
            }
106
        }
107

    
108
    }
109

    
110
	private MHandledMenuItem createMenuItem(TermType termType) {
111

    
112
	    MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
113
        menuItem.setLabel(termType.getMessage());
114
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
115
        mCommand.setElementId(COMMAND_ID);
116

    
117
        //set params
118
        menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
119

    
120
        menuItem.setCommand(mCommand);
121
        return menuItem;
122
	}
123

    
124
	private void createFeatureTreeMenuItem() {
125

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

    
171
	}
172

    
173
	private MHandledMenuItem createDefaultMenuItem(TermType termType) {
174

    
175
        MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
176
        menuItem.setLabel(String.format(Messages.DefinedTermMenu_OTHER_S, termType.getMessage()));
177
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
178
        mCommand.setElementId(COMMAND_ID);
179

    
180
        //set params
181
        menuItem.getTransientData().put(COMMAND_ID+".termTypeUuid", termType.getUuid());
182

    
183
        menuItem.setCommand(mCommand);
184

    
185
        return menuItem;
186
	}
187

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

    
202

    
203
}
(4-4/4)