1 5 package org.exoplatform.portal.faces.component; 6 7 import java.util.ArrayList ; 8 import java.util.List ; 9 import javax.faces.component.UIComponent; 10 import javax.faces.context.FacesContext; 11 import javax.faces.event.PhaseId; 12 import javax.faces.validator.Validator; 13 import javax.faces.validator.ValidatorException; 14 import org.exoplatform.faces.application.ExoFacesMessage; 15 import org.exoplatform.faces.core.Util; 16 import org.exoplatform.faces.core.component.*; 17 import org.exoplatform.faces.core.component.model.*; 18 import org.exoplatform.faces.core.event.ExoActionEvent; 19 import org.exoplatform.faces.core.event.ExoActionListener; 20 import org.exoplatform.faces.core.validator.EmptyFieldValidator; 21 import org.exoplatform.portal.faces.listener.page.ShowCurrentPageActionListener; 22 import org.exoplatform.portal.session.ExoPortal; 23 import org.exoplatform.services.portal.PortalConfigService; 24 import org.exoplatform.services.portal.model.Node; 25 import org.exoplatform.services.portal.model.PageReference; 26 32 public class UIPageNodeForm extends UISimpleForm { 33 final static public String SEARCH_XHTML_REF_PAGE = 34 Util.encodeActionPhase("searchXhtmlRefPage", PhaseId.APPLY_REQUEST_VALUES) ; 35 final static public String SEARCH_XHTMLMP_REF_PAGE = 36 Util.encodeActionPhase("searchXhtmlmpRefPage", PhaseId.APPLY_REQUEST_VALUES) ; 37 38 static private List PERMISSIONS = new ArrayList () ; 39 static { 40 PERMISSIONS.add(new SelectItem("owner", "owner")); 41 PERMISSIONS.add(new SelectItem("any", "any")); 42 } 43 44 private Cell uriCell_; 45 private UIStringInput nameInput_; 46 private UIStringInput labelInput_; 47 private UISelectBox viewPermissionInput_; 48 private UISelectBox editPermissionInput_; 49 private UIStringInput iconInput_; 50 private UITextArea descriptionInput_; 51 private UIStringInput uiXHTMLReference_; 52 private UICheckBox uiXHTMLVisible_; 53 private UIStringInput uiXHTMLMPReference_; 54 private UICheckBox uiXHTMLMPVisible_; 55 private Node selectNode_; 56 57 private PortalConfigService service_; 58 private String lastSearchAction_; 59 60 public UIPageNodeForm(PortalConfigService service) { 61 super("pageNodeForm", "post", null); 62 service_ = service; 63 uriCell_ = new Cell(""); 64 65 nameInput_ = new UIStringInput("name", ""). 66 addValidator(EmptyFieldValidator.class). 67 addValidator(EmptyFieldValidator.class) ; 68 69 labelInput_ = new UIStringInput("displayName", ""). 70 addValidator(EmptyFieldValidator.class); 71 72 iconInput_ = new UIStringInput("icon", "") ; 73 viewPermissionInput_ = new UISelectBox("viewPermission", "", PERMISSIONS); 74 editPermissionInput_ = new UISelectBox("editPermission", "", PERMISSIONS); 75 descriptionInput_ = new UITextArea("description", "") ; 76 uiXHTMLReference_ = new UIStringInput("xhtmlPageReference", ""). 77 addValidator(EmptyFieldValidator.class) ; 78 79 uiXHTMLVisible_ = new UICheckBox("xhtmlVisible", "true") ; 80 uiXHTMLMPReference_ = new UIStringInput("xhtmlmpPageReference", "") ; 81 uiXHTMLMPVisible_ = new UICheckBox("xhtmlmpVisible", "true") ; 82 83 String saveButton = "#{UIPageNodeForm.link.save}"; 84 String cancelButton = "#{UIPageNodeForm.link.cancel}"; 85 add(new HeaderRow(). 86 add(new Cell("#{UIPageNodeForm.header.add-edit-navigation-node}"). 87 addColspan("2"))); 88 add(new Row(). 89 add(new LabelCell("#{UIPageNodeForm.label.uri}")). 90 add(uriCell_)); 91 add(new Row(). 92 add(new LabelCell("#{UIPageNodeForm.label.name}")). 93 add(new ComponentCell(this, nameInput_))); 94 add(new Row(). 95 add(new LabelCell("#{UIPageNodeForm.label.display-name}")). 96 add(new ComponentCell(this, labelInput_))); 97 add((new Row()). 98 add(new LabelCell("#{UIPageNodeForm.label.view-permission}")). 99 add(new ComponentCell(this, viewPermissionInput_))); 100 add((new Row()). 101 add(new LabelCell("#{UIPageNodeForm.label.edit-permission}")). 102 add(new ComponentCell(this, editPermissionInput_))); 103 add(new Row(). 104 add(new LabelCell("#{UIPageNodeForm.label.icon}")). 105 add(new ComponentCell(this, iconInput_))); 106 add(new Row(). 107 add(new LabelCell("#{UIPageNodeForm.label.xhtml-page-reference}")). 108 add(new ListComponentCell(). 109 add(this, uiXHTMLReference_ ). 110 add(new FormButton("#{UIPageNodeForm.button.search}", SEARCH_XHTML_REF_PAGE)))); 111 add(new Row(). 112 add(new LabelCell("#{UIPageNodeForm.label.xhtml-visible}")). 113 add(new ComponentCell(this, uiXHTMLVisible_ ))); 114 add(new Row(). 115 add(new LabelCell("#{UIPageNodeForm.label.xhtmlmp-page-reference}")). 116 add(new ListComponentCell(). 117 add(this, uiXHTMLMPReference_ ). 118 add(new FormButton("#{UIPageNodeForm.button.search}", SEARCH_XHTMLMP_REF_PAGE)))); 119 add(new Row(). 120 add(new LabelCell("#{UIPageNodeForm.label.xhtmlmp-visible}")). 121 add(new ComponentCell(this, uiXHTMLMPVisible_ ))); 122 add(new Row(). 123 add(new LabelCell("#{UIPageNodeForm.label.description}")). 124 add(new ComponentCell(this, descriptionInput_))); 125 add(new Row().setClazz("footer"). 126 add(new ListComponentCell(). 127 add(new FormButton(saveButton, SAVE_ACTION)). 128 add(new FormButton(cancelButton, CANCEL_ACTION)). 129 addColspan("2").addAlign("center"))) ; 130 131 addValidator(NodeNameValidator.class) ; 132 addActionListener(SaveActionListener.class, SAVE_ACTION) ; 133 addActionListener(ShowCurrentPageActionListener.class, CANCEL_ACTION) ; 134 addActionListener(SearchPageActionListener.class, SEARCH_XHTML_REF_PAGE) ; 135 addActionListener(SearchPageActionListener.class, SEARCH_XHTMLMP_REF_PAGE) ; 136 } 137 138 139 public void setEditingNavigationNode(Node node) { 140 selectNode_ = node ; 141 if (node != null) { 142 uriCell_.setValue(node.getUri()) ; 143 nameInput_.setValue(node.getName()) ; 144 nameInput_.setEditable(false) ; 145 labelInput_.setValue(node.getLabel()) ; 146 viewPermissionInput_.setValue(node.getViewPermission()) ; 147 editPermissionInput_.setValue(node.getEditPermission()) ; 148 iconInput_.setValue("") ; 149 descriptionInput_.setValue("") ; 150 PageReference xhtmlPageRef = node.getPageReference(ExoPortal.XHTML_MIME_TYPE); 151 uiXHTMLReference_.setValue(xhtmlPageRef.getPageReference()) ; 152 uiXHTMLVisible_.setSelect(xhtmlPageRef.isVisible()) ; 153 PageReference xhtmlmpPageRef = node.getPageReference(ExoPortal.XHTMLMP_MIME_TYPE) ; 154 if(xhtmlmpPageRef == null) { 155 uiXHTMLMPReference_.setValue("") ; 156 uiXHTMLMPVisible_.setSelect(false) ; 157 } else { 158 uiXHTMLMPReference_.setValue(xhtmlmpPageRef.getPageReference()) ; 159 uiXHTMLMPVisible_.setSelect(xhtmlmpPageRef.isVisible()) ; 160 } 161 } else { 162 uriCell_.setValue(""); 163 nameInput_.setValue(""); 164 nameInput_.setEditable(true); 165 labelInput_.setValue(""); 166 viewPermissionInput_.setValue("") ; 167 editPermissionInput_.setValue("") ; 168 iconInput_.setValue(""); 169 descriptionInput_.setValue(""); 170 uiXHTMLReference_.setValue(""); 171 uiXHTMLVisible_.setSelect(false); 172 uiXHTMLMPReference_.setValue(""); 173 uiXHTMLMPVisible_.setSelect(false); 174 } 175 } 176 177 public void returnReferencePage(String refPage) { 178 if (lastSearchAction_.equals(SEARCH_XHTMLMP_REF_PAGE)) { 179 uiXHTMLMPReference_.setValue(refPage); 180 } else { 181 uiXHTMLReference_.setValue(refPage); 182 } 183 } 184 185 static public class SearchPageActionListener extends ExoActionListener { 186 public void execute(ExoActionEvent event) throws Exception { 187 UIPageNodeForm uiForm = (UIPageNodeForm) event.getComponent(); 188 String action = event.getAction(); 189 uiForm.lastSearchAction_ = action; 190 UIPortal uiPortal = (UIPortal) uiForm.getAncestorOfType(UIPortal.class); 191 UIPageBrowser uiPageBrowser = 192 (UIPageBrowser) uiPortal.getPortalComponent(UIPageBrowser.class); 193 if (uiPageBrowser != null) { 194 uiPageBrowser.setReturnModule(UIPageBrowser.UI_NAVIGATION_NODE_FORM); 195 uiPortal.setBodyComponent(uiPageBrowser); 196 } 197 } 198 } 199 200 static public class SaveActionListener extends ExoActionListener { 201 public void execute(ExoActionEvent event) throws Exception { 202 UIPageNodeForm uiForm = (UIPageNodeForm) event.getComponent(); 203 InformationProvider iprovider = findInformationProvider(uiForm); 204 uiForm.save(iprovider); 205 } 206 } 207 208 public void save(InformationProvider iprovider) throws Exception { 209 UIPortal uiPortal = (UIPortal) getAncestorOfType(UIPortal.class); 210 if (selectNode_ == null) { 211 Node currentNode = uiPortal.getSelectedNode(); 212 String parentURI = currentNode.getUri(); 213 if ("/".equals(parentURI)) 214 parentURI = ""; 215 selectNode_ = service_.createNodeInstance(); 216 selectNode_.setUri(parentURI + "/" + nameInput_.getValue()); 217 selectNode_.setName(nameInput_.getValue()); 218 PageReference pref = new PageReference(); 219 pref.setType(ExoPortal.XHTML_MIME_TYPE); 220 pref.setVisible(true); 221 selectNode_.getPageReference().add(pref); 222 currentNode.addChild(selectNode_); 223 } 224 selectNode_.setLabel(labelInput_.getValue()); 225 selectNode_.setViewPermission(viewPermissionInput_.getValue()) ; 226 selectNode_.setEditPermission(editPermissionInput_.getValue()) ; 227 PageReference pageRef = 228 selectNode_.getPageReference(ExoPortal.XHTML_MIME_TYPE); 229 pageRef.setPageReference(uiXHTMLReference_.getValue()); 230 pageRef.setVisible("true".equals(uiXHTMLVisible_.getValue())); 231 String xhtmlmpReference = uiXHTMLMPReference_.getValue(); 232 if (xhtmlmpReference == null || xhtmlmpReference.length() == 0) { 233 selectNode_.removePageReference(ExoPortal.XHTMLMP_MIME_TYPE); 234 } else { 235 pageRef = selectNode_.getPageReference(ExoPortal.XHTMLMP_MIME_TYPE); 236 if (pageRef == null) { 237 pageRef = new PageReference(); 238 pageRef.setType(ExoPortal.XHTMLMP_MIME_TYPE); 239 selectNode_.getPageReference().add(pageRef); 240 } 241 pageRef.setPageReference(xhtmlmpReference); 242 pageRef.setVisible("true".equals(uiXHTMLMPVisible_.getValue())); 243 } 244 uiPortal.setComponentModified(true); 245 uiPortal.setSelectedNode(selectNode_); 246 uiPortal.setBodyComponent(uiPortal.getCurrentUIPage()); 247 } 248 249 static public class NodeNameValidator implements Validator { 250 public void validate(FacesContext context, UIComponent component, 251 Object value) throws ValidatorException { 252 UIPageNodeForm uiForm = (UIPageNodeForm) component; 253 if (uiForm.selectNode_ == null) { 254 UIPortal uiPortal = (UIPortal) uiForm.getAncestorOfType(UIPortal.class); 255 Node currentNode = uiPortal.getSelectedNode(); 256 if (currentNode.hasChild(uiForm.nameInput_.getValue())) { 257 throw new ValidatorException(new ExoFacesMessage("#{UIPageNodeForm.msg.node-name-exist}")); 258 } 259 } 260 } 261 } 262 } | Popular Tags |