OptionGroup

Ausgabe von Class Attributen

git-svn-id: https://svn.libreccm.org/ccm/trunk@2478 8810af33-2d31-482b-a856-94f89814c4df
master
quasi 2013-12-10 15:24:59 +00:00
parent 8a7131a612
commit a62d12d63a
1 changed files with 323 additions and 326 deletions

View File

@ -43,33 +43,27 @@ import org.apache.log4j.Logger;
* @author Uday Mathur * @author Uday Mathur
* @author Rory Solomon * @author Rory Solomon
* @author Michael Pih * @author Michael Pih
* @version $Id: OptionGroup.java 738 2005-09-01 12:36:52Z sskracic $ */ * @version $Id: OptionGroup.java 738 2005-09-01 12:36:52Z sskracic $
*/
public abstract class OptionGroup extends Widget public abstract class OptionGroup extends Widget
implements BebopConstants { implements BebopConstants {
private static final Logger s_log = Logger.getLogger( OptionGroup.class ); private static final Logger s_log = Logger.getLogger(OptionGroup.class);
/** /**
* The XML element to be used by individual options belonging to this * The XML element to be used by individual options belonging to this group.
* group. This variable has to be initialized by every subclass of * This variable has to be initialized by every subclass of OptionGroup.
* OptionGroup.
* LEGACY: An abstract method would be the better design, but changing it * LEGACY: An abstract method would be the better design, but changing it
* would break the API. */ * would break the API.
*/
protected String m_xmlElement; protected String m_xmlElement;
// this only needs to be an ArrayList for multiple selection option groups // this only needs to be an ArrayList for multiple selection option groups
private ArrayList m_selected; private ArrayList m_selected;
private ArrayList m_options; private ArrayList m_options;
private Widget m_otherOption = null; private Widget m_otherOption = null;
private Form m_form = null; private Form m_form = null;
private boolean m_isDisabled = false; private boolean m_isDisabled = false;
private boolean m_isReadOnly = false; private boolean m_isReadOnly = false;
public static final String OTHER_OPTION = "__other__"; public static final String OTHER_OPTION = "__other__";
// request-local copy of selected elements, options // request-local copy of selected elements, options
private RequestLocal m_requestOptions = new RequestLocal() { private RequestLocal m_requestOptions = new RequestLocal() {
@Override @Override
@ -81,13 +75,13 @@ public abstract class OptionGroup extends Widget
public final boolean isCompound() { public final boolean isCompound() {
return true; return true;
} }
// this is only used for single selection option groups // this is only used for single selection option groups
private final static String TOO_MANY_OPTIONS_SELECTED = private final static String TOO_MANY_OPTIONS_SELECTED =
"Only one option may be selected by default on this option group."; "Only one option may be selected by default on this option group.";
/** The ParameterModel for mutliple OptionGroups is always an array /**
* parameter */ * The ParameterModel for mutliple OptionGroups is always an array parameter
*/
protected OptionGroup(ParameterModel model) { protected OptionGroup(ParameterModel model) {
super(model); super(model);
m_options = new ArrayList(); m_options = new ArrayList();
@ -102,14 +96,14 @@ public abstract class OptionGroup extends Widget
} }
/** /**
* Returns an Iterator of all the default Options in this group, * Returns an Iterator of all the default Options in this group, plus any
* plus any request-specific options. * request-specific options.
*/ */
public Iterator getOptions(PageState ps) { public Iterator getOptions(PageState ps) {
ArrayList allOptions = new ArrayList(); ArrayList allOptions = new ArrayList();
allOptions.addAll(m_options); allOptions.addAll(m_options);
ArrayList requestOptions = (ArrayList)m_requestOptions.get(ps); ArrayList requestOptions = (ArrayList) m_requestOptions.get(ps);
for (Iterator i = requestOptions.iterator(); i.hasNext(); ) { for (Iterator i = requestOptions.iterator(); i.hasNext();) {
Object obj = i.next(); Object obj = i.next();
if (!allOptions.contains(obj)) { if (!allOptions.contains(obj)) {
allOptions.add(obj); allOptions.add(obj);
@ -125,9 +119,9 @@ public abstract class OptionGroup extends Widget
/** /**
* Adds a new option. * Adds a new option.
* @param opt The {@link Option} to be added. Note: the argument *
* is modified and associated with this OptionGroup, regardless of * @param opt The {@link Option} to be added. Note: the argument is modified
* what its group was. * and associated with this OptionGroup, regardless of what its group was.
*/ */
public void addOption(Option opt) { public void addOption(Option opt) {
addOption(opt, null, false); addOption(opt, null, false);
@ -139,9 +133,9 @@ public abstract class OptionGroup extends Widget
/** /**
* Adds a new option.at the beginning of the list * Adds a new option.at the beginning of the list
* @param opt The {@link Option} to be added. Note: the argument *
* is modified and associated with this OptionGroup, regardless of * @param opt The {@link Option} to be added. Note: the argument is modified
* what its group was. * and associated with this OptionGroup, regardless of what its group was.
*/ */
public void prependOption(Option opt) { public void prependOption(Option opt) {
addOption(opt, null, true); addOption(opt, null, true);
@ -156,39 +150,38 @@ public abstract class OptionGroup extends Widget
} }
/** /**
* Adds a new option for the scope of the current request, or * Adds a new option for the scope of the current request, or to the page as
* to the page as a whole if there is no current request. * a whole if there is no current request.
* *
* @param opt The {@link Option} to be added. Note: the argument * @param opt The {@link Option} to be added. Note: the argument is modified
* is modified and associated with this OptionGroup, regardless of * and associated with this OptionGroup, regardless of what its group was.
* what its group was.
* @param ps the current page state. if ps is null, adds option to the * @param ps the current page state. if ps is null, adds option to the
* default option list. * default option list.
* @param prepend If true, prepend option to the list instead of appending it * @param prepend If true, prepend option to the list instead of appending
* it
*/ */
public void addOption(Option opt, PageState ps, boolean prepend) { public void addOption(Option opt, PageState ps, boolean prepend) {
ArrayList list = m_options; ArrayList list = m_options;
if (ps == null) { if (ps == null) {
Assert.isUnlocked(this); Assert.isUnlocked(this);
} else { } else {
list = (ArrayList)m_requestOptions.get(ps); list = (ArrayList) m_requestOptions.get(ps);
} }
opt.setGroup( this ); opt.setGroup(this);
if(prepend == true) { if (prepend == true) {
list.add(0, opt); list.add(0, opt);
} else { } else {
list.add(opt); list.add(opt);
} }
} }
public void removeOption(Option opt, PageState ps) { public void removeOption(Option opt, PageState ps) {
ArrayList list = m_options; ArrayList list = m_options;
if (ps == null) { if (ps == null) {
Assert.isUnlocked(this); Assert.isUnlocked(this);
} else { } else {
list = (ArrayList)m_requestOptions.get(ps); list = (ArrayList) m_requestOptions.get(ps);
} }
list.remove(opt); list.remove(opt);
} }
@ -198,8 +191,8 @@ public abstract class OptionGroup extends Widget
} }
/** /**
* Removes the first option whose key is isEqual * Removes the first option whose key is isEqual to the key that is passed
* to the key that is passed in. * in.
*/ */
public void removeOption(String key, PageState ps) { public void removeOption(String key, PageState ps) {
// This is not an entirely efficient technique. A more // This is not an entirely efficient technique. A more
@ -208,14 +201,14 @@ public abstract class OptionGroup extends Widget
if (ps == null) { if (ps == null) {
Assert.isUnlocked(this); Assert.isUnlocked(this);
} else { } else {
list = (ArrayList)m_requestOptions.get(ps); list = (ArrayList) m_requestOptions.get(ps);
} }
Iterator i = list.iterator(); Iterator i = list.iterator();
Option o = null; Option o = null;
while ( i.hasNext() ) { while (i.hasNext()) {
o = (Option) i.next(); o = (Option) i.next();
if ( o.getValue().equals(key) ) { if (o.getValue().equals(key)) {
list.remove(o); list.remove(o);
break; break;
} }
@ -231,87 +224,90 @@ public abstract class OptionGroup extends Widget
* @param height The height, in characters, of the "Other" entry area. If * @param height The height, in characters, of the "Other" entry area. If
* this is 1 then a TextField is used. Otherwise a TextArea is used. * this is 1 then a TextField is used. Otherwise a TextArea is used.
*/ */
public void addOtherOption( String label, int width, int height ) { public void addOtherOption(String label, int width, int height) {
Assert.isUnlocked(this); Assert.isUnlocked(this);
Option otherOption = new Option( OTHER_OPTION, label ); Option otherOption = new Option(OTHER_OPTION, label);
addOption( otherOption ); addOption(otherOption);
final ParameterModel model = getParameterModel(); final ParameterModel model = getParameterModel();
if( 1 == height ) { if (1 == height) {
TextField field = TextField field =
new TextField( model.getName() + ".other" ); new TextField(model.getName() + ".other");
field.setSize( width ); field.setSize(width);
m_otherOption = field; m_otherOption = field;
} else { } else {
TextArea area = TextArea area =
new TextArea( model.getName() + ".other" ); new TextArea(model.getName() + ".other");
area.setCols( width ); area.setCols(width);
area.setRows( height ); area.setRows(height);
m_otherOption = area; m_otherOption = area;
} }
if( null != m_form ) { if (null != m_form) {
m_otherOption.setForm( m_form ); m_otherOption.setForm(m_form);
if( m_isDisabled ) { if (m_isDisabled) {
m_otherOption.setDisabled(); m_otherOption.setDisabled();
} }
if( m_isReadOnly ) { if (m_isReadOnly) {
m_otherOption.setReadOnly(); m_otherOption.setReadOnly();
} }
} }
setParameterModel( new ParameterModelWrapper( model ) { setParameterModel(new ParameterModelWrapper(model) {
@Override @Override
public ParameterData createParameterData( final HttpServletRequest request, public ParameterData createParameterData(final HttpServletRequest request,
Object defaultValue, Object defaultValue,
boolean isSubmission ) { boolean isSubmission) {
final String[] values = final String[] values =
request.getParameterValues( getName() ); request.getParameterValues(getName());
String[] otherValues = String[] otherValues =
request.getParameterValues( getName() + ".other" ); request.getParameterValues(getName() + ".other");
String other = ( null == otherValues ) ? null : otherValues[0]; String other = (null == otherValues) ? null : otherValues[0];
if( null != values ) { if (null != values) {
for( int i = 0; i < values.length; i++ ) { for (int i = 0; i < values.length; i++) {
if( OTHER_OPTION.equals( values[i] ) ) { if (OTHER_OPTION.equals(values[i])) {
values[i] = other; values[i] = other;
} }
} }
} }
s_log.debug( "createParameterData in OptionGroup" ); s_log.debug("createParameterData in OptionGroup");
return super.createParameterData( new HttpServletRequestWrapper( request ) { return super.createParameterData(new HttpServletRequestWrapper(request) {
@Override @Override
public String[] getParameterValues( String key ) { public String[] getParameterValues(String key) {
if( s_log.isDebugEnabled() ) { if (s_log.isDebugEnabled()) {
s_log.debug( "Getting values for " + key ); s_log.debug("Getting values for " + key);
} }
if( model.getName().equals( key ) ) { if (model.getName().equals(key)) {
return values; return values;
} }
return super.getParameterValues( key ); return super.getParameterValues(key);
} }
}, defaultValue, isSubmission ); }, defaultValue, isSubmission);
} }
private void replaceOther( String[] values, String other ) { private void replaceOther(String[] values, String other) {
} }
} ); });
} }
/** Make an option selected by default. Updates the parameter /**
* model for the option group accordingly. * Make an option selected by default. Updates the parameter model for the
* option group accordingly.
*
* @param value the value of the option to be added to the * @param value the value of the option to be added to the
* by-default-selected set. */ * by-default-selected set.
*/
public void setOptionSelected(String value) { public void setOptionSelected(String value) {
Assert.isUnlocked(this); Assert.isUnlocked(this);
if (!isMultiple()) { if (!isMultiple()) {
@ -319,14 +315,16 @@ public abstract class OptionGroup extends Widget
// to this selected list better be empty // to this selected list better be empty
Assert.isTrue(m_selected.size() == 0, TOO_MANY_OPTIONS_SELECTED); Assert.isTrue(m_selected.size() == 0, TOO_MANY_OPTIONS_SELECTED);
m_selected.add(value); m_selected.add(value);
getParameterModel().setDefaultValue( value ); getParameterModel().setDefaultValue(value);
} else { } else {
m_selected.add(value); m_selected.add(value);
getParameterModel().setDefaultValue( m_selected.toArray() ); getParameterModel().setDefaultValue(m_selected.toArray());
} }
} }
/** make an option selected by default /**
* make an option selected by default
*
* @param option the option to be added to the by-default-selected set. * @param option the option to be added to the by-default-selected set.
*/ */
public void setOptionSelected(Option option) { public void setOptionSelected(Option option) {
@ -335,7 +333,7 @@ public abstract class OptionGroup extends Widget
@Override @Override
public Object clone() throws CloneNotSupportedException { public Object clone() throws CloneNotSupportedException {
OptionGroup cloned = (OptionGroup)super.clone(); OptionGroup cloned = (OptionGroup) super.clone();
cloned.m_options = (ArrayList) m_options.clone(); cloned.m_options = (ArrayList) m_options.clone();
cloned.m_selected = cloned.m_selected =
(ArrayList) m_selected.clone(); (ArrayList) m_selected.clone();
@ -343,14 +341,14 @@ public abstract class OptionGroup extends Widget
} }
/** /**
* Is this a multiple (and not single) selection option group? * Is this a multiple (and not single) selection option group? Note that
* Note that this should really be declared abstract, but we can't * this should really be declared abstract, but we can't because it used to
* because it used to be in the direct subclass Select and making * be in the direct subclass Select and making it abstract could break other
* it abstract could break other subclasses that don't declare * subclasses that don't declare isMultiple. So we have a trivial
* isMultiple. So we have a trivial implementation instead. * implementation instead.
* *
* @return true if this OptionGroup can have more than one * @return true if this OptionGroup can have more than one selected option;
* selected option; false otherwise. * false otherwise.
*/ */
public boolean isMultiple() { public boolean isMultiple() {
return true; return true;
@ -360,7 +358,7 @@ public abstract class OptionGroup extends Widget
public void setDisabled() { public void setDisabled() {
m_isDisabled = true; m_isDisabled = true;
if( null != m_otherOption ) { if (null != m_otherOption) {
m_otherOption.setDisabled(); m_otherOption.setDisabled();
} }
@ -371,7 +369,7 @@ public abstract class OptionGroup extends Widget
public void setReadOnly() { public void setReadOnly() {
m_isReadOnly = true; m_isReadOnly = true;
if( null != m_otherOption ) { if (null != m_otherOption) {
m_otherOption.setReadOnly(); m_otherOption.setReadOnly();
} }
@ -379,39 +377,38 @@ public abstract class OptionGroup extends Widget
} }
@Override @Override
public void setForm( Form form ) { public void setForm(Form form) {
m_form = form; m_form = form;
if( null != m_otherOption ) { if (null != m_otherOption) {
m_otherOption.setForm(form); m_otherOption.setForm(form);
} }
super.setForm( form ); super.setForm(form);
} }
/** /**
* Generates the DOM for the select widget * Generates the DOM for the select widget <p>Generates DOM fragment: <p><pre><code>&lt;bebop:* name=... [onXXX=...]&gt;
* <p>Generates DOM fragment:
* <p><pre><code>&lt;bebop:* name=... [onXXX=...]&gt;
* &lt;bebop:option name=... [selected]&gt; option value &lt;/bebop:option%gt; * &lt;bebop:option name=... [selected]&gt; option value &lt;/bebop:option%gt;
* ... * ...
* &lt;/bebop:*select&gt;</code></pre> * &lt;/bebop:*select&gt;</code></pre>
*/ */
@Override @Override
public void generateWidget( PageState state, Element parent ) { public void generateWidget(PageState state, Element parent) {
Element optionGroup = Element optionGroup =
parent.newChildElement( getElementTag(), BEBOP_XML_NS ); parent.newChildElement(getElementTag(), BEBOP_XML_NS);
optionGroup.addAttribute( "name", getName() ); optionGroup.addAttribute("name", getName());
if ( isMultiple() ) { optionGroup.addAttribute("class", getName().replace(".", " "));
optionGroup.addAttribute( "multiple", "multiple" ); if (isMultiple()) {
optionGroup.addAttribute("multiple", "multiple");
} }
exportAttributes( optionGroup ); exportAttributes(optionGroup);
for ( Iterator i = getOptions(state); i.hasNext(); ) { for (Iterator i = getOptions(state); i.hasNext();) {
Option o = (Option) i.next(); Option o = (Option) i.next();
o.generateXML( state, optionGroup ); o.generateXML(state, optionGroup);
} }
if( null != m_otherOption ) { if (null != m_otherOption) {
m_otherOption.generateXML(state, optionGroup); m_otherOption.generateXML(state, optionGroup);
} }
} }