diff options
| author | Jonathan Bauer | 2018-04-25 18:35:42 +0200 |
|---|---|---|
| committer | Jonathan Bauer | 2018-04-25 18:35:42 +0200 |
| commit | ababf4a73a61150f24f1ff17f3a49c591d04193a (patch) | |
| tree | 443699406bcf842f651d04c315a857cac25c28f0 /dozentenmodul/src/main/java | |
| parent | [client] setTheBoxes -> initializeComboBoxes (diff) | |
| download | tutor-module-ababf4a73a61150f24f1ff17f3a49c591d04193a.tar.gz tutor-module-ababf4a73a61150f24f1ff17f3a49c591d04193a.tar.xz tutor-module-ababf4a73a61150f24f1ff17f3a49c591d04193a.zip | |
[client] handle formatting of xml while editing
Diffstat (limited to 'dozentenmodul/src/main/java')
3 files changed, 67 insertions, 56 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java index e1ba414a..9dcf7e5b 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/ImageDetailsWindow.java @@ -398,7 +398,7 @@ public class ImageDetailsWindow extends ImageDetailsWindowLayout implements UiFe String tmp = ThriftUtil.byteBufferToString(machineDescription); if (TConst.VIRT_QEMU.equals(image.virtId)) { VirtConfigEditorWindow.open(me, actionHandler, selected.versionId, - machineDescription, tmp); + ThriftUtil.byteBufferToString(machineDescription), tmp); } else { VirtDropDownConfigEditorWindow.open(me, actionHandler, selected.versionId, machineDescription); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtConfigEditorWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtConfigEditorWindow.java index 5d354fad..542b278e 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtConfigEditorWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtConfigEditorWindow.java @@ -3,6 +3,7 @@ package org.openslx.dozmod.gui.window; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.Charset; @@ -21,7 +22,8 @@ import org.openslx.dozmod.gui.window.layout.VirtConfigEditorWindowLayout; import org.openslx.dozmod.thrift.ImageDetailsActions; import org.openslx.dozmod.thrift.ImageDetailsActions.VirtConfCallback; import org.openslx.dozmod.thrift.cache.MetaDataCache; -import org.openslx.util.ThriftUtil; +import org.openslx.util.XmlHelper; +import org.openslx.util.vm.VboxMetaData; import org.openslx.util.vm.VmMetaData; import org.openslx.util.vm.VmwareConfig; @@ -38,17 +40,16 @@ public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout impleme private final String originalMachineDescription; private final VirtConfigEditorWindow me = this; private final ImageDetailsActions actionHandler; - + private boolean configWasSaved = false; protected VirtConfigEditorWindow(Window modalParent, final ImageDetailsActions actionHandler, - final String imageVersionId, final ByteBuffer currentMachineDescription, + final String imageVersionId, final String currentMachineDescription, final String originalmachineDescription) { super(modalParent); this.actionHandler = actionHandler; this.imageVersionId = imageVersionId; - // listener for the text fields pnlEditor.getDocument().addDocumentListener(new TextChangeListener() { @Override @@ -70,11 +71,11 @@ public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout impleme safeClose(); } }); + // for vbox, originalMachineDescription is unformatted, the new one will be though // save original description for later use - this.originalMachineDescription = originalmachineDescription; - // finally set the editor's content to the new given description - String cmdString = ThriftUtil.byteBufferToString(currentMachineDescription); - pnlEditor.setText(cmdString); + this.originalMachineDescription = XmlHelper.getFormattedXml( + new ByteArrayInputStream(originalmachineDescription.getBytes(StandardCharsets.UTF_8))); + pnlEditor.setText(currentMachineDescription); pnlEditor.setCaretPosition(0); } @@ -86,19 +87,24 @@ public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout impleme // cs is now either the detected encoding, or latin1 as a default uiBytes = userInput.getBytes(cs); // now we should have the correct bytes... - VmMetaData<?,?,?,?> metaCandidate = null; + VmMetaData<?, ?, ?, ?> metaCandidate = null; try { metaCandidate = VmMetaData.getInstance(MetaDataCache.getOperatingSystems(), uiBytes, uiBytes.length); } catch (IOException e) { LOGGER.error("Could not get VmMetaData instance from given machine description: ", e); } - if ( metaCandidate == null ) { + if (metaCandidate == null) { dispose(); } - + byte[] uiBytesFiltered = metaCandidate.getDefinitionArray(); - final String userInputFiltered = new String(uiBytesFiltered, cs); + final String userInputFiltered; + if (metaCandidate instanceof VboxMetaData) { + userInputFiltered = XmlHelper.getFormattedXml(new ByteArrayInputStream(uiBytesFiltered)); + } else { + userInputFiltered = new String(uiBytesFiltered, cs); + } // So here we have: // - uiBytes is the unfiltered user input // - uiBytesFiltered is the user input filtered by VmwareMetaData @@ -116,18 +122,23 @@ public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout impleme if (!Gui.showMessageBox(errorText, MessageType.ERROR_RETRY, LOGGER, null)) return; } - + ByteBuffer toSave; + if (metaCandidate instanceof VboxMetaData) { + String unformattedInput = XmlHelper.getUnformattedXml(new ByteArrayInputStream(uiBytesFiltered)); + toSave = ByteBuffer.wrap(unformattedInput.getBytes(cs)); + } else { + toSave = ByteBuffer.wrap(uiBytesFiltered); + } // we have a valid vmx or the user accepted to push the filtered input - actionHandler.setVirtualizerConfig(imageVersionId, ByteBuffer.wrap(uiBytesFiltered), - new VirtConfCallback() { - @Override - public void virtConfCallback(boolean success) { - if (success) { - configWasSaved = true; - dispose(); - } - } - }); + actionHandler.setVirtualizerConfig(imageVersionId, toSave, new VirtConfCallback() { + @Override + public void virtConfCallback(boolean success) { + if (success) { + configWasSaved = true; + dispose(); + } + } + }); } private TreeSet<String> stringToTreeSet(final String s) { @@ -139,7 +150,7 @@ public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout impleme } public static boolean open(Window modalParent, final ImageDetailsActions handler, - final String imageVersionId, final ByteBuffer currentmachineDescription, + final String imageVersionId, final String currentmachineDescription, final String originalMachineDescription) { VirtConfigEditorWindow win = new VirtConfigEditorWindow(modalParent, handler, imageVersionId, currentmachineDescription, originalMachineDescription); diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java index 36f4d817..5266e157 100644 --- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java +++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java @@ -3,6 +3,7 @@ package org.openslx.dozmod.gui.window; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.io.ByteArrayInputStream; import java.io.IOException; import java.nio.ByteBuffer; @@ -15,6 +16,8 @@ import org.openslx.dozmod.thrift.ImageDetailsActions; import org.openslx.dozmod.thrift.ImageDetailsActions.VirtConfCallback; import org.openslx.dozmod.thrift.cache.MetaDataCache; import org.openslx.util.ThriftUtil; +import org.openslx.util.XmlHelper; +import org.openslx.util.vm.VboxMetaData; import org.openslx.util.vm.VmMetaData; import org.openslx.util.vm.VmMetaData.DDAcceleration; import org.openslx.util.vm.VmMetaData.EthernetDevType; @@ -22,8 +25,8 @@ import org.openslx.util.vm.VmMetaData.HWVersion; import org.openslx.util.vm.VmMetaData.SoundCardType; @SuppressWarnings("serial") -public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWindowLayout implements - UiFeedback { +public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWindowLayout + implements UiFeedback { private final static Logger LOGGER = Logger.getLogger(VirtDropDownConfigEditorWindow.class); @@ -56,6 +59,7 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind if (metaCandidate == null) { dispose(); } + meta = metaCandidate; initializeSoundBox(meta.getSupportedSoundCards()); @@ -72,7 +76,7 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind public void actionPerformed(ActionEvent e) { SoundCardType selected = (SoundCardType) cboSound.getSelectedItem(); meta.setSoundCard(selected); - checkForChange(); + btnSave.setEnabled(hasChanged()); } }); @@ -82,7 +86,7 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind public void actionPerformed(ActionEvent e) { DDAcceleration selected = (DDAcceleration) cbo3DAcceleration.getSelectedItem(); meta.setDDAcceleration(selected); - checkForChange(); + btnSave.setEnabled(hasChanged()); } }); @@ -92,7 +96,7 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind public void actionPerformed(ActionEvent e) { HWVersion selected = (HWVersion) cboHWVersion.getSelectedItem(); meta.setHWVersion(selected); - checkForChange(); + btnSave.setEnabled(hasChanged()); } }); @@ -102,7 +106,7 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind public void actionPerformed(ActionEvent e) { EthernetDevType selected = (EthernetDevType) cboE0VirtDev.getSelectedItem(); meta.setEthernetDevType(0, selected); - checkForChange(); + btnSave.setEnabled(hasChanged()); } }); @@ -122,13 +126,22 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind } }); - //listener for the profi button + // listener for the expert mode btnMore.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { + // here we need special logic for formatting vbox file in a human-readable way... + // we will just format it before sending it to the editor window and reformat it right after... + String currentMachineDesc; + if (meta instanceof VboxMetaData) + currentMachineDesc = XmlHelper + .getFormattedXml(new ByteArrayInputStream(meta.getDefinitionArray())); + else + currentMachineDesc = ThriftUtil + .byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray())); boolean didSave = VirtConfigEditorWindow.open(me, actionHandler, imageVersionId, - ByteBuffer.wrap(meta.getDefinitionArray()), originalMachineDescription); - // User did save the changes via the expert mode window - close this one too + currentMachineDesc, originalMachineDescription); + // user did save the changes via the expert mode window - close this one too if (didSave) { dispose(); } @@ -137,21 +150,6 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind this.originalMachineDescription = ThriftUtil.byteBufferToString(machineDescription); btnSave.setEnabled(false); - //TODO sort this hack out...and make it work - /* - if ( meta instanceof QemuMetaData) { - this.btnMore.doClick(); - } */ - } - - protected void checkForChange() { - if (!originalMachineDescription - .equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray())))) { - btnSave.setEnabled(true); - } else { - btnSave.setEnabled(false); - } - } /* setting the boxes to their initial value...value is read from the given metaData @@ -195,20 +193,22 @@ public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWind win.setVisible(true); } + private boolean hasChanged() { + return !originalMachineDescription + .equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray()))); + } + private void safeClose() { - if (originalMachineDescription - .equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray()))) - || Gui.showMessageBox(me, - "Wollen Sie wirklich abbrechen?\n" + "Ihre Änderungen werden verworfen.", - MessageType.QUESTION_YESNO, null, null)) { + if (!hasChanged() || Gui.showMessageBox(me, + "Wollen Sie wirklich abbrechen?\n" + "Ihre Änderungen werden verworfen.", + MessageType.QUESTION_YESNO, null, null)) { dispose(); } } @Override public boolean wantConfirmQuit() { - return !originalMachineDescription - .equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray()))); + return hasChanged(); } @Override |
