KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.IOException JavaDoc;
8 import java.util.List JavaDoc;
9 import java.util.Map JavaDoc;
10 import javax.faces.component.UICommand;
11 import javax.faces.component.UIComponent;
12 import javax.faces.context.ExternalContext;
13 import javax.faces.context.FacesContext;
14 import javax.faces.render.Renderer;
15 import org.apache.commons.logging.Log;
16 import org.exoplatform.Constants;
17 import org.exoplatform.container.PortalContainer;
18 import org.exoplatform.container.SessionContainer;
19 import org.exoplatform.faces.ActionListenerManager;
20 import org.exoplatform.faces.UIComponentFactory;
21 import org.exoplatform.faces.context.PortletExternalContext;
22 import org.exoplatform.faces.core.Util;
23 import org.exoplatform.faces.core.event.ExoActionEvent;
24 import org.exoplatform.faces.core.event.ExoActionListener;
25 import org.exoplatform.faces.core.renderer.html.Decorator;
26 import org.exoplatform.portal.session.RequestInfo;
27 import org.exoplatform.services.log.LogService;
28 import org.exoplatform.text.template.DataHandler;
29 import org.exoplatform.text.template.xhtml.Element;
30 /**
31  * Wed, Dec 22, 2003 @ 23:14
32  * @author: Tuan Nguyen
33  * @email: tuan08@users.sourceforge.net
34  * @version: $Id: UIExoCommand.java,v 1.36 2004/10/27 02:52:15 tuan08 Exp $
35  */

