View Javadoc
1   /**
2    * This file Copyright (c) 2010-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.form.field.definition;
35  
36  import info.magnolia.repository.RepositoryConstants;
37  
38  import java.util.ArrayList;
39  import java.util.Comparator;
40  import java.util.List;
41  
42  import com.vaadin.v7.shared.ui.combobox.FilteringMode;
43  
44  /**
45   * Basic definition class for various types of select fields.
46   * <p>
47   * Offers two distinct ways of populating selection options:
48   * <ul>
49   * <li>an explicit set of {@link SelectFieldOptionDefinition option definitions} can be provided via {@link #setOptions(List)}</li>
50   * <li><strong>if no explicit options are specified</strong> - option set can be resolved from a remote path in JCR repository by means of 
51   * {@link #setPath(String)}, {@link #setLabelProperty(String)} and {@link #setValueProperty(String)} methods</li>
52   * </ul>
53   */
54  public class SelectFieldDefinition extends ConfiguredFieldDefinition {
55  
56      public static final String OPTION_VALUE_PROPERTY_NAME = "value";
57      public static final String OPTION_NAME_PROPERTY_NAME = "name";
58      public static final String OPTION_SELECTED_PROPERTY_NAME = "selected";
59      public static final String OPTION_ICONSRC_PROPERTY_NAME = "iconSrc";
60      public static final String OPTION_LABEL_PROPERTY_NAME = "label";
61  
62      public static final String DEFAULT_REPOSITORY_NAME = RepositoryConstants.CONFIG;
63  
64      private String path;
65  
66      private String repository = DEFAULT_REPOSITORY_NAME;
67  
68      private String valueProperty = OPTION_VALUE_PROPERTY_NAME;
69  
70      private String labelProperty = OPTION_LABEL_PROPERTY_NAME;
71  
72      private FilteringMode filteringMode = FilteringMode.OFF;
73  
74      private boolean sortOptions = true;
75  
76      private Class<? extends Comparator<SelectFieldOptionDefinition>> comparatorClass;
77  
78      private List<SelectFieldOptionDefinition> options = new ArrayList<SelectFieldOptionDefinition>();
79  
80      private boolean textInputAllowed = false;
81  
82      private int pageLength = 0;
83  
84      public List<SelectFieldOptionDefinition> getOptions() {
85          return options;
86      }
87  
88      public void setOptions(List<SelectFieldOptionDefinition> options) {
89          this.options = options;
90      }
91  
92      public void addOption(SelectFieldOptionDefinition option) {
93          options.add(option);
94      }
95  
96      public String getPath() {
97          return path;
98      }
99  
100     public void setPath(String path) {
101         this.path = path;
102     }
103 
104     public String getRepository() {
105         return repository;
106     }
107 
108     public void setRepository(String repository) {
109         this.repository = repository;
110     }
111 
112     public String getValueProperty() {
113         return valueProperty;
114     }
115 
116     public void setValueProperty(String valueProperty) {
117         this.valueProperty = valueProperty;
118     }
119 
120     public String getLabelProperty() {
121         return labelProperty;
122     }
123 
124     public void setLabelProperty(String labelProperty) {
125         this.labelProperty = labelProperty;
126     }
127 
128     /**
129      * Defines the filtering mode (off, startswith, contains) of this select field. Additionally, mind that filtering
130      * mode is only enabled with paging, i.e. when the pageLength property is greater than 0.
131      *
132      * @return a member of the FilteringMode enum. Defaults to <code>off</code>.
133      * @see com.vaadin.shared.ui.combobox.FilteringMode
134      */
135     public FilteringMode getFilteringMode() {
136         return filteringMode;
137     }
138 
139     public void setFilteringMode(FilteringMode filteringMode) {
140         this.filteringMode = filteringMode;
141     }
142 
143     /**
144      * @deprecated since 5.3.15, use {@link #setFilteringMode(FilteringMode)} instead.
145      */
146     @Deprecated
147     public void setFilteringMode(int filteringMode) {
148         switch (filteringMode) {
149         case 1:
150             this.filteringMode = FilteringMode.CONTAINS;
151         case 2:
152             this.filteringMode = FilteringMode.STARTSWITH;
153         default:
154             this.filteringMode = FilteringMode.OFF;
155         }
156     }
157 
158     /**
159      * By default, options labels are sorted alphabetically (in ascending order) unless <code>false</code> is specified. In that case, the JCR "natural order" should be kept.
160      */
161     public void setSortOptions(boolean sortOptions) {
162         this.sortOptions = sortOptions;
163     }
164 
165     public boolean isSortOptions() {
166         return sortOptions;
167     }
168 
169     public Class<? extends Comparator<SelectFieldOptionDefinition>> getComparatorClass() {
170         return comparatorClass;
171     }
172 
173     public void setComparatorClass(Class<? extends Comparator<SelectFieldOptionDefinition>> comparatorClass) {
174         this.comparatorClass = comparatorClass;
175     }
176 
177     public boolean isTextInputAllowed() {
178         return textInputAllowed;
179     }
180 
181     public void setTextInputAllowed(boolean textInputAllowed) {
182         this.textInputAllowed = textInputAllowed;
183     }
184 
185     public int getPageLength() {
186         return pageLength;
187     }
188 
189     public void setPageLength(int pageLength) {
190         this.pageLength = pageLength;
191     }
192 }