Board index » jbuilder » displaying child nodes on demand

displaying child nodes on demand


2003-10-26 04:45:22 AM
jbuilder12
Hi there,
I'm working on a client/server project where the client side app has a
JTree. I am trying to set up a TreeModel so that it retrieves from the
server app the names of the child nodes of a given node when the
treeWillExpand(TreeExpansionEvent tee) method gets triggered. So far I've
accomplished this. In my TreeModel class I have a method called:
ReceiveChildrenFromServer(DefaultTreeNode parent, String ParentsWholePath,
DataInputStream input)
This method does what it is supposed to, however, once it has populated the
parent node, I expected the JTree to display the nodes. This is not
happening. Can someone please explain how I get the JTree to then display
the child nodes.
Thanks,
Alan
 
 

Re:displaying child nodes on demand

Hi there,
Please help me along here. JTree itself doesn't have a
fireTreeStructureChanged() method nor does my class which implements
TreeModel. The only thing I could find in the JavaDocs at
java.sun.com/j2se/1.4.2/docs/api/
was an interface called TreeModelListener. It has a declared method called
treeStructureChanged(TreeModelEvent e). If I am to use this interface
somehow do I implement it in MyTreeModel class or do I subclass JTree and
implement it there? I'm not quite sure how to set this up right.
Incidentally, I don't have JBuilder any more. I'm currently just using Sun
One Studio 4. Hope to purchase JBuilder again someday soon, but money is a
little tight right now. Just using this newsgroup for techical help.
Alan
"Tor Iver Wilhelmsen (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote
"Alan Shiers" < XXXX@XXXXX.COM >writes:

>This method does what it is supposed to, however, once it has
>populated the parent node, I expected the JTree to display the
>nodes. This is not happening. Can someone please explain how I get
>the JTree to then display the child nodes.

You need to tell the view. In the model, call
fireTreeStructureChanged() for the TreePath down to where they were
inserted.

(To get the fire* methods generated for you, use the BeansExpress
events tab and click TreeModel in the list of events to fire.)
 

Re:displaying child nodes on demand

Alan Shiers wrote:
Quote
Hi there,

Actually, the technique I have always used is nodeChanged() method of
DefaultTreeModel. I think it will do everything you need, all you have
to do is pass it the PARENT node where you added all the other nodes. If
you then want to show everything expanded, you can use the normal
methods on JTree for that.
That way you don't have to subclass anything.
--
Tad Frysinger [TeamB]
 

{smallsort}

Re:displaying child nodes on demand

