View Javadoc
1   /*
2    * Copyright (C) 2003-2008 eXo Platform SAS.
3    *
4    * This program is free software; you can redistribute it and/or
5    * modify it under the terms of the GNU Affero General Public License
6    * as published by the Free Software Foundation; either version 3
7    * of the License, or (at your option) any later version.
8    *
9    * This program is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12   * GNU General Public License for more details.
13   *
14   * You should have received a copy of the GNU General Public License
15   * along with this program; if not, see<http://www.gnu.org/licenses/>.
16   */
17  package org.exoplatform.ecm.webui.form.field;
18  
19  import org.exoplatform.ecm.webui.form.DialogFormField;
20  import org.exoplatform.ecm.webui.utils.DialogFormUtil;
21  import org.exoplatform.webui.form.UIFormInputBase;
22  import org.exoplatform.webui.form.wysiwyg.UIFormWYSIWYGInput;
23  
24  /**
25   * Created by The eXo Platform SAS
26   * @author : Hoa.Pham
27   *          hoa.pham@exoplatform.com
28   * Jun 23, 2008
29   */
30  public class UIFormWYSIWYGField extends DialogFormField {
31    private final String TOOBAR = "toolbar";
32    private final String SOURCE_MODE = "SourceModeOnStartup";
33    private final String WIDTH = "width";
34    private final String HEIGHT = "height";
35  
36    private String toolbarName;
37    private boolean sourceModeOnStartup = false;
38    private String width;
39    private String height;
40  
41    public UIFormWYSIWYGField(String name, String label, String[] arguments) {
42      super(name, label, arguments);
43    }
44  
45    @SuppressWarnings("unchecked")
46    public <T extends UIFormInputBase> T createUIFormInput() throws Exception {
47      UIFormWYSIWYGInput wysiwyg = new UIFormWYSIWYGInput(name, name, defaultValue);
48      parseOptions();
49      wysiwyg.setToolBarName(toolbarName);
50      wysiwyg.setSourceModeOnStartup(sourceModeOnStartup);
51      wysiwyg.setWidth(width);
52      wysiwyg.setHeight(height);
53      if(validateType != null) {
54        DialogFormUtil.addValidators(wysiwyg, validateType);
55      }
56      return (T)wysiwyg;
57    }
58  
59    private void parseOptions() {
60      if("basic".equals(options)) {
61        toolbarName = UIFormWYSIWYGInput.BASIC_TOOLBAR;
62      }else if("default".equals(options) || options == null) {
63        toolbarName = UIFormWYSIWYGInput.DEFAULT_TOOLBAR;
64      }else if(options.indexOf(",")>0){
65        for(String s: options.split(",")) {
66          setPredefineOptions(s);
67        }
68      }else if(options.indexOf(":")>0) {
69        setPredefineOptions(options);
70      }else {
71        toolbarName = UIFormWYSIWYGInput.DEFAULT_TOOLBAR;
72        sourceModeOnStartup = false;
73      }
74    }
75  
76    private void setPredefineOptions(String option) {
77      String[] entry = option.split(":");
78      if(TOOBAR.equals(entry[0])) {
79        toolbarName = entry[1];
80      }else if(SOURCE_MODE.equals(entry[0])) {
81        sourceModeOnStartup = Boolean.parseBoolean(entry[1]);
82      }else if(WIDTH.equals(entry[0])) {
83        width = entry[1];
84      }else if(HEIGHT.equals(entry[0])) {
85        height = entry[1];
86      }
87    }
88  }