Project

General

Profile

Download (4.8 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.BulkEditor;
29
import eu.etaxonomy.taxeditor.bulkeditor.input.OccurrenceEditorInput;
30
import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.GroupCreator;
31
import eu.etaxonomy.taxeditor.bulkeditor.input.entitycreator.UserCreator;
32
import eu.etaxonomy.taxeditor.l10n.Messages;
33

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

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

    
49
        BulkEditor bulkEditor = (BulkEditor) activePart.getObject();
50

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

    
80
            if (dialog == null || dialog.open() != Window.CANCEL) {
81
                IEntityCreator<?> entityCreator = bulkEditor.getEditorInput().getEntityCreator();
82
                String title = null;
83
                if (dialog != null){
84
                    title = dialog.getValue();
85
                }
86
                Object createdEntity = entityCreator.createEntity(key, title);
87
                if (createdEntity == null){
88
                    return;
89
                }
90
                bulkEditor.getEditorInput().getModel().add(createdEntity);
91
                if (createdEntity instanceof CdmBase){
92
                    if (!((CdmBase)createdEntity).isPersited()){
93
                        bulkEditor.getEditorInput().addSaveCandidate((CdmBase)createdEntity);
94
                        bulkEditor.setDirty();
95
                    }
96

    
97
                }
98
                IStructuredSelection selection = new StructuredSelection(createdEntity);
99
                bulkEditor.refresh();
100
                bulkEditor.setFocus();
101
                bulkEditor.setSelection(selection);
102
            }
103
        }
104
    }
105

    
106
    @CanExecute
107
    public boolean canExecute(@Named(IServiceConstants.ACTIVE_PART)MPart activePart,
108
            MHandledMenuItem menuItem){
109
        boolean canExecute = false;
110
        canExecute = activePart.getObject() instanceof BulkEditor;
111
        menuItem.setVisible(canExecute);
112
        return canExecute;
113
    }
114
}
    (1-1/1)