Welcome back

November 1st, 2009

During the past weeks this site has been offline quite a while. Since the problem was caused by my ISP everything should work now without any problems. They have done some maintenance and forget to change some parameter…

So now it’s up to me to try to add some new posts in the following weeks…I’ll do my best!

Jan Uncategorized

localhost on Vista

April 27th, 2009

Last weekend I had to solve some problem on a Vista-computer. The problem was explained quite badly but well I thought I could give it a try and solve it… And since I’m an IT-guy, problems can always get solved using Google!

So the problem was explained as follow:  “MySQL is acting strange, I can’t connect to it using Java.”. Oh well they also gave me the exception stacktrace so I could see what the problem was:

Exception in thread “main” java.net.ConnectException: Connection refused: connect
at java.net.PlainSocketImpl.socketConnect(Native Method)
at java.net.PlainSocketImpl.doConnect(PlainSocketImpl.java:333)
at java.net.PlainSocketImpl.connectToAddress(PlainSocketImpl.java:195)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:182)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366)
at java.net.Socket.connect(Socket.java:519)
at java.net.Socket.connect(Socket.java:469)
at java.net.Socket.<init>(Socket.java:366)
at java.net.Socket.<init>(Socket.java:179)

Not that much to work with as you can see… So as I’m the Java freak, I first had to see that Java-code…

Socket s = new Socket("localhost", 3306);
s.close();

So first thing first, check whether MySQL is running and if it’s running on port 3306. Well, it was… So after some seconds on the Internet, I found out that Vista could have the strange behaviour to erase the “localhost” setting in the hosts file. So before I changed something on the computer I tried the above code but change “localhost” by the IP-address “127.0.0.1″. And since that was working, I was quite sure that the host-resolving from localhost to 127.0.0.1 wasn’t working properly.

So, where does Vista store the host-resolving? Well Linux-guys will be happy to notice that they are stored in some folder called “etc”. I have no clue if the Unix guys riped it from Windows or vice versa but I do notice the fact that it’s the same folder. But Vista is still Vista so that folder isn’t just under your C-drive!! It’s located at C:\Windows\System32\drivers\etc\.  And there we can find the hosts file… And yes as I opened the file only one line was available: “::0 localhost“. That doesn’t seem to be that much, so to get the localhost resolved again one should add the line “127.0.0.1 localhost” to that file. And since the security is that tight on Vista one can’t just open that file using notepad and edit it… No, you should open notepad as an Administrator and edit the file. Otherwise, Vista will be so kind to tell you, that you don’t have the rights to save the file.

So, a quite long story to tell something not that complicated. But I needed to share this Vista stuff with others. So next time, someone is telling you that something related to localhost isn’t working on Vista! First check if it is working with the IP-address 127.0.0.1! Ifso, you can be quite sure that the problem is solved once you updated the hosts file!

Goodluck!

Jan Windows , ,

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