View Javadoc
1   /**
2    * This file Copyright (c) 2012-2018 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
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   * Magnolia5 / Vaadin7 compatibility class. Extended CKEditorTextField for custom made Magnolia plugins.
53   * By default CKEditor wrapper for Vaadin does not allow
54   * custom events between CKEditor plugins and server.
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              // See if client sends events
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         // tell client that server is interested of these events
118         if (customEvents != null) {
119             target.addAttribute(VAR_EVENTNAMES, customEvents);
120         }
121 
122         // send event to plugin
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 }