Project

General

Profile

Download (3.77 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.annotatedlineeditor.e4.handler;
11

    
12
import javax.inject.Named;
13

    
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

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

    
40
    @Execute
41
    public void execute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
42
            @Named(IServiceConstants.ACTIVE_SHELL)Shell shell,
43
            MHandledMenuItem menuItem) {
44

    
45
        Object key = menuItem.getTransientData().get(IBulkEditorConstants.DYNAMIC_OPEN_OBJECT_ID+".key");
46

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

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

    
75
            if (dialog.open() != Window.CANCEL) {
76
                IEntityCreator entityCreator = bulkEditor.getEditorInput().getEntityCreator();
77
                Object createdEntity = entityCreator.createEntity(key, dialog.getValue());
78
                bulkEditor.getEditorInput().getModel().add(createdEntity);
79
                if (createdEntity instanceof CdmBase){
80
                    bulkEditor.getEditorInput().addSaveCandidate((CdmBase)createdEntity);
81
                }
82
                IStructuredSelection selection = new StructuredSelection(createdEntity);
83

    
84
                bulkEditor.refresh();
85
                bulkEditor.setDirty();
86
                bulkEditor.setFocus();
87

    
88
                bulkEditor.setSelection(selection);
89

    
90

    
91
            }
92
        }
93
    }
94
}
    (1-1/1)