36 public class UIExoCommand extends UICommand implements UIExoComponent {
37
38   protected String JavaDoc baseURL_ ;
39   private String JavaDoc clazz_;
40   private Decorator decorator_ ;
41   private transient Renderer cacheRenderer_ ;
42   
43   public String JavaDoc getClazz() { return clazz_ ; }
44   public UIExoComponent setClazz(String JavaDoc clazz) {
45     clazz_ = clazz ;
46     return this ;
47   }
48   
49   public String JavaDoc getFamily() { return UIExoComponent.COMPONENT_FAMILY ; }
50
51   public boolean canDecodeInvalidState() { return true ; }
52   
53   public UIExoComponent setDecorator(Decorator decorator) {
54     decorator_ = decorator ;
55     return this;
56   }
57   
58   public void decode(FacesContext context) {
59     Map JavaDoc paramMap = context.getExternalContext().getRequestParameterMap() ;
60     String JavaDoc uicomponent = (String JavaDoc) paramMap.get(UICOMPONENT) ;
61     if (uicomponent != null && uicomponent.equals(getId())) {
62         String JavaDoc action = (String JavaDoc) paramMap.get(ACTION) ;
63         if(action != null) {
64             broadcast(new ExoActionEvent(this, action, paramMap)) ;
65         }
66     }
67   }
68   
69   public void processDecodes(FacesContext context) {
70     List JavaDoc children = getChildren() ;
71     for(int i = 0 ; i < children.size(); i++) {
72       UIComponent child = (UIComponent) children.get(i);
73       if (child.isRendered()) {
74         child.processDecodes(context) ;
75         if (context.getRenderResponse()) {
76             return ;
77         }
78       }
79     }
80     decode(context) ;
81   }
82   
83   public void setId(String JavaDoc id) {
84     super.setId(id) ;
85     baseURL_ = null ;
86   }
87   
88   public void setRendererType(String JavaDoc type) {
89     cacheRenderer_ = null ;
90     super.setRendererType(type) ;
91   }
92   
93   protected Renderer getRenderer(FacesContext context) {
94     if(cacheRenderer_ == null) {
95       cacheRenderer_ = Util.getRenderer(context, getRendererType(), getFamily());
96     }
97     return cacheRenderer_ ;
98   }
99   
100   public UIExoComponent findComponentById(String JavaDoc id) {
101     return ComponentUtil.findComponentById(this, id) ;
102   }
103   
104   public UIExoComponent findRenderedComponentById(String JavaDoc id) {
105     return ComponentUtil.findRenderedComponentById(this, id) ;
106   }
107   
108   public Object JavaDoc getChildComponentOfType(Class JavaDoc classType) {
109     return ComponentUtil.getChildComponentOfType(this, classType) ;
110   }
111   
112   public UIComponent getAncestorOfType(Class JavaDoc classType) {
113     return ComponentUtil.getAncestorOfType(this, classType) ;
114   }
115   
116   public void setRenderedComponent(String JavaDoc id) {
117     ComponentUtil.setRenderedComponent(this, id) ;
118   }
119
120   public void setRenderedComponent(Class JavaDoc type) {
121     ComponentUtil.setRenderedComponent(this, type) ;
122   }
123   
124   public void setRenderedSibling(Class JavaDoc type) {
125     ComponentUtil.setRenderedSibling(this, type) ;
126   }
127   
128   public UIComponent getSibling(Class JavaDoc type) {
129     return ComponentUtil.getSibling(this, type) ;
130   }
131
132   public List JavaDoc findDescendantsOfType(Class JavaDoc type) {
133     return ComponentUtil.findDescendantsOfType(this, type) ;
134   }
135   
136   public String JavaDoc getBaseURL(FacesContext context) {
137     if (baseURL_ == null) {
138       ExternalContext econtext = context.getExternalContext() ;
139       if(econtext instanceof PortletExternalContext) {
140         baseURL_ = econtext.encodeActionURL("") ;
141         baseURL_ += Constants.AMPERSAND + UICOMPONENT + "=" + getId() ;
142       } else {
143         RequestInfo rinfo = (RequestInfo)SessionContainer.getComponent(RequestInfo.class);
144         baseURL_ = rinfo.getOwnerURI();
145         baseURL_ += "?" + UICOMPONENT + "=" + getId() ;
146       }
147     }
148     return baseURL_ ;
149   }
150   
151   public String JavaDoc getBaseURL() {
152     if(baseURL_ != null) return baseURL_ ;
153     return getBaseURL(FacesContext.getCurrentInstance());
154   }
155
156   static public Log getLog(String JavaDoc name) {
157     PortalContainer manager = PortalContainer.getInstance();
158     LogService service = (LogService) manager.getComponentInstanceOfType(LogService.class) ;
159     Log log = service.getLog(name);
160     return log ;
161   }
162   
163   
164   protected boolean hasOwnerRole(String JavaDoc user) {
165     if(user == null) return false ;
166     FacesContext context = FacesContext.getCurrentInstance() ;
167         ExternalContext econtext = context.getExternalContext() ;
168         if(user.equals(econtext.getRemoteUser())) {
169             return true ;
170         }
171     return false ;
172   }
173   
174   protected boolean hasRole(String JavaDoc role) {
175     FacesContext context = FacesContext.getCurrentInstance() ;
176         ExternalContext econtext = context.getExternalContext() ;
177         return econtext.isUserInRole(role) ;
178   }
179   
180   public void addActionListener(Class JavaDoc clazz, String JavaDoc actionToListen) {
181     PortalContainer pcontainer = PortalContainer.getInstance() ;
182     ActionListenerManager manager =
183       (ActionListenerManager) pcontainer.getComponentInstanceOfType(ActionListenerManager.class) ;
184     ExoActionListener result = manager.getActionListener(clazz, actionToListen) ;
185     addActionListener(result) ;
186   }
187   
188   public UIExoComponent addChild(Class JavaDoc cl) throws Exception JavaDoc {
189     UIExoComponent uiComponent = UIComponentFactory.createComponent(cl) ;
190     getChildren().add(uiComponent) ;
191     uiComponent.registerActionListener(this) ;
192     uiComponent.registerComponentObserver(this) ;
193     return uiComponent ;
194   }
195   
196   public void decorate(FacesContext context) throws IOException JavaDoc {
197     if(decorator_ != null) {
198       decorator_.decorate(context, this) ;
199     } else {
200       encodeBegin(context) ;
201       encodeChildren(context) ;
202       encodeEnd(context) ;
203     }
204   }
205   
206   public void registerActionListener(UIExoComponent parent) {
207     
208   }
209   
210   public void registerComponentObserver(UIExoComponent parent) {
211     
212   }
213   
214   public DataHandler getDataHandler(Class JavaDoc dataType) {
215     throw new RuntimeException JavaDoc("You need to override this method and return at least one default data handler") ;
216   }
217   
218   public Element getTemplate() {
219     throw new RuntimeException JavaDoc("You need to override this method") ;
220   }
221 }
Popular Tags