KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > faces > core > component > UISimpleForm


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5 package org.exoplatform.faces.core.component;
6
7 import java.util.ArrayList JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import javax.faces.application.FacesMessage;
12 import javax.faces.component.UIComponent;
13 import javax.faces.context.FacesContext;
14 import javax.faces.validator.Validator;
15 import javax.faces.validator.ValidatorException;
16 import org.exoplatform.container.PortalContainer;
17 import org.exoplatform.faces.FacesConstants;
18 import org.exoplatform.faces.ValidatorManager;
19 import org.exoplatform.faces.core.Util;
20
21 /**
22  * Wed, Dec 22, 2003 @ 23:14
23  * @author: Tuan Nguyen
24  * @email: tuan08@users.sourceforge.net
25  * @version: $Id: UISimpleForm.java,v 1.23 2004/08/19 14:53:44 tuan08 Exp $
26  */

27 public class UISimpleForm extends UIGrid {
28   public static final String JavaDoc COMPONENT_FAMILY = "org.exoplatform.faces.core.component.UISimpleForm" ;
29   final public static String JavaDoc SIMPLE_FORM_RENDERER = "SimpleFormRenderer";
30   final static public short ALL_MODE = 0 ;
31   final static public short VIEW_MODE = 1 ;
32   final static public short EDIT_MODE = 2 ;
33   
34   private List JavaDoc hiddenInputs_;
35   private boolean error_;
36   private String JavaDoc formName_;
37   private String JavaDoc method_;
38   private String JavaDoc formId_;
39   private String JavaDoc userAction_;
40   protected List JavaDoc validators_ ;
41   private String JavaDoc script_;
42   private short mode_ ;
43
44   public UISimpleForm(String JavaDoc name, String JavaDoc method, String JavaDoc formId) {
45     hiddenInputs_ = new ArrayList JavaDoc(3);
46     formName_ = name;
47     method_ = method;
48     formId_ = formId;
49     if (formId_ == null) formId_ = name ;
50     setRendererType(SIMPLE_FORM_RENDERER);
51     setId(name) ;
52     mode_ = EDIT_MODE ;
53   }
54
55   public boolean hasError() { return error_; }
56   public void setError(boolean b) { error_ = b; }
57
58   public String JavaDoc getFormName() { return formName_; }
59
60   public String JavaDoc getMethod() { return method_; }
61
62   public String JavaDoc getFormId() { return formId_; }
63
64   public String JavaDoc getUserAction() { return userAction_; }
65
66   public void setUserAction(String JavaDoc userAction) { userAction_ = userAction; }
67
68   public List JavaDoc getHiddenInputs() { return hiddenInputs_;}
69
70   public String JavaDoc getScript() {
71     if (script_ == null) script_ = getNormalScript();
72     return script_;
73   }
74
75   public UISimpleForm add(UIHiddenInput input) {
76     getChildren().add(input);
77     hiddenInputs_.add(input);
78     return this;
79   }
80   
81   public void addValidator(Validator validator) {
82     if(validators_ == null ) validators_ = new ArrayList JavaDoc(3) ;
83     validators_.add(validator) ;
84   }
85   
86   public void addValidator(Class JavaDoc clazz) {
87     if(validators_ == null ) validators_ = new ArrayList JavaDoc(3) ;
88     ComponentUtil.addValidator(validators_, clazz) ;
89   }
90   
91   public short getMode() { return mode_ ; }
92   public void setMode(short mode) {
93     List JavaDoc children = getChildren() ;
94     for(int i = 0; i < children.size(); i++) {
95       Object JavaDoc child = children.get(i) ;
96       if(child instanceof UIInput) {
97         UIInput input = (UIInput) child ;
98         if(VIEW_MODE == mode) {
99           input.setReadonly(true) ;
100           mode_ = VIEW_MODE ;
101         } else {
102           input.setReadonly(false) ;
103           mode_ = EDIT_MODE ;
104         }
105       }
106     }
107   }
108   
109   public void decode(FacesContext context) {
110     getRenderer(context).decode(context, this) ;
111   }
112   
113   public void processDecodes(FacesContext context) {
114     error_ = false;
115     Map JavaDoc paramMap = context.getExternalContext().getRequestParameterMap();
116     String JavaDoc uicomponent = (String JavaDoc) paramMap.get(UICOMPONENT);
117     if (!getId().equals(uicomponent)) return;
118     List JavaDoc children = getChildren();
119     for (int i = 0; i < children.size(); i++) {
120       UIComponent child = (UIComponent) children.get(i);
121       child.processDecodes(context);
122     }
123     decode(context);
124   }
125
126   final public void processValidators(FacesContext context) {
127     if(!isRendered()) return ;
128     List JavaDoc children = getChildren() ;
129     for(int i = 0; i < children.size(); i++) {
130       UIComponent uiChild = (UIComponent) children.get(i) ;
131       if(uiChild.isRendered()) {
132         uiChild.processValidators(context) ;
133       }
134     }
135     if(context.getRenderResponse()) return ;
136     error_ = false ;
137     if(validators_ != null) {
138       Validator validator = null ;
139       try {
140         for(int i = 0; i < validators_.size(); i++) {
141           validator = (Validator) validators_.get(i) ;
142           validator.validate(context, this, null) ;
143         }
144       } catch(ValidatorException ex) {
145         error_ = true ;
146         FacesMessage message = ex.getFacesMessage() ;
147         InformationProvider iprovider = Util.findInformationProvider(this) ;
148         iprovider.addMessage(message) ;
149         context.renderResponse() ;
150       }
151     }
152   }
153   
154   private String JavaDoc getNormalScript() {
155     String JavaDoc script =
156     "<script type='text/javascript'>\n" +
157     " //ie bug you cannot have more than one button tag\n" +
158     " function submit_" + formName_ + "(action) {\n" +
159     " document." + formName_ + ".elements['" + FacesConstants.ACTION + "'].value = action ;\n" +
160     " document." + formName_ + ".submit();\n" +
161     " }\n" +
162     "</script>\n";
163     return script;
164   }
165   
166   public void setNoScript() {
167       script_ = "";
168   }
169   
170   public void setEnhancedScript(String JavaDoc taId, String JavaDoc width, String JavaDoc height) {
171     String JavaDoc script =
172     "<script type='text/javascript'>\n" +
173         " _editor_url = '/htmlarea';\n" +
174         " _editor_lang = 'en'; \n" +
175         "</script>" +
176     "<script type='text/javascript' SRC='/htmlarea/htmlarea.js'></script>\n" +
177     "<script type='text/javascript'>\n" +
178     " var editor = null;\n" +
179     " function initEditor() {\n" +
180     " editor = new HTMLArea('" + taId + "');\n" +
181     " var cfg = editor.config; // this is the default configuration\n" +
182     //" cfg.height = '" + height + "';\n" +
183
//" cfg.width = '" + width + "';\n" +
184
" editor.generate();\n" +
185     " editor._iframe.style.width = \"" + width + "\";" +
186     " editor._textArea.style.width = \"" + width + "\";" +
187     " editor._iframe.style.height = \"" + height + "\";" +
188     " editor._textArea.style.height = \"" + height + "\";" +
189     " }\n" +
190         
191     " //ie bug you cannot have more than one button tag\n" +
192     " function submit_" + formName_ + "(action) {\n" +
193     " document." + formName_ + ".elements['" + ACTION + "'].value = action ;\n" +
194     " document." + formName_ + ".onsubmit();\n" +
195     " document." + formName_ + ".submit();\n" +
196     " }\n" +
197     " initEditor() ;\n" +
198     "</script>\n";
199     script_ = script;
200   }
201   
202   public String JavaDoc getFamily() { return COMPONENT_FAMILY ; }
203   
204 }
Popular Tags