Hi there,
I tried this nodeChanged() method but the root node doesn't display the
child nodes when I expand it. However, I discovered that if I call
nodeStructureChanged(TreeNode) which is a method in DefaultMutableTreeModel,
it in turn calls fireTreeStructureChanged(...) method.
What I have so far only works when initially getting the child nodes for the
root node when I log onto the server. The problem comes when I try to
expand one of the root node's children. I always wind up with the following
error message:
java.lang.ArrayIndexOutOfBoundsException: node has no children
at
javax.swing.tree.DefaultMutableTreeNode.getChildAt(DefaultMutableTreeNode.ja
va:228)
at
javax.swing.tree.DefaultTreeModel.getChild(DefaultTreeModel.java:156)
at
javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(VariableHeig
htLayoutCache.java:1475)
at
javax.swing.tree.VariableHeightLayoutCache$TreeStateNode.expand(VariableHeig
htLayoutCache.java:1270)
....
.... etc.
I don't understand how I can give the expanding node its children before the
model tries to access this getChildAt(...) method :(
I thought I was handling it with the if statement in my getChildCount()
method that checks whether or not a particular node in indeed expanding. If
NodeExpanding is set to true by treeWillExpand( ) method, then the following
if structure should execute, requesting the children's names from the
server. The server in turn sends an OK message back and starts sending the
names which I collect using my ReceiveChildrenFromServer(...) method.
if(ReceivedChildCount && NodeExpanding)
{
NodeExpanding = false; //reset
RequestChildrenFromServer(ParentsWholePath);
//Set up TreeDataTimer so that we know we have a
//response from the server.
timer2 = new TreeDataTimer(this);
timer2.start();
timer2.join();
}
The other, most annoying problem I have, is that whenever I do expand on a
node getChildCount(...) winds up getting called three times. The only thing
I can think of is it is being called this many times because of the three
stages of mouse movements: mouseDown, mouseClick, and mouseUp. I'm thinking
that perhaps each of these are firing an event causing the
getChildCount(...) method to also fire three times. I need somehow to stop
it from doing that and only call the getChildCount(...) method only once.
Please look over by code below and recommend what I need to do to make it
work properly. I'm tacking on both my Response.java and MyTreeModel.java
classes so that you can see both. I'll edit out methods not pertinent to
the problem. Any recommendations you have I will try.
Thanks,
Alan
*************************************************************
public class Response extends Thread implements TreeWillExpandListener,
TreeModelListener
{
clientInterface ref;
JTree jTree1 = null;
DataInputStream input;
DefaultTreeNode rootNode;
String FileSelected = "";
String clientOS = "";
boolean gotSendTreeDataMessage;
long FileSize = 0;
MyTreeModel model = null;
DefaultTreeNode expandingNode = null;
public Response(clientInterface reference)
{
ref = reference;
clientOS = getPlatform();
ConnectDataInputStream();
}
protected void ConnectDataInputStream()
{ //System.out.println("Entering ConnectDataInputStream method.");
try
{
input = new DataInputStream(ref.connection1.getInputStream());
}
catch(IOException ioe)
{
}
}
private int contains(String st, String stringToSearchFor)
{
return st.indexOf(stringToSearchFor);
}
private String getPlatform()
{
String platform = System.getProperty("os.name");
//System.out.println("Platform = " + platform);
if(contains(platform, "Unix")>-1)
{
return String.valueOf(clientInterface.UNIX);
}
else if(contains(platform, "Linux")>-1)
{
return String.valueOf(clientInterface.LINUX);
}
return String.valueOf(clientInterface.WINDOWS);
}
/**
* As this class extends class Thread, it is required we overwrite
* the run() method.
*/
public void run()
{
int connected = 0;
//First let's try to connect to the server.
try
{
//We need to send our username and password
//before the server will return our home directory.
String out = ref.username + ":" + ref.password + ":" + clientOS;
//System.out.println("UserName&Password = " + out);
ref.RequestOut.output.writeUTF(out);
ref.RequestOut.output.flush();
while(true) //Wait for a proper answer from the server.
{
connected = input.readInt();
//System.out.println(connected);
if(connected == ref.ACCEPTED || connected == ref.FAILED ||
connected == ref.PASSWORD_INVALID)
break;
}
if (connected == ref.ACCEPTED)
{
//Get the users' home directory rootPath from the server
ref.rootPath = input.readUTF();
//System.out.println(ref.rootPath);
int OS = input.readInt();
if(OS == ref.WINDOWS)
ref.serverOS = "Windows";
else if(OS == ref.UNIX)
ref.serverOS = "Unix";
else
ref.serverOS = "Linux";
ref.setTitle(ref.getTitle() + " - Connection successful.");
ref.jLabel1.setText("Status: ");
ref.connected = true;
//extract the name of the last directory name
//from ref.rootPath. and populate JTree with one root Node
String root = getNodeName(ref.rootPath);
populateTree(root);
//reset icon and text for connection labels
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
//Enable menus
ref.jMenuItem23.setEnabled(true); //Directories/MkDir
ref.jMenuItem24.setEnabled(true); //Directories/RmDir
ref.jMenuItem25.setEnabled(true); //Upload/Graphic Files
ref.jMenuItem26.setEnabled(true); //Upload/Flash Files
ref.jMenuItem27.setEnabled(true); //Upload/Applet Files
ref.jMenuItem28.setEnabled(true); //Upload/HTML Files
ref.jMenuItem29.setEnabled(true); //Upload/Database Files
ref.jMenuItem30.setEnabled(true); //Upload/Sound Files
ref.jMenuItem31.setEnabled(true); //Upload/Compressed Files
/*This try block will attempt to receive string info from the
server
//in order that it can be used to reconstruct the directory
tree.
try
{
//This could take a while depending on the users connection
speed,
//so let him know he may have to wait a bit.
ref.jLabel1.setText("Status: Please wait while the server
provides a directory tree.");
//run the timer to check after 15 seconds if
gotSendTreeDataMessage
//got set to true or not.
timer.setThreadSuspended(false);
connected = input.readInt();
if(connected == ref.SENDING_TREE_DATA)
{
gotSendTreeDataMessage = true;
//extract the name of the last directory name
//from ref.rootPath.
String root = getNodeName(ref.rootPath);
populateTree(root);
//reset icon and text for connection labels
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
//Enable menus
ref.jMenuItem23.setEnabled(true); //Directories/MkDir
ref.jMenuItem24.setEnabled(true); //Directories/RmDir
ref.jMenuItem25.setEnabled(true); //Upload/Graphic Files
ref.jMenuItem26.setEnabled(true); //Upload/Flash Files
ref.jMenuItem27.setEnabled(true); //Upload/Applet Files
ref.jMenuItem28.setEnabled(true); //Upload/HTML Files
ref.jMenuItem29.setEnabled(true); //Upload/Database Files
ref.jMenuItem30.setEnabled(true); //Upload/Sound Files
ref.jMenuItem31.setEnabled(true); //Upload/Compressed
Files
}
}
catch(NullPointerException npe)
{
//If we come in here it's only because there simply is no
connection
//to the server application.
ref.jLabel1.setText("Status: NullPointerException in
Response.run() method.");
//reset icon and text for connection labels
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
disableMenus(true);
//System.out.println("NullPointerException during
input.readUTF() procedure.");
npe.printStackTrace();
}
*/
}
else if(connected == ref.FAILED)
{
ref.disconnect("Status: Unable to connect to server. Please
ensure your username is correct.");
//reset icon and text for connection labels
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
ref.connected = false;
return;
}
else if(connected == ref.PASSWORD_INVALID)
{
ref.disconnect("Status: Unable to connect to server. Please
ensure your password is correct.");
//reset icon and text for connection labels
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
ref.connected = false;
return;
}
}
catch(NullPointerException npe)
{
//If we come in here it's only because there simply is no
connection
//to the server application.
ref.jLabel1.setText("Status: NullPointerException in Response.run()
method.");
//reset icon and text for connection labels
ref.connected = false;
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
//System.out.println("outer NullPointerException while attempting
to connect.");
input = null; //reset
disableMenus(true);
npe.printStackTrace();
return;
}
catch(IOException e)
{
ref.jLabel1.setText("Status: IOException in Response.run()
method.");
JOptionPane.showMessageDialog(ref, "IOException in Response.run()
method.\n" +
"This is caused either by a loss of
connection\n" +
"to the server, or there is
interference\n" +
"facilitated by other Parental
Internet\n" +
"Filtering software running on your
workstation.\n" +
"If this is the case, disable the
filtering\n" +
"software, then attempt to reconnect
to the server.");
//reset icon and text for connection labels
ref.connected = false;
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
disableMenus(true);
//e.printStackTrace();
//There may still be a connection to the server,
//However, there is sometimes interference due
//to Internet Filtering Software. So we'll send
//a quit message back to the server and allow the user
//to attempt a reconnect.
try{
ref.RequestOut.output.writeInt(ref.QUIT);
ref.RequestOut.output.flush();
}catch(IOException ioe){}
return;
}
catch(Exception ex)
{
ref.jLabel1.setText("Status: Exception in Response.run() method.");
//reset icon and text for connection labels
ref.connected = false;
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
ex.printStackTrace();
disableMenus(true);
return;
}
//Now handle Responses from the server
ref.jLabel1.setText("Status:");
ListenForResponses();
}
private void ListenForResponses()
{
int connected = 0;
while(true)
{
try
{
//System.out.println("Reading next response...");
connected = input.readInt();
//System.out.println("connected: " + connected);
if(connected == ref.DOWNLOAD_FILE)
{
FileSize = input.readLong();
receiveFile();
}
else if(connected == ref.ERROR_SAVING)
{
ref.jLabel1.setText("Status: Error Saving " + ref.FileOpen);
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
ref.isDirty = true;
}
else if(connected == ref.ALLOTTED_WEBSPACE_USED_UP)
{
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
ref.RequestOut.WebSpaceFull = true;
JOptionPane.showMessageDialog(ref, "If you are attempting to
save/upload a new file, or upload multiple files,\n" +
"you do not have enough
allotted web space to accomodate the file(s).\n" +
"If you require more space,
contact your System Administrator.", "Message", JOptionPane.PLAIN_MESSAGE);
ref.jLabel1.setText("Status:");
}
else if(connected == ref.WEBSPACE_NOT_USED_UP)
{ //System.out.println("Got the go ahead.");
ref.RequestOut.proceed = true;
}
else if(connected == ref.SAVE_SUCCESSFUL)
{
//we only get this if the user has used the File/SaveAs menu.
refreshTree();
ref.jLabel1.setText("Status: Save Successful");
ref.isDirty = false;
}
else if(connected == ref.SAVE_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: Save Not Successful");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.UPLOAD_SUCCESSFUL)
{ //System.out.println("Got Upload Successful");
refreshTree();
ref.jLabel1.setText("Status: Data Transfer Successful");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.UPLOAD_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: Data Transfer Not Successful");
JOptionPane.showMessageDialog(ref, "The file(s) uploaded
were\n" +
"unsuccessful in their
completion.",
"Upload Status:",
JOptionPane.INFORMATION_MESSAGE);
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.UPLOAD_UNSUCCESSFUL_SECURITY_EXCEPTION)
{
ref.jLabel1.setText("Status: Data Transfer Not Successful,
Security Exception");
JOptionPane.showMessageDialog(ref, "The file(s) uploaded
were\n" +
"unsuccessful in their
completion.\n" +
"There was a problem
regarding security\n" +
"access. See your System Administrator for help.",
"Upload Status:",
JOptionPane.INFORMATION_MESSAGE);
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.DIRECTORY_CREATION_SUCCESSFUL)
{
refreshTree();
ref.jLabel1.setText("Status: Directory creation successful");
}
else if(connected == ref.DIRECTORY_CREATION_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: Directory creation not
successful");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.DIRECTORY_DELETION_SUCCESSFUL)
{
refreshTree();
ref.jLabel1.setText("Status: Directory Deletion Successful");
}
else if(connected == ref.DIRECTORY_DELETION_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: Directory Deletion Not
Successful");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.FILE_NOT_FOUND_EXCEPTION)
{
ref.jLabel1.setText("Status: FileNotFoundException. Perhaps
the node is really an empty directory?");
ref.isDirty = false;
ref.setTitle("JText Editor - Connection successful.");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.SERVER_RESET)
{
ref.jLabel1.setText("Status: The main server is being
manually reset. You have been disconnected.");
ref.setTitle("JText Editor");
disableMenus(true);
}
else if(connected == ref.SERVER_SHUTTING_DOWN)
{
ref.jLabel1.setText("Status: The main server is being turned
off. You have been disconnected.");
ref.setTitle("JText Editor");
disableMenus(true);
}
else if(connected == ref.SENDING_TREE_DATA)
{
System.out.println("Received message: SendingTreeData");
model.setGotTreeData(true);
model.ReceiveChildrenFromServer(expandingNode,
expandingNode.getWholeServerPath(), input);
//expandingNode = null;//reset
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.SENDING_CHILDCOUNT)
{
System.out.println("Received message: SendingChildCount");
model.setRequestedChildCount(input.readInt());
model.setReceivedChildCount(true);
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.PASSWORD_CHANGE_SUCCESSFUL)
{
ref.jLabel1.setText("Status: Password changed successfully.");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.PASSWORD_CHANGE_NOT_SUCCESSFUL)
{
ref.jLabel1.setText("Status: Password change not successful.
See your administrator for help.");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.FILE_DELETION_SUCCESSFUL)
{
ref.jLabel1.setText("Status: File Deleted Successfully.");
//Now, refresh the tree.
refreshTree();
}
else if(connected == ref.FILE_DELETION_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: File Deletion Not Successful.");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.FILE_RENAME_SUCCESSFUL)
{
ref.jLabel1.setText("Status: File Rename Successful.");
//Now, refresh the tree.
refreshTree();
}
else if(connected == ref.FILE_RENAME_UNSUCCESSFUL)
{
ref.jLabel1.setText("Status: File Rename Not Successful.");
ref.imageLabel.setIcon(ref.blank);
ref.textLabel.setText("");
}
else if(connected == ref.FILE_CONTENTS)
{
//System.out.println("Received queue from server for file
contents.");
FileSize = input.readLong();
displayTextFile();
ref.isDirty = false;
//System.out.println("Finished receiving file");
}
}
catch(EOFException eof){break;}
catch(NullPointerException npex)
{
//If we come in here it's only because there simply is no
connection
//to the server application.
ref.jLabel1.setText("NullPointerException in run() method of
communicationPort.");
npex.printStackTrace();
ref.imageLabel.setIcon(ref.blank);
//System.out.println("NullPointerException during input.readUTF()
procedure of communicationPort.");
disableMenus(true);
break;
}
catch(IOException e)
{
ref.jLabel1.setText("Status: Client disconnected.");
ref.imageLabel.setIcon(ref.blank);
disableMenus(true);
//e.printStackTrace();
break;
}
catch(Exception exc)
{
ref.jLabel1.setText("Status: Client disconnected.");
ref.imageLabel.setIcon(ref.blank);
disableMenus(true);
//exc.printStackTrace();
break;
}
}
}
/**
* This function creates an instance of a JTree object and populates
* it with the data sent by Directory Administrator.
*/
protected void populateTree(String NodeName)
{
//System.out.println("Entering populatTree() method");
//This could take a while, so change the cursor icon
//ref.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
try
{
String separator = "";
if(ref.serverOS.equals("Windows"))
separator = "\\";
else
separator = "/";
rootNode = new DefaultTreeNode(NodeName, true, separator);
String rootPath = getLeadingPath(ref.rootPath);
rootNode.setLeadingPath(rootPath);
//insert child nodes
model = new MyTreeModel(rootNode);
model.setOutputStream(ref.RequestOut.output);
model.setResponseReference(this);
model.addTreeModelListener(this);
//model.setInputStream(input);
//model.ReceiveChildrenFromServer(rootNode,
rootNode.getWholeServerPath());
jTree1 = new JTree(model);
jTree1.setRootVisible(true);
jTree1.setShowsRootHandles(true);
jTree1.collapseRow(0);
MouseListener popupListener = new PopupListener(ref);
jTree1.addMouseListener(popupListener);
//Listen to our own expand/collapse events to keep the labels in
sync
jTree1.addTreeWillExpandListener(this);
}
catch(Exception e){System.out.println("Exception in populateTree()
method"); e.printStackTrace();return;}
jTree1.addTreeSelectionListener(new TreeSelectionListener()
{
public void valueChanged(TreeSelectionEvent tse)
{
System.out.println("valueChanged");
DefaultTreeNode temp;
if(ref.connected)
{
try
{
TreePath tp = tse.getNewLeadSelectionPath();
/**
* SelectedFilePath IS VERY IMPORTANT!
* It will contain the complete path to the file
* clicked on by the user's mouse.
* We can then use it to actually open the file on the server
side.
*/
ref.SelectedFilePath = ref.getWholePath(tp.toString());
//SelectedTreePath = tp.toString();
//System.out.println(ref.FileOpenPath);
temp = (DefaultTreeNode)tp.getLastPathComponent();
String st = tp.getLastPathComponent().toString();
ref.SelectedFile = st;
FileSelected = st;
ref.jLabel1.setText("Selected: " + st);
//We need to toggle the File/Open, Save, and SaveAs menus from
enabled to disabled
//depending on what the user is selecting. We don't want
//the user trying to open a directory. They should only
//be allowed to open and save leaf objects (files).
if(temp.isLeaf())
{
//System.out.println("This node is a leaf.");
//Enable menus
ref.jMenuItem1.setEnabled(true); //File/New
ref.jMenuItem2.setEnabled(true); //File/New with HTML Tags
ref.jMenuItem3.setEnabled(true); //File/Open
ref.popopenFile.setEnabled(true); //File/Open
if(ref.FileOpen.equals("")) //nothing has been opened
{
ref.jMenuItem4.setEnabled(false); //File/Save
ref.Save.setEnabled(false); //File/Save button
ref.jMenuItem5.setEnabled(false); //File/SaveAs
ref.popsaveFile.setEnabled(false);
ref.popsaveFileAs.setEnabled(false);
}
else if(ref.FileOpen.equals(ref.SelectedFile)) //the
selected file and the open file are the same
{
ref.jMenuItem4.setEnabled(true); //File/Save
ref.Save.setEnabled(true); //File/Save Button
ref.jMenuItem5.setEnabled(true); //File/SaveAs
ref.popsaveFile.setEnabled(true);
ref.popsaveFileAs.setEnabled(true);
}
else //the selected file and the open file are not the same
{
ref.jMenuItem4.setEnabled(false); //File/Save
ref.Save.setEnabled(false); //File/Save Button
ref.jMenuItem5.setEnabled(false); //File/SaveAs
ref.popsaveFile.setEnabled(false);
ref.popsaveFileAs.setEnabled(false);
}
ref.jMenuItem6.setEnabled(true); //File/Delete
ref.jMenuItem7.setEnabled(true); //File/Rename
ref.jMenuItem8.setEnabled(true); //File/Download
ref.jMenuItem9.setEnabled(true); //File/Print
//enable popup menus
ref.popdeleteFile.setEnabled(true); //File/Delete
ref.poprenameFile.setEnabled(true); //File/Rename
ref.popdownloadFile.setEnabled(true); //File/Download
//Enable buttons
ref.New.setEnabled(true);
ref.Open.setEnabled(true);
ref.Print.setEnabled(true);
ref.Tags.setEnabled(true);
}
else
{
//System.out.println("This node is a not a leaf.");
//Disable menus
ref.jMenuItem1.setEnabled(false); //File/New
ref.jMenuItem2.setEnabled(false); //File/New with HTML
ref.jMenuItem3.setEnabled(false); //File/Open
ref.jMenuItem4.setEnabled(false); //File/Save
ref.jMenuItem5.setEnabled(false); //File/SaveAs
ref.jMenuItem6.setEnabled(false); //File/Delete
ref.jMenuItem7.setEnabled(false); //File/Rename
ref.jMenuItem8.setEnabled(false); //File/Download
ref.jMenuItem9.setEnabled(false); //File/Print
//Disable popup menus
ref.popopenFile.setEnabled(false); //File/Open
ref.popsaveFile.setEnabled(false); //File/Save
ref.popsaveFileAs.setEnabled(false); //File/SaveAs
ref.popdeleteFile.setEnabled(false); //File/Delete
ref.poprenameFile.setEnabled(false); //File/Rename
ref.popdownloadFile.setEnabled(false); //File/Download
//Disable buttons
ref.New.setEnabled(false);
ref.Open.setEnabled(false);
ref.Save.setEnabled(false);
ref.Print.setEnabled(false);
ref.Tags.setEnabled(false);
}
}
catch(NullPointerException npe){}
}
}
});
ref.jScrollPane1.getViewport().add(jTree1, null);
//ref.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
public void treeWillCollapse(javax.swing.event.TreeExpansionEvent tee)
throws javax.swing.tree.ExpandVetoException {
}
public void treeWillExpand(javax.swing.event.TreeExpansionEvent tee)
throws javax.swing.tree.ExpandVetoException
{
System.out.println("Node Expanding.");
model.setNodeExpanding(true);
model.setParentNodesPath(tee.getPath());
expandingNode = (DefaultTreeNode)tee.getPath().getLastPathComponent();
//model.getChildCount(expandingNode);
}
public void treeNodesChanged(javax.swing.event.TreeModelEvent
treeModelEvent) {
}
public void treeNodesInserted(javax.swing.event.TreeModelEvent
treeModelEvent) {
}
public void treeNodesRemoved(javax.swing.event.TreeModelEvent
treeModelEvent) {
}
public void treeStructureChanged(javax.swing.event.TreeModelEvent
treeModelEvent)
{
System.out.println("treeStructureChanged");
}
}
**************************************************************
public class MyTreeModel extends DefaultTreeModel //implements TreeModel
{
private DataOutputStream output;
private Response ref;
private int RequestedChildCount;
private boolean ReceivedChildCount;
private boolean GotTreeData;
private boolean NodeExpanding;
private ChildCountTimer timer1 = null;
private TreeDataTimer timer2 = null;
private TreePath ParentNodesPath;
public MyTreeModel(TreeNode node)
{
super(node);
}
public MyTreeModel(TreeNode node, boolean asksAllowsChildren)
{
super(node, asksAllowsChildren);
}
public void setResponseReference(Response reference)
{
ref = reference;
}
public void setOutputStream(DataOutputStream out)
{
output = out;
}
public void insertNodeInto(DefaultTreeNode child, DefaultTreeNode
parent, int param)
{
parent.insert(child, param);
}
public int getChildCount(Object node)
{
//System.out.println("Entering GetChildCount(Object)");
int count = 0;
if(node instanceof DefaultTreeNode)
{
count =
this.getChildCount(((DefaultTreeNode)node).getWholeServerPath());
}
return count;
}
public int getChildCount(String ParentsWholePath)
{
System.out.println("Entering GetChildCount(String)\nParentsWholePath:
" + ParentsWholePath);
DefaultTreeNode node = null;
String name = "";
try
{
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
output.writeInt(ref.ref.PATH);
output.flush();
output.writeUTF(ParentsWholePath);
output.flush();
output.writeInt(ref.ref.REQUEST_CHILD_COUNT);
output.flush();
System.out.println("waiting for response...");
timer1 = new ChildCountTimer(this);
timer1.start();
timer1.join();
System.out.println("ReceivedChildCount: " + ReceivedChildCount +
"\nRequestedChildCount: " + RequestedChildCount);
if(ReceivedChildCount && NodeExpanding)
{
NodeExpanding = false; //reset
RequestChildrenFromServer(ParentsWholePath);
//Set up TreeDataTimer so that we know we have a
//response from the server.
timer2 = new TreeDataTimer(this);
timer2.start();
timer2.join();
}
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
catch(InterruptedException ie){}
catch(IOException ioe){ioe.printStackTrace();}
catch(NullPointerException npe){npe.printStackTrace();}
return RequestedChildCount;
}
protected void RequestChildrenFromServer(String ParentsWholePath)
{
System.out.println("Entering RequestChildrenFromServer()");
System.out.println("ParentsWholePath: " + ParentsWholePath);
//Request Children from server
try
{
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
output.writeInt(ref.ref.PATH);
output.flush();
output.writeUTF(ParentsWholePath);
output.flush();
//Sending REQUEST_CHILDREN should cause a response
//from the server: SENDING_TREE_DATA
output.writeInt(ref.ref.REQUEST_CHILDREN);
output.flush();
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
catch(IOException ioe){ioe.printStackTrace();}
System.out.println("Finished calling server for children");
}
protected void ReceiveChildrenFromServer(DefaultTreeNode parent, String
ParentsWholePath, DataInputStream input)
{
if(parent.getHasChildren())return;
if(RequestedChildCount == 0) return;
System.out.println("Entering ReceiveChildrenFromServer()");
DefaultTreeNode child = null;
String name = "";
try
{
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
System.out.println("Getting children from server...");
int nodeType = 0;//default
//iterate through each of the children as they come in
//from the server.
for(int i = 0; i < RequestedChildCount; i++)
{
name = input.readUTF(); //get the name
//System.out.println("child node: " + name);
child = new DefaultTreeNode(name); //create a new child node
//determine if this node is a directory
//or a file. Empty directories are denoted
//as having (nodeType == -1)
nodeType = input.readInt();
//System.out.println("node type: " + nodeType);
if(nodeType == 0)
{
//Since a file is a leaf node
//we won't be needing to continue
//reading in nodes for it.
child.setAllowsChildren(false);
child.setHasChildren(false);
child.setLeadingPath(ParentsWholePath);
if(ref.ref.serverOS.equals("Windows"))
child.setSeparator("\\");
else
child.setSeparator("/");
}
else if(nodeType == -1)
{
child.setAllowsChildren(true); //It's an empty directory
child.setHasChildren(false);
child.setLeadingPath(ParentsWholePath);
if(ref.ref.serverOS.equals("Windows"))
child.setSeparator("\\");
else
child.setSeparator("/");
}
else
{
child.setAllowsChildren(true); //It's a directory
child.setHasChildren(true);
child.setLeadingPath(ParentsWholePath);
if(ref.ref.serverOS.equals("Windows"))
child.setSeparator("\\");
else
child.setSeparator("/");
}
insertNodeInto(child, parent, i);
}
this.nodeStructureChanged(parent);
ref.ref.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR));
}
catch(IOException ioe){ioe.printStackTrace();}
catch(NullPointerException npe){npe.printStackTrace();}
}
protected void setNodeExpanding(boolean value)
{
NodeExpanding = value;
}
protected void setReceivedChildCount(boolean value)
{
ReceivedChildCount = value;
}
protected boolean getReceivedChildCount()
{
return ReceivedChildCount;
}
protected void setGotTreeData(boolean value)
{
GotTreeData = value;
}
protected boolean getGotTreeData()
{
return GotTreeData;
}
protected void setRequestedChildCount(int value)
{
RequestedChildCount = value;
}
protected void setParentNodesPath(TreePath path)
{
ParentNodesPath = path;
}
/*
public Object getChild(Object node, int index)
{
try
{
if(node instanceof DefaultTreeNode)
{
return ((DefaultTreeNode)node).getChildAt(index);
}
}
catch(ArrayIndexOutOfBoundsException
aie){System.out.println("ArrayIndexOutOfBounds");return null;}
return null;
}
public boolean isLeaf(Object node)
{
return ((DefaultTreeNode)node).isLeaf();
}
public void valueForPathChanged(javax.swing.tree.TreePath treePath,
Object obj) {
}
public int getIndexOfChild(Object parent, Object child)
{
if(parent instanceof DefaultTreeNode && child instanceof
DefaultTreeNode)
{
return
((DefaultTreeNode)parent).getIndex((DefaultTreeNode)child);
}
return -1;
}
public void addTreeModelListener(TreeModelListener treeModelListener)
{
if(listeners == null)
{
listeners = new Vector();
}
listeners.addElement(treeModelListener);
}
public void removeTreeModelListener(TreeModelListener treeModelListener)
{
if(listeners != null)
{
listeners.removeElement(treeModelListener);
}
}
*/
}