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




                                      
                                    
                           

                           
                               
                                  
                                          



                                                                                 
                                         

                                                                      
                                              
                                   
                                  
                                                                                      
                                          


                                                                                            

                                                                                          
 

                                                                                        
 




                                                                         

                                                                                                    






                                                               
                                                                                    









                                                                                                             
                                                                                     
                     

                                                                                                                           

                                                                                                              
                 
                
                                                   
                                                               
                                  
                               
                 
 

                                                                                  
                                                                                                  

                                                                                     

                                                      
                                                           





                                                                                                    
                                                                            
                                                                 







                                                                                                               
                                                                                 
                                                                 






                                                                     

                                                                                            
                                                                 






                                                                     
                                                                                                            
                                                                                     
                                                                 

                         
                




                                                                                                        
                                                                                      



                                                                         
















                                                                    
                                               


                                                                    


                                                                                                                               
                                                                                                          
                                                                      
                                                                                                                                                       

                                                                       
                                                                                                                                                 
                                                                                                                

                                                                                                            


                                                  




                                                                                                    

         

                                                                                                               
           
                                                                                      




                                                                   







                                                                                                                          

                                                                       

                                                                 

                                                      
                                                                                                                                  

                                                                      



                                                                                      
                                                                                                                                       
















                                                                                                             

                                                  
                                                                                                                                            

         
                                  
                                                           
                                                                                                          
                                                                          
                                  
                 



                                          
                                    





                                     
 
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 org.apache.log4j.Logger;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.I18n;
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.Session;
import org.openslx.dozmod.thrift.ImageDetailsActions.VirtConfCallback;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.sat.thrift.version.Feature;
import org.openslx.util.ThriftUtil;
import org.openslx.util.XmlHelper;
import org.openslx.virtualization.configuration.VirtualizationConfigurationVirtualBox;
import org.openslx.virtualization.Version;
import org.openslx.virtualization.configuration.VirtualizationConfiguration;
import org.openslx.virtualization.configuration.VirtualizationConfiguration.DDAcceleration;
import org.openslx.virtualization.configuration.VirtualizationConfiguration.EthernetDevType;
import org.openslx.virtualization.configuration.VirtualizationConfiguration.SoundCardType;
import org.openslx.virtualization.configuration.VirtualizationConfiguration.UsbSpeed;

public class VirtDropDownConfigEditorWindow extends VirtDropDownConfigEditorWindowLayout
		implements UiFeedback {

	/**
	 * Version for serialization.
	 */
	private static final long serialVersionUID = 230794205942072411L;

	private final static Logger LOGGER = Logger.getLogger(VirtDropDownConfigEditorWindow.class);

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

	private final String imageVersionId;
	private final String originalMachineDescription;
	private final VirtDropDownConfigEditorWindow me = this;
	private VirtualizationConfiguration<?, ?, ?, ?> virtualizationConfig = null;
	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
		byte[] machineData = ThriftUtil.unwrapByteBuffer(machineDescription);
		try {
			virtualizationConfig = VirtualizationConfiguration.getInstance(MetaDataCache.getOperatingSystems(),
					machineData, machineData.length);
		} catch (IOException e) {
			LOGGER.error("Could not get VmMetaData instance from given machine description: ", e);
		}
		
		if (virtualizationConfig == null) {
			this.originalMachineDescription = null;
			dispose();
			return;
		}

		initializeSoundBox(virtualizationConfig.getSupportedSoundCards());
		initializeDDABox(virtualizationConfig.getSupportedDDAccs());
		initializeHWVersBox(virtualizationConfig.getVirtualizer().getSupportedVersions());
		initializeEDTBox(virtualizationConfig.getSupportedEthernetDevices());
		initializeUsbBox(virtualizationConfig.getSupportedUsbSpeeds());

		//set the initial state of the buttons
		initializeComboBoxes(virtualizationConfig);

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

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

		// listener for the Hardware version drop box
		cboHWVersion.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				Version selected = (Version) cboHWVersion.getSelectedItem();
				virtualizationConfig.setVirtualizerVersion(selected);
				btnSave.setEnabled(hasChanged());
			}
		});

		// listener for the Virtual Ethernet device drop box
		cboE0VirtDev.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				EthernetDevType selected = (EthernetDevType) cboE0VirtDev.getSelectedItem();
				virtualizationConfig.setEthernetDevType(0, selected);
				btnSave.setEnabled(hasChanged());
			}
		});
		
		if (Session.hasFeature(Feature.CONFIGURE_USB)) {
			cboMaxUsbSpeed.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent e) {
					UsbSpeed selected = (UsbSpeed) cboMaxUsbSpeed.getSelectedItem();
					virtualizationConfig.setMaxUsbSpeed(selected);
					btnSave.setEnabled(hasChanged());
				}
			});
		}

		// 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 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 (virtualizationConfig instanceof VirtualizationConfigurationVirtualBox)
					currentMachineDesc = XmlHelper
							.getFormattedXml(new ByteArrayInputStream(virtualizationConfig.getConfigurationAsByteArray()));
				else
					currentMachineDesc = ThriftUtil
							.byteBufferToString(ByteBuffer.wrap(virtualizationConfig.getConfigurationAsByteArray()));
				boolean didSave = VirtConfigEditorWindow.open(me, actionHandler, imageVersionId,
						currentMachineDesc, 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);
	}

	/* 
	 * setting the boxes to their initial value...value is read from the given virtualization configuration
	 */
	private void initializeComboBoxes(VirtualizationConfiguration<?, ?, ?, ?> m) {
		try {
			SoundCardType SCT = m.getSoundCard();
			cboSound.setSelectedItem(SCT);
			DDAcceleration DDA = m.getDDAcceleration();
			cbo3DAcceleration.setSelectedItem(DDA);
			
			// initialize version value in combobox
			Version HWV = m.getVirtualizerVersion();
			if (HWV != null) {
				// set version value specified in the virtualization configuration as default if specified
				cboHWVersion.setSelectedItem(HWV);
			}

			EthernetDevType E0VD = m.getEthernetDevType(0);
			cboE0VirtDev.setSelectedItem(E0VD);
			UsbSpeed usbSpeed = m.getMaxUsbSpeed();
			cboMaxUsbSpeed.setSelectedItem(usbSpeed);
		} catch (IllegalArgumentException e) {
			// TODO improve user message 
			Gui.showMessageBox(I18n.WINDOW.getString("VirtDropDownConfigEditor.Message.warning.initializeComboBoxes"),
					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(virtualizationConfig.getConfigurationAsByteArray()),
				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 boolean hasChanged() {
		return !originalMachineDescription
				.equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(virtualizationConfig.getConfigurationAsByteArray())));
	}

	private void safeClose() {
		if (!hasChanged() || Gui.showMessageBox(me,
				I18n.WINDOW.getString("VirtDropDownConfigEditor.Message.yesNo.safeClose"),
				MessageType.QUESTION_YESNO, null, null)) {
			dispose();
		}
	}

	@Override
	public boolean wantConfirmQuit() {
		return hasChanged();
	}

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