summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/window/VirtDropDownConfigEditorWindow.java
blob: f30ece9105bce088751194dd523926de47e0a9fb (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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package org.openslx.dozmod.gui.window;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Insets;
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.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import org.apache.log4j.Logger;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.control.ComboBox;
import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
import org.openslx.dozmod.gui.control.WordWrapLabel;
import org.openslx.dozmod.gui.helper.GridManager;
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.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.virtualization.configuration.VirtualizationConfiguration;
import org.openslx.virtualization.configuration.VirtualizationConfiguration.ConfigurableOptionGroup;
import org.openslx.virtualization.configuration.VirtualizationConfigurationException;
import org.openslx.virtualization.configuration.VirtualizationConfigurationVirtualBox;
import org.openslx.virtualization.hardware.AbstractConfigurableOption;

public class VirtDropDownConfigEditorWindow extends JDialog
		implements UiFeedback {

	protected final JScrollPane pnlScrollPane;
	protected final JEditorPane pnlEditor;
	protected final JButton btnSave;
	protected final JButton btnCancel;
	protected final JButton btnMore;
	protected final List<JComboBox<AbstractConfigurableOption>> cboHardwareOptions;

	/**
	 * 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 final VirtualizationConfiguration virtualizationConfig;
	private final ImageDetailsActions actionHandler;

	protected VirtDropDownConfigEditorWindow(Window modalParent, final ImageDetailsActions actionHandler,
			final String imageVersionId, final ByteBuffer machineDescription)
			throws IOException, VirtualizationConfigurationException {
		super(modalParent, I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Dialog.title"),
				modalParent != null ? ModalityType.APPLICATION_MODAL : ModalityType.MODELESS);

		byte[] machineData = ThriftUtil.unwrapByteBuffer(machineDescription);
		virtualizationConfig = VirtualizationConfiguration.getInstance(MetaDataCache.getOperatingSystems(),
				machineData, machineData.length);

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

		// Configurable options, list of groups, each containing all valid options for group
		List<ConfigurableOptionGroup> groups = virtualizationConfig.getConfigurableOptions();
		cboHardwareOptions = new ArrayList<JComboBox<AbstractConfigurableOption>>(groups.size());

		GridManager grid = new GridManager(getContentPane(), 2, true, new Insets(2, 2, 2, 2));

		/* 
		 * extra JPanel for the Warning message
		 */
		JPanel pnlWarning = new JPanel();
		pnlWarning.setBorder(BorderFactory.createTitledBorder(
				I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.TitledBorder.pnlWarning.title")));
		pnlWarning.setLayout(new BorderLayout());
		pnlWarning.add(new WordWrapLabel(
				I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Label.pnlWarning.text")),
				BorderLayout.CENTER);

		ComboBoxRenderer<AbstractConfigurableOption> renderer = new ComboBoxRenderer<AbstractConfigurableOption>() {
			@Override
			public String renderItem(AbstractConfigurableOption item) {
				if (item == null)
					return "???";
				return item.getDisplayName();
			}
		};

		pnlEditor = new JEditorPane("text/plain", null);
		pnlScrollPane = new JScrollPane(pnlEditor, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);

		grid.add(pnlWarning, 2).fill(true, false).expand(true, false);
		grid.nextRow();

		for (ConfigurableOptionGroup group : groups) {
			ComboBox<AbstractConfigurableOption> box = new ComboBox<AbstractConfigurableOption>(renderer, AbstractConfigurableOption.class);
			for (AbstractConfigurableOption option : group.availableOptions) {
				box.addItem(option);
			}
			box.setSelectedItem(group.getSelected());
			cboHardwareOptions.add(box);
			grid.add(new JLabel(I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Label."
					+ group.groupIdentifier.i18n + ".text")))
					.fill(true, false).expand(false, false);
			grid.add(box).fill(true, false).expand(true, false);
			grid.nextRow();
		}

		grid.add(Box.createVerticalGlue(), 2).expand(true, true);
		grid.nextRow();

		JPanel buttonPane = new JPanel();
		buttonPane.setLayout(new BoxLayout(buttonPane, BoxLayout.LINE_AXIS));
		buttonPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		buttonPane.add(Box.createHorizontalGlue());
		btnMore = new JButton(I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Button.more.text"));
		buttonPane.add(btnMore);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		btnCancel = new JButton(I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Button.cancel.text"));
		buttonPane.add(btnCancel);
		buttonPane.add(Box.createRigidArea(new Dimension(10, 0)));
		btnSave = new JButton(I18n.WINDOW_LAYOUT.getString("VirtDropDownConfigEditor.Button.save.text"));
		buttonPane.add(btnSave);
		grid.add(buttonPane, 2).fill(true, false).expand(true, false);
		grid.finish(false);

		setPreferredSize(Gui.getScaledDimension(600, 400));
		setMinimumSize(Gui.getScaledDimension(450, 375));
		if (modalParent != null) {
			Gui.centerShellOverShell(modalParent, this);
		}

		// listener for the Virtual Ethernet device drop box
		ActionListener hwListener = new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				AbstractConfigurableOption selected = (AbstractConfigurableOption) ((JComboBox<AbstractConfigurableOption>) e.getSource()).getSelectedItem();
				selected.apply();
				btnSave.setEnabled(hasChanged());
			}
		};
		for (JComboBox<AbstractConfigurableOption> box : cboHardwareOptions) {
			box.addActionListener(hwListener);
		}
		
		// 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);
	}

	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;
		try {
			win = new VirtDropDownConfigEditorWindow(modalParent, handler,
					imageVersionId, machineDescription);
		} catch (IOException | VirtualizationConfigurationException e) {
			Gui.showMessageBox(I18n.WINDOW.getString("VirtDropDownConfigEditor.Message.error.loadMachineConfig"),
					MessageType.WARNING, LOGGER, e);
			return;
		}
		win.setVisible(true);
	}

	private boolean hasChanged() {
		return originalMachineDescription != null && !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();
	}
}