Apache Commons HashCodeBuilder and EqualsBuilder

Overriding equals and hashcode methods is dull.  🙂  Fortunately, Apache have provided helper classes in their commons library.

Lets say you have a class containing a String member variable called name, and an int member variable called number. To override the hashcode method, you create a new HashCodeBuilder, and append your member variables to it. Once you’re done, you call its toHashCode() method.

@Override

public int hashcode() {
return new HashCodeBuilder().append(this.name).append(this.number).toHashCode();
}

Generating the equals method is similar. This time you use the EqualsBuilder class:

@Override

public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj instanceof MyClass == false) {
return false;
}
MyClass other = (MyClass) obj;
return new EqualsBuilder().append(this.name, other.name).append(this.number, other.number).isEquals();
}

Adding search filter to JFace table viewer

This topic is basically covered in the excellent Vogella JFace Table tutorial.  The upshot of it is that you can add a ViewerFilter to your table viewer.  The implementation of the ViewerFilter determines whether entries in the table are displayed.

Implement your own, and then add to the table viewer via the viewer.addFilter(filter) method.

In the example given in the tutorial, the implementation of the ViewerFilter contains a method – setSearchText(String s) – which allows you to define what the current search string is.  Add a text control to your GUI, and implement a KeyAdapter to listen for keyReleased() messages.  When the user types something in, set the search string on the filter from the contents of the text control.

Passing parameters to a RESTful web service

Within your web.xml, you can define configuration parameters using the <env-entry> tag. The <env-entry> element defines the name of our parameter, its type, and its value. For example:

<web-app>
 <!– Rest of the web app configuration here –>

  <env-entry>
    <env-entry-name>max-customers-size</env-entry-name>
    <env-entry-type>java.lang.Integer</env-entry-type>
    <env-entry-value>10</env-entry-value>
  </env-entry>
</web-app>

Within your servlet, you use the following code to get hold of the parameter:

  try  {

InitialContext ctx = new InitialContext();
Integer size = (Integer) ctx.lookup("java:comp/env/max-customers-size");
} catch (Exception e) {
// handle exception here
}

Many thanks to the O’Reilly JAX-RS book for the example.

Combining multiple images in SWT

The task I have been set is to combine multiple images (icons and so on) into one image, along with some text. I found that the best approach to do this within SWT was to make use of the GC object.

Once a GC object has been created, you can draw an image within using gc.drawImage(image, x, y). You can add text using gc.drawText(string, x, y). If you need to find the extent of text within the GC, you can use gc.textExtent(string), which returns a Point.

I created a GC with a new image. Once I had finished drawing to it via the GC, I could just return the image from my helper method.

Speeding up builds in Eclipse

The application I’m working on at the moment has a lot of Eclipse plug-in projects, and I was finding that the build process was starting to take a long time.

We produce a PDE build of our application, so I found that by setting this as my build target, I could remove most of the source code from my workspace, and just import the projects I was directly working on.

I have Jadclipse installed, which is a very good tool. However, since I have the source code available, it would be useful to actually view it to see stuff like comments.

I found the following link (how-do-i-decompile-java-classes-in-eclipse)
, which shows how you can attach source to jars in your workspace. It is quite manual, and you would have to do it for all jars which you depend on, but it should do for now.

Adding images to JFace TableViewer

When adding images to a JFace TableViewer, make sure that the column containing the images is wide enough. The TableViewer behaviour seems to be that it squashes the image if it is not in the currently selected row, but hides the image completely if it is in the selected row.

Worth remembering – it took me about half an hour today to work out what was going on! 🙂