1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34 package info.magnolia.ui.vaadin.richtext;
35
36 import static info.magnolia.ui.vaadin.gwt.client.ckeditor.VMagnoliaCKEditorTextEvents.*;
37
38 import info.magnolia.ui.vaadin.ckeditor.MagnoliaCKEditorTextFieldEvents;
39
40 import java.util.ArrayList;
41 import java.util.List;
42 import java.util.Map;
43
44 import org.apache.commons.lang3.StringUtils;
45 import org.vaadin.openesignforms.ckeditor.CKEditorTextField;
46
47 import com.vaadin.server.PaintException;
48 import com.vaadin.server.PaintTarget;
49
50
51
52
53
54
55
56 public class MagnoliaRichTextField extends CKEditorTextField implements MagnoliaCKEditorTextFieldEvents {
57
58 private String fireEvent;
59 private String fireEventValue;
60 private String[] customEvents;
61 private Map<String, String> serverPlugins;
62 private List<PluginListener> listeners = new ArrayList<>();
63 private MagnoliaRichTextFieldConfig config;
64
65 public MagnoliaRichTextField() {
66 super();
67 }
68
69 @Override
70 public void addListener(PluginListener listener) {
71 listeners.add(listener);
72 }
73
74 public MagnoliaRichTextField(MagnoliaRichTextFieldConfig config) {
75 super(config);
76 this.config = config;
77 serverPlugins = config.getExternalPlugins();
78 customEvents = config.getListenedEvents();
79 }
80
81 @Override
82 public void changeVariables(Object source, Map<String, Object> variables) {
83 super.changeVariables(source, variables);
84
85 if(isEmpty() && !isReadOnly()) {
86 this.setValue(null, true);
87 }
88
89 if (config != null && config.getListenedEvents().length > 0) {
90
91 for (String eventName : config.getListenedEvents()) {
92 String eventNameResolved = VAR_EVENT_PREFIX + eventName;
93 if (variables.containsKey(eventNameResolved)) {
94 for (PluginListener listener : listeners) {
95 listener.onPluginEvent(eventName, variables.get(eventNameResolved).toString());
96 }
97 }
98 }
99 }
100 }
101
102 @Override
103 public void firePluginEvent(String event, String value) {
104 fireEvent = event;
105 fireEventValue = value;
106 markAsDirty();
107 }
108
109 @Override
110 public void paintContent(PaintTarget target) throws PaintException {
111 super.paintContent(target);
112
113 if (serverPlugins != null) {
114 target.addAttribute(VAR_SERVERPLUGINS, serverPlugins);
115 }
116
117
118 if (customEvents != null) {
119 target.addAttribute(VAR_EVENTNAMES, customEvents);
120 }
121
122
123 if (fireEvent != null && fireEventValue != null) {
124 target.addAttribute(VAR_FIRE_PLUGIN_EVENT, fireEvent);
125 target.addAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE, fireEventValue);
126 fireEvent = null;
127 fireEventValue = null;
128 }
129 }
130
131 @Override
132 public boolean isEmpty() {
133 return StringUtils.isEmpty(getValue());
134 }
135 }