1 5 package org.exoplatform.portal.faces.component; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import org.exoplatform.faces.application.ExoFacesMessage; 10 import org.exoplatform.faces.core.component.InformationProvider; 11 import org.exoplatform.faces.core.component.UIRadioBox; 12 import org.exoplatform.faces.core.component.UISimpleForm; 13 import org.exoplatform.faces.core.component.UIStringInput; 14 import org.exoplatform.faces.core.component.model.*; 15 import org.exoplatform.faces.core.event.ExoActionEvent; 16 import org.exoplatform.faces.core.event.ExoActionListener; 17 import org.exoplatform.faces.core.validator.EmptyFieldValidator; 18 import org.exoplatform.services.portal.PortalConfigService; 19 import org.exoplatform.services.portal.model.Page; 20 21 22 28 public class UIPageModelForm extends UISimpleForm { 29 private UIStringInput nameInput_ ; 30 private UIRadioBox templateInput_ ; 31 private PortalConfigService service_ ; 32 33 public UIPageModelForm(PortalConfigService service) { 34 super("pageModelForm", "post", null) ; 35 service_ = service ; 36 nameInput_ = new UIStringInput("name", ""). 37 addValidator(EmptyFieldValidator.class) ; 38 List pageTemplates = getTemplates("template", service) ; 39 templateInput_ = new UIRadioBox("template", null, pageTemplates) ; 40 add(new HeaderRow(). 41 add(new Cell("#{UIPageModelForm.header.new-page}"). 42 addColspan("2"))); 43 add(new Row(). 44 add(new LabelCell("#{UIPageModelForm.label.name}")). 45 add(new ComponentCell(this, nameInput_))); 46 add(new Row(). 47 add(new LabelCell("#{UIPageModelForm.label.select-template}")). 48 add(new ComponentCell(this, templateInput_))); 49 add(new Row().add(new ListComponentCell(). 50 add(new FormButton("#{UIPageModelForm.link.save}", SAVE_ACTION)). 51 add(new FormButton("#{UIPageModelForm.link.cancel}", CANCEL_ACTION)). 52 addColspan("2").addAlign("center"))) ; 53 addActionListener(SaveActionListener.class, SAVE_ACTION) ; 54 addActionListener(CancelActionListener.class, CANCEL_ACTION) ; 55 } 56 57 58 59 static public class SaveActionListener extends ExoActionListener { 60 public void execute(ExoActionEvent event) throws Exception { 61 UIPageModelForm uiForm = (UIPageModelForm) event.getSource(); 62 UIPortal uiPortal = (UIPortal) uiForm.getAncestorOfType(UIPortal.class); 63 String pageName = uiForm.nameInput_.getValue(); 64 String pageId = uiPortal.getOwner() + ":" + uiForm.nameInput_.getValue(); 65 if (uiForm.service_.getPage(pageId) != null) { 66 InformationProvider iprovider = findInformationProvider(uiForm); 67 iprovider.addMessage(new ExoFacesMessage("#{UIPageModelForm.msg.reference-page-exist}")); 68 } 69 Page page = 70 uiForm.service_.getPredefinedTemplate("template", uiForm.templateInput_.getValue()); 71 page.setOwner(uiPortal.getOwner()); 72 page.setName(pageName); 73 uiForm.service_.savePage(page); 74 UIPageList uiPageList = (UIPageList) uiForm.getSibling(UIPageList.class); 75 uiPageList.update(); 76 uiForm.setRenderedSibling(UIPageList.class); 77 } 78 } 79 80 static public class CancelActionListener extends ExoActionListener { 81 public void execute(ExoActionEvent event) throws Exception { 82 UIPageModelForm uiForm = (UIPageModelForm) event.getSource(); 83 uiForm.setRenderedSibling(UIPageList.class); 84 } 85 } 86 87 static private List getTemplates(String user, PortalConfigService service) { 88 List pageTemplates = service.getPredefinedTemplates(user); 89 List templates = new ArrayList (pageTemplates.size()); 90 String label = null; 91 for (int i = 0; i < pageTemplates.size(); i++) { 92 Page page = (Page) pageTemplates.get(i); 93 String icon = page.getIcon(); 94 if (icon != null) { 95 StringBuffer b = new StringBuffer (); 96 b.append("<img SRC='" + icon + "' title='" + page.getTitle() + "'/>"); 97 label = b.toString(); 98 } else { 99 label = page.getTitle(); 100 } 101 templates.add(new SelectItem(label, page.getName())); 102 } 103 return templates; 104 } 105 106 } | Popular Tags |