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