Announcement Announcement Module
Collapse
No announcement yet.
handleDeletedObject in AbstractObjectTable Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • handleDeletedObject in AbstractObjectTable

    Hello

    I have a problem with deleting rows in a AbstractObjectTable via life cycle events. I have assumed that object identity is managed via equals but it seems that identity is managed by the PropertyCompartor used to do the sorting.

    The following fails/works depending on the how the PropertyComperator is constructed:

    IncidentAttachment a = new IncidentAttachment();
    a.setId("1");
    a.setName("xxx");
    IncidentAttachment b = new IncidentAttachment();
    b.setId("1");
    assertEquals(a, b); // uses id
    EventList rawList = GlazedLists.eventList(Arrays.asList(new Object[] {}));
    SortedList baseList = new SortedList(rawList, new PropertyComparator("name", false, true));
    baseList.add(a);
    assertEquals(1, baseList.size());
    baseList.remove(b);
    assertEquals(0, baseList.size());

    How can I make deletes work on id (equals) when the table is sorted?

    Seems to me that the javadoc is wrong "..(by equals).."

    /**
    * Handle the deletion of an object in this table. Locate this entry (by equals) and delete it.
    * @param object Updated object being deleted
    */
    protected void handleDeletedObject(final Object object) {
    runWithWriteLock(new Runnable() {
    public void run() {
    int index = getFinalEventList().indexOf(object);
    if (index >= 0) {
    getFinalEventList().remove(index);
    }
    }
    });
    }


    BR
    Carsten

  • #2
    http://markmail.org/message/nju7adzw...+state:results

    IncidentAttachment a = new IncidentAttachment();
    a.setId("1");
    a.setName("xxx");
    IncidentAttachment b = new IncidentAttachment();
    b.setId("1");
    assertEquals(a, b);
    EventList rawList = GlazedLists.eventList(Arrays.asList(new Object[] {}));
    SortedList baseList = new SortedList(rawList, new PropertyComparator("name", false, true));
    baseList.add(a);
    assertEquals(1, rawList.size());
    assertEquals(1, baseList.size());
    //baseList.remove(b);
    rawList.remove(b);
    assertEquals(0, rawList.size());
    assertEquals(0, baseList.size());


    I think AbstractObjectTable should keep a ref to rawList and make deletes on that instead of the baseList/finallist.

    Comment


    • #3
      Here is a hacked version that uses equals to remove objects

      package ui.vtsdb.generic;

      import java.lang.reflect.Field;
      import java.util.Arrays;

      import org.springframework.beans.support.PropertyComparat or;
      import org.springframework.context.ApplicationEvent;
      import org.springframework.richclient.application.event.L ifecycleApplicationEvent;
      import org.springframework.richclient.table.support.Abstr actObjectTable;

      import ca.odell.glazedlists.EventList;
      import ca.odell.glazedlists.GlazedLists;
      import ca.odell.glazedlists.SortedList;

      public abstract class MyTable<T> extends AbstractObjectTable {

      private EventList<Object> rawList;

      private Class<T> clazz;

      /**
      * Default constructor.
      */
      public MyTable(String modelId, String[] columnPropertyNames,
      Class<T> clazz) {
      super(modelId, columnPropertyNames);
      this.clazz = clazz;
      }

      /**
      * Provide the initial data for the table. Note that this is hard coded for
      * this sample. You would normally access a persistent store, or some other
      * source to get the data for the table.
      */
      protected Object[] getDefaultInitialData() {
      return new Object[] {};
      }

      /**
      * Get the array of selected Contact objects in the table.
      *
      * @return array of Contacts, zero length if nothing is selected
      */
      public T[] getSelectedObjects() {
      int[] selected = getTable().getSelectedRows();
      T[] objects = (T[]) new Object[selected.length];
      for (int i = 0; i < selected.length; i++) {
      objects[i] = (T) getTableModel().getElementAt(selected[i]);
      }
      return objects;
      }

      public T getSelectedObject() {
      if (getSelectedObjects().length != 0)
      return getSelectedObjects()[0];
      return null;
      }

      @Override
      protected boolean shouldHandleEvent(ApplicationEvent e) {
      if (e instanceof LifecycleApplicationEvent) {
      LifecycleApplicationEvent le = (LifecycleApplicationEvent) e;
      if (le.getObject().getClass() == this.clazz)
      return true;
      }
      return false;
      }

      /**
      * Get the base event list for the table model. This can be used to build
      * layered event models for filtering.
      *
      * @return base event list
      */
      @Override
      public EventList getBaseEventList() {
      Field baseListField = null;
      EventList result = null;
      try {
      baseListField = AbstractObjectTable.class
      .getDeclaredField("baseList");
      baseListField.setAccessible(true);

      if (baseListField.get(this) == null) {

      // Construct on demand

      Object[] data = getInitialData();
      if (logger.isDebugEnabled()) {
      logger.debug("Table data: got " + data.length + " entries");
      }
      // Construct the event list of all our data and layer on the
      // sorting
      rawList = GlazedLists.eventList(Arrays.asList(data));
      int initialSortColumn = getInitialSortColumn();
      if (initialSortColumn >= 0) {
      String sortProperty = getColumnPropertyNames()[initialSortColumn];
      baseListField.set(this, new SortedList(rawList,
      new PropertyComparator(sortProperty, false, true)));
      // baseList = new SortedList(rawList, new
      // PropertyComparator(
      // sortProperty, false, true));
      } else {
      baseListField.set(this, new SortedList(rawList));
      // baseList = new SortedList(rawList);
      }
      }
      result = (EventList) baseListField.get(this);
      } catch (Exception e) {
      e.printStackTrace();
      }
      return result;
      }

      @Override
      protected void handleDeletedObject(final Object object) {
      runWithWriteLock(new Runnable() {
      public void run() {
      int index = rawList.indexOf(object);
      if (index >= 0) {
      rawList.remove(index);
      }
      }
      });
      }

      }

      Comment

      Working...
      X