Simplified the logic in AbstractOrderedLayout/CssLayout#addComponent*() - super.addComponent() is now called first so that the invariant that there's never duplicate elements in the component list always holds, this fixes #7668. addComponent(c, index) now has a piece of additional logic handling the case when a component is moved to an index *after* its current one.
svn changeset:22117/svn branch:6.7
*/
@Override
public void addComponent(Component c) {
+ super.addComponent(c);
components.add(c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
- }
+ requestRepaint();
}
/**
* the component to be added.
*/
public void addComponentAsFirst(Component c) {
+ super.addComponent(c);
components.addFirst(c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
- }
+ requestRepaint();
}
/**
* in and after the position are shifted forwards.
*/
public void addComponent(Component c, int index) {
- components.add(index, c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
+ // if this already contains c, super.addComponent() will remove it
+ // so everything after c's original position shifts one place down
+ if(this == c.getParent() && index > getComponentIndex(c)) {
+ index--;
}
+ super.addComponent(c);
+ components.add(index, c);
+ requestRepaint();
}
/**
*/
@Override
public void addComponent(Component c) {
+ super.addComponent(c);
components.add(c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
- }
+ requestRepaint();
}
/**
* the component to be added.
*/
public void addComponentAsFirst(Component c) {
+ super.addComponent(c);
components.addFirst(c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
- }
+ requestRepaint();
}
/**
* in and after the position are shifted forwards.
*/
public void addComponent(Component c, int index) {
- components.add(index, c);
- try {
- super.addComponent(c);
- requestRepaint();
- } catch (IllegalArgumentException e) {
- components.remove(c);
- throw e;
+ // if this already contains c, super.addComponent() will remove it
+ // so everything after c's original position shifts one place down
+ if(this == c.getParent() && index > components.indexOf(c)) {
+ index--;
}
+ super.addComponent(c);
+ components.add(index, c);
+ requestRepaint();
}
/**
layout1.addComponent(children[3], 0);\r
assertOrder(layout1, new int[] { 3, 0 });\r
assertOrder(layout2, new int[] { 2, 1 });\r
+ \r
+ layout2.addComponent(children[0]);\r
+ assertOrder(layout1, new int[] { 3 });\r
+ assertOrder(layout2, new int[] { 2, 1, 0 });\r
+ \r
+ layout1.addComponentAsFirst(children[1]);\r
+ assertOrder(layout1, new int[] { 1, 3 });\r
+ assertOrder(layout2, new int[] { 2, 0 });\r
}\r
\r
@Test\r
// Move D from #2 to #0\r
layout.addComponent(children[3], 0);\r
assertOrder(layout, new int[] { 3, 0, 1, 2 });\r
+ \r
+ // Move A from #1 to end (#4 which becomes #3)\r
+ layout.addComponent(children[0]);\r
+ assertOrder(layout, new int[] { 3, 1, 2, 0 });\r
+\r
+ // Keep everything in place\r
+ layout.addComponent(children[0]);\r
+ assertOrder(layout, new int[] { 3, 1, 2, 0 });\r
+ \r
+ // Move C from #2 to #0\r
+ layout.addComponentAsFirst(children[2]);\r
+ assertOrder(layout, new int[] { 2, 3, 1, 0 });\r
+\r
+ // Keep everything in place\r
+ layout.addComponentAsFirst(children[2]);\r
+ assertOrder(layout, new int[] { 2, 3, 1, 0 });\r
}\r
\r
/**\r
layout1.addComponent(children[3], 0);\r
assertOrder(layout1, new int[] { 3, 0 });\r
assertOrder(layout2, new int[] { 2, 1 });\r
+ \r
+ layout2.addComponent(children[0]);\r
+ assertOrder(layout1, new int[] { 3 });\r
+ assertOrder(layout2, new int[] { 2, 1, 0 });\r
+ \r
+ layout1.addComponentAsFirst(children[1]);\r
+ assertOrder(layout1, new int[] { 1, 3 });\r
+ assertOrder(layout2, new int[] { 2, 0 });\r
}\r
\r
@Test\r
// Move D from #2 to #0\r
layout.addComponent(children[3], 0);\r
assertOrder(layout, new int[] { 3, 0, 1, 2 });\r
+ \r
+ // Move A from #1 to end (#4 which becomes #3)\r
+ layout.addComponent(children[0]);\r
+ assertOrder(layout, new int[] { 3, 1, 2, 0 });\r
+\r
+ // Keep everything in place\r
+ layout.addComponent(children[0]);\r
+ assertOrder(layout, new int[] { 3, 1, 2, 0 });\r
+ \r
+ // Move C from #2 to #0\r
+ layout.addComponentAsFirst(children[2]);\r
+ assertOrder(layout, new int[] { 2, 3, 1, 0 });\r
+\r
+ // Keep everything in place\r
+ layout.addComponentAsFirst(children[2]);\r
+ assertOrder(layout, new int[] { 2, 3, 1, 0 });\r
}\r
\r
/**\r