}
@Override
public int moveConfig(IConfig config, int newPos) throws MException {
if (!(config instanceof XmlConfig))
throw new MException("not a xmlconfig");
if ( ((XmlConfig)config).element.getParentNode() != element)
throw new MException("not child of this config");
NodeList list = MXml.getLocalElements(element);
if (list.getLength() == 1) {
if (newPos == MOVE_FIRST || newPos == MOVE_LAST || newPos == 0)
return 0;
throw new MException("out of range");
}
if (newPos == MOVE_FIRST) {
element.removeChild(((XmlConfig)config).element);
list = MXml.getLocalElements(element);
element.insertBefore(((XmlConfig)config).element, list.item(0)); //XXX
return 0;
}
if(newPos == MOVE_LAST) {
element.removeChild(((XmlConfig)config).element);
element.appendChild(((XmlConfig)config).element);
return list.getLength()-1;
}
if (newPos == MOVE_DOWN) {
for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
if (item == ((XmlConfig)config).element) { // find element in children list
if (i == list.getLength()-1) // already last one
throw new MException("out of range");
Node next = list.item(i+1);
element.removeChild(((XmlConfig)config).element);
element.insertBefore(((XmlConfig)config).element, next); //XXX
return i+1;
}
}
}
if (newPos == MOVE_UP) {
for (int i = 0; i < list.getLength(); i++) {
Node item = list.item(i);
if (item == ((XmlConfig)config).element) { // find element in children list
if (i == 0) // already first one
throw new MException("out of range");
Node before = list.item(i-1);
element.removeChild(((XmlConfig)config).element);
element.insertBefore(((XmlConfig)config).element, before); //XXX
return i-1;
}
}
}
if (newPos < 0 || newPos >= list.getLength())
throw new MException("out of range");
Node before = list.item(newPos);
if (before == ((XmlConfig)config).element) // already on this pos
return newPos;