Project

General

Profile

Download (4.47 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
package eu.etaxonomy.taxeditor.annotatedlineeditor.e4.handler;
10

    
11
import javax.inject.Named;
12

    
13
import org.eclipse.e4.core.di.annotations.CanExecute;
14
import org.eclipse.e4.core.di.annotations.Execute;
15
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
16
import org.eclipse.e4.ui.model.application.ui.menu.MHandledMenuItem;
17
import org.eclipse.e4.ui.services.IServiceConstants;
18
import org.eclipse.jface.dialogs.IInputValidator;
19
import org.eclipse.jface.dialogs.InputDialog;
20
import org.eclipse.jface.viewers.IStructuredSelection;
21
import org.eclipse.jface.viewers.StructuredSelection;
22
import org.eclipse.jface.window.Window;
23
import org.eclipse.swt.widgets.Shell;
24

    
25
import eu.etaxonomy.cdm.model.common.CdmBase;
26
import eu.etaxonomy.taxeditor.annotatedlineeditor.IEntityCreator;
27
import eu.etaxonomy.taxeditor.bulkeditor.IBulkEditorConstants;
28
import eu.etaxonomy.taxeditor.bulkeditor.e4.BulkEditorE4;
29
import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.GroupCreator;
30
import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.UserCreator;
31
import eu.etaxonomy.taxeditor.l10n.Messages;
32

    
33
/**
34
 * @author pplitzner
35
 * @date 12.09.2017
36
 */
37
public class NewObjectHandlerE4 {
38

    
39
    @Execute
40
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
41
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
42
            MHandledMenuItem menuItem) {
43
        if(!(activePart.getObject() instanceof BulkEditorE4)){
44
            return;
45
        }
46
        Object key = menuItem.getTransientData().get(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID+".key");
47

    
48
        BulkEditorE4 bulkEditor = (BulkEditorE4) activePart.getObject();
49

    
50
        if (key != null) {
51
            String text = menuItem.getCommand().getCommandName();
52
            //FIXME : This should probably go into some ValidatorFactory
53
            IInputValidator nonEmptyInputValidator = null;
54
            //FIXME : This is a workaround to not allow empty strings in the
55
            //        input dialog for User and Group entities.
56
            //        Normally this should be default
57
            //        behaviour, so we need to discuss whether this handler
58
            //        should be used to handle the creating new entities of
59
            //        type other than User and Group.
60
            //        Once #4348 is fixed this check can be removed.
61
            if(text.equals(UserCreator.USER) || text.equals(GroupCreator.GROUP)) {
62
                nonEmptyInputValidator = new IInputValidator() {
63
                    @Override
64
                    public String isValid(String text) {
65
                        if(text == null || text.isEmpty()) {
66
                            return Messages.GROUP_CREATOR_Name_not_accepted_message;
67
                        }
68
                        return null;
69
                    }
70
                };
71
            }
72
            InputDialog dialog = new InputDialog(shell,
73
                    String.format("Create %s", text), String.format("Enter new %s", text), "",
74
                    nonEmptyInputValidator);
75

    
76
            if (dialog.open() != Window.CANCEL) {
77
                IEntityCreator<?> entityCreator = bulkEditor.getEditorInput().getEntityCreator();
78
                Object createdEntity = entityCreator.createEntity(key, dialog.getValue());
79
                if (createdEntity == null){
80
                    return;
81
                }
82
                bulkEditor.getEditorInput().getModel().add(createdEntity);
83
                if (createdEntity instanceof CdmBase){
84
                    if (!((CdmBase)createdEntity).isPersited()){
85
                        bulkEditor.getEditorInput().addSaveCandidate((CdmBase)createdEntity);
86
                        bulkEditor.setDirty();
87
                    }
88

    
89
                }
90
                IStructuredSelection selection = new StructuredSelection(createdEntity);
91
                bulkEditor.refresh();
92
                bulkEditor.setFocus();
93
                bulkEditor.setSelection(selection);
94
            }
95
        }
96
    }
97

    
98
    @CanExecute
99
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
100
            MHandledMenuItem menuItem){
101
        boolean canExecute = false;
102
        canExecute = activePart.getObject() instanceof BulkEditorE4;
103
        menuItem.setVisible(canExecute);
104
        return canExecute;
105
    }
106
}
    (1-1/1)