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.io.IOException;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Map;
28  
29  import javax.faces.component.UIComponent;
30  import javax.faces.context.FacesContext;
31  import javax.faces.context.ResponseWriter;
32  import javax.faces.convert.Converter;
33  
34  import org.primefaces.util.ComponentUtils;
35  import org.primefaces.util.WidgetBuilder;
36  
37  /**
38   * Base renderer for both the {@link MonacoEditorFramed framed} and {@link MonacoEditorInline inline} monaco editor.
39   *
40   * @since 10.0.0
41   */
42  abstract class MonacoEditorBaseRenderer<TEditor extends MonacoEditorBase>
43                                         extends MonacoEditorCommonRenderer<TEditor, org.primefaces.extensions.model.monacoeditor.EditorOptions> {
44      private static final String INPUT_SUFFIX = "_input";
45  
46      private static final List<String> PASSTHROUGH_ATTRS = Arrays.asList(//
47                  "alt", "accesskey", "autocomplete", //
48                  "cols", "dir", "lang", //
49                  "maxlength", "placeholder", "rows", //
50                  "size", "title" //
51      );
52  
53      protected MonacoEditorBaseRenderer(final Class<TEditor> clazz) {
54          super(clazz);
55      }
56  
57      protected abstract void addWidgetProperties(FacesContext context, WidgetBuilder wb, TEditor monacoEditor)
58                  throws IOException;
59  
60      @Override
61      public final void decode(final FacesContext context, final UIComponent component) {
62          final TEditor monacoEditor = componentClass.cast(component);
63  
64          // Do not allow modifications if component is not allowed to submit values.
65          // Read-only is fine, we should still accept the submitted value when read-only
66          if (monacoEditor.isDisabled()) {
67              return;
68          }
69  
70          // Decode value
71          final String clientId = monacoEditor.getClientId() + INPUT_SUFFIX;
72          final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
73          if (params.containsKey(clientId)) {
74              monacoEditor.setSubmittedValue(params.get(clientId));
75          }
76  
77          // Decode behaviors
78          decodeBehaviors(context, component);
79      }
80  
81      @Override
82      protected final void encodeHiddenInput(final FacesContext context, final TEditor monacoEditor) throws IOException {
83          final ResponseWriter writer = context.getResponseWriter();
84          final String clientId = monacoEditor.getClientId();
85  
86          writer.startElement("div", null);
87          writer.writeAttribute("class", "ui-helper-hidden-accessible", null);
88  
89          writer.startElement("textarea", null);
90          writer.writeAttribute("id", clientId + INPUT_SUFFIX, null);
91          writer.writeAttribute("name", clientId + INPUT_SUFFIX, null);
92          writer.writeAttribute("autocomplete", "off", null);
93          if (monacoEditor.isReadonly()) {
94              writer.writeAttribute("readonly", "readonly", null);
95          }
96          if (monacoEditor.isDisabled()) {
97              writer.writeAttribute("disabled", "disabled", null);
98          }
99          renderPassThruAttributes(context, monacoEditor, PASSTHROUGH_ATTRS);
100 
101         final String valueToRender = ComponentUtils.getValueToRender(context, monacoEditor);
102         if (valueToRender != null) {
103             writer.writeText(valueToRender, null);
104         }
105         writer.endElement("textarea");
106 
107         writer.endElement("div");
108     }
109 
110     @Override
111     public final Object getConvertedValue(final FacesContext context, final UIComponent component,
112                 final Object submittedValue) {
113         final TEditor monacoEditor = componentClass.cast(component);
114         final String value = (String) submittedValue;
115         final Converter<?> converter = ComponentUtils.getConverter(context, monacoEditor);
116 
117         if (converter != null) {
118             return converter.getAsObject(context, monacoEditor, value);
119         }
120 
121         return value;
122     }
123 
124     @Override
125     protected final void addBaseWidgetProperties(final FacesContext context, final WidgetBuilder wb, final TEditor monacoEditor)
126                 throws IOException {
127         addWidgetProperties(context, wb, monacoEditor);
128     }
129 
130     @Override
131     protected boolean isEntireEditorDisabled(final TEditor monacoEditor) {
132         return monacoEditor.isDisabled();
133     }
134 
135     @Override
136     protected String getLanguage(final TEditor monacoEditor) {
137         return monacoEditor.getEditorOptions().getLanguage();
138     }
139 }