View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.suneditor;
23  
24  import java.util.Collection;
25  import java.util.Locale;
26  
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.context.FacesContext;
30  
31  import org.primefaces.extensions.component.base.AbstractEditorInputTextArea;
32  import org.primefaces.util.LangUtils;
33  import org.primefaces.util.LocaleUtils;
34  
35  /**
36   * <code>SunEditor</code> component.
37   *
38   * @author Matthieu Valente
39   * @since 12.0.6
40   */
41  
42  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
43  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
44  @ResourceDependency(library = "primefaces", name = "core.js")
45  @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
46  @ResourceDependency(library = "primefaces-extensions", name = "suneditor/suneditor.css")
47  @ResourceDependency(library = "primefaces-extensions", name = "suneditor/suneditor.js")
48  public class SunEditor extends AbstractEditorInputTextArea implements ClientBehaviorHolder {
49      public static final String EDITOR_CLASS = "ui-suneditor";
50      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.SunEditor";
51      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
52      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.SunEditorRenderer";
53  
54      private static final Collection<String> EVENT_NAMES = LangUtils.unmodifiableList("change", "scroll", "mousedown", "click", "input", "keydown", "keyup",
55                  "focus", "blur", "paste", "copy", "cut", "drop", "save", "initialize");
56  
57      @SuppressWarnings("java:S115")
58      protected enum PropertyKeys {
59          // @formatter:off
60          width,
61          height,
62          mode,
63          locale,
64          // @formatter:on
65      }
66  
67      private Locale appropriateLocale;
68  
69      public SunEditor() {
70          setRendererType(DEFAULT_RENDERER);
71      }
72  
73      @Override
74      public Collection<String> getEventNames() {
75          return EVENT_NAMES;
76      }
77  
78      @Override
79      public String getDefaultEventName() {
80          return "change";
81      }
82  
83      @Override
84      public String getFamily() {
85          return COMPONENT_FAMILY;
86      }
87  
88      public String getWidth() {
89          return (String) getStateHelper().eval(PropertyKeys.width, "100%");
90      }
91  
92      public void setWidth(String width) {
93          getStateHelper().put(PropertyKeys.width, width);
94      }
95  
96      public String getHeight() {
97          return (String) getStateHelper().eval(PropertyKeys.height, "auto");
98      }
99  
100     public void setHeight(String height) {
101         getStateHelper().put(PropertyKeys.height, height);
102     }
103 
104     public String getMode() {
105         return (String) getStateHelper().eval(PropertyKeys.mode, "classic");
106     }
107 
108     public void setMode(String mode) {
109         getStateHelper().put(PropertyKeys.mode, mode);
110     }
111 
112     public Object getLocale() {
113         return getStateHelper().eval(PropertyKeys.locale, null);
114     }
115 
116     public void setLocale(final Object locale) {
117         getStateHelper().put(PropertyKeys.locale, locale);
118     }
119 
120     public Locale calculateLocale() {
121         if (appropriateLocale == null) {
122             final FacesContext fc = FacesContext.getCurrentInstance();
123             appropriateLocale = LocaleUtils.resolveLocale(fc, getLocale(), getClientId(fc));
124         }
125         return appropriateLocale;
126     }
127 
128     @Override
129     public Object saveState(FacesContext context) {
130         // reset component for MyFaces view pooling
131         appropriateLocale = null;
132 
133         return super.saveState(context);
134     }
135 
136 }