You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tapestry.apache.org by Ron Piterman <mp...@vollbio.de> on 2005/07/05 15:51:13 UTC

4.0: Problem using internal class

Hi, I am starting to port an unfinished application to 4.0b1.
I have a Model class which uses an internal class as comparator. In 
3.0.2 it did ok, but under 4.0b1 I get a java.lang.NoClassDefError.

could that have something with tapestry/hivemind ?

Here is the class. The exception is being thrown on sort();

Thanx, Ron

PS - I would suggest to add this class to 4.0 (after buetifying...)


=============================================================================
/*
  * Created on Mar 10, 2005
  *
  * TODO To change the template for this generated file go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
package net.tutim.tapestry.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Set;

import net.tutim.util.OgnlExpression;
import ognl.Ognl;
import ognl.OgnlException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.tapestry.ApplicationRuntimeException;
import org.apache.tapestry.form.IPropertySelectionModel;

/**
  * @author ron1
  *
  * TODO To change the template for this generated type comment go to
  * Window - Preferences - Java - Code Style - Code Templates
  */
public class OgnlPropertySelectionModel implements IPropertySelectionModel {
     private static final Log log = LogFactory
         .getLog(OgnlPropertySelectionModel.class);

     private String _valueProperty;
     private String _labelProperty;
     private String _optionProperty;

     protected List list;

     protected boolean nullable;

     public OgnlPropertySelectionModel(Set set_, String labelProp_, 
String valueProp_, String optionProp_, boolean nullable_) {
         this(new ArrayList(set_),labelProp_, valueProp_, optionProp_, 
nullable_);
     }

     public OgnlPropertySelectionModel sort(String ognl) {
         Comparator c = new ListComparator(ognl);
         Collections.sort(list, c);
         return this;
     }

     public OgnlPropertySelectionModel sort() {
         return sort(_labelProperty);
     }

     /**
      * Constructs a Property Selection model which uses reflection on 
the obejcts to
      * get the label, value, and option object.
      * If either of the given property names is null,
      * it will return, for label and value, the toString() value of the
      * object.
      * For the option object, it will return the option object itself 
(if the
      * optionProp property is null).
      */
     public OgnlPropertySelectionModel(List list_, String labelProp_, 
String valueProp_, String optionProp_) {
         this(list_, labelProp_, valueProp_, optionProp_, false);
     }

     public OgnlPropertySelectionModel(List list_, String labelProp_, 
String valueProp_, String optionProp_, boolean nullable_) {
         _valueProperty = valueProp_;
         _labelProperty = labelProp_;
         _optionProperty = optionProp_;
         list = list_;
         nullable = nullable_;
     }

     /* (non-Javadoc)
      * @see 
org.apache.tapestry.form.IPropertySelectionModel#getOptionCount()
      */
     public int getOptionCount() {
         return list == null ? 0 : list.size() + (nullable ? 1 : 0);
     }

     /* (non-Javadoc)
      * @see org.apache.tapestry.form.IPropertySelectionModel#getOption(int)
      */
     public Object getOption(int index) {
         if (nullable && index == 0)
             return null;

         index -= (nullable ? 1 : 0);

         if (_optionProperty == null)
             return list.get(index);
         else if ("".equals(_optionProperty))
             return list.get(index).toString();
         else
             try {
                 return Ognl.getValue(_optionProperty, list.get(index));
             } catch (Exception e) {
                 log.error(this,e);
                 return "ERROR";
             }
     }

     /* (non-Javadoc)
      * @see org.apache.tapestry.form.IPropertySelectionModel#getLabel(int)
      */
     public String getLabel(int index) {
         if (nullable && index == 0)
             return " ";

         index -= (nullable ? 1 : 0);

         Object o = list.get(index);
         try {
             if (_labelProperty == null)
                 return o.toString();
             else
                 return Ognl.getValue(_labelProperty,o).toString();
         }
         catch (Exception x)  {
             log.error(this,x);
             return "ERROR";
         }
     }

     /* (non-Javadoc)
      * @see org.apache.tapestry.form.IPropertySelectionModel#getValue(int)
      */
     public String getValue(int index) {
         if (nullable && index == 0)
             return " ";

         index -= (nullable ? 1 : 0);

         Object o = list.get(index);
         try {
             if (_valueProperty == null)
                 return o.toString();
             else
                 return Ognl.getValue(_valueProperty,o).toString();
         }
         catch (Exception x)  {
             log.error(this,x);
             return "ERROR";
         }
     }

     /* (non-Javadoc)
      * @see 
org.apache.tapestry.form.IPropertySelectionModel#translateValue(java.lang.String)
      */
     public Object translateValue(String value) {
         for (int i = 0; i < getOptionCount(); i++ )
             if (getValue(i).equals(value))
                 return getOption(i);
         return null;
     }

     public void remove(Object o) {
         list.remove(o);
     }

     public class ListComparator implements Comparator {
         private OgnlExpression _ognl;

         public ListComparator( String ognl ) {
             try {
                 _ognl = new OgnlExpression(ognl);
             }
             catch (OgnlException ex) {
                 throw new ApplicationRuntimeException(ex);
             }
         }

         public int compare(Object arg0, Object arg1) {
             try {
                 return 
((Comparable)_ognl.getValue(arg0)).compareTo(_ognl.getValue(arg1));
             }
             catch (Exception ex) {
                 throw new IllegalArgumentException(ex);
             }
         }

     }
}

===============================================================================
package net.tutim.util;

import ognl.Ognl;
import ognl.OgnlContext;
import ognl.OgnlException;

public class OgnlExpression
{
     private Object       expression;
     private OgnlContext context = new OgnlContext();

     public OgnlExpression(String expressionString) throws OgnlException
     {
         super();
         expression = Ognl.parseExpression(expressionString);
     }

     public Object getExpression()
     {
         return expression;
     }

     public Object getValue( Object rootObject) throws OgnlException
     {
         return Ognl.getValue(getExpression(), context, rootObject);
     }

     public void setValue( Object rootObject, Object value) throws 
OgnlException
     {
         Ognl.setValue(getExpression(), context, rootObject, value);
     }
}

===================================================================


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-dev-help@jakarta.apache.org