Here's a sample of how to use the newly added functionality
<inside the execute function of an Action used for setup>
LabelValueBean[] sortableList = new LabelValueBean[] {
new LabelValueBean("unorganized", "ung"),
new LabelValueBean("out of order", "ood"),
new LabelValueBean("Capitalized", "Cap"),
new LabelValueBean"("Not Lowercase", "Nl"),
};
// to sort the list alphabetically by label
java.util.Arrays.sort(sortableList);
// or to sort the list case insensitive by label
java.util.Arrays.sort(sortableList, LabelValueBean.CASE_INSENSITIVE_ORDER);request.setAttribute("sortableList", sortableList);
//then the sortableList is used in the <html:options> tags....
I have attached the complete file beside including the diff in case that is useful.
The diff versus version 1.6 of the file (
# diff LabelValueBean.java.version1.6 LabelValueBean.java
65c65
<
---
> import java.util.Comparator;
74a75,76
> * @author Paul Sundling
> *
77c79
< public class LabelValueBean implements Serializable {
---
> public class LabelValueBean implements Comparable, Serializable {
181a184,210
> /**
> * The comparable interface allows sorting. This is done based on the
> * label, since that is the human viewable part of the object.
> * @see java.lang.Comparable
> */
> public int compareTo(Object otherBean) {
> // implicitly tests for the correct type, throwing ClassCastException
> // as required by interface
> String otherLabel = ((LabelValueBean)otherBean).getLabel();
>
> //compare the labels of the LabelValueBean objects
> return this.getLabel().compareTo(otherLabel);
> }
>
> /**
> * Comparator object that can be used for a case insensitive order
> * sort of LabelValueBean objects. The idea for this comes from
> * java.lang.String
> */
> public static Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
> public int compare(Object Bean1, Object Bean2) {
> String label1 = ((LabelValueBean)Bean1).getLabel();
> String label2 = ((LabelValueBean)Bean2).getLabel();
> return label1.compareToIgnoreCase(label2);
>
> }
> };
/* * $Header: /home/cvspublic/jakarta-struts/src/share/org/apache/struts/util/LabelValueBean.java,v 1.6 2003/07/04 18:26:19 dgraham Exp $ * $Revision: 1.6 $ * $Date: 2003/07/04 18:26:19 $ * * ==================================================================== * * The Apache Software License, Version 1.1 * * Copyright (c) 1999-2003 The Apache Software Foundation. All rights * reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * 3. The end-user documentation included with the redistribution, if * any, must include the following acknowlegement: * "This product includes software developed by the * Apache Software Foundation (http://www.apache.org/)." * Alternately, this acknowlegement may appear in the software itself, * if and wherever such third-party acknowlegements normally appear. * * 4. The names "The Jakarta Project", "Struts", and "Apache Software * Foundation" must not be used to endorse or promote products derived * from this software without prior written permission. For written * permission, please contact [EMAIL PROTECTED] * * 5. Products derived from this software may not be called "Apache" * nor may "Apache" appear in their names without prior written * permission of the Apache Group. * * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * ==================================================================== * * This software consists of voluntary contributions made by many * individuals on behalf of the Apache Software Foundation. For more * information on the Apache Software Foundation, please see * <http://www.apache.org/>. * */
package org.apache.struts.util;
import java.io.Serializable;
import java.util.Comparator;
/**
* A simple JavaBean to represent label-value pairs. This is most commonly used
* when constructing user interface elements which have a label to be displayed
* to the user, and a corresponding value to be returned to the server. One
* example is the <code><html:options></code> tag.
*
* @author Craig R. McClanahan
* @author Martin F N Cooper
* @author David Graham
* @author Paul Sundling
*
* @version $Revision: 1.6 $ $Date: 2003/07/04 18:26:19 $
*/
public class LabelValueBean implements Comparable, Serializable {
// ----------------------------------------------------------- Constructors
/**
* Default constructor.
*/
public LabelValueBean() {
super();
}
/**
* Construct an instance with the supplied property values.
*
* @param label The label to be displayed to the user.
* @param value The value to be returned to the server.
*/
public LabelValueBean(String label, String value) {
this.label = label;
this.value = value;
}
// ------------------------------------------------------------- Properties
/**
* The property which supplies the option label visible to the end user.
*/
private String label = null;
public String getLabel() {
return this.label;
}
public void setLabel(String label) {
this.label = label;
}
/**
* The property which supplies the value returned to the server.
*/
private String value = null;
public String getValue() {
return this.value;
}
public void setValue(String value) {
this.value = value;
}
// --------------------------------------------------------- Public Methods
/**
* Return a string representation of this object.
*/
public String toString() {
StringBuffer sb = new StringBuffer("LabelValueBean[");
sb.append(this.label);
sb.append(", ");
sb.append(this.value);
sb.append("]");
return (sb.toString());
}
/**
* LabelValueBeans are equal if their values are both null or equal.
* @see java.lang.Object#equals(java.lang.Object)
*/
public boolean equals(Object obj) {
if (obj == this) {
return true;
}
if (!(obj instanceof LabelValueBean)) {
return false;
}
LabelValueBean bean = (LabelValueBean) obj;
int nil = (this.getValue() == null) ? 1 : 0;
nil += (bean.getValue() == null) ? 1 : 0;
if (nil == 2) {
return true;
} else if (nil == 1) {
return false;
} else {
return this.getValue().equals(bean.getValue());
}
}
/**
* The hash code is based on the object's value.
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return (this.getValue() == null) ? 17 : this.getValue().hashCode();
}
/**
* The comparable interface allows sorting. This is done based on the
* label, since that is the human viewable part of the object.
* @see java.lang.Comparable
*/
public int compareTo(Object otherBean) {
// implicitly tests for the correct type, throwing ClassCastException
// as required by interface
String otherLabel = ((LabelValueBean)otherBean).getLabel();
//compare the labels of the LabelValueBean objects
return this.getLabel().compareTo(otherLabel);
}
/**
* Comparator object that can be used for a case insensitive order
* sort of LabelValueBean objects. The idea for this comes from
* java.lang.String
*/
public static Comparator CASE_INSENSITIVE_ORDER = new Comparator() {
public int compare(Object Bean1, Object Bean2) {
String label1 = ((LabelValueBean)Bean1).getLabel();
String label2 = ((LabelValueBean)Bean2).getLabel();
return label1.compareToIgnoreCase(label2);
}
};
}
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
