1 5 6 package org.exoplatform.portlet.faces.component; 7 8 import java.util.Enumeration; 9 import java.util.ResourceBundle; 10 import javax.faces.context.FacesContext; 11 import javax.portlet.ActionResponse; 12 import javax.portlet.PortletMode; 13 import javax.portlet.PortletPreferences; 14 import javax.portlet.PortletRequest; 15 import org.exoplatform.faces.core.component.UISimpleForm; 16 import org.exoplatform.faces.core.component.UIStringInput; 17 import org.exoplatform.faces.core.component.model.FormButton; 18 import org.exoplatform.faces.core.component.model.LabelCell; 19 import org.exoplatform.faces.core.component.model.ListComponentCell; 20 import org.exoplatform.faces.core.component.model.Row; 21 import org.exoplatform.faces.core.event.ExoActionEvent; 22 import org.exoplatform.faces.core.event.ExoActionListener; 23 24 29 public class UIPreferencesForm extends UISimpleForm { 30 private static final String ENCODING = "pref_"; 31 protected ResourceBundle res; 32 33 34 public UIPreferencesForm(ResourceBundle res) { 35 super("preferencesForm", "post", null); 36 setClazz("UIPreferencesForm"); 37 this.res = res; 38 FacesContext context = FacesContext.getCurrentInstance(); 39 PortletRequest request = (PortletRequest) context.getExternalContext().getRequest(); 40 PortletPreferences prefs = request.getPreferences(); 41 Enumeration names = prefs.getNames(); 42 String[] defaultValues = {""}; 43 while (names.hasMoreElements()) { 44 String name = (String) names.nextElement(); 45 Row row = new Row(); 46 row.add(new LabelCell(name)); 47 ListComponentCell listComponentCell = new ListComponentCell(); 48 String[] values = prefs.getValues(name, defaultValues); 49 for (int i = 0; i < values.length; i++) { 50 String value = values[i]; 51 UIStringInput input = new UIStringInput(ENCODING + name, value); 52 listComponentCell.add(this, input); 53 } 54 add(row.add(listComponentCell)); 55 } 56 57 add(new Row(). 58 add(new ListComponentCell(). 59 add(new FormButton("Save", SAVE_ACTION)).addAlign("center"))); 60 61 addActionListener(ExecuteActionListener.class, SAVE_ACTION); 62 } 63 64 static public class ExecuteActionListener extends ExoActionListener { 65 public void execute(ExoActionEvent event) throws Exception { 66 FacesContext facesContext = FacesContext.getCurrentInstance(); 67 PortletRequest request = (PortletRequest) facesContext.getExternalContext().getRequest(); 68 PortletPreferences prefs = request.getPreferences(); 69 70 Enumeration enum = request.getParameterNames(); 71 while (enum.hasMoreElements()) { 72 String paramName = (String) enum.nextElement(); 73 if (paramName.startsWith(ENCODING)) { 74 String name = paramName.substring(5, paramName.length()); 75 String[] values = request.getParameterValues(paramName); 76 prefs.setValues(name, values); 77 } 78 } 79 prefs.store(); 80 } 82 } 83 } 84 | Popular Tags |