Search code examples
javaswingsocketsjtree

jTreeModel does not expand after sending over a socket connection


I have written a client and server application who are connected with sockets. The server is multithreaded and can hold more than one client.

Now am i using in my client application a jTree model, and on my server i have a folder with fictitious project folders and files in it. First this application only was client sided with a database on a server, now i want it to fully operate over a socket connection.

The only problem is that the jTree model is created on the server. So when the program starts the client asks the server to create a jTree model of the project folder the user will use in the client application with its files in it.

The Server will send a respone with a jTree model in it and the client puts this model in the jTree swing component. The jTree is succesfully shown to the user (so the Tree model is succesfully sended over the socket connection), but when the user wants to expand the first projectfolder map: the jTree shows for example: 'testProject' as folder. And for example the folder testProject contains some files:

  • testProject.exe
  • bin
    • test.java
    • test2.java
  • src

As you see in the above example the jTree should show the above tree to the user when the user double clicks on 'testProject' in the jTree.

Now my problem

I think the problem is that the server only creates the model of the first folder. Like it only sends a model with 'projectFolder' in it. But i want the above structure send to the user so the user can use the jTree to download the files from the server. (something i will implement later).

How can i get a full model from the server, that the jTree can show a full model to the user with the projectfolder and all his files.

To make everything a little more clear:

My code

This fragment of code i use on my client to invoke the method that created the jTree model on the server.

public ProjectTreeModel getTreeModel(String projectName) throws ClassNotFoundException {
    try {
        //Creating empty object to invoke te mehod on the server.
        paramObject parameters = new paramObject();
        parameters.string1 = projectName;

        //Creating package to invoke the right method on the server.
        Packet data = new Packet("treemodel.GetModel",parameters);

        //sends in this case an empty object to the server and invokes the treemodel.GetModel method on the server.
        oos.writeObject(data);
        oos.flush();

        ProjectTreeModel model = (ProjectTreeModel) ois.readObject();

        return model;

    } catch (IOException ex) {
        ex.printStackTrace();
    }

    return null;
}

This piece of code i use on the server to create the projectModel and send it back as object to the client to return it as a jTree model in the previous code example.

if(data.getMethod().equals("treemodel.GetModel")) 
         { 
             //in this example i don't use this object, just ignore it.
             paramObject parameters = (paramObject) data.getObject();


             //Extract the information from User
             String projectName = parameters.string1;

             ProjectTreeModel treemodel = new ProjectTreeModel();
             treemodel.newRootPath(projectName);



             oos.writeObject(treemodel);
             output.flush();
             form.sendOutput("ProjectTreeModel succesfully sended to: " + Email);
         }

And at last this is how i create the TreeModel.

import java.io.File;
import java.io.Serializable;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;


public class ProjectTreeModel implements TreeModel, Serializable
{
File f;
private static String rootPath = "src/authorsystem/Projects";

public static void newRootPath(String newRoot)
{
    String desktopPath = System.getProperty("user.home") + "\\Desktop";
    desktopPath.replace("\\", "/");
    rootPath = desktopPath + "\\projectfolders";
}

@Override
public Object getRoot() {
    //userName = LogInForm.ingelogt;

    f = new File(rootPath);
    return f;
}

@Override
public Object getChild(Object parent, int index) {
    File pathName = (File) parent;
    String[] fileNames = pathName.list();
    return new File(pathName.getPath(), fileNames[index]);
}

@Override
public int getChildCount(Object parent) {
    File pathName = (File) parent;
    if(pathName.list() != null)
    {
        String[] fileNames = pathName.list();
        return fileNames.length;
    }
    else
    {
        return 0;
    }
}

@Override
public boolean isLeaf(Object node) {
    return ((File)node).isFile();
}

@Override
public void valueForPathChanged(TreePath path, Object newValue) {

}

@Override
public int getIndexOfChild(Object parent, Object child) {
    File pathName = (File) parent;
    File childName = (File) child;
    if(pathName.isDirectory())
    {
        String[] fileNames = pathName.list();
        for (int i = 0; i < fileNames.length; i++) {
            if(pathName.compareTo(childName) == 0)
            {
                return i;
            }
        }
    }
    return -1;
}

@Override
public void addTreeModelListener(TreeModelListener l) {

}

@Override
public void removeTreeModelListener(TreeModelListener l) {

}

}

I hope somebody can help me out, or just help me with ideas how to solve this problem.


Solution

  • Ok just to close this topic in case if someone find this. Its really long ago i worked on this but this is how we managed it:

    For the treeModel we made our custom nodes. Instead of sending a JTreeModel object trough a socket connection we only send the data that must be shown in the tree. After the client received the data from the server we iterated trough all the data and checked if the node is a parent or child. This can be done basically with the overrided TreeModel class code snipper above in my Question.