KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portal > faces > component > UIPortlet


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.portal.faces.component;
6
7 import java.util.*;
8 import javax.portlet.PortletMode;
9 import javax.portlet.WindowState;
10 import org.exoplatform.Constants;
11 import org.exoplatform.commons.utils.ExpressionUtil;
12 import org.exoplatform.commons.utils.Formater;
13 import org.exoplatform.container.PortalContainer;
14 import org.exoplatform.container.SessionContainer;
15 import org.exoplatform.portal.PortalConstants;
16 import org.exoplatform.portal.faces.listener.portlet.PortletActionListener;
17 import org.exoplatform.portal.faces.listener.share.DeleteActionListener;
18 import org.exoplatform.portal.faces.listener.share.EditPropertiesActionListener;
19 import org.exoplatform.portal.faces.listener.share.MoveActionListener;
20 import org.exoplatform.portal.session.PortalResources;
21 import org.exoplatform.services.portal.model.Component;
22 import org.exoplatform.services.portal.model.Container;
23 import org.exoplatform.services.portal.skin.SkinConfigService;
24 import org.exoplatform.services.portal.skin.model.Style;
25 import org.exoplatform.services.portletcontainer.PortletContainerService;
26 import org.exoplatform.services.portletcontainer.pci.ExoWindowID;
27 import org.exoplatform.services.portletcontainer.pci.PortletData;
28 import org.exoplatform.services.portletcontainer.pci.model.Supports;
29 /**
30  * Fri, May 30, 2003 @
31  * @author : Mestrallet Benjamin
32  * @email: benjmestrallet@users.sourceforge.net
33  * @author: Tuan Nguyen
34  * @email: tuan08@users.sourceforge.net
35  * @version: $Id: UIPortlet.java,v 1.14 2004/11/01 21:06:49 tuan08 Exp $
36  */

