1 5 package org.exoplatform.portal.faces.component; 6 7 import java.util.List ; 8 import javax.faces.component.UIComponent; 9 import javax.faces.context.FacesContext; 10 import org.exoplatform.commons.exception.ExoMessageException; 11 import org.exoplatform.container.PortalContainer; 12 import org.exoplatform.faces.application.ExoFacesMessage; 13 import org.exoplatform.faces.core.component.InformationProvider; 14 import org.exoplatform.faces.core.component.UIExoCommand; 15 import org.exoplatform.faces.core.component.UIToolbar; 16 import org.exoplatform.faces.core.component.model.Button; 17 import org.exoplatform.faces.core.component.model.Parameter; 18 import org.exoplatform.faces.core.event.ExoActionEvent; 19 import org.exoplatform.faces.core.event.ExoActionListener; 20 import org.exoplatform.services.portal.PortalACL; 21 import org.exoplatform.services.portal.PortalConfigService; 22 import org.exoplatform.services.portal.model.Page; 23 28 public class UIPagePreview extends UIExoCommand { 29 public static Parameter[] saveParams = { new Parameter(ACTION , SAVE_ACTION)} ; 30 public static Parameter[] editParams = { new Parameter(ACTION , EDIT_ACTION)} ; 31 public static Parameter[] viewParams = { new Parameter(ACTION , "view")} ; 32 public static Parameter[] cancelParams = { new Parameter(ACTION , CANCEL_ACTION)} ; 33 34 public UIPagePreview() { 35 setRendererType("PagePreviewRenderer"); 36 setId("UIPagePreview"); 37 38 List children = getChildren() ; 39 UIToolbar uiToolbar = new UIToolbar("UIPagePreviewToolbar"); 40 uiToolbar.addLeftButton(new Button("#{UIPagePreview.button.view}", viewParams)); 41 uiToolbar.addLeftButton(new Button("#{UIPagePreview.button.edit}", editParams)); 42 uiToolbar.addLeftButton(new Button("#{UIPagePreview.button.save}", saveParams)); 43 uiToolbar.addRightButton(new Button("#{UIPagePreview.button.cancel}", cancelParams)); 44 uiToolbar.setRendered(true); 45 children.add(uiToolbar); 46 47 addActionListener(ViewActionListener.class, "view") ; 48 addActionListener(EditActionListener.class, EDIT_ACTION) ; 49 addActionListener(CancelActionListener.class, CANCEL_ACTION) ; 50 addActionListener(SaveActionListener.class, SAVE_ACTION) ; 51 } 52 53 public boolean setPage(UIPortal uiPortal, String pageId) throws Exception { 54 PortalContainer pcontainer = PortalContainer.getInstance(); 55 PortalConfigService service = 56 (PortalConfigService) pcontainer.getComponentInstanceOfType(PortalConfigService.class) ; 57 Page config = service.getPage(pageId); 58 PortalConfigService configService = 59 (PortalConfigService) PortalContainer.getComponent(PortalConfigService.class) ; 60 PortalACL portalACL = configService.getPortalACL() ; 61 if(!portalACL.hasViewPagePermission(config, uiPortal.getOwner())) { 62 uiPortal.addMessage(new ExoFacesMessage("#{UIPagePreview.msg.no-view-permission}")); 63 return false ; 64 } 65 UIPage uiPage = new UIPage(config, "default") ; 66 new ChangeLocaleVisitor().changeLocale(uiPage) ; 67 PortalACL acl = service.getPortalACL() ; 68 if( acl.hasEditPagePermission(config, uiPortal.getOwner())) { 69 uiPage.setComponentAdminRole(true) ; 70 } 71 uiPage.setRendered(true) ; 72 List children = getChildren() ; 73 if(children.size() > 1) children.remove(1) ; 74 children.add(uiPage) ; 75 return true ; 76 } 77 78 public UIPage getUIPage() { return (UIPage)getChildren().get(1) ; } 79 80 public String getFamily() { 81 return "org.exoplatform.portal.faces.component.UIPagePreview"; 82 } 83 84 public UIComponent getUIToolbar() { return (UIComponent)getChildren().get(0) ; } 85 86 public void processDecodes(FacesContext context) { 87 List children = getChildren() ; 88 for(int i = 0 ; i < children.size(); i++) { 89 UIComponent child = (UIComponent) children.get(i); 90 if (child.isRendered() && child instanceof UIToolbar) { 91 child.processDecodes(context) ; 92 if (context.getRenderResponse()) { 93 return ; 94 } 95 } 96 } 97 decode(context) ; 98 } 99 100 static public class ViewActionListener extends ExoActionListener { 101 public void execute(ExoActionEvent event) throws Exception { 102 UIPagePreview uiComponent = (UIPagePreview) event.getSource() ; 103 UIPage uiPage = (UIPage)uiComponent.getChildren().get(1) ; 104 uiPage.setComponentMode(UIBasicComponent.COMPONENT_VIEW_MODE) ; 105 } 106 } 107 108 static public class EditActionListener extends ExoActionListener { 109 public void execute(ExoActionEvent event) throws Exception { 110 UIPagePreview uiComponent = (UIPagePreview) event.getSource() ; 111 UIPage uiPage = (UIPage)uiComponent.getChildren().get(1) ; 112 uiPage.setComponentMode(UIBasicComponent.COMPONENT_EDIT_MODE) ; 113 } 114 } 115 116 static public class CancelActionListener extends ExoActionListener { 117 public void execute(ExoActionEvent event) throws Exception { 118 UIPagePreview uiComponent = (UIPagePreview) event.getSource() ; 119 UIPortal uiPortal = (UIPortal) uiComponent.getAncestorOfType(UIPortal.class) ; 120 UIPageBrowser uiPageBrowser = 121 (UIPageBrowser)uiPortal.getPortalComponent(UIPageBrowser.class); 122 uiPortal.setBodyComponent(uiPageBrowser); 123 } 124 } 125 126 static public class SaveActionListener extends ExoActionListener { 127 public void execute(ExoActionEvent event) throws Exception { 128 UIPagePreview uiComponent = (UIPagePreview) event.getSource() ; 129 UIPage uiPage = (UIPage)uiComponent.getChildren().get(1) ; 130 uiPage.buildTreeModel(null) ; 131 PortalConfigService service = 132 (PortalConfigService)PortalContainer.getInstance(). 133 getComponentInstanceOfType(PortalConfigService.class) ; 134 service.savePage(uiPage.getPageModel()) ; 135 uiPage.clearComponentModified() ; 136 uiPage.setComponentDirty(false) ; 137 } 138 } 139 140 } | Popular Tags |