Dec 24

Static methods cannot call non-static methods. An instance of the class is required to call its methods and static methods are not accociated with an instance (they are class methods). To fix it you have a few choices depending on your exact needs.


/**
*  Will not compile
*/

public class StaticReferenceToNonStatic
{
   public static void myMethod()
   {
      // Cannot make a static reference
      // to the non-static method
      myNonStaticMethod();
   }

   public void myNonStaticMethod()
   {
   }
}

/**
* you can make your method non-static
*/

public class MyClass
{
   public void myMethod()
   {
      myNonStaticMethod();
   }

   public void myNonStaticMethod()
   {
   }
}

/**
*  you can provide an instance of the
*  class to your static method for it
*  to access methods from
*/

public class MyClass
{
   public static void myStaticMethod(MyClass o)
   {
      o.myNonStaticMethod();
   }

   public void myNonStaticMethod()
   {
   }
}

/**
*  you can make the method static
*/

public class MyClass
{
   public static void myMethod()
   {
      f();
   }

   public static void f()
   {
   }
}

written by objects \\ tags: ,

Dec 20

To include a calculated field in a ListGrid you need to create a new ListGridField and specify the cellFormatter property to handle calculating the fields value.

// Following example shows a data source that return two fields
// firstName and lastName
// A CellFormatter is used to concatenate the first and last name
// in a single field in the ListGrid

DataSource peopleDS = new RestDataSource();
peopleDS.addField(new DataSourceTextField("firstName"));
peopleDS.addField(new DataSourceTextField("lastName"));

final ListGrid people = new ListGrid();
people.setDataSource(peopleDS);

// Create a new "Name" field in ListGrid

ListGridField name = new ListGridField("name", "Name");

// Specify a CellFormatter to calculate the fields value

name.setCellFormatter(new CellFormatter()
{
   public String format(Object value, ListGridRecord record,
      int row, int column)
   {
        // Get 'firstName' and 'lastName' values
        // from record being displayed

        String firstName = record.getAttributeAsString("firstName");
        String lastName = record.getAttributeAsString("lastName");

        // return concatenated name

        return firstName + " " + lastName;
   }
});

// Add calculated field to the ListGrid

people.setFields(name);

written by objects \\ tags: , , ,

Dec 19

By default the RestDataSource only supports static url’s. If your backend requires dynamic url’s then you need to subclass RestDataSource and override transformRequest(). In transformRequest() you can set the actionURL property as required by your backend server.


// Following example shows how to append
// the entities if to the static update url

final RestDataSource restDataSource = new RestDataSource()
{
   @Override
   public Object transformRequest(DSRequest dsRequest)
   {
       if (dsRequest.getOperationType().equals(DSOperationType.UPDATE))
       {
          int id = JSOHelper.getAttributeAsInt(
              dsRequest.getData(), "id"));
          dsRequest.setActionURL(
             getUpdateDataURL()+"/"+id;
       }
       return super.transformRequest(dsRequest);
   }
};

written by objects \\ tags: , , ,