37 public class UIPortlet extends UIBasicComponent {
38   public static final String JavaDoc DEFAULT_PORTLET_RENDERER = "PortletRenderer";
39   //states
40
public static final String JavaDoc MINIMIZED_VIEW = "min_view";
41   public static final String JavaDoc NORMAL_VIEW = "normal_view";
42   public static final String JavaDoc MAXIMIZED_VIEW = "max_view";
43   //modes
44
public static final String JavaDoc PORTLET_VIEW_MODE = "view";
45   public static final String JavaDoc PORTLET_EDIT_MODE = "edit";
46   public static final String JavaDoc PORTLET_CONFIG_MODE = "config";
47   public static final String JavaDoc PORTLET_HELP_MODE = "help";
48   public static final String JavaDoc PORTLET_WSRP_MODE = "wsrp";
49   public static final String JavaDoc PORTLET_MONITOR_MODE = "monitor";
50   //actions
51
public static final String JavaDoc ACTION = "action";
52   
53   private static HashMap supportModes_ ;
54   private static HashMap supportStates_ ;
55
56   static {
57     supportModes_ = new HashMap(6) ;
58     supportModes_.put(PORTLET_VIEW_MODE, PortletMode.VIEW) ;
59     supportModes_.put(PORTLET_EDIT_MODE, PortletMode.EDIT) ;
60     supportModes_.put(PORTLET_HELP_MODE, PortletMode.HELP) ;
61     supportModes_.put(PORTLET_CONFIG_MODE, new PortletMode("config")) ;
62     supportModes_.put(PORTLET_WSRP_MODE, new PortletMode("wsrp")) ;
63     supportModes_.put(PORTLET_MONITOR_MODE, new PortletMode("monitor")) ;
64     
65     supportStates_ = new HashMap(5) ;
66     supportStates_.put(NORMAL_VIEW , WindowState.NORMAL) ;
67     supportStates_.put(MINIMIZED_VIEW , WindowState.MINIMIZED) ;
68     supportStates_.put(MAXIMIZED_VIEW , WindowState.MAXIMIZED) ;
69   }
70   
71   private org.exoplatform.services.portal.model.Portlet componentModel_ ;
72   private WindowState windowState_ = WindowState.NORMAL;
73   private PortletMode portletMode_ = PortletMode.VIEW ;
74   private List htmlSupportModes_ ;
75   private String JavaDoc displayTitle_ ;
76   private ExoWindowID windowID_ ;
77   private Map renderParameters_ ;
78   private boolean updateCache_ ;
79   private boolean new_ ;
80   private String JavaDoc error_ = null;
81
82   public UIPortlet(String JavaDoc owner, String JavaDoc portletApp, String JavaDoc portletName, String JavaDoc id) {
83     new_ = true ;
84     initPortlet(owner, portletApp, portletName, id) ;
85     setComponentAdminRole(true) ;
86     registerListeners() ;
87   }
88   
89   public UIPortlet(String JavaDoc portletApp, String JavaDoc portletName, String JavaDoc id) {
90     this(Constants.ANON_USER, portletApp, portletName, id) ;
91   }
92
93   public UIPortlet(org.exoplatform.services.portal.model.Portlet config,
94                    String JavaDoc defaultStyle, String JavaDoc pageRefId) {
95         componentModel_ = config ;
96         initBasicComponent(config, defaultStyle) ;
97     windowID_ = new ExoWindowID(config.getWindowId()) ;
98     windowID_.setConfigurationSource(pageRefId) ;
99     portletMode_ = PortletMode.VIEW ;
100     windowState_ = WindowState.NORMAL;
101     PortalContainer manager = PortalContainer.getInstance() ;
102         PortletContainerService portletContainer =
103       (PortletContainerService) manager.getComponentInstanceOfType(PortletContainerService.class);
104     String JavaDoc portletId = windowID_.getPortletApplicationName() + "/" + windowID_.getPortletName();
105         PortletData portlet =
106       (PortletData) portletContainer.getAllPortletMetaData().get(portletId);
107     if(portlet == null) {
108       Object JavaDoc[] args = {portletId} ;
109       PortalResources appres =
110         (PortalResources)SessionContainer.getComponent(PortalResources.class);
111       ResourceBundle res = appres.getApplicationResource();
112       error_ = Formater.getDefaultFormater().
113                         format(res.getString("UIPortlet.msg.invalid-portlet-id"), args);
114       return ;
115     }
116     initPortletModes(portlet) ;
117     setUpdateCache(true) ;
118     setId(windowID_.getUniqueID());
119     registerListeners() ;
120     setRendered(true) ;
121   }
122
123   public void initPortlet(String JavaDoc owner, String JavaDoc portletApp, String JavaDoc portletName, String JavaDoc id) {
124         componentModel_ = new org.exoplatform.services.portal.model.Portlet() ;
125         componentModel_.setTitle(portletName) ;
126     displayTitle_ = portletName;
127         initBasicComponent(componentModel_, "default") ;
128     windowID_ = new ExoWindowID( owner + ":/" + portletApp + "/" + portletName + "/" + id);
129     componentModel_.setWindowId(windowID_.generatePersistenceId()) ;
130     PortalContainer manager = PortalContainer.getInstance() ;
131         PortletContainerService portletContainer =
132       (PortletContainerService) manager.getComponentInstanceOfType(PortletContainerService.class);
133     String JavaDoc portletId = portletApp + "/" + portletName;
134         PortletData portlet = (PortletData) portletContainer.getAllPortletMetaData().get(portletId);
135     initPortletModes(portlet) ;
136     setUpdateCache(true) ;
137     setId(windowID_.getUniqueID());
138   }
139   
140   private void initPortletModes(PortletData portlet) {
141     boolean adminRole = hasRole("admin") ;
142     List supportsList = portlet.getSupports() ;
143     htmlSupportModes_ = new ArrayList() ;
144     for (int i = 0; i < supportsList.size(); i++) {
145       Supports supports = (Supports) supportsList.get(i) ;
146       String JavaDoc mimeType = supports.getMimeType() ;
147       if ("text/html".equals(mimeType)) {
148         List modes = supports.getPortletMode() ;
149         for (int j =0 ; j < modes.size() ; j++) {
150           String JavaDoc mode =(String JavaDoc)modes.get(j) ;
151           mode = mode.toLowerCase() ;
152           if("config".equals(mode)) {
153             if(adminRole) htmlSupportModes_.add(mode) ;
154           } else {
155             htmlSupportModes_.add(mode) ;
156           }
157         }
158         break ;
159       }
160     }
161   }
162   
163   private void registerListeners() {
164     addActionListener(EditPropertiesActionListener.class, PortalConstants.EDIT_ACTION) ;
165     addActionListener(DeleteActionListener.class, PortalConstants.DELETE_ACTION) ;
166     addActionListener(MoveActionListener.class, PortalConstants.MOVE_UP_ACTION) ;
167     addActionListener(MoveActionListener.class, PortalConstants.MOVE_DOWN_ACTION) ;
168     addActionListener(PortletActionListener.class, "portletAction") ;
169   }
170   
171   public Component getComponentModel() { return componentModel_ ; }
172   public org.exoplatform.services.portal.model.Portlet getPortletModel() { return componentModel_ ; }
173   public org.exoplatform.services.portal.model.Portlet getEditablePortletModel() {
174     if(!modified_) {
175         componentModel_ =
176             (org.exoplatform.services.portal.model.Portlet)componentModel_.softCloneObject() ;
177     }
178     return componentModel_ ;
179   }
180   
181   public boolean isNew() { return new_ ; }
182   public void setComponentModified(boolean b) {
183     if(b == false) new_ = false ;
184     super.setComponentModified(b) ;
185   }
186   
187   public void clearComponentModified() {
188     modified_ = false ;
189     new_ = false ;
190   }
191   
192   public boolean hasError() { return error_ != null ; }
193   public String JavaDoc getErrorMessage() { return error_ ; }
194   
195   protected String JavaDoc getIdPrefix() { return "p" ; }
196   public String JavaDoc getFamily() { return "org.exoplatform.portal.faces.component.UIPortlet" ; }
197   protected String JavaDoc getSkinName() {
198     return windowID_.getPortletApplicationName() + "/" + windowID_.getPortletName();
199   }
200   
201   protected String JavaDoc getDefaultRendererType() { return DEFAULT_PORTLET_RENDERER ; }
202   
203   protected Style getDecoratorStyle(SkinConfigService service, String JavaDoc renderer, String JavaDoc style) {
204     return service.getPortletDecoratorStyle(renderer, style);
205   }
206
207   public String JavaDoc getDisplayTitle() { return displayTitle_ ; }
208   public void setDisplayTitle(String JavaDoc s) { displayTitle_ = s; }
209   
210   public WindowState getWindowState() { return windowState_ ; }
211   public void setWindowState(WindowState state) { windowState_ = state ;}
212   public void setWindowState(String JavaDoc state) {
213     windowState_ = (WindowState) supportStates_.get(state) ;
214     if(windowState_ == null ) windowState_ = WindowState.NORMAL ;
215   }
216
217   public PortletMode getPortletMode() { return portletMode_ ; }
218   public void setPortletMode(PortletMode mode) { portletMode_ = mode ;}
219   public void setPortletMode(String JavaDoc mode) {
220     mode = mode.toLowerCase() ;
221     portletMode_ = (PortletMode) supportModes_.get(mode) ;
222     if (portletMode_ == null) portletMode_ = PortletMode.VIEW ;
223   }
224
225   public ExoWindowID getWindowId() { return windowID_; }
226   
227   public List getHtmlSupportModes() { return htmlSupportModes_ ; }
228
229   public Map getRenderParameters() { return renderParameters_; }
230   public void setRenderParameters(Map map) { renderParameters_ = map ; }
231
232   public boolean getUpdateCache() { return updateCache_ ; }
233   public void setUpdateCache(boolean b) { updateCache_ = b ; }
234   
235   public void changeLocale(ResourceBundle res) {
236     displayTitle_ = ExpressionUtil.getExpressionValue(res, componentModel_.getTitle()) ;
237     setUpdateCache(true) ;
238   }
239   
240   public void buildTreeModel(Container parent) {
241     parent.getChildren().add(componentModel_) ;
242   }
243 }
Popular Tags