Project

General

Profile

Download (3.01 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2007 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.bulkeditor.e4.command;
11

    
12
import java.util.Collections;
13
import java.util.List;
14
import java.util.Map;
15

    
16
import javax.inject.Named;
17

    
18
import org.eclipse.e4.ui.di.AboutToShow;
19
import org.eclipse.e4.ui.model.application.commands.MCommand;
20
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
21
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
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
import org.eclipse.e4.ui.services.IServiceConstants;
27

    
28
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
29
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
30
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
31
import eu.etaxonomy.taxeditor.bulkeditor.input.AbstractBulkEditorInput;
32
import eu.etaxonomy.taxeditor.l10n.Messages;
33

    
34
/**
35
 * @author pplitzner
36
 * @date 12.09.2017
37
 */
38
public class DynamicNewObjectMenuE4 {
39

    
40
	private Map<? extends Object, String> classLabelPairs;
41
    private BulkEditorE4 editor;
42

    
43
    @AboutToShow
44
    public void aboutToShow(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
45
            List<MMenuElement> items) {
46

    
47
        editor = (BulkEditorE4) activePart.getObject();
48

    
49
		classLabelPairs = getClassLabelPairs();
50

    
51
		MMenu menu = MMenuFactory.INSTANCE.createMenu();
52
        menu.setLabel(Messages.DynamicNewObjectMenuE4_NEW);
53
        items.add(menu);
54

    
55
        for(final Object key : classLabelPairs.keySet()){
56
            createMenuItem(menu, key);
57
        }
58
	}
59

    
60
	private void createMenuItem(MMenu menu, final Object key){
61
	    MHandledMenuItem menuItem = MMenuFactory.INSTANCE.createHandledMenuItem();
62
        menuItem.setLabel(classLabelPairs.get(key));
63
        MCommand mCommand = MCommandsFactory.INSTANCE.createCommand();
64
        mCommand.setElementId(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID);
65
        mCommand.setCommandName(classLabelPairs.get(key));
66
        //set params
67
        menuItem.getTransientData().put(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID+".key", key); //$NON-NLS-1$
68

    
69
        menuItem.setCommand(mCommand);
70
        menu.getChildren().add(menuItem);
71
	}
72

    
73
	/**
74
	 * Get class label pairs from Annotated Line Editor's entity creator.
75
	 * @return
76
	 */
77
	private Map<? extends Object, String> getClassLabelPairs() {
78
		if (editor != null){
79
		    AbstractBulkEditorInput<?> input = editor.getEditorInput();
80

    
81
		    IEntityCreator<?> entityCreator = input.getEntityCreator();
82
		    if(entityCreator!=null){
83
		        return entityCreator.getKeyLabelPairs();
84
		    }
85
		}
86

    
87
		@SuppressWarnings("unchecked")
88
        Map<? extends Object, String> result = Collections.EMPTY_MAP;
89
		return result;
90
	}
91
}
(2-2/2)