summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java
blob: 072d648e11ac09573322085664e47affdeb22388 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
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.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.util.vm.VboxMetaData;
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;
import org.openslx.util.vm.VmMetaData.UsbSpeed;

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

	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 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 e) {
			LOGGER.error("Could not get VmMetaData instance from given machine description: ", e);
		}

		meta = metaCandidate;
		
		if (metaCandidate == null) {
			this.originalMachineDescription = null;
			dispose();
			return;
		}

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

		//set the initial state of the buttons
		initializeComboBoxes(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);
				btnSave.setEnabled(hasChanged());
			}
		});

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

		// 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);
				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();
				meta.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();
					meta.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 (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,
						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 metaData
	 * @param: given VmwareMetaData
	 */
	private void initializeComboBoxes(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);
			UsbSpeed usbSpeed = m.getMaxUsbSpeed();
			cboMaxUsbSpeed.setSelectedItem(usbSpeed);
		} 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 boolean hasChanged() {
		return !originalMachineDescription
				.equals(ThriftUtil.byteBufferToString(ByteBuffer.wrap(meta.getDefinitionArray())));
	}

	private void safeClose() {
		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 hasChanged();
	}

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