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.monacoeditor;
23  
24  import java.util.Locale;
25  import java.util.Map;
26  
27  import javax.faces.component.behavior.ClientBehaviorHolder;
28  import javax.faces.component.html.HtmlInputTextarea;
29  import javax.faces.context.FacesContext;
30  
31  import org.primefaces.component.api.PrimeClientBehaviorHolder;
32  import org.primefaces.component.api.Widget;
33  import org.primefaces.util.LocaleUtils;
34  
35  /**
36   * Base component for both the standalone and diff monaco code editor widget, in its framed and inline variants.
37   *
38   * @since 11.1.0
39   */
40  @SuppressWarnings("java:S110")
41  public abstract class MonacoEditorCommon<TEditorOpts> extends HtmlInputTextarea
42              implements ClientBehaviorHolder, PrimeClientBehaviorHolder, Widget {
43  
44      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
45  
46      static final boolean DEFAULT_AUTO_RESIZE = false;
47      static final String DEFAULT_BASENAME = "";
48      static final String DEFAULT_DIRECTORY = "";
49      static final boolean DEFAULT_DISABLED = false;
50      static final String DEFAULT_EXTENSION = "";
51      static final String DEFAULT_HEIGHT = "600px";
52      static final String DEFAULT_LANGUAGE = "plaintext";
53      static final String DEFAULT_PLACEHOLDER = "";
54      static final boolean DEFAULT_READONLY = false;
55      static final String DEFAULT_SCHEME = "inmemory";
56      static final String DEFAULT_TABINDEX = null;
57      static final String DEFAULT_WIDTH = "200px";
58  
59      private Locale appropriateLocale;
60      private final Class<TEditorOpts> editorOptionsClass;
61  
62      protected MonacoEditorCommon(final String rendererType, final Class<TEditorOpts> editorOptionsClass) {
63          this.editorOptionsClass = editorOptionsClass;
64          setRendererType(rendererType);
65      }
66  
67      public Locale calculateLocale() {
68          if (appropriateLocale == null) {
69              final FacesContext context = FacesContext.getCurrentInstance();
70              appropriateLocale = LocaleUtils.resolveLocale(context, getLocale(), getClientId(context));
71          }
72          return appropriateLocale;
73      }
74  
75      public final String getBasename() {
76          return (String) getStateHelper().eval(BaseEditorPropertyKeys.basename, DEFAULT_BASENAME);
77      }
78  
79      @SuppressWarnings("unchecked")
80      public final Map<String, org.primefaces.extensions.model.monacoeditor.EditorStandaloneTheme> getCustomThemes() {
81          return (Map<String, org.primefaces.extensions.model.monacoeditor.EditorStandaloneTheme>) getStateHelper().eval(
82                      BaseEditorPropertyKeys.customThemes, null);
83      }
84  
85      public final String getDirectory() {
86          return (String) getStateHelper().eval(BaseEditorPropertyKeys.directory, DEFAULT_DIRECTORY);
87      }
88  
89      public final TEditorOpts getEditorOptions() {
90          final TEditorOpts editorOptions = editorOptionsClass.cast(
91                      getStateHelper().eval(BaseEditorPropertyKeys.editorOptions, null));
92          return editorOptions != null ? editorOptions : newEditorOptions();
93      }
94  
95      public final String getExtension() {
96          return (String) getStateHelper().eval(BaseEditorPropertyKeys.extension, DEFAULT_EXTENSION);
97      }
98  
99      @Override
100     public final String getFamily() {
101         return COMPONENT_FAMILY;
102     }
103 
104     public final String getHeight() {
105         return (String) getStateHelper().eval(BaseEditorPropertyKeys.height, DEFAULT_HEIGHT);
106     }
107 
108     public final String getOninitialized() {
109         return (String) getStateHelper().eval(BaseEditorPropertyKeys.oninitialized, null);
110     }
111 
112     public final String getOnpaste() {
113         return (String) getStateHelper().eval(BaseEditorPropertyKeys.onpaste, null);
114     }
115 
116     public final String getPlaceholder() {
117         return (String) getStateHelper().eval(BaseEditorPropertyKeys.placeholder, null);
118     }
119 
120     public final String getScheme() {
121         return (String) getStateHelper().eval(BaseEditorPropertyKeys.scheme, DEFAULT_SCHEME);
122     }
123 
124     public Object getLocale() {
125         return getStateHelper().eval(BaseEditorPropertyKeys.locale, null);
126     }
127 
128     public final String getLocaleUrl() {
129         return (String) getStateHelper().eval(BaseEditorPropertyKeys.localeUrl, null);
130     }
131 
132     public final String getWidgetVar() {
133         return (String) getStateHelper().eval(BaseEditorPropertyKeys.widgetVar, null);
134     }
135 
136     public final String getWidth() {
137         return (String) getStateHelper().eval(BaseEditorPropertyKeys.width, DEFAULT_WIDTH);
138     }
139 
140     public final boolean isAutoResize() {
141         return (Boolean) getStateHelper().eval(BaseEditorPropertyKeys.autoResize, DEFAULT_AUTO_RESIZE);
142     }
143 
144     public final void setAutoResize(final boolean autoResize) {
145         getStateHelper().put(BaseEditorPropertyKeys.autoResize, autoResize);
146     }
147 
148     public final void setBasename(final String basename) {
149         getStateHelper().put(BaseEditorPropertyKeys.basename, basename);
150     }
151 
152     public final void setCustomThemes(
153                 final Map<String, org.primefaces.extensions.model.monacoeditor.EditorStandaloneTheme> customThemes) {
154         getStateHelper().put(BaseEditorPropertyKeys.customThemes, customThemes);
155     }
156 
157     public final void setDirectory(final String directory) {
158         getStateHelper().put(BaseEditorPropertyKeys.directory, directory);
159     }
160 
161     public final void setEditorOptions(final org.primefaces.extensions.model.monacoeditor.EditorOptions editorOptions) {
162         getStateHelper().put(BaseEditorPropertyKeys.editorOptions, editorOptions);
163     }
164 
165     public final void setExtension(final String extension) {
166         getStateHelper().put(BaseEditorPropertyKeys.extension, extension);
167     }
168 
169     public final void setHeight(final String height) {
170         getStateHelper().put(BaseEditorPropertyKeys.height, height);
171     }
172 
173     public final void setOninitialized(final String oninitialized) {
174         getStateHelper().put(BaseEditorPropertyKeys.oninitialized, oninitialized);
175     }
176 
177     public final void setOnpaste(final String onpaste) {
178         getStateHelper().put(BaseEditorPropertyKeys.onpaste, onpaste);
179     }
180 
181     public final void setPlaceholder(final String placeholder) {
182         getStateHelper().put(BaseEditorPropertyKeys.placeholder, placeholder);
183     }
184 
185     public final void setScheme(final String scheme) {
186         getStateHelper().put(BaseEditorPropertyKeys.scheme, scheme);
187     }
188 
189     public final void setLocale(final Object locale) {
190         getStateHelper().put(BaseEditorPropertyKeys.locale, locale);
191     }
192 
193     public final void setLocaleUrl(final String localeUrl) {
194         getStateHelper().put(BaseEditorPropertyKeys.localeUrl, localeUrl);
195     }
196 
197     public final void setWidgetVar(final String widgetVar) {
198         getStateHelper().put(BaseEditorPropertyKeys.widgetVar, widgetVar);
199     }
200 
201     public final void setWidth(final String width) {
202         getStateHelper().put(BaseEditorPropertyKeys.width, width);
203     }
204 
205     @Override
206     public Object saveState(final FacesContext context) {
207         // reset component for MyFaces view pooling
208         appropriateLocale = null;
209 
210         return super.saveState(context);
211     }
212 
213     private TEditorOpts newEditorOptions() {
214         try {
215             return editorOptionsClass.getConstructor().newInstance();
216         }
217         catch (final Exception e) {
218             return null;
219         }
220     }
221 }