2 * Copyright (C) 2018 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.vaadin
.event
;
11 import java
.util
.Collection
;
12 import java
.util
.Stack
;
14 import org
.apache
.commons
.lang3
.StringUtils
;
16 import com
.vaadin
.ui
.Component
;
18 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
19 import eu
.etaxonomy
.cdm
.ref
.TypedEntityReference
;
20 import eu
.etaxonomy
.cdm
.vaadin
.event
.EditorActionContextFormat
.TargetInfoType
;
21 import eu
.etaxonomy
.cdm
.vaadin
.model
.CdmEntityAdapterDTO
;
22 import eu
.etaxonomy
.vaadin
.mvp
.AbstractPopupEditor
;
23 import eu
.etaxonomy
.vaadin
.util
.PropertyIdPath
;
26 * @author a.kohlbecker
30 public class EditorActionContextFormatter
{
32 private static final String NEW
= "New";
34 private static final String EDIT
= "Edit";
36 public String
format(EditorActionContext cntxt
, EditorActionContextFormat format
) {
38 String className
= null;
39 String targetInfo
= null;
40 String targetEntityStr
= null;
41 String createOrNew
= null;
43 Object parentEntity
= cntxt
.getParentEntity();
45 if (parentEntity
!= null) {
46 if (parentEntity
instanceof TypedEntityReference
) {
47 className
= ((TypedEntityReference
) cntxt
.getParentEntity()).getType().getSimpleName();
49 } else if (CdmEntityAdapterDTO
.class.isAssignableFrom(parentEntity
.getClass())) {
50 CdmBase cdmEntity
= ((CdmEntityAdapterDTO
) parentEntity
).cdmEntity();
51 className
= cdmEntity
.getClass().getSimpleName();
52 createOrNew
= cdmEntity
.isPersited() ? EDIT
: NEW
;
53 } else if (CdmBase
.class.isAssignableFrom(parentEntity
.getClass())) {
54 CdmBase cdmEntity
= ((CdmBase
) parentEntity
);
55 className
= cdmEntity
.getClass().getSimpleName();
56 createOrNew
= cdmEntity
.isPersited() ? EDIT
: NEW
;
58 className
= parentEntity
.getClass().getSimpleName();
59 // can not decide for createOrNew in this case
62 className
+= "[NULL_CLASS]";
65 if ((format
.doTargetInfo
|| format
.doTargetEntity
) && cntxt
.getParentView() != null && AbstractPopupEditor
.class.isAssignableFrom(cntxt
.getParentView().getClass())) {
66 // the top element is the cntxt istself!! we need to dig one step deeper to get the previous popup editor
67 // TODO chaining the EditorActionContext would ease find the contexts of parent editors
68 Stack
<EditorActionContext
> ctxtStack
= ((AbstractPopupEditor
)cntxt
.getParentView()).getEditorActionContext();
69 int parentPopupPos
= ctxtStack
.size() - 2;
70 if(parentPopupPos
> -1){
71 EditorActionContext parentCtx
= ctxtStack
.get(parentPopupPos
);
72 if(format
.targetInfoType
.equals(TargetInfoType
.PROPERTIES
)){
73 PropertyIdPath propertyIdPath
= parentCtx
.getTargetPropertyIdPath();
74 if (propertyIdPath
!= null) {
75 targetInfo
= propertyIdPath
.toString();
78 // TargetInfoType.FIELD_CAPTION
79 if(parentCtx
.getTargetField() != null){
80 Component captionComponent
= parentCtx
.getTargetField();
81 while(captionComponent
!= null){
82 targetInfo
= captionComponent
.getCaption();
83 if(targetInfo
!= null){
86 captionComponent
= captionComponent
.getParent();
92 if(format
.doTargetEntity
){
93 targetEntityStr
= formatTargetEntityString(cntxt
.parentEntity
, format
);
99 if (format
.doCreateOrNew
&& createOrNew
!= null) {
100 if(format
.tagName
!= null){
101 outStr
+= "<" + format
.tagName
+ " class=\"operation " + format
.classAttributes
+ "\">" + createOrNew
+ "</" + format
.tagName
+ ">";
103 outStr
+= createOrNew
;
107 if (format
.doTargetInfo
) {
108 if(targetInfo
== null && format
.classNameForMissingTargetData
&& className
!= null){
109 targetInfo
= className
;
111 if(targetInfo
!= null){
112 if(!outStr
.isEmpty()){
114 targetInfo
= normalizeTargetInfo(targetInfo
);
116 if(format
.tagName
!= null){
117 outStr
+= "<" + format
.tagName
+ " class=\"target " + format
.classAttributes
+ "\">" + targetInfo
+ "</" + format
.tagName
+ ">";
119 outStr
+= targetInfo
;
123 if(format
.doTargetEntity
&& targetEntityStr
!= null){
124 if(format
.tagName
!= null){
125 outStr
+= "<" + format
.tagName
+ " class=\"target-entity" + format
.classAttributes
+ "\">" + targetEntityStr
+ "</" + format
.tagName
+ ">";
127 outStr
+= " (" + targetEntityStr
+ ")";
133 public String
format(Collection
<EditorActionContext
> cntxts
, EditorActionContextFormat format
) {
135 for(EditorActionContext ctx
: cntxts
){
136 if(!outStr
.isEmpty()){
137 outStr
+= " > "; // FIXME allow configuring the separator
139 outStr
+= format(ctx
, format
) ;
149 private String
formatTargetEntityString(Object value
, EditorActionContextFormat format
) {
155 if(value
instanceof CdmBase
){
156 outStr
= ((CdmBase
)value
).instanceToString();
157 } else if(value
instanceof CdmEntityAdapterDTO
) {
158 outStr
= value
.getClass().getSimpleName() + ": ";
159 outStr
+= ((CdmEntityAdapterDTO
)value
).cdmEntity().toString();
161 outStr
= value
.getClass().getSimpleName() + ": " + value
.toString();
170 public String
normalizeTargetInfo(String targetInfo
) {
171 targetInfo
= StringUtils
.join(StringUtils
.splitByCharacterTypeCamelCase(targetInfo
), " ");
172 targetInfo
= targetInfo
.toLowerCase();