0

Before all I'm sorry for my bad english and tell me if anything is not understandable.

I have 2 Jtree. Each tree apparently has the same information. The only thing that changes in them are the names of the properties that has each node.

Eg JTree1 has an ID and a ParentID. These properties have as a name and value. Name: ID_Tree1. Value: TESTID1 / / Name: ParentID_Tree1. Value: TESTPID1 In JTree2 has the same values ​​as in the JTree1 but the names are different.

There is a moment in which I transfer a node from JTree1 to JTree2 to create it. The transfer/creation is correct but when I read the nodes, it has a different property name architecture(Jtree1 arch.) and can't be read because need to have the JTree2 architecture. I have the function changeAttributesNamesFromDOORSToTC() to solve the problem because it just change the name to the correct name and understandable for JTree2

The real problem: The function make the change in the node of JTree2 but at the same time it change the values name of the same node in JTree1. It makes reference data instead of assignments I think.

How can I solve this!?

Thanks!

JTree treeDOORSCode; //JTree1
JTree treeTCCode;  //JTree2

Main Code:

//ACTUAL NODE
DefaultMutableTreeNode selectedTreeNode = (DefaultMutableTreeNode) CurrentSelection.getLastPathComponent();
NodeClass actualNode = (NodeClass)selectedTreeNode.getUserObject();


//ACTUAL PARENT NODE
DefaultMutableTreeNode selectedParentTreeNode = (DefaultMutableTreeNode)     selectedTreeNode.getParent();
NodeClass parentNode = (NodeClass) selectedParentTreeNode.getUserObject();
DefaultMutableTreeNode parent = findNode(NodeClass.getNodeParentIdentifierAttrDOORS(parentNode), treeTCCode);


//NEW NODE
DefaultMutableTreeNode newSelectedTreeNode = selectedTreeNode;

//NEW PART
NodeClass newNode = new NodeClass();
        newNode = insertNodeInfo(actualNode);

//Create the Model and insert the node
DefaultTreeModel treeModelTC = (DefaultTreeModel)treeTCCode.getModel();
treeModelTC.insertNodeInto(newSelectedTreeNode, parent, 0);

//NEW PART
newNode .changeAttributesNamesFromDOORSToTC();
newSelectedTreeNode.setUserObject(newNode);

Function which change the attr Name values:

public void changeAttributesNamesFromDOORSToTC(){

    for (int i = 0; i < this.attributes.size(); i++) {
        if (this.attributes.get(i).attributeName.equals(DOORS_ID)){
            if (this.tag.equals(TYPE_NAME_CASE)){
                this.attributes.get(i).attributeName = TC_IDCASE;
            }
            if (this.tag.equals(TYPE_NAME_FOLDER)){
                this.attributes.get(i).attributeName = TC_IDFOLDER;
            }
            if (this.tag.equals(TYPE_NAME_FEATURE)){
                this.attributes.get(i).attributeName = TC_IDFEATURE;
            }
        }
        if (this.attributes.get(i).attributeName.equals(DOORS_PARENTID)){
            this.attributes.get(i).attributeName = TC_PARENTID;
        }
        if (this.attributes.get(i).attributeName.equals(DOORS_SRS)){
            this.attributes.get(i).attributeName = TC_SRS;
        }

    }
}

Attributes Class:

NodeAttributesClass (String attributeName, String attributeValue)
{
    this.attributeName = attributeName;
    this.attributeValue = attributeValue;
}

Let me know if need more info!

chiri4
  • 43
  • 8
  • 1
    for better help sooner post an [SSCCE](http://sscce.org/), short, runnable, compilable, just about a.m. issue, with hardcoded value for TreeModel, – mKorbel Jul 30 '13 at 10:50
  • You may be able to use either approach suggested [here](http://stackoverflow.com/a/11113648/230513). – trashgod Jul 30 '13 at 10:53

1 Answers1

0

Object assignment in java is actually a reference copy.

NodeClass newActualNode = actualNode;

That line doesn't mean "put the values of object actualNode into object newActualNode", because actually the variable actualNode is not an instance of NodeClass, but a reference to an instance of NodeClass. So when you do NodeClass newActualNode = actualNode; you are copying the reference and now both variables effectively point to the same instance.

Then when you change the attribute names in one "the other" also changes, because there is no such other, it's the same place in memory.

Now, what you need to do is to create a new instance of NodeClass with the values you want. There are several ways to do so, it's hard for me to know wich one is more suitable as I don't know the internal structures, but in the end you need to:

  • Create a new NodeClass instance for the newActualNode variable
  • Put the values (fields) of actualNode into the newActualNode
  • Assign the attribute names that you want in newActualNode

So, it could be something like this:

NodeClass newActualNode = new NodeClass(actualNode);  //copy constructor  NodeClass(NodeClass anotherNode);
newActualNode.changeAttributesNamesFromDOORSToTC(); //assuming the constructor doesn't put them right

or you could use a flag in the NodeClass construtors to indicate what kind of attribute names you want.

and the copy constructor should look like this:

public NodeClass(NodeClass anotherNode)
{
    this(anotherNode.someFields); //call to your "normal" constructor, with whatever params you need

    //copy the values into the new instance this, if you didn't do it in the above line
    this.field1 = anotherNode.field1;
    this.field2 = anotherNode.field2;
    //...
    this.fieldn = anotherNode.fieldn;
}

You can take this code with a grain of salt, there are several ways to do it, the differences are subtle if any. The important thing is that you need to have another instance for the other tree.

EDIT

If that doesn't work my guess would be, that either you need to do newSelectedTreeNode.setUserObject(newNode); before the insert, like this:

NodeClass newNode = new NodeClass();
newNode = insertNodeInfo(actualNode);
newNode.changeAttributesNamesFromDOORSToTC();
DefaultMutableTreeNode newSelectedTreeNode = new DefaultMutableTreeNode();
newSelectedTreeNode.setUserObject(newNode);
treeModelTC.insertNodeInto(newSelectedTreeNode, parent, 0);

or that parent is not properly calculated, in particular that you are getting the parent node of the treeDOORSCode, and not the one of the treeTCCode.

DSquare
  • 2,458
  • 17
  • 19
  • Thanks for the answer but it's not correct at all. You understood what I mean and need. I need a way to clone the DefaultMutableTreeNode but I no want that it point to cloner. It's a silly question but I'm starting to become crazy... – chiri4 Jul 30 '13 at 12:16
  • Can you rephrase "I need a way to clone the DefaultMutableTreeNode but I no want that it point to cloner"? – DSquare Jul 30 '13 at 17:55
  • I want to create the same DefaultMutableTreeNode (with same properties and attributes) but if I want to change one property, I don't want that the changes are replicated to the main. Unlink the two DefaultmutableTreeNode objects. Do you understand? – chiri4 Jul 31 '13 at 06:40
  • The problem is what "create the same" means. There are 2 options. 1) You can create a copy(clone) of the node, having 2 instances with the same values initially, so when you modify one the other is not modified. 2) You can have two references to the same instance, one reference from each tree. In the second one (the one you tried to do in your code) it is impossible to "unlink" them, they are not linked, they are the same object, exactly the same, you can't unlink one object from itself, it just has no meaning. If you want to have 2 nodes with potentially diferent values, you need 2 instances. – DSquare Jul 31 '13 at 07:21
  • I create a new different instance of NodeClass, but when I give the values of the "father object" to have the same values it still share the values in both objects, I will become crazy because i don't know a solution to solve this.. – chiri4 Jul 31 '13 at 07:45
  • I'm sorry, i o editted the code. Now u can see. You can see it in the comments NEW PARTS. Still having the problem. – chiri4 Jul 31 '13 at 08:12