Question

I came across this code on Vogella which demonstrates creating a LabelProvider for a JFACE tree.

I'm finding it really confusing however. The Image objects, named FOLDER and FILE are defined by calling a method which returns either FOLDER or FILE.

Essentially it's like a dictionary saying insured means someone with insurance and insurance is what some who is insured has. I am confused.

Why doesn't it cause an infinite loop?

package de.vogella.jface.treeviewer.provider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.ISharedImages;
import org.eclipse.ui.PlatformUI;
import de.vogella.jface.treeviewer.model.Category;
import de.vogella.jface.treeviewer.model.Todo;

public class TodoLabelProvider extends LabelProvider {

  private static final Image FOLDER = getImage("folder.gif");
  private static final Image FILE = getImage("file.gif");


  @Override
  public String getText(Object element) {
    if (element instanceof Category) {
      Category category = (Category) element;
      return category.getName();
    }
    return ((Todo) element).getSummary();
  }

  @Override
  public Image getImage(Object element) {
    if (element instanceof Category) {
      return FOLDER;
    }
    return FILE;
  }

  // Helper Method to load the images
  private static Image getImage(String file) {
    Bundle bundle = FrameworkUtil.getBundle(TodoLabelProvider.class);
    URL url = FileLocator.find(bundle, new Path("icons/" + file), null);
    ImageDescriptor image = ImageDescriptor.createFromURL(url);
    return image.createImage();

  } 
} 
Was it helpful?

Solution

You're confused because of the method overloading. The calls here:

private static final Image FOLDER = getImage("folder.gif");
private static final Image FILE = getImage("file.gif");

don't call this method:

public Image getImage(Object element) {

They call this one:

private static Image getImage(String file) {

They couldn't call the first method anyway, as it's an instance method and the call is in a static field initializer with no instance to call the method on. But they call the second method anyway because the argument is a string.

It's still pretty odd code - particularly the implementation of getImage(Object element).

Even if it was written like this:

// Bogus code!
private static Image FOLDER = getImage("folder.gif");
private static Image FILE = getImage("file.gif");

private static Image getImage(String name)
{
    return name.equals("FRED") ? FOLDER : FILE;
}

... that still wouldn't cause an infinite loop. You'd just end up with null values. The static field initializer just sets the initial value for the variable. It doesn't associate the variable with the method call such that any access to the variable invokes the method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top