Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » JFace » WritableList casting issues(Trying to create a ArrayList from a WritableList)
WritableList casting issues [message #1007435] Tue, 05 February 2013 20:41 Go to next message
James Teater is currently offline James Teater
Messages: 17
Registered: July 2011
Junior Member
I am using a writable list to store data the user selects. I am using a writable list so that when users add or remove data my table viewers update.

This is part of the class that creates the writable list.

public class AplotDataModel  {

 IObservableList observableList = new WritableList();
 private static AplotDataModel instance = null;

 //////////////////////////////////////////////////////////////////////////
 //                         Constructor                                  //
 //////////////////////////////////////////////////////////////////////////
 private AplotDataModel() {

 }// end Constructor

 //////////////////////////////////////////////////////////////////////////
 //             SingletonSelectTable getInstance()                       //
 //////////////////////////////////////////////////////////////////////////
 public static AplotDataModel getInstance() {
    if (instance == null) {
       instance = new AplotDataModel();
  }
 return instance;
 }


 //////////////////////////////////////////////////////////////////////////
 //                              add()                                   //
 //////////////////////////////////////////////////////////////////////////
 public void add(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
    AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);
       if (!observableList.contains(pp)) {
          observableList.add(pp);
       }
 }

 //////////////////////////////////////////////////////////////////////////
 //                       clearTableArray()                              //
 //////////////////////////////////////////////////////////////////////////
 public void clearTableArray() {
    observableList.clear();
 }
}// End Class


I am using the class AplotDataset to format the list.

AplotDatasetData pp = new AplotDatasetData(tcRevision, selectedDataset);


Some of the class

public class AplotDatasetData {

  TCComponentItemRevision rev;
  TCComponentDataset   componentdataset;
  String markUp = "no";

  //////////////////////////////////////////////////////////////////////////
  //                           Constructor                                //
  //////////////////////////////////////////////////////////////////////////
  public AplotDatasetData(TCComponentItemRevision tcRevision, TCComponentDataset selectedDataset) {
     rev = tcRevision;
     componentdataset = selectedDataset;

  }// end Constructor

  //////////////////////
  //   getDataset()   //
  //////////////////////
  public TCComponent getDataset() {
     return componentdataset;
  }

  //////////////////
  //   getRev()   //
  //////////////////
  public TCComponent getRev() {
     return rev;
  }

  //////////////////
  //   equals()   //
  //////////////////
  @Override
  public boolean equals(Object o) {
     AplotDatasetData p = (AplotDatasetData) o;
     if (rev.equals(p.getRev()) && componentdataset.equals(p.getDataset())) {
       return true;
      }
     else {
        return false;
      }
  }// end equals()


Everything works as it should to this point.

In a different class I am trying to create a new ArrayList using data in my Writable List.

public void buildPlotArray() {
      
      ArrayList<AplotDataModel.AplotDatasetData> tableData = new ArrayList<AplotDataModel.AplotDatasetData>(AplotDataModel.getInstance().getObservableList());//Getting Writable List
      
      String yesECM = "yes";
      String noECM = "no";
      
      for(int i = 0; i < tableData.size(); i++){
         TCComponentItemRevision rev = tableData.get(i).rev;
         TCComponentDataset dataset = tableData.get(i).componentdataset;
         String ecmCheck = tableData.get(i).getMarkupValue();
         if(ecmCheck == "both"){
            PlotData yfd = new PlotData(rev , dataset, yesECM);
            plotData.add(yfd);
            PlotData nfd = new PlotData(rev , dataset, noECM);
            plotData.add(nfd);
         }
         else {
            PlotData sfd = new PlotData(rev , dataset, ecmCheck);
            plotData.add(sfd);
         }
      }
   } 


The code compiles, but I try to run specific operations. I get the following error.

ERROR: 11:31:00,591 - TcLogger$IC_LogListener.logging:? org.eclipse.core.runtime - org.eclipse.ui - 0 - Unhandled event loop exception org.eclipse.swt.SWTException: Failed to execute runnable (java.lang.ClassCastExc eption: org.eclipse.core.databinding.observable.list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData)
etc .....

Caused by: java.lang.ClassCastException: org.eclipse.core.databinding.observable .list.WritableList cannot be cast to com.lexmark.aplot.datamodels.AplotDataModel $AplotDatasetData at com.lexmark.aplot.datamodels.AplotDataModel$AplotDatasetData.equals(A plotDataModel.java:167)

So what I know from the error. It is a casting issue with the Writable List.
The error is occurring in the equal method in AplotDatasetData

@Override
      public boolean equals(Object o) {
         AplotDatasetData p = (AplotDatasetData) o; //Right here
         if (rev.equals(p.getRev()) && componentdataset.equals(p.getDataset())) {
            return true;
         }
         else {
           return false;
         }
      }// end equals()


I also know that the only place I am trying to cast the list is when I am trying to create the new ArrayList.

 ArrayList<AplotDataModel.AplotDatasetData> tableData = new ArrayList<AplotDataModel.AplotDatasetData>(AplotDataModel.getInstance().getObservableList())


So at this point I don't know if I need to:

1. Work on the equals method, trying to get it to work with the writable list?
2. If I need to work on the place where I am trying to create a new ArrayList from the writable list?
3. If there is a better way to define the WritableList, when creating it?

I have to have the equals method, so the users can not put duplicate data in the list. I also have to have the writable list.

This issue is keeping me from be able to go live with my application.

Any help will be greatly appreciated!!
Re: WritableList casting issues [message #1007614 is a reply to message #1007435] Wed, 06 February 2013 17:55 Go to previous messageGo to next message
Ben Cox is currently offline Ben Cox
Messages: 17
Registered: June 2012
Junior Member
The answer is 1: You need to do more work on the equals() method of AplotDatasetData.

If you auto-generate an equals() method using Eclipse, you'll commonly notice that the first few lines look like this:

if (this == obj)
  return true;
if (obj == null)
  return false;
if (getClass() != obj.getClass())
  return false;


You need this code to sanitise any calls to equals(). Any code is at liberty to call equals() on any object with any argument, and your code needs to be able to deal with that. You should also check for rev and componentdataset being null, to avoid NullPointerExceptions in equals(). In fact, just do right-click -> Source -> Generate hashcode() and equals() - it'll automatically generate best-practice methods for you.

Other than that, it's difficult to tell because you haven't pasted the full stack trace, but it could be a harmless call from inside JFace. Complete your equals() method and hopefully everything will work better.
Re: WritableList casting issues [message #1007616 is a reply to message #1007614] Wed, 06 February 2013 18:01 Go to previous message
James Teater is currently offline James Teater
Messages: 17
Registered: July 2011
Junior Member
You were exactly right! Updates the equals method, as you suggested and it works correctly now.

Thank you for your quick repsonse
Previous Topic:Using Markers in a Form Editor
Next Topic:Arrays as dynamic properties (TableViewer Databinding)
Goto Forum:
  


Current Time: Sat Oct 25 03:35:09 GMT 2014

Powered by FUDForum. Page generated in 0.01779 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software