Identical Item in Cut/Copy/Paste

J

Jason Cavett

I am having a problem with cut/copy/paste of items in a JTree. At
first glance, it appears that cut/copy/paste is working, but after
some inspection, it has a (serious) flaw. Here's a step-by-step
description of the problem.

1. Select item in the tree and one of its children at the same time.
2. Copy.
3. Select the top level tree item and select paste.
4. The top-level item is copied into the tree and so is the child
node. So, it looks something like this...

A // selected this
- B // and this; did copy and pasted at top level

Result:
A1
- B1
A2
- B2
B3

The problem is, when I open up the view to B2, B3 is also marked as
being opened and any changes to "B2" is made to "B3" as well (although
not to B1). Basically, it seems as those they are the same exact
object (and I think they are). Here is the code I am using for
exporting the data - I'm pretty sure my problem lies here.

protected DataModel[] exportDataModel(JComponent c) {
ProjectTree tree = (ProjectTree) c;
TreePath[] paths = tree.getSelectionPaths();
Collection<DataModel> data = new ArrayList<DataModel>();

for (int i = 0; paths != null && i < paths.length; i++) {
data.add((DataModel) paths.getLastPathComponent());
}

// cast correctly and return the array
DataModel[] dataModel = new DataModel[data.size()];
dataModel = data.toArray(dataModel);

return dataModel;
}

I'm guessing this has to do with the fact that Java passes a copy of
the reference (resulting in me pointing to the same DataModel object),
but I'm not positive. But, I can't figure out a way around it. This
problem is making me crazy. Can anybody make any suggestions?

Thank you.
 
B

Brandon McCombs

Jason said:
I am having a problem with cut/copy/paste of items in a JTree. At
first glance, it appears that cut/copy/paste is working, but after
some inspection, it has a (serious) flaw. Here's a step-by-step
description of the problem.

1. Select item in the tree and one of its children at the same time.
2. Copy.
3. Select the top level tree item and select paste.
4. The top-level item is copied into the tree and so is the child
node. So, it looks something like this...

A // selected this
- B // and this; did copy and pasted at top level

Result:
A1
- B1
A2
- B2
B3

The problem is, when I open up the view to B2, B3 is also marked as
being opened and any changes to "B2" is made to "B3" as well (although
not to B1). Basically, it seems as those they are the same exact
object (and I think they are). Here is the code I am using for
exporting the data - I'm pretty sure my problem lies here.

protected DataModel[] exportDataModel(JComponent c) {
ProjectTree tree = (ProjectTree) c;
TreePath[] paths = tree.getSelectionPaths();
Collection<DataModel> data = new ArrayList<DataModel>();

for (int i = 0; paths != null && i < paths.length; i++) {

No need to test if paths != null every time. Test if path != null before
the loop and if it is then just return null and skip everything else.
data.add((DataModel) paths.getLastPathComponent());


I'm having trouble getting cut/paste to work in my own JTree and I
haven't gotten as far as you have so I could be wrong in my advice here
but I would think that there isn't any reason to get the last node of a
path in order to move them to another tree location. Just take the node
the user cut from the tree and all children will move with it to the
pasted location I believe. Of course that only changes what the JTree
displays so you still have to update your actual data to show that all
child nodes changed location.

It also seems your tree nodes are called DataModel which is a little odd
to me. They should be some sort of node (like the
DefaultMutableTreeNode) and the whole tree model itself should be called
the DataModel but that has nothing to do with this problem except for
making your code a bit confusing upon first glance.
}

// cast correctly and return the array
DataModel[] dataModel = new DataModel[data.size()];
dataModel = data.toArray(dataModel);

return dataModel;
}

I'm guessing this has to do with the fact that Java passes a copy of
the reference (resulting in me pointing to the same DataModel object),

Well you are right but I don't see anywhere in this code where B3 could
be affected as long as it wasn't one of the selected paths. To be sure I
would change the following line (mentioned above):

data.add((DataModel) paths.getLastPathComponent());

to

data.add( new DataModel(paths.getLastPathComponent()));

to give it a new reference to be stored in 'data' but like I said above,
I don't think you need to get the last component of the path, just the
top-level node.
but I'm not positive. But, I can't figure out a way around it. This
problem is making me crazy. Can anybody make any suggestions?

Thank you.

Take all this with a grain of salt unless someone else chimes in in
agreement.
 
J

Jason Cavett

Jason said:
I am having a problem with cut/copy/paste of items in a JTree. At
first glance, it appears that cut/copy/paste is working, but after
some inspection, it has a (serious) flaw. Here's a step-by-step
description of the problem.
1. Select item in the tree and one of its children at the same time.
2. Copy.
3. Select the top level tree item and select paste.
4. The top-level item is copied into the tree and so is the child
node. So, it looks something like this...
A // selected this
- B // and this; did copy and pasted at top level
Result:
A1
- B1
A2
- B2
B3
The problem is, when I open up the view to B2, B3 is also marked as
being opened and any changes to "B2" is made to "B3" as well (although
not to B1). Basically, it seems as those they are the same exact
object (and I think they are). Here is the code I am using for
exporting the data - I'm pretty sure my problem lies here.
protected DataModel[] exportDataModel(JComponent c) {
ProjectTree tree = (ProjectTree) c;
TreePath[] paths = tree.getSelectionPaths();
Collection<DataModel> data = new ArrayList<DataModel>();
for (int i = 0; paths != null && i < paths.length; i++) {

No need to test if paths != null every time. Test if path != null before
the loop and if it is then just return null and skip everything else.
data.add((DataModel) paths.getLastPathComponent());


I'm having trouble getting cut/paste to work in my own JTree and I
haven't gotten as far as you have so I could be wrong in my advice here
but I would think that there isn't any reason to get the last node of a
path in order to move them to another tree location. Just take the node
the user cut from the tree and all children will move with it to the
pasted location I believe. Of course that only changes what the JTree
displays so you still have to update your actual data to show that all
child nodes changed location.

It also seems your tree nodes are called DataModel which is a little odd
to me. They should be some sort of node (like the
DefaultMutableTreeNode) and the whole tree model itself should be called
the DataModel but that has nothing to do with this problem except for
making your code a bit confusing upon first glance.
// cast correctly and return the array
DataModel[] dataModel = new DataModel[data.size()];
dataModel = data.toArray(dataModel);
return dataModel;
}
I'm guessing this has to do with the fact that Java passes a copy of
the reference (resulting in me pointing to the same DataModel object),

Well you are right but I don't see anywhere in this code where B3 could
be affected as long as it wasn't one of the selected paths. To be sure I
would change the following line (mentioned above):

data.add((DataModel) paths.getLastPathComponent());

to

data.add( new DataModel(paths.getLastPathComponent()));

to give it a new reference to be stored in 'data' but like I said above,
I don't think you need to get the last component of the path, just the
top-level node.
but I'm not positive. But, I can't figure out a way around it. This
problem is making me crazy. Can anybody make any suggestions?
Thank you.

Take all this with a grain of salt unless someone else chimes in in
agreement.- Hide quoted text -

- Show quoted text -


DataModel implements MutableTreeNode
The tree is called ProjectTree.

And, after doing some research, I discovered that I need to make an
actual copy of the DataModel (deep copy) through serialization in
order to ensure that the new DataModel has its own references instead
of holding onto current references.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top