1 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 37 public class UIPortlet extends UIBasicComponent { 38 public static final String DEFAULT_PORTLET_RENDERER = "PortletRenderer"; 39 public static final String MINIMIZED_VIEW = "min_view"; 41 public static final String NORMAL_VIEW = "normal_view"; 42 public static final String MAXIMIZED_VIEW = "max_view"; 43 public static final String PORTLET_VIEW_MODE = "view"; 45 public static final String PORTLET_EDIT_MODE = "edit"; 46 public static final String PORTLET_CONFIG_MODE = "config"; 47 public static final String PORTLET_HELP_MODE = "help"; 48 public static final String PORTLET_WSRP_MODE = "wsrp"; 49 public static final String PORTLET_MONITOR_MODE = "monitor"; 50 public static final String 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 displayTitle_ ; 76 private ExoWindowID windowID_ ; 77 private Map renderParameters_ ; 78 private boolean updateCache_ ; 79 private boolean new_ ; 80 private String error_ = null; 81 82 public UIPortlet(String owner, String portletApp, String portletName, String id) { 83 new_ = true ; 84 initPortlet(owner, portletApp, portletName, id) ; 85 setComponentAdminRole(true) ; 86 registerListeners() ; 87 } 88 89 public UIPortlet(String portletApp, String portletName, String id) { 90 this(Constants.ANON_USER, portletApp, portletName, id) ; 91 } 92 93 public UIPortlet(org.exoplatform.services.portal.model.Portlet config, 94 String defaultStyle, String 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 portletId = windowID_.getPortletApplicationName() + "/" + windowID_.getPortletName(); 105 PortletData portlet = 106 (PortletData) portletContainer.getAllPortletMetaData().get(portletId); 107 if(portlet == null) { 108 Object [] 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 owner, String portletApp, String portletName, String 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 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 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 mode =(String )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 getErrorMessage() { return error_ ; } 194 195 protected String getIdPrefix() { return "p" ; } 196 public String getFamily() { return "org.exoplatform.portal.faces.component.UIPortlet" ; } 197 protected String getSkinName() { 198 return windowID_.getPortletApplicationName() + "/" + windowID_.getPortletName(); 199 } 200 201 protected String getDefaultRendererType() { return DEFAULT_PORTLET_RENDERER ; } 202 203 protected Style getDecoratorStyle(SkinConfigService service, String renderer, String style) { 204 return service.getPortletDecoratorStyle(renderer, style); 205 } 206 207 public String getDisplayTitle() { return displayTitle_ ; } 208 public void setDisplayTitle(String s) { displayTitle_ = s; } 209 210 public WindowState getWindowState() { return windowState_ ; } 211 public void setWindowState(WindowState state) { windowState_ = state ;} 212 public void setWindowState(String 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 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 |