简体   繁体   English

使用数据网格源的挂毯

[英]tapestry using data gridsource

I am trying to learn tapestry from tapestry tutorial , however part of it is outdated. 我正在尝试从挂毯教程中学习挂毯,但是其中一部分已经过时了。

Using GridDataSource code for it is: 为此使用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;
  }
}

method implementation prepare has changed now it looks like 方法实现的prepare已经改变,现在看起来像

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

one of the few examples on google.com showing new method implementation is: 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);
  }
}

Is there any way to create functionality of the old code work using prepare method as it is now? 有什么方法可以像现在一样使用prepare方法创建旧代码工作的功能?

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

Also would appreciate any other solution to this problem. 也将不胜感激对此问题的任何其他解决方案。

It looks like the old prepare() method only let you sort on one criteria, whilst the new prepare() method lets you sort on many. 看起来旧的prepare()方法只允许您对一个条件进行排序,而新的prepare()方法可以让您对多个条件进行排序。 (ie you can specify a secondary sort.) (即,您可以指定辅助排序。)

The propertyModel and an ascending / descending enum are now held in the SortConstraint object, but looking at the code, none of that is used! 现在,该propertyModel和一个升/降枚举枚举都保存在SortConstraint对象中,但是看一下代码,没有使用任何代码! So to just get the example working, try this: 因此,为了使示例正常工作,请尝试以下操作:

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

if you need the properyModel (for debugging) it should be accessible from: 如果您需要properyModel (用于调试),则可以从以下位置访问它:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM