简体   繁体   English

当源对象顺序属性更改时,上釉列表SortedList更新

[英]Glazed Lists SortedList update when source object order property changed

I have some object with orderProperty 我有一些与orderProperty有关的对象

class Car
{
    private int order;

    // getter and setter implementation
}

Then I create EventList: 然后我创建EventList:

EventList<Car> cars=new BasicEventList<Car>()
for (i=0;i<10;i++) {Car car=new Car(i); cars.add(car);}

Then I create SortedList based on cars to use it for TableModel 然后我基于汽车创建SortedList,以将其用于TableModel

SortedList<Car> sortedCars=new SortedList<Car>(cars,carsComparator);

Here is comparator: 这是比较器:

Comparator<Car> carsComparator implements Comparator<Car>
{
            @Override
            public int compare(Car o1, Car o2) {
                return o1.getOrder() - o2.getOrder();
            }
}

In program there are some events that change car.order property. 在程序中,有些事件会更改car.order属性。 How to notify Lists about this changes? 如何通知列表有关此更改?

Q: How to notify Lists about order change ? 问:如何通知清单有关订单变更的信息?

Possible case scenario not necessarily the best one. 可能的情况不一定是最好的情况。

The class car could implement the Observer design pattern . 上课的汽车可以实现观察者设计模式 Then in each setter after, value change you could notify the listeners about it. 然后在每个设置器中,值更改可以将其通知给侦听器。

In the code where you are creating the list, you could add the listener to car, that check for order property change and reorder the list. 在创建列表的代码中,可以将侦听器添加到car中,以检查订单属性是否更改并重新排序列表。

The example event 示例事件

public class PropertyChangeEvent extends EventObject {

  private final String propertyName;
  private final Object oldValue;
  private final Object newValue;

  public PropertyChange(String propertyName, Object oldValue, Object newValue) {
     this.propertyName = propertyName;
     this.oldValue = oldValue;
     this.newValue = newValue;
  }

  public String getProperty() {

   reutnr this.propertyName;
 }

  //Rest of getters were omitted. 

}

And the listener 和听众

public abstract interface PropertyChangeListener extends EventListener {

   public abstract void propertyChange(PropertyChangeEvent event);

}

Then you should write a class that will support the property change, is should have methods like addPropertyChangeListener(PropertyChangeListener pcl), firePropertyChange(PropertyChangeEvent pce). 然后,您应该编写一个支持属性更改的类,该类应具有诸如addPropertyChangeListener(PropertyChangeListener pcl),firePropertyChange(PropertyChangeEvent pce)之类的方法。

When the manger of property change is done. 完成属性更改的管理器。 Only thing what you have to do is. 您唯一要做的就是。

public class Car { 

    private PropertyChangeManager manager = new PropertyChangeManager(Car.class);


    /* The omitted code*/

    public void setOrder(int order) {
      int oldValue = this.order;
      this.order = order;
      manager.firePropertyChange("order", oldValue, this.order);
    }

    public void addPropertyChangeListener(PropertyChangeListener pcl) { 
     this.manager.addPropertyChangeLIstener(pcl);
   }
}

And in the class where you use that list. 在使用该列表的课程中。

    private SortedList<Car> sortedCars=new SortedList<Car>();

    private PropertyChangeListener listReorder = new ProprtyChangeListener() {

        @Override
        public void propertyChange(PropertyChangeEvent event) {

            if("order".equals(event.getProperty()) {
               reoderCars();
            }

        }

   public boolean addCarToOrderList(Car car) {
          /* Safety code omitted  */

        boolean result = false;

        if(sortedCars.contains(car) == false) {
           result = sortedCars.add(car);
         }

        if(result) {
            car.addPropertyChangeListener(listReorder); //We assume that this is safe
        }

       return result;
    }

   public void reoderCars() {

     synchronized(this.sortedCar) {
      //the code that will sort the list.
     }
  }

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

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