package er.directtoweb.assignments.defaults;
import com.webobjects.directtoweb.D2WContext;
import com.webobjects.eocontrol.EOKeyValueUnarchiver;
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSDictionary;
import er.directtoweb.assignments.ERDAssignment;
import er.extensions.foundation.ERXDictionaryUtilities;
import er.extensions.foundation.ERXStringUtilities;
/**
* An assignment to auto-compute a unique(ish), human-readable DOM form control names from the d2wContext for Selenium, CSS, Ajax, Javascript, etc.
*
* This assignment provides defaults for the following keys:
* <ul>
* <li><code>nameForProperty</code></li>
* </ul>
*
* To use: Bind D2W component class binding to d2wContext.name (or d2wContext.nameForProperty, etc)
*
* @author mendis
*
*/
public class ERDDefaultFormControlNameAssignment extends ERDAssignment {
/**
* Do I need to update serialVersionUID?
* See section 5.6 <cite>Type Changes Affecting Serialization</cite> on page 51 of the
* <a href="http://java.sun.com/j2se/1.4/pdf/serial-spec.pdf">Java Object Serialization Spec</a>
*/
private static final long serialVersionUID = 1L;
/** holds the array of keys this assignment depends upon */
protected static final NSDictionary keys = ERXDictionaryUtilities.dictionaryWithObjectsAndKeys( new Object [] {
new NSArray(new Object[] {"propertyKey"}), "nameForProperty",
});
/**
* Implementation of the {@link er.directtoweb.assignments.ERDComputingAssignmentInterface}. This array
* of keys is used when constructing the
* significant keys for the passed in keyPath.
* @param keyPath to compute significant keys for.
* @return array of context keys this assignment depends upon.
*/
public NSArray dependentKeys(String keyPath) {
return (NSArray)keys.valueForKey(keyPath);
}
/**
* Static constructor required by the EOKeyValueUnarchiver
* interface. If this isn't implemented then the default
* behavior is to construct the first super class that does
* implement this method. Very lame.
* @param eokeyvalueunarchiver to be unarchived
* @return decoded assignment of this class
*/
// ENHANCEME: Only ever need one of these assignments.
public static Object decodeWithKeyValueUnarchiver(EOKeyValueUnarchiver eokeyvalueunarchiver) {
return new ERDDefaultFormControlNameAssignment(eokeyvalueunarchiver);
}
/**
* Public constructor
* @param u key-value unarchiver used when unarchiving
* from rule files.
*/
public ERDDefaultFormControlNameAssignment (EOKeyValueUnarchiver u) { super(u); }
/**
* Public constructor
* @param key context key
* @param value of the assignment
*/
public ERDDefaultFormControlNameAssignment (String key, Object value) { super(key,value); }
/**
* A DOM form control name based on the propertyKey
*
* @param c d2w context
* @return a name from the propertyKey
*/
public Object nameForProperty(D2WContext c) {
String nameForProperty = (c.propertyKey() != null) ? ERXStringUtilities.safeIdentifierName(c.propertyKey()) : "";
return nameForProperty;
}
}