From 13e5df2169b53317cbac5fbbb0a2b2fe989d1ac9 Mon Sep 17 00:00:00 2001 From: Santosh Puranik Date: Wed, 28 Nov 2018 10:49:21 +0530 Subject: [PATCH] Add ability to export cards This commit adds the ability to export parts (cards) out of an already loaded system XML. A part can be exported by selecting the card in the target instance tree view and selecting the "Export Part" button. Any busses internal to the card (or any sub-cards) will be retained in the exported part XML. When exporting, the user can choose whether to retain attribute values on the part or to default all of them. The exported part XML is just like any other part XML. It can be placed in the parts directory and following a Serverwiz tool restart, will be picked up and be available in the target dropdown for adding to a system XML. --- .../controller/TargetWizardController.java | 16 +++++++ .../ibm/ServerWizard2/model/Connection.java | 15 ++++++ .../ibm/ServerWizard2/model/SystemModel.java | 22 ++++++++- src/com/ibm/ServerWizard2/model/Target.java | 47 +++++++++++++++---- .../ibm/ServerWizard2/view/MainDialog.java | 37 +++++++++++++++ 5 files changed, 125 insertions(+), 12 deletions(-) diff --git a/src/com/ibm/ServerWizard2/controller/TargetWizardController.java b/src/com/ibm/ServerWizard2/controller/TargetWizardController.java index d0a95ed..be11a86 100644 --- a/src/com/ibm/ServerWizard2/controller/TargetWizardController.java +++ b/src/com/ibm/ServerWizard2/controller/TargetWizardController.java @@ -128,6 +128,7 @@ public void addTargetInstance(Target targetModel, Target parentTarget, // target instance found of this model type targetInstance = new Target(instanceCheck); targetInstance.copyChildren(instanceCheck); + targetInstance.copyBusses(instanceCheck); } else { targetInstance = new Target(targetModel); } @@ -157,6 +158,21 @@ public Target copyTargetInstance(Target target, Target parentTarget, } return newTarget; } + + public void exportAsPart(Target target, String fileName, Boolean defaultAttributes) { + try { + String tmpFilename = fileName + ".tmp"; + model.writeXML(tmpFilename, target, defaultAttributes); + File from = new File(tmpFilename); + File to = new File(fileName); + Files.copy(from.toPath(), to.toPath(), + StandardCopyOption.REPLACE_EXISTING); + Files.delete(from.toPath()); + ServerWizard2.LOGGER.info(fileName + " Saved"); + } catch (Exception e) { + ServerwizMessageDialog.openError(null, "Export Part Error", e.getMessage()); + } + } public void deleteConnection(Target target, Target busTarget, Connection conn) { diff --git a/src/com/ibm/ServerWizard2/model/Connection.java b/src/com/ibm/ServerWizard2/model/Connection.java index cbb6497..66e3ded 100644 --- a/src/com/ibm/ServerWizard2/model/Connection.java +++ b/src/com/ibm/ServerWizard2/model/Connection.java @@ -23,6 +23,21 @@ public String getName() { } return source.getName()+seperator+dest.getName(); } + public Connection() { + // TODO Auto-generated constructor stub + } + public Connection(Connection other) { + id = other.id; + busType = other.busType; + source = new ConnectionEndpoint(); + dest = new ConnectionEndpoint(); + source.setTargetName(other.source.getTargetName()); + source.setPath(other.source.getPath()); + dest.setTargetName(other.dest.getTargetName()); + dest.setPath(other.dest.getPath()); + cabled = other.cabled; + busTarget = new Target(other.busTarget); + } public void writeInstanceXML(Writer out) throws Exception { out.write("\t\n"); out.write("\t\t"+getName()+"\n"); diff --git a/src/com/ibm/ServerWizard2/model/SystemModel.java b/src/com/ibm/ServerWizard2/model/SystemModel.java index d23a288..406ae13 100644 --- a/src/com/ibm/ServerWizard2/model/SystemModel.java +++ b/src/com/ibm/ServerWizard2/model/SystemModel.java @@ -615,7 +615,7 @@ public void writeXML(String filename, Boolean partsMode) throws Exception { HashMap targetWritten = new HashMap(); for (Target target : targetList) { if (partsMode) { - target.writeTargetXML(out, targetLookup, targetWritten); + target.writeTargetXML(out, targetLookup, targetWritten, false, false, null); } else { target.writeInstanceXML(out, targetLookup, targetWritten); } @@ -624,7 +624,25 @@ public void writeXML(String filename, Boolean partsMode) throws Exception { out.write("\n"); out.close(); } - + + /* + * Write XML to a parts file (only the supplied target and it's children) + */ + public void writeXML(String filename, Target topTarget, Boolean defaultAttributes) throws Exception { + Writer out = new BufferedWriter(new FileWriter(filename)); + + out.write("\n"); + out.write(""+ServerWizard2.getVersionString()+"\n"); + out.write("\n"); + HashMap targetWritten = new HashMap(); + topTarget.writeTargetXML(out, targetLookup, targetWritten, true, true, + defaultAttributes ? attributes : null); + out.write("\n"); + out.write("\n"); + out.close(); + } + + /* * Add a target instance to the model */ diff --git a/src/com/ibm/ServerWizard2/model/Target.java b/src/com/ibm/ServerWizard2/model/Target.java index 5691062..517ee96 100644 --- a/src/com/ibm/ServerWizard2/model/Target.java +++ b/src/com/ibm/ServerWizard2/model/Target.java @@ -102,6 +102,17 @@ public void copyChildren(Target t) { this.childrenHidden.add(c); } } + public void copyBusses(Target t) { + for (Map.Entry> entry : t.busses.entrySet()) { + Target newTarget = new Target(entry.getKey()); + Vector newConns = new Vector(); + for (Connection conn : entry.getValue()) { + newConns.add(new Connection(conn)); + } + busses.put(newTarget, newConns); + } + busInited = true; + } public void removeChildren(String child) { children.remove(child); childrenHidden.remove(child); @@ -234,7 +245,8 @@ public Boolean isOutput() { public Boolean isSystem() { return (this.getAttribute("CLASS").equals("SYS")); } - Boolean isCard() { + + public Boolean isCard() { return (this.getAttribute("CLASS").equals("CARD") || this.getAttribute("CLASS").equals( "MOTHERBOARD")); } @@ -527,19 +539,31 @@ public void readTargetXML(Element t, TreeMap targetModels, HashM } conn.busTarget = new Target(busTarget); conn.readInstanceXML(bus); - busses.get(busTarget).add(conn); + + if(busses.containsKey(busTarget)) { + busses.get(busTarget).add(conn); + } else { + Vector conns = new Vector(); + conns.add(conn); + busses.put(busTarget, conns); + } } } - public void writeTargetXML(Writer out,HashMap targetLookup, HashMaptargetWritten) throws Exception { + public void writeTargetXML(Writer out,HashMap targetLookup, HashMaptargetWritten, + Boolean targetExport, Boolean forceRoot, HashMap attrMap) throws Exception { if (targetWritten.containsKey(this.getName())) { return; } targetWritten.put(this.getName(), true); out.write("\n"); out.write("\t" + this.getName() + "\n"); - out.write("\t" + this.getType() + "\n"); + if(forceRoot) { + out.write("\t" + this.getAttribute("CLASS").toLowerCase() + "-" + this.name + "\n"); + } else { + out.write("\t" + this.getType() + "\n"); + } String rootStr = "false"; - if (this.isRoot()) { rootStr = "true"; } + if (this.isRoot() || forceRoot) { rootStr = "true"; } out.write("\t" + rootStr + "\n"); if (!this.name.isEmpty()) { out.write("\t" + this.name + "\n"); @@ -547,7 +571,7 @@ public void writeTargetXML(Writer out,HashMap targetLookup, HashM out.write("\t" + this.getIdPrefix() + "\n"); } - out.write("\t" + getPosition() + "\n"); + out.write("\t" + (forceRoot ? "-1" : getPosition()) + "\n"); out.write("\t" + this.parent + "\n"); for (String p_type : this.parentType) { out.write("\t" + p_type + "\n"); @@ -562,9 +586,12 @@ public void writeTargetXML(Writer out,HashMap targetLookup, HashM } //write attributes for (Map.Entry entry : getAttributes().entrySet()) { - Attribute attr = new Attribute(entry.getValue()); - attr.writeInstanceXML(out); - + if(attrMap != null) { + attrMap.get(entry.getKey()).writeInstanceXML(out); + } else { + Attribute attr = new Attribute(entry.getValue()); + attr.writeInstanceXML(out); + } } //write busses for (Map.Entry> entry : busses.entrySet()) { @@ -577,7 +604,7 @@ public void writeTargetXML(Writer out,HashMap targetLookup, HashM //recursively write children for (String childStr : this.getAllChildren()) { Target child = targetLookup.get(childStr); - child.writeTargetXML(out, targetLookup, targetWritten); + child.writeTargetXML(out, targetLookup, targetWritten, targetExport, false, attrMap); } } } diff --git a/src/com/ibm/ServerWizard2/view/MainDialog.java b/src/com/ibm/ServerWizard2/view/MainDialog.java index 9cafa56..db453da 100644 --- a/src/com/ibm/ServerWizard2/view/MainDialog.java +++ b/src/com/ibm/ServerWizard2/view/MainDialog.java @@ -75,6 +75,7 @@ public class MainDialog extends Dialog { // Buttons private Button btnAddTarget; private Button btnCopyInstance; + private Button btnExportPart; private Button btnDefaults; private Button btnDeleteTarget; private Button btnSave; @@ -287,6 +288,12 @@ protected Control createDialogArea(Composite parent) { btnCopyInstance.setFont(SWTResourceManager.getFont("Arial", 9, SWT.NORMAL)); btnCopyInstance.setEnabled(false); + btnExportPart = new Button(compositeInstance, SWT.NONE); + btnExportPart.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1)); + btnExportPart.setText("Export Part"); + btnExportPart.setFont(SWTResourceManager.getFont("Arial", 9, SWT.NORMAL)); + btnExportPart.setEnabled(false); + btnDefaults = new Button(compositeInstance, SWT.NONE); btnDefaults.setLayoutData(new GridData(SWT.CENTER, SWT.CENTER, true, false, 1, 1)); btnDefaults.setText("Restore Defaults"); @@ -714,6 +721,7 @@ private void updateView() { btnAddTarget.setEnabled(false); btnDeleteTarget.setEnabled(false); btnCopyInstance.setEnabled(false); + btnExportPart.setEnabled(false); btnDefaults.setEnabled(false); updateChildCombo(null); return; @@ -743,6 +751,11 @@ private void updateView() { } else { btnCopyInstance.setEnabled(false); } + if (targetInstance.isCard()) { + btnExportPart.setEnabled(true); + } else { + btnExportPart.setEnabled(false); + } btnDefaults.setEnabled(true); } @@ -1211,6 +1224,30 @@ public void widgetSelected(SelectionEvent arg0) { setDirtyState(true); } }); + + btnExportPart.addSelectionListener(new SelectionAdapter() { + @Override + public void widgetSelected(SelectionEvent arg0) { + TreeItem selectedItem = tree.getSelection()[0]; + if (selectedItem == null) { + return; + } + Target target = (Target) selectedItem.getData(); + Button b = (Button) arg0.getSource(); + FileDialog fdlg = new FileDialog(b.getShell(), SWT.SAVE); + String ext[] = { "*.xml" }; + fdlg.setFilterExtensions(ext); + fdlg.setOverwrite(true); + String filename = fdlg.open(); + if (filename == null) { + return; + } + Boolean defaultAttrs = + MessageDialog.openQuestion(null, "Default Attributes", "Would you like to default all attributes of the exported part?"); + controller.exportAsPart(target, filename, defaultAttrs); + } + }); + btnDefaults.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent arg0) {