summaryrefslogblamecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java
blob: c151f5055adbb67cd86851c1efe2898d07522bc7 (plain) (tree)
1
2
3
4
5
6




                                      
                           









                                                                                 




                                                      

                           

                                                                                                   







                                                               
                                                  









                                                                                                             
                                                            
                                                                                     














                                                                                                                





































                                                                                                               

                                                                                                            























                                                                    
                                                                                                                
                                                                                                                        



                                                                                                            




                                                                                                    




                                                            














                                                                                                                     















                                                                                                                     


























                                                                                                                          
                                                                                          
                                  
                 











                                                                                                                   
 
package org.openslx.dozmod.gui.window;

import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.nio.ByteBuffer;

import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.MessageType;
import org.openslx.dozmod.gui.helper.UiFeedback;
import org.openslx.dozmod.gui.window.layout.VirtDropDownConfigEditorWindowLayout;
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.VmMetaData;
import org.openslx.util.vm.VmMetaData.DDAcceleration;
import org.openslx.util.vm.VmMetaData.EthernetDevType;
import org.openslx.util.vm.VmMetaData.HWVersion;
import org.openslx.util.vm.VmMetaData.SoundCardType;

@SuppressWarnings("serial")
public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWindowLayout implements
		UiFeedback {

	public interface VirtConfigChanged {
		public void virtConfigChanged(String newVmx);
	}

	private final String imageVersionId;
	private final String originalMachineDescription;
	private final VirtDropDownConfigEditorWindow me = this;
	private final VmMetaData<?, ?, ?, ?> meta;
	private final ImageDetailsActions actionHandler;

	protected VirtDropDownConfigEditorWindow(Window modalParent, final ImageDetailsActions actionHandler,
			final String imageVersionId, final ByteBuffer machineDescription) {
		super(modalParent);

		this.actionHandler = actionHandler;
		this.imageVersionId = imageVersionId;

		// and now for the meta data
		VmMetaData<?, ?, ?, ?> metaCandidate = null;
		byte[] machineData = ThriftUtil.unwrapByteBuffer(machineDescription);
		try {
			metaCandidate = VmMetaData.getInstance(MetaDataCache.getOperatingSystems(), machineData,
					machineData.length);
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		if (metaCandidate == null) {
			dispose();
		}
		meta = metaCandidate;

		initializeSoundBox(meta.getSupportedSoundCards());
		initializeDDABox(meta.getSupportedDDAccs());
		initializeHWVersBox(meta.getSupportedHWVersions());
		initializeEDTBox(meta.getSupportedEthernetDevices());

		//set the initial state of the buttons
		setTheBoxes(meta);

		// listener for the sound card drop box
		cboSound.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				SoundCardType selected = (SoundCardType) cboSound.getSelectedItem();
				meta.setSoundCard(selected);
				checkForChange();
			}
		});

		// listener for the 3Dacceleration drop box
		cbo3DAcceleration.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				DDAcceleration selected = (DDAcceleration) cbo3DAcceleration.getSelectedItem();
				meta.setDDAcceleration(selected);
				checkForChange();
			}
		});

		// listener for the Hardware version drop box
		cboHWVersion.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				HWVersion selected = (HWVersion) cboHWVersion.getSelectedItem();
				meta.setHWVersion(selected);
				checkForChange();
			}
		});

		// listener for the Virtual Ethernet device drop box
		cboE0VirtDev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EthernetDevType selected = (EthernetDevType) cboE0VirtDev.getSelectedItem();
				meta.setEthernetDevType(0, selected);
				checkForChange();
			}
		});

		// listener for the save button
		btnSave.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				saveClicked();
			}
		});

		// listener for the cancel button
		btnCancel.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				safeClose();
			}
		});

		//listener for the profi button
		btnMore.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				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
				if (didSave) {
					dispose();
				}
			}
		});

		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
	 * @param: given VmwareMetaData
	 */
	private void setTheBoxes(VmMetaData<?, ?, ?, ?> m) {
		try {
			SoundCardType SCT = m.getSoundCard();
			cboSound.setSelectedItem(SCT);
			DDAcceleration DDA = m.getDDAcceleration();
			cbo3DAcceleration.setSelectedItem(DDA);
			HWVersion HWV = m.getHWVersion();
			cboHWVersion.setSelectedItem(HWV);
			EthernetDevType E0VD = m.getEthernetDevType(0);
			cboE0VirtDev.setSelectedItem(E0VD);
		} catch (IllegalArgumentException e) {
			// TODO improve user message 
			Gui.showMessageBox("Sie haben beim letzten Konfigurieren \n"
					+ "einen falschen Eintrag eingespeichert. \n" + "Die VM wird nicht starten!",
					MessageType.WARNING, null, e);
		}
	}

	private void saveClicked() {
		// we have a valid vmx or the user accepted to push the filtered input
		actionHandler.setVirtualizerConfig(imageVersionId, ByteBuffer.wrap(meta.getDefinitionArray()),
				new VirtConfCallback() {
					@Override
					public void virtConfCallback(boolean success) {
						if (success) {
							dispose();
						}
					}
				});
	}

	public static void open(Window modalParent, final ImageDetailsActions handler,
			final String imageVersionId, final ByteBuffer machineDescription) {
		VirtDropDownConfigEditorWindow win = new VirtDropDownConfigEditorWindow(modalParent, handler,
				imageVersionId, machineDescription);
		win.setVisible(true);
	}

	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)) {
			dispose();
		}
	}

	@Override
	public boolean wantConfirmQuit() {
		return !originalMachineDescription
				.equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray())));
	}

	@Override
	public void escapePressed() {
		safeClose();
	}
}