final Map<Node, VmsXml> domainNodesToSave = new HashMap<Node, VmsXml>();
final String clusterName = getBrowser().getCluster().getName();
getBrowser().vmStatusLock();
guiData.startProgressIndicator(clusterName, "VM view update");
for (final Host host : getBrowser().getClusterHosts()) {
final Widget hostWi = definedOnHostComboBoxHash.get(host.getName());
application.invokeLater(new Runnable() {
@Override
public void run() {
final Widget wizardHostWi = definedOnHostComboBoxHash.get(WIZARD_HOST_PREFIX + host.getName());
if (wizardHostWi != null) {
wizardHostWi.setEnabled(false);
}
}
});
final Value value = definedOnHostComboBoxHash.get(host.getName()).getValue();
final boolean needConsole = needConsole();
if (DEFINED_ON_HOST_TRUE.equals(value)) {
final Node domainNode;
VmsXml vmsXml;
if (getResource().isNew()) {
vmsXml = new VmsXml(host);
getBrowser().vmsXmlPut(host, vmsXml);
domainNode = vmsXml.createDomainXML(getUUID(), getDomainName(), parameters, needConsole);
for (final HardwareInfo hi : allHWP.keySet()) {
hi.modifyXML(vmsXml, domainNode, getDomainName(), allHWP.get(hi));
hi.getResource().setNew(false);
}
vmsXml.saveAndDefine(domainNode, getDomainName(), getVirshOptions());
} else {
vmsXml = getBrowser().getVmsXml(host);
if (vmsXml == null) {
vmsXml = new VmsXml(host);
getBrowser().vmsXmlPut(host, vmsXml);
}
if (vmsXml.getDomainNames().contains(getDomainName())) {
domainNode = vmsXml.modifyDomainXML(getDomainName(), parameters);
if (domainNode != null) {
for (final HardwareInfo hi : allModifiedHWP.keySet()) {
if (hi.checkResourceFields(null, hi.getRealParametersFromXML(), true).isChanged()) {
hi.modifyXML(vmsXml, domainNode, getDomainName(), allModifiedHWP.get(hi));
hi.getResource().setNew(false);
}
}
}
} else {
/* new on this host */
domainNode = vmsXml.createDomainXML(getUUID(), getDomainName(), parameters, needConsole);
if (domainNode != null) {
for (final HardwareInfo hi : allHWP.keySet()) {
hi.modifyXML(vmsXml, domainNode, getDomainName(), allHWP.get(hi));
hi.getResource().setNew(false);
}
}
}
}
if (domainNode != null) {
domainNodesToSave.put(domainNode, vmsXml);
}
definedOnHosts.add(host);
} else {
final VmsXml vmsXml = getBrowser().getVmsXml(host);
if (vmsXml != null && vmsXml.getDomainNames().contains(getDomainName())) {
VIRSH.undefine(host, getDomainName(), getVirshOptions());
}
}
}
for (final Node dn : domainNodesToSave.keySet()) {
domainNodesToSave.get(dn).saveAndDefine(dn, getDomainName(), getVirshOptions());
}
for (final HardwareInfo hi : allHWP.keySet()) {
hi.setApplyButtons(null, hi.getRealParametersFromXML());
}
if (getResource().isNew()) {
final DefaultMutableTreeNode thisNode = getNode();
if (thisNode != null) {
for (final Info info : treeMenuController.nodesToInfos(thisNode.children())) {
final HardwareInfo hardwareInfo = (HardwareInfo) info;
if (hardwareInfo != null) {
final MyButton applyButton = hardwareInfo.getApplyButton();
if (applyButton != null) {
applyButton.setVisible(true);
}
}
}
}
}
VIRSH.setParameters(definedOnHosts.toArray(new Host[definedOnHosts.size()]),
getDomainName(),
parameters,
getVirshOptions());
getResource().setNew(false);
if (Application.isLive(runMode)) {
storeComboBoxValues(params);
}
getBrowser().periodicalVmsUpdate(getBrowser().getClusterHosts());
updateParameters();
guiData.stopProgressIndicator(clusterName, "VM view update");
getBrowser().vmStatusUnlock();
application.invokeLater(new Runnable() {
@Override
public void run() {
for (final Host host : getBrowser().getClusterHosts()) {
final Widget hostWi = definedOnHostComboBoxHash.get(host.getName());
final Widget wizardHostWi = definedOnHostComboBoxHash.get(WIZARD_HOST_PREFIX + host.getName());
if (wizardHostWi != null) {
wizardHostWi.setEnabled(true);
}
}
}
});
}