Running your example I saw that the algorithm in “drop” does not work correct. I could even see how ids were duplicated. I’ve corrected the algorithm a bit, but that won’t help in the long run.
After inserting the first two nodes this layout is created:
Root [id=1294246467149]
Node[ HORIZONTAL , id=1294233071307, placeholders={}]
Leaf[ green, placeholders={}, id=1294230065870 ]
Leaf[ red, placeholders={}, id=1294233012424 ]
Now when the third Dockable is inserted, the id of the real node and of the second node in the path of “blue” match. The result is this layout:
Node[ VERTICAL , id=1294232966380, placeholders={}]
Node[ HORIZONTAL , id=1294233071307, placeholders={}]
Leaf[ green, placeholders={}, id=1294230065870 ]
Leaf[ red, placeholders={}, id=1294233012424 ]
Leaf[ blue, placeholders={}, id=1294232804505 ]
This may not be what you expect, and the reason for this is that “1294233071307” is a horizontal node, while in the path of “blue” the node (implicitly) is vertical. The algorithm resolves this problem by declaring the path invalid starting at “1294233071307”.
Now it would certainly be possible to modify the algorithm such that this issue would resolve correctly, but I can guarantee you with 99.9% certainty that all the repairing in the world could not end in an algorithm that solves all combinations of paths.
I have a strong argument supporting my claim: imagine a layout with many more Dockables (let’s say 100). Because of bad luck two Dockables with long but different paths are loaded first. The algorithm will put them in the same node, because there is nothing else to do. Now you have two paths, two ids, but can only store one id in the real node. The other id is lost for future runs of the algorithm. This means every time you insert a Dockable some information is lost, until there is a time when ambiguities arise and any algorithm fails.
Ordering the Dockables when inserting may help, but in the end it would be just a workaround of the bigger issue. And finding the correct order might not be easy either.
In short: your initial solution will most certainly never work properly.
I still strongly advise to use the classes “DockSituation” or “PredefinedDockSituation” to store the layout *. Both are quite easy to use and both can create XML as well. They have been used in many applications and are quite reliable today.
You just create a “DockSituation”, add factories (and/or in the case of PredefinedDocksituation: call “put(String,DockElement)” to assign unique identifiers to Dockables/DockStations), and then use “writeXML” to write the layout. If you wish I can write you an example.
- or “DockFrontend” which offers some other nice methods and hides at least some complexity.