1 23 package com.sun.enterprise.tools.jsfext.event; 24 25 import com.sun.enterprise.tools.jsfext.layout.LayoutDefinitionManager; 26 import com.sun.enterprise.tools.jsfext.layout.descriptor.ComponentType; 27 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutComponent; 28 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutDefinition; 29 import com.sun.enterprise.tools.jsfext.layout.descriptor.LayoutElement; 30 31 import java.io.IOException ; 32 import java.util.EventObject ; 33 import java.util.Iterator ; 34 import java.util.List ; 35 36 import javax.faces.component.UIComponent; 37 import javax.faces.component.UIViewRoot; 38 import javax.faces.context.FacesContext; 39 import javax.faces.event.ActionEvent; 40 41 42 50 public class CommandActionListener implements java.io.Serializable { 51 52 58 public CommandActionListener() { 59 super(); 60 } 61 62 66 public void invokeCommandHandlers(ActionEvent event) { 67 UIComponent command = (UIComponent) event.getSource(); 69 if (command == null) { 70 throw new IllegalArgumentException ( 71 "Action invoked, however, no source was given!"); 72 } 73 74 FacesContext context = FacesContext.getCurrentInstance(); 76 77 LayoutElement desc = null; 79 List handlers = (List ) command.getAttributes().get(COMMAND_HANDLERS); 80 if ((handlers != null) && (handlers.size() > 0)) { 81 87 desc = new LayoutComponent( 89 (LayoutElement) null, command.getId(), (ComponentType) null); 90 desc.setHandlers(CommandEvent.EVENT_TYPE, handlers); 91 } else { 92 String viewId = getViewId(command); 95 desc = findLayoutElementByClientId( 96 context, viewId, command.getClientId(context)); 97 if (desc == null) { 98 desc = findLayoutElementById(context, viewId, command.getId()); 100 } 101 } 102 103 if (desc == null) { 105 throw new IllegalArgumentException ( 106 "Unable to locate handlers for '" 107 + command.getClientId(context) + "'."); 108 } 109 110 desc.dispatchHandlers(context, CommandEvent.EVENT_TYPE, event); 112 } 113 114 118 public static String getViewId(UIComponent comp) { 119 String result = null; 120 while ((comp != null) && !(comp instanceof UIViewRoot)) { 121 comp = comp.getParent(); 123 } 124 if (comp != null) { 125 result = ((UIViewRoot) comp).getViewId(); 127 } 128 return result; 130 } 131 132 144 public static LayoutElement findLayoutElementByClientId(FacesContext ctx, String layoutDefKey, String clientId) { 145 LayoutElement result = null; 146 try { 147 result = 148 findLayoutElementByClientId( 149 LayoutDefinitionManager.getManager(ctx). 150 getLayoutDefinition(layoutDefKey), clientId); 151 } catch (IOException ex) { 152 } 154 return result; 155 } 156 157 public static LayoutElement findLayoutElementByClientId(LayoutDefinition def, String clientId) { 158 return null; 161 } 162 163 170 public static LayoutElement findLayoutElementById(FacesContext ctx, String layoutDefKey, String id) { 171 if (id == null) { 173 return null; 174 } 175 176 LayoutElement result = null; 177 try { 178 result = findLayoutElementById( 179 LayoutDefinitionManager.getManager(ctx). 180 getLayoutDefinition(layoutDefKey), id); 181 } catch (IOException ex) { 182 } 184 return result; 185 } 186 187 193 public static LayoutElement findLayoutElementById(LayoutElement elt, String id) { 194 if (elt.getUnevaluatedId().equals(id)) { 196 return elt; 197 } 198 199 LayoutElement child = null; 201 Iterator it = elt.getChildLayoutElements().iterator(); 202 while (it.hasNext()) { 203 child = (LayoutElement) it.next(); 204 if (child instanceof LayoutComponent) { 205 child = findLayoutElementById(child, id); 206 if (child != null) { 207 return child; 208 } 209 } 210 } 211 return null; 212 } 213 214 218 public static final String COMMAND_HANDLERS = "command"; 219 } 220 | Popular Tags |