package org.openslx.dozmod.gui.window; import java.awt.Window; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.nio.ByteBuffer; import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.TreeSet; import org.apache.log4j.Logger; import org.openslx.dozmod.gui.Gui; import org.openslx.dozmod.gui.helper.MessageType; import org.openslx.dozmod.gui.helper.TextChangeListener; import org.openslx.dozmod.gui.helper.UiFeedback; 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.vm.VmwareConfig; import org.openslx.util.vm.VmwareMetaData; @SuppressWarnings("serial") public class VirtConfigEditorWindow extends VirtConfigEditorWindowLayout implements UiFeedback { private final static Logger LOGGER = Logger.getLogger(VirtConfigEditorWindow.class); public interface VirtConfigChanged { public void virtConfigChanged(String newVmx); } private final String imageVersionId; private final ByteBuffer originalMachineDescription; private final VirtConfigEditorWindow me = this; private final ImageDetailsActions actionHandler; protected VirtConfigEditorWindow(Window modalParent, final ImageDetailsActions actionHandler, final String imageVersionId, final ByteBuffer machineDescription) { super(modalParent); this.originalMachineDescription = machineDescription; this.actionHandler = actionHandler; this.imageVersionId = imageVersionId; // listener for the text fields pnlEditor.getDocument().addDocumentListener(new TextChangeListener() { @Override public void changed() { btnSave.setEnabled(!originalMachineDescription.equals(pnlEditor.getText()));; } }); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { saveClicked(); } }); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { safeClose(); } }); // now build the editor's content from the given buffer String mdString = ThriftUtil.byteBufferToString(machineDescription); // finally set the editor's content to the vmx pnlEditor.setText(mdString); pnlEditor.setCaretPosition(0); } private void saveClicked() { // we make use of the fact that we saved the BB here! final String userInput = pnlEditor.getText(); byte[] uiBytes = userInput.getBytes(StandardCharsets.ISO_8859_1); Charset cs = VmwareConfig.getCharset(uiBytes, uiBytes.length); // cs is now either the detected encoding, or latin1 as a default uiBytes = userInput.getBytes(cs); // now we should have the correct bytes.... VmwareMetaData meta = new VmwareMetaData(MetaDataCache.getOperatingSystems(), uiBytes, uiBytes.length); byte[] uiBytesFiltered = meta.getDefinitionArray(); final String userInputFiltered = new String(uiBytesFiltered, cs); // So here we have: // - uiBytes is the unfiltered user input // - uiBytesFiltered is the user input filtered by VmwareMetaData TreeSet unfilteredSet = stringToTreeSet(userInput); TreeSet filteredSet = stringToTreeSet(userInputFiltered); if (!filteredSet.equals(unfilteredSet)) { unfilteredSet.removeAll(filteredSet); // not equals, means there was some invalid input String errorText = "Invalide Eingaben:" + System.lineSeparator(); for (String s : unfilteredSet) { errorText += s + System.lineSeparator(); } errorText += System.lineSeparator() + "Wollen Sie trotzdem speichern? (Die invaliden Zeilen werden dabei automatisch gelöscht.)"; if (!Gui.showMessageBox(errorText, MessageType.ERROR_RETRY, LOGGER, null)) return; } // 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) { dispose(); } } }); } private TreeSet stringToTreeSet(final String s) { String[] split = s.split(System.lineSeparator()); List splitList = new ArrayList(Arrays.asList(split)); splitList.removeAll(Arrays.asList("", null)); TreeSet set = new TreeSet(splitList); return set; } public static void open(Window modalParent, final ImageDetailsActions handler, final String imageVersionId, final ByteBuffer machineDescription) { VirtConfigEditorWindow win = new VirtConfigEditorWindow(modalParent, handler, imageVersionId, machineDescription); win.setVisible(true); } private void safeClose() { if (ThriftUtil.byteBufferToString(originalMachineDescription).equals(pnlEditor.getText()) || Gui.showMessageBox(me, "Ihre Änderungen werden verloren gehen, wollen Sie trotzdem abbrechen?", MessageType.QUESTION_YESNO, null, null)) dispose(); } @Override public boolean wantConfirmQuit() { return !originalMachineDescription.equals(pnlEditor.getText()); } @Override public void escapePressed() { safeClose(); } }