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