View Javadoc
1   /*
2    * Copyright (C) 2003-2010 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.portal.resource.SkinConfig;
22  import org.exoplatform.portal.resource.SkinService;
23  import org.exoplatform.portal.webui.util.Util;
24  import org.exoplatform.services.wcm.utils.WCMCoreUtils;
25  import org.exoplatform.webui.form.UIFormInputBase;
26  import org.exoplatform.webui.form.UIFormRichtextInput;
27  
28  import java.util.Collection;
29  
30  /**
31   * Created by The eXo Platform SAS
32   * Author : Phan Le Thanh Chuong
33   *          chuong.phan@exoplatform.com; phan.le.thanh.chuong@gmail.com
34   * May 10, 2010
35   */
36  public class UIFormRichtextField extends DialogFormField {
37  
38    private final String TOOLBAR = "toolbar";
39    private final String WIDTH = "width";
40    private final String HEIGHT = "height";
41    private final String ENTERMODE = "enterMode";
42    private final String SHIFT_ENTER_MODE = "shiftEnterMode";
43  
44    private String toolbar;
45    private String width;
46    private String height;
47    private String enterMode;
48    private String shiftEnterMode;
49  
50    public UIFormRichtextField(String name, String label, String[] arguments) {
51      super(name, label, arguments);
52    }
53  
54    @SuppressWarnings("unchecked")
55    public <T extends UIFormInputBase> T createUIFormInput() throws Exception {
56      UIFormRichtextInput richtext = new UIFormRichtextInput(name, name, defaultValue);
57      setPredefineOptions();
58      richtext.setToolbar(toolbar);
59      richtext.setWidth(width);
60      richtext.setHeight(height);
61      richtext.setEnterMode(enterMode);
62      richtext.setShiftEnterMode(shiftEnterMode);
63      richtext.setIgnoreParserHTML(true);
64  
65      StringBuffer contentsCss = new StringBuffer();
66      contentsCss.append("[");
67      SkinService skinService = WCMCoreUtils.getService(SkinService.class);
68      String skin = Util.getUIPortalApplication().getUserPortalConfig().getPortalConfig().getSkin();
69      String portal = Util.getUIPortal().getName();
70      Collection<SkinConfig> portalSkins = skinService.getPortalSkins(skin);
71      SkinConfig customSkin = skinService.getSkin(portal, Util.getUIPortalApplication()
72                                                              .getUserPortalConfig()
73                                                              .getPortalConfig()
74                                                              .getSkin());
75      if (customSkin != null) portalSkins.add(customSkin);
76      for (SkinConfig portalSkin : portalSkins) {
77        contentsCss.append("'").append(portalSkin.createURL(Util.getPortalRequestContext().getControllerContext())).append("',");
78      }
79      contentsCss.append("'/commons-extension/ckeditor/contents.css'");
80      contentsCss.append("]");    
81      richtext.setCss(contentsCss.toString());
82      
83      if(validateType != null) {
84        DialogFormUtil.addValidators(richtext, validateType);
85      }
86      return (T)richtext;
87    }
88  
89    private void setPredefineOptions() {
90      if (options == null) return;
91      for(String option: options.split(",")) {
92        String[] entry = option.split(":");
93        if(TOOLBAR.equals(entry[0])) {
94          toolbar = entry[1];
95        } else if(WIDTH.equals(entry[0])) {
96          width = entry[1];
97        } else if(HEIGHT.equals(entry[0])) {
98          height = entry[1];
99        } else if(ENTERMODE.equals(entry[0])) {
100         enterMode = entry[1];
101       } else if(SHIFT_ENTER_MODE.equals(entry[0])) {
102         shiftEnterMode = entry[1];
103       }
104     }
105   }
106 
107 }