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.gwt.client.richtext;
35
36
37 import java.util.Arrays;
38 import java.util.List;
39
40 import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.CKEditor;
41 import org.vaadin.openesignforms.ckeditor.widgetset.client.ui.VCKEditorTextField;
42
43 import com.google.gwt.user.client.Timer;
44 import com.vaadin.client.ApplicationConnection;
45 import com.vaadin.client.UIDL;
46 import com.vaadin.client.ValueMap;
47
48
49
50
51
52 public class VMagnoliaRichTextField extends VCKEditorTextField implements VMagnoliaRichTextEditor.Listener {
53
54 public static final String VAR_EVENTNAMES = "eventnames";
55 public static final String VAR_SERVERPLUGINS = "serverplugins";
56 public static final String VAR_EVENT_PREFIX = "pluginEvent:";
57 public static final String VAR_FIRE_PLUGIN_EVENT = "firePluginEvent";
58 public static final String VAR_FIRE_PLUGIN_EVENT_VALUE = "firePluginEventValue";
59
60 private VMagnoliaRichTextEditor editor;
61 private List<String> pluginEvents;
62 private ValueMap customPlugins;
63 private boolean immediate;
64
65 @Override
66 public void updateFromUIDL(UIDL uidl, ApplicationConnection client) {
67
68
69 if (uidl.hasAttribute(VAR_SERVERPLUGINS)) {
70 customPlugins = uidl.getMapAttribute(VAR_SERVERPLUGINS);
71 }
72
73
74 if (uidl.hasAttribute(VAR_EVENTNAMES)) {
75 pluginEvents = Arrays.asList(uidl.getStringArrayAttribute(VAR_EVENTNAMES));
76 }
77
78 if (uidl.hasAttribute(ATTR_IMMEDIATE)) {
79 immediate = uidl.getBooleanAttribute(ATTR_IMMEDIATE);
80 }
81
82 super.updateFromUIDL(uidl, client);
83
84
85 if (uidl.hasAttribute(VAR_FIRE_PLUGIN_EVENT) && this.editor != null) {
86 this.editor.fire(
87 uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT),
88 uidl.getStringAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE)
89 );
90 }
91 }
92
93 @Override
94 protected CKEditor loadEditor(String inPageConfig) {
95
96 if (customPlugins != null && customPlugins.getKeySet() != null && !customPlugins.getKeySet().isEmpty()) {
97 for (String plugin : customPlugins.getKeySet()) {
98 addExternalPlugin(plugin, customPlugins.getString(plugin));
99 }
100 }
101
102
103 setVaadinDirUrl(clientToServer.getConfiguration().getVaadinDirUrl());
104
105
106 editor = (VMagnoliaRichTextEditor) super.loadEditor(inPageConfig);
107 return editor;
108 }
109
110 private native void setVaadinDirUrl(String vaadinDirUrl)
111
112 ;
113
114 private native void addExternalPlugin(String pluginName, String path)
115
116 ;
117
118
119
120
121 @Override
122 public void onInstanceReady() {
123 super.onInstanceReady();
124
125
126 if (pluginEvents != null && !pluginEvents.isEmpty()) {
127 for (String eventName : pluginEvents) {
128 editor.addPluginListener(eventName, this);
129 }
130 }
131 }
132
133 @Override
134 public void onPluginEvent(String eventName, String data) {
135 if (pluginEvents.contains(eventName)) {
136 clientToServer.updateVariable(paintableId, VAR_EVENT_PREFIX + eventName, data == null ? "" : data, true);
137 }
138 }
139
140
141
142
143 @Override
144 public void onChange() {
145 if (editor != null && !editor.isReadOnly()) {
146 clientToServer.updateVariable(paintableId, VAR_TEXT, editor.getData(), false);
147 if (immediate) {
148 valueUpdateTimer.schedule(200);
149 }
150 }
151 }
152
153 private Timer valueUpdateTimer = new Timer() {
154 @Override
155 public void run() {
156 clientToServer.sendPendingVariableChanges();
157 }
158 };
159
160 @Override
161 public void doResize() {
162 super.doResize();
163 }
164 }