2012-08-31 87 views
0

我想从tapestry tutorial学习织锦,但是它 一部分是过时的。挂毯数据gridsource

使用GridDataSource代码是:

package com.packtpub.celebrities.util; 
import com.packtpub.celebrities.data.IDataSource; 
import com.packtpub.celebrities.model.Celebrity; 
import java.util.List; 
import org.apache.tapestry.beaneditor.PropertyModel; 
import org.apache.tapestry.grid.GridDataSource; 

public class CelebritySource implements GridDataSource { 
    private IDataSource dataSource; 
    private List<Celebrity> selection; 
    private int indexFrom; 

    public CelebritySource(IDataSource ds) { 
    this.dataSource = ds; 
    } 

    public int getAvailableRows() { 
    return dataSource.getAllCelebrities().size(); 
    } 

    public void prepare(int indexFrom, int indexTo, PropertyModel propertyModel, boolean ascending) { 
    String propertyName = propertyModel == null ? null : propertyModel.getPropertyName(); 

    System.out.println("Preparing selection."); 
    System.out.println("Index from " + indexFrom + " to " + indexTo);   
    System.out.println("Property name is: " + propertyName); 
    System.out.println("Sorting order ascending: " + ascending); 

    this.selection = dataSource.getRange(indexFrom, indexTo); 
    this.indexFrom = indexFrom; 
    } 

    public Object getRowValue(int i) { 
    System.out.println("Getting value for row " + i); 
    return selection.get(i - this.indexFrom); 
    } 

    public Class getRowType() { 
    return Celebrity.class; 
    } 
} 

法实施准备现在已经改变了,它看起来像

public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {} 

少数examples的google.com上的一个展示新方法实现:

public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) { 
    for (SortConstraint constraint : sortConstraints) { 
    final ColumnSort sort = constraint.getColumnSort(); 

    if (sort == ColumnSort.UNSORTED) continue; 

    final PropertyConduit conduit = constraint.getPropertyModel().getConduit(); 

    final Comparator valueComparator = new Comparator<Comparable>() { 
     public int compare(Comparable o1, Comparable o2) { 
     // Simplify comparison, and handle case where both are nulls. 
     if (o1 == o2) return 0; 
     if (o2 == null) return 1; 
     if (o1 == null) return -1; 
     return o1.compareTo(o2); 
     } 
    }; 

    final Comparator rowComparator = new Comparator() { 
     public int compare(Object row1, Object row2) { 
     Comparable value1 = (Comparable) conduit.get(row1); 
     Comparable value2 = (Comparable) conduit.get(row2); 
     return valueComparator.compare(value1, value2); 
     } 
    }; 

    final Comparator reverseComparator = new Comparator() { 
     public int compare(Object o1, Object o2) { 
     int modifier = sort == ColumnSort.ASCENDING ? 1 : -1; 
     return modifier * rowComparator.compare(o1, o2); 
     } 
    }; 

    // We can freely sort this list because its just a copy. 
    Collections.sort(_list, reverseComparator); 
    } 
} 

有什么办法创造functionali使用,因为它是现在准备方法的旧代码工作的TY?

public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) {} 

也希望任何其他解决这个问题的方法。

回答

1

它看起来像旧的prepare()方法只允许你排序一个标准,而新的prepare()方法可以让你排序很多。 (即你可以指定一个二次排序)。

propertyModel和一个升序/降序枚举现在被保存在SortConstraint对象中,但看着代码,没有使用!所以,只得到了例如工作,试试这个:

public void prepare(int startIndex, int endIndex, List<SortConstraint> sortConstraints) { 
    this.selection = dataSource.getRange(startIndex, endIndex); 
    this.indexFrom = startIndex; 
} 

如果你需要properyModel(调试),它应该可以访问来自:

PropertyModel propertyModel = sortConstraints.get(0).getPropertyModel(); 
+0

坦克似乎工作,具有的下一个部分的问题教程http://stackoverflow.com/questions/12232909/error-tapestry-using-data-gridsource-2 – nkvnkv