View Javadoc
1   /**
2    * This file Copyright (c) 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.ckeditor;
35  
36  
37  import static info.magnolia.ui.vaadin.gwt.client.ckeditor.VMagnoliaCKEditorTextEvents.*;
38  
39  import java.util.ArrayList;
40  import java.util.Arrays;
41  import java.util.List;
42  import java.util.Map;
43  import java.util.Objects;
44  import java.util.Optional;
45  
46  import org.apache.commons.lang3.StringUtils;
47  import org.vaadin.alump.ckeditor.CKEditorTextField;
48  
49  import com.vaadin.server.PaintException;
50  import com.vaadin.server.PaintTarget;
51  
52  
53  /**
54   * Magnolia 6 - Vaadin 8 extended CKEditorTextField for custom made Magnolia plugins.
55   * By default CKEditor wrapper for Vaadin does not allow
56   * custom events between CKEditor plugins and server.
57   */
58  public class MagnoliaCKEditorTextField extends CKEditorTextField implements MagnoliaCKEditorTextFieldEvents {
59  
60      private String fireEvent;
61      private String fireEventValue;
62      private String[] customEvents;
63      private Map<String, String> serverPlugins;
64      private List<PluginListener> listeners = new ArrayList<>();
65      private MagnoliaCKEditorConfig config;
66  
67      public void addListener(PluginListener listener) {
68          listeners.add(listener);
69      }
70  
71      public MagnoliaCKEditorTextField(MagnoliaCKEditorConfig config) {
72          super(config);
73          this.config = config;
74          serverPlugins = config.getExternalPlugins();
75          customEvents = config.getListenedEvents();
76      }
77  
78      @Override
79      public void changeVariables(Object source, Map<String, Object> variables) {
80          super.changeVariables(source, variables);
81  
82          if (isEmpty()) {
83              this.setValue(null, true);
84          }
85  
86          Optional.ofNullable(config).ifPresent(c -> {
87              // See if client sends events
88              Arrays.stream(c.getListenedEvents())
89                      .filter(Objects::nonNull)
90                      .forEach(eventName -> {
91                          final String eventNameResolved = VAR_EVENT_PREFIX + eventName;
92                          if (variables.containsKey(eventNameResolved)) {
93                              listeners.forEach(l -> l.onPluginEvent(eventName, variables.get(eventNameResolved).toString()));
94                          }
95                      });
96          });
97      }
98      @Override
99      public void firePluginEvent(String event, String value) {
100         fireEvent = event;
101         fireEventValue = value;
102         markAsDirty();
103     }
104 
105     @Override
106     public void paintContent(PaintTarget target) throws PaintException {
107         super.paintContent(target);
108 
109         if (serverPlugins != null) {
110             target.addAttribute(VAR_SERVERPLUGINS, serverPlugins);
111         }
112 
113         // tell client that server is interested of these events
114         if (customEvents != null) {
115             target.addAttribute(VAR_EVENTNAMES, customEvents);
116         }
117 
118         // send event to plugin
119         if (fireEvent != null && fireEventValue != null) {
120             target.addAttribute(VAR_FIRE_PLUGIN_EVENT, fireEvent);
121             target.addAttribute(VAR_FIRE_PLUGIN_EVENT_VALUE, fireEventValue);
122             fireEvent = null;
123             fireEventValue = null;
124         }
125     }
126 
127     @Override
128     public void setHeight(float height, Unit unit) {
129         super.setHeight(height, unit);
130     }
131 
132     @Override
133     public boolean isEmpty() {
134         return StringUtils.isEmpty(getValue());
135     }
136 }