Archive

Archive for March, 2009

CheckboxTreeViewer

March 3rd, 2009

Screenshot of application with CheckboxTreeViewer

Screenshot of application with CheckboxTreeViewer

Today I found out that quite a lot of people struggle with the CheckboxTreeViewer class and especially with the methods:

  • setChecked(Object element, boolean state)
  • setCheckedElements(Object[] elements)

Those names are very selfexplaining, but still, getting those methods to work took me some time. To shorten up your building time, I will explain how to use the setCheckedElements method in this post. But first I’ll start with the beginning, and that’s the creation of a CheckboxTreeViewer…

So let’s assume you have a Treestructure that you would like to visualize and that structure is build up by two different parts, lets call them Folders and Files. Now, to create the Treestructure we think about a file system, where every Folder can contain zero or multiple Files. You can see a screenshot of the final application on the right, I hope that my structure is a little bit more understandable once you’ve seen the application.

So what do we need to get a CheckboxTreeViewer in our application? Well, you need an CheckboxTreeViewer object and at least one ContentProvider and a LabelProvider.

So lets first start with the ContentProvider

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;

import be.jcreation.checkboxtreeviewerexample.model.FolderHandler;
import be.jcreation.checkboxtreeviewerexample.model.interfaces.File;
import be.jcreation.checkboxtreeviewerexample.model.interfaces.Folder;

public class TreeContentProvider implements ITreeContentProvider {

	@Override
	public Object[] getChildren(Object parentElement) {
		if(parentElement instanceof Folder){
			return ((Folder) parentElement).getAllFiles().toArray();
		}
		return null;
	}

	@Override
	public Object getParent(Object element) {
		if(element instanceof File){
			return ((File) element).getFolder();
		}
		return null;
	}

	@Override
	public boolean hasChildren(Object element) {
		if(element instanceof Folder)
			return true;
		return false;
	}

	@Override
	public Object[] getElements(Object inputElement) {
		if(inputElement instanceof Folder){
			return ((Folder) inputElement).getAllFiles().toArray();
		}
		return FolderHandler.getInstance().getAllFolders().toArray();
	}

	@Override
	public void dispose() {}

	@Override
	public void inputChanged(Viewer viewer, Object oldInput,
            Object newInput) {}

}

The major part of the above code can be found in any book about JFace, but it’s the method getElements(Object inputElement) that has been causing me a lot of problems. As you can see, you also have to keep in mind that the Treestructure should be used here also. So if the inputElement is an object of the class Folder, you should return the children of that object. I assume, I should check the code to be sure of course, that this method is used to search through the structure…

The used LabelProvider isn’t that spectacular, but to give you all the code, here we go:

import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.swt.graphics.Image;

import be.jcreation.checkboxtreeviewerexample.model.interfaces.File;
import be.jcreation.checkboxtreeviewerexample.model.interfaces.Folder;

public class LabelProvider implements ILabelProvider {

    @Override
    public Image getImage(Object element) {
        return null;
    }

    @Override
    public String getText(Object element) {
        if(element instanceof Folder)
            return ((Folder) element).getName();
        else if(element instanceof File)
            return ((File) element).getName();
        return null;
    }

    @Override
    public void addListener(ILabelProviderListener listener) {}

    @Override
    public void dispose() {    }

    @Override
    public boolean isLabelProperty(Object element, String property) {
        return false;
    }

    @Override
    public void removeListener(ILabelProviderListener listener) {}

}

And last, but not least, how to create the CheckboxTreeViewer, and how to use that setCheckedElements method!

        tv = new CheckboxTreeViewer(panel, SWT.NONE);
        tv.setContentProvider(new TreeContentProvider());
        tv.setLabelProvider(new LabelProvider());
        tv.setInput("root");
        // When the parent is checked, all children should be checked to
        tv.addCheckStateListener(new ICheckStateListener(){
            public void checkStateChanged(CheckStateChangedEvent event){
                if(event.getChecked()){
                    tv.setSubtreeChecked(event.getElement(), true);
                }
            }
        });        

        Button loadSomeSelection = new Button(parent, SWT.PUSH);
        loadSomeSelection.setText("Load");
        loadSomeSelection.addSelectionListener(new SelectionAdapter(){
            @Override
            public void widgetSelected(SelectionEvent e) {
                tv.setCheckedElements(FolderHandler.getInstance().
                                getSomeFilesToSelect().toArray());
            }
        });

If you have read the code carefully, you are now probably wondering what the method getSomeFilesToSelect is returning? Well:

    public List<File> getSomeFilesToSelect(){
        List<File> selectedFiles = new ArrayList<File>();
        for(Folder f : allFolders){
            if(f.getAllFiles().size() > 2){
                selectedFiles.add(f.getAllFiles().get(2));
            }else if(f.getAllFiles().size()>1){
                selectedFiles.add(f.getAllFiles().get(1));
            }else if(f.getAllFiles().size()>0){
                selectedFiles.add(f.getAllFiles().get(0));
            }
        }
        return selectedFiles;
    }

This is just some random selection, well not really random but every time the last child of a parent. But to make my point clear, as you can see, I’m giving a list of Files to the setCheckedElements method and those Files are checked (as the screenshot at the top of the page is showing you).

I hope this post is helping those who had the same problem with the CheckboxTreeViewer as I had. And to end my post, I’ll give you the link to my Ecipse Project. It’s an RCP-application to keep it simple.

Jan

Jan SWT - JFace , ,

Mozilla Thunderbird and Google Calendar

March 1st, 2009

This will be my first IT-related post and I’ll be taking off slowly.

Yesterday, a friend was talking about the fact that he’s using Google Calendar together with Mozilla Thunderbird. This way he can see his appointments when using Thunderbird, but when his not behind his own computer, he can still use the Google Calendar to manager is appointments! Quite nice if you’re asking me. So today I started reading some things about this combination, and now I’m using Google Calendar in my own Thunderbird!

During my reading on the Internet, I came across a very detailed installation guide for this combination, you can find this step-by-step guide here.

So, if you’re already using Thunderbird and/or Google Calendar, just try it out! You’ll see that your life just became easier…

Jan Information , ,

Welcome

March 1st, 2009

Welcome, Bienvenue, Tervetuloa, Welkom, Willkommen, Bienvenidos…

There are a thousand words to say welcome, I just picked a few out of the list! This is my first blog-entry but, I hope, not my last.
So from now on, I’ll try to post interesting IT-related stuff… No clue what It will be, but you’ll soon notice!

I’m open for every kind of comments, just keep it nice.

See you next time!

Jan

Jan General