Project

General

Profile

Download (4.88 KB) Statistics
| Branch: | Tag: | Revision:
1
/**
2
* Copyright (C) 2018 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.cdm.vaadin.event;
10

    
11
import java.util.Stack;
12

    
13
import org.apache.commons.lang3.StringUtils;
14

    
15
import com.vaadin.ui.Component;
16

    
17
import eu.etaxonomy.cdm.model.common.CdmBase;
18
import eu.etaxonomy.cdm.ref.TypedEntityReference;
19
import eu.etaxonomy.cdm.vaadin.event.EditorActionContextFormat.TargetInfoType;
20
import eu.etaxonomy.cdm.vaadin.model.CdmEntityAdapterDTO;
21
import eu.etaxonomy.vaadin.mvp.AbstractPopupEditor;
22
import eu.etaxonomy.vaadin.util.PropertyIdPath;
23

    
24
/**
25
 * @author a.kohlbecker
26
 * @since Oct 29, 2018
27
 *
28
 */
29
public class EditorActionContextFormatter {
30

    
31
    private static final String NEW = "New";
32

    
33
    private static final String EDIT = "Edit";
34

    
35
    public String format(EditorActionContext cntxt, EditorActionContextFormat format) {
36

    
37
        String className = null;
38
        String targetInfo = null;
39
        String createOrNew = null;
40

    
41
        Object parentEntity = cntxt.getParentEntity();
42

    
43
        if (parentEntity != null) {
44
            if (parentEntity instanceof TypedEntityReference) {
45
                className = ((TypedEntityReference) cntxt.getParentEntity()).getType().getSimpleName();
46
                createOrNew = EDIT;
47
            } else if (CdmEntityAdapterDTO.class.isAssignableFrom(parentEntity.getClass())) {
48
                CdmBase cdmEntity = ((CdmEntityAdapterDTO) parentEntity).cdmEntity();
49
                className = cdmEntity.getClass().getSimpleName();
50
                createOrNew = cdmEntity.isPersited() ? EDIT : NEW;
51
            } else if (CdmBase.class.isAssignableFrom(parentEntity.getClass())) {
52
                CdmBase cdmEntity = ((CdmBase) parentEntity);
53
                className = cdmEntity.getClass().getSimpleName();
54
                createOrNew = cdmEntity.isPersited() ? EDIT : NEW;
55
            } else {
56
                className = parentEntity.getClass().getSimpleName();
57
                // can not decide for createOrNew in this case
58
            }
59
        } else {
60
            className += "[NULL_CLASS]";
61
        }
62

    
63
        if (format.doTargetInfo && cntxt.getParentView() != null && AbstractPopupEditor.class.isAssignableFrom(cntxt.getParentView().getClass())) {
64
            // the top element is the cntxt istself!! we need to dig one step deeper to get the previous popup editor
65
            // TODO chaining the EditorActionContext would ease find the contexts of parent editors
66
            Stack<EditorActionContext> ctxtStack = ((AbstractPopupEditor)cntxt.getParentView()).getEditorActionContext();
67
            int parentPopupPos = ctxtStack.size() - 2;
68
            if(parentPopupPos > -1){
69
                EditorActionContext parentCtx = ctxtStack.get(parentPopupPos);
70
                if(format.targetInfoType.equals(TargetInfoType.PROPERTIES)){
71
                    PropertyIdPath propertyIdPath = parentCtx.getTargetPropertyIdPath();
72
                    if (propertyIdPath != null) {
73
                        targetInfo = propertyIdPath.toString();
74
                    }
75
                } else {
76
                    // TargetInfoType.FIELD_CAPTION
77
                    if(parentCtx.getTargetField() != null){
78
                        Component captionComponent = parentCtx.getTargetField();
79
                        while(captionComponent != null){
80
                            targetInfo = captionComponent.getCaption();
81
                            if(targetInfo != null){
82
                                break;
83
                            }
84
                            captionComponent = captionComponent.getParent();
85
                        }
86
                    }
87
                }
88
            }
89
        }
90

    
91
        // create output
92
        String markup = "";
93

    
94
        if (format.doCreateOrNew && createOrNew != null) {
95
            markup += "<" + format.tagName + " class=\"operation " + format.classAttributes + "\">" + createOrNew + "</" + format.tagName + ">";
96
        }
97

    
98
        if (format.doTargetInfo) {
99
            if(targetInfo == null && format.classNameForMissingTargetData && className != null){
100
                targetInfo = className;
101
            }
102
            if(targetInfo != null){
103
                if(!markup.isEmpty()){
104
                    markup += " ";
105
                    targetInfo = normalizeTargetInfo(targetInfo);
106
                }
107
                markup += "<" + format.tagName + " class=\"target " + format.classAttributes + "\">" + targetInfo + "</" + format.tagName + ">";
108
            }
109
        }
110
        return markup;
111
    }
112

    
113
    /**
114
     * @param targetInfo
115
     * @return
116
     */
117
    public String normalizeTargetInfo(String targetInfo) {
118
        targetInfo = StringUtils.join(StringUtils.splitByCharacterTypeCamelCase(targetInfo), " ");
119
        targetInfo = targetInfo.toLowerCase();
120
        return targetInfo;
121
    }
122

    
123
}
(10-10/29)