Project

General

Profile

Download (4.04 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.List;
13

    
14
import javax.inject.Named;
15

    
16
import org.eclipse.e4.core.di.annotations.CanExecute;
17
import org.eclipse.e4.ui.di.AboutToShow;
18
import org.eclipse.e4.ui.model.application.commands.MCommand;
19
import org.eclipse.e4.ui.model.application.commands.MCommandsFactory;
20
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
21
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
22
import org.eclipse.e4.ui.model.application.ui.menu.MMenu;
23
import org.eclipse.e4.ui.model.application.ui.menu.MMenuElement;
24
import org.eclipse.e4.ui.model.application.ui.menu.MMenuFactory;
25
import org.eclipse.e4.ui.services.IServiceConstants;
26
import org.eclipse.jface.viewers.StructuredSelection;
27

    
28
import eu.etaxonomy.cdm.model.common.IAnnotatableEntity;
29
import eu.etaxonomy.cdm.model.common.MarkerType;
30
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditor;
31
import eu.etaxonomy.taxeditor.l10n.Messages;
32
import eu.etaxonomy.taxeditor.store.CdmStore;
33

    
34
/**
35
 * @author pplitzner
36
 * @date 26.09.2017
37
 */
38
public class DynamicMarkerTypeEditingMenuE4 {
39

    
40
    public static final String COMMAND_ID = "taxeditor-bulkeditor.command.setMarkerFlag"; //$NON-NLS-1$
41
    public static final String COMMAND_PARAMETER_MARKER_TYPE = COMMAND_ID+".markerType"; //$NON-NLS-1$
42
    public static final String COMMAND_PARAMETER_MARKER_STATE = COMMAND_ID+".markerState"; //$NON-NLS-1$
43

    
44
    @AboutToShow
45
    public void aboutToShow(List<MMenuElement> items, @Named(IServiceConstants.ACTIVE_SELECTION)Object selection) {
46
        if(selection instanceof StructuredSelection && ((StructuredSelection)selection).isEmpty()){
47
            return;
48
        }
49
        MMenu menu = MMenuFactory.INSTANCE.createMenu();
50
        menu.setLabel(Messages.DynamicMarkerTypeEditingMenuE4_CHOOSE_MARKER_TYPE);
51
        items.add(menu);
52
        for(MarkerType markerType : CdmStore.getTermManager().getPreferredTerms(MarkerType.class)){
53
            createMenuItem(menu, markerType);
54
        }
55
    }
56

    
57
	private void createMenuItem(MMenu menu, final MarkerType markerType) {
58

    
59
	    MMenu subMenu = MMenuFactory.INSTANCE.createMenu();
60
	    subMenu.setLabel(String.format(Messages.DynamicMarkerTypeEditingMenuE4_SET_FLAG, markerType.getLabel()));
61
	    menu.getChildren().add(subMenu);
62

    
63
	    MHandledMenuItem subMenuItemYes = MMenuFactory.INSTANCE.createHandledMenuItem();
64
	    subMenuItemYes.setLabel(Messages.DynamicMarkerTypeEditingMenuE4_YES);
65
	    MCommand mCommandYes = MCommandsFactory.INSTANCE.createCommand();
66
	    mCommandYes.setElementId(COMMAND_ID);
67
	    //set params
68
	    subMenuItemYes.getTransientData().put(COMMAND_PARAMETER_MARKER_TYPE, markerType);
69
	    subMenuItemYes.getTransientData().put(COMMAND_PARAMETER_MARKER_STATE, true);
70
	    subMenuItemYes.setCommand(mCommandYes);
71
	    subMenu.getChildren().add(subMenuItemYes);
72

    
73
	    MHandledMenuItem subMenuItemNo = MMenuFactory.INSTANCE.createHandledMenuItem();
74
	    subMenuItemNo.setLabel(Messages.DynamicMarkerTypeEditingMenuE4_NO);
75
	    MCommand mCommandNo = MCommandsFactory.INSTANCE.createCommand();
76
	    mCommandNo.setElementId(COMMAND_ID);
77
	    //set params
78
	    subMenuItemNo.getTransientData().put(COMMAND_PARAMETER_MARKER_TYPE, markerType);
79
	    subMenuItemNo.getTransientData().put(COMMAND_PARAMETER_MARKER_STATE, false);
80
	    subMenuItemNo.setCommand(mCommandNo);
81
	    subMenu.getChildren().add(subMenuItemNo);
82
	}
83

    
84
	 @CanExecute
85
	    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
86
	            MHandledMenuItem menuItem){
87
	        boolean canExecute = false;
88
	        StructuredSelection selection = (StructuredSelection)((BulkEditor)activePart.getObject()).getSelection();
89
	        canExecute = !selection.isEmpty() && selection.getFirstElement() instanceof IAnnotatableEntity;
90
	        menuItem.setVisible(canExecute);
91
	        return canExecute;
92
	    }
93

    
94
}
(1-1/2)