List<Node> children = null;
switch (aggregationPosition) {
case LEFT:
case TOP:
children = Arrays.asList(secondLeaf,
new Divider(),
firstLeaf);
break;
case RIGHT:
case BOTTOM:
children = Arrays.asList(firstLeaf,
new Divider(),
secondLeaf);
break;
}
boolean rowLayout = (aggregationPosition == AggregationPosition.LEFT || aggregationPosition == AggregationPosition.RIGHT);
Split split = new Split();
split.setRowLayout(rowLayout);
split.setBounds(split.getBounds());
split.setChildren(children);
// update the model
multiSplitPaneModelRoot = split;
if (!multiSplitPane.getMultiSplitLayout().getFloatingDividers())
multiSplitPane.getMultiSplitLayout().setFloatingDividers(true);
validateModel(multiSplitPaneModelRoot);
multiSplitPane.setModel(multiSplitPaneModelRoot);
// update the components in the container
Component wrapper;
if (rootLeaf.getDockables().size() > 1) {
wrapper = multiSplitPane.getComponent(0);
} else {
Dockable delegator = entries.values().iterator().next().dockable;
wrapper = getWrapperForComponent(delegator, getComponentFromWrapper(multiSplitPane.getComponent(0)), Action.ADD_DOCK);
}
multiSplitPane.removeAll();
multiSplitPane.add(wrapper, firstLeaf.getName());
multiSplitPane.add(getWrapperForComponent(dockable, content, Action.ADD_DOCK), secondLeaf.getName());
}
} else {
// Take the root, it's a split
Split splitRoot = (Split) multiSplitPaneModelRoot;
// Build content to add
boolean addCmp = true;
String leafName = null;
// Modify model
if (aggregationOnDockable != null) {
// Search for aggregationOnDockable leaf
Stack<Split> stack = new Stack<Split>();
stack.push(splitRoot);
while (!stack.isEmpty()) {
Split split = stack.pop();
for (Node child : split.getChildren()) {
if (child instanceof DockableLeaf) {
DockableLeaf leaf = (DockableLeaf) child;
if (leaf.getDockables().contains(aggregationOnDockable.getId())) {
if (invalidAggregationPosition) {
// The requeste is to add more than one dockable on the same leaf...
addToWrapper(multiSplitPane.getMultiSplitLayout().getChildMap().get(leaf.getName()),
dockable,
aggregationIndexLocation,
content);
leaf.addDockable(dockable.getId());
addCmp = false;
resetB = false;
} else {
leafName = getNextLeanName();
boolean step1Failed = false;
// Check for concordance to leaf.getParent().isRowLayout and aggregationPosition
Split parent = leaf.getParent();
boolean rowLayout = parent.isRowLayout();
List<Node> parentChildren = parent.getChildren();
int startIndex = parentChildren.indexOf(leaf);
if (rowLayout) {
boolean finalize = false;
switch (aggregationPosition) {
case LEFT:
parentChildren.add(startIndex, new DockableLeaf(leafName, dockable.getId()));
parentChildren.add(startIndex + 1, new Divider());
finalize = true;
break;
case RIGHT:
parentChildren.add(startIndex + 1, new Divider());
parentChildren.add(startIndex + 2, new DockableLeaf(leafName, dockable.getId()));
finalize = true;
break;
default:
step1Failed = true;
}
if (finalize) {
// Set new children
forceWeight(parentChildren);
parent.setChildren(parentChildren);
}
} else {
boolean finalize = false;
switch (aggregationPosition) {
case TOP:
parentChildren.add(startIndex, new DockableLeaf(leafName, dockable.getId()));
parentChildren.add(startIndex + 1, new Divider());
finalize = true;
break;
case BOTTOM:
parentChildren.add(startIndex + 1, new Divider());
parentChildren.add(startIndex + 2, new DockableLeaf(leafName, dockable.getId()));
finalize = true;
break;
default:
step1Failed = true;
}
if (finalize) {
// Set new children
forceWeight(parentChildren);
parent.setChildren(parentChildren);
}
}
if (step1Failed) {
// Create two leafs
Leaf newleaf = new DockableLeaf(leafName, dockable.getId());
newleaf.setWeight(0.5);
// Creat the split
Split newSplit = new Split();
newSplit.setBounds(leaf.getBounds());
newSplit.setRowLayout((aggregationPosition == AggregationPosition.LEFT || aggregationPosition == AggregationPosition.RIGHT));
newSplit.setWeight(leaf.getWeight());
leaf.getParent().removeNode(leaf);
switch (aggregationPosition) {
case LEFT:
case TOP:
newSplit.setChildren(Arrays.asList(newleaf,
new Divider(),
leaf));
break;
default:
newSplit.setChildren(Arrays.asList(leaf,
new Divider(),
newleaf));
break;
}
leaf.setWeight(0.5);
// Switch the leaf with the new split
parentChildren.set(startIndex, newSplit);
parent.setChildren(parentChildren);
}
}
stack.clear();
break;
}
} else if (child instanceof Split) {
stack.push((Split) child);
}
}
}
if (!multiSplitPane.getMultiSplitLayout().getFloatingDividers())
multiSplitPane.getMultiSplitLayout().setFloatingDividers(true);
} else {
leafName = getNextLeanName();
boolean rowLayout = (aggregationPosition == AggregationPosition.LEFT || aggregationPosition == AggregationPosition.RIGHT);
if (splitRoot.isRowLayout() == rowLayout) {
List<Node> children = splitRoot.getChildren();
switch (aggregationPosition) {
case LEFT:
case TOP:
children.add(0, new DockableLeaf(leafName, dockable.getId()));
children.add(1, new Divider());
break;
case RIGHT:
case BOTTOM:
children.add(new Divider());
children.add(new DockableLeaf(leafName, dockable.getId()));
break;
}
forceWeight(children);
splitRoot.setChildren(children);
} else {
Split newRoot = new Split();
newRoot.setRowLayout(rowLayout);
newRoot.setBounds(multiSplitPaneModelRoot.getBounds());
Leaf leaf = new DockableLeaf(leafName, dockable.getId());
leaf.setWeight(0.5);
multiSplitPaneModelRoot.setWeight(0.5);
List<Node> children = null;
switch (aggregationPosition) {
case LEFT:
case TOP:
children = Arrays.asList(leaf,
new Divider(),
multiSplitPaneModelRoot);
break;
case RIGHT:
case BOTTOM:
children = Arrays.asList(multiSplitPaneModelRoot,
new Divider(),
leaf);
break;
}
forceWeight(children);
newRoot.setChildren(children);