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.field.factory;
35  
36  import static com.google.common.base.Enums.getIfPresent;
37  import static org.apache.commons.io.FilenameUtils.getExtension;
38  import static org.apache.commons.lang3.StringUtils.isNotBlank;
39  import static org.vaadin.aceeditor.AceMode.forFileEnding;
40  
41  import info.magnolia.context.MgnlContext;
42  import info.magnolia.objectfactory.ComponentProvider;
43  import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
44  import info.magnolia.ui.field.CodeFieldDefinition;
45  
46  import java.util.Locale;
47  import java.util.Optional;
48  
49  import javax.inject.Inject;
50  
51  import org.vaadin.aceeditor.AceEditor;
52  import org.vaadin.aceeditor.AceMode;
53  
54  import com.vaadin.server.Sizeable;
55  
56  /**
57   * Creates and initializes a code field, based on the {@link AceEditor} add-on for Vaadin.
58   */
59  public class CodeFieldFactory extends AbstractFieldFactory<CodeFieldDefinition, String, AceEditor> {
60  
61      private static final String FREEMARKER_LANGUAGE = "freemarker";
62      private static final String ACE_EDITOR_FTL_ID = "ftl";
63      private static final String ACE_EDITOR_RESOURCES = "/.resources/ace/";
64  
65      private AceEditor field;
66  
67      private final CodeFieldDefinition definition;
68  
69      @Inject
70      public CodeFieldFactory(CodeFieldDefinition definition, ComponentProvider componentProvider, Locale locale, I18NAuthoringSupport i18NAuthoringSupport) {
71          super(definition, componentProvider, locale, i18NAuthoringSupport);
72          this.definition = definition;
73      }
74  
75      @Override
76      protected AceEditor createFieldComponent() {
77          field = newAceEditor();
78  
79          // Set style
80          field.setStyleName("textcodefield");
81          field.setUseWorker(false);
82  
83          return field;
84      }
85  
86      /**
87       * @return newly constructed {@link AceEditor}.
88       */
89      protected AceEditor newAceEditor() {
90          AceEditor aceEditor = new AceEditor();
91          if (MgnlContext.isWebContext()) {
92              String aceEditorResourcePath = MgnlContext.getContextPath() + ACE_EDITOR_RESOURCES;
93              aceEditor.setModePath(aceEditorResourcePath);
94              aceEditor.setWorkerPath(aceEditorResourcePath);
95              aceEditor.setThemePath(aceEditorResourcePath);
96          }
97  
98          // If language is set, then we don't want to conflict with it.
99          if (isNotBlank(definition.getLanguage())) {
100             aceEditor.setMode(getModeType(definition.getLanguage()));
101         } else if (isNotBlank(definition.getFileNameProperty())) {
102             String fileName = field.getValue();
103             if (fileName != null) {
104                 String extension = getExtension(fileName);
105                 AceMode mode = getAceModeByFileExtension(extension);
106                 if (mode != null) {
107                     aceEditor.setMode(mode);
108                 }
109             }
110         }
111 
112         if (definition.getHeight() > 0) {
113             aceEditor.setHeight(definition.getHeight(), Sizeable.Unit.PIXELS);
114         }
115 
116         return aceEditor;
117     }
118 
119     /**
120      * Get Ace-editor compliant mode type name for a language.
121      *
122      * TODO: currently only 'freemarker' -> 'ftl' case is handled, otherwise the method returns the string from definition.
123      * If needed we could craft our own mapping from human-readable language names into the Ace editor mode identifiers.
124      */
125     private String getModeType(String language) {
126         return FREEMARKER_LANGUAGE.equals(language) ? ACE_EDITOR_FTL_ID : language;
127     }
128 
129     /**
130      * Get the {@link AceMode} by file extension.
131      * <p>
132      * First tries to match the given extension against an AceMode value, otherwise looks into AceMode's additional mappings (<code>endingModeMap</code>).
133      */
134     private AceMode getAceModeByFileExtension(String extension) {
135         // Trying the get AceMode from the Enum
136         Optional<AceMode> aceModeValue = getIfPresent(AceMode.class, extension).toJavaUtil();
137         // Trying to get AceMode from the AceMode.endingModeMap
138         return aceModeValue.orElseGet(() -> forFileEnding(extension));
139     }
140 }