summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/wizard/page/ImageMetaDataPage.java
blob: 7314d5e3850764029e94cc4f61350858777328f2 (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
package org.openslx.dozmod.gui.wizard.page;

import org.openslx.bwlp.thrift.iface.OperatingSystem;
import org.openslx.bwlp.thrift.iface.ShareMode;
import org.openslx.dozmod.gui.Gui;
import org.openslx.dozmod.gui.helper.I18n;
import org.openslx.dozmod.gui.helper.TextChangeListener;
import org.openslx.dozmod.gui.wizard.Wizard;
import org.openslx.dozmod.gui.wizard.layout.ImageMetaDataPageLayout;
import org.openslx.dozmod.state.UploadWizardState;
import org.openslx.dozmod.thrift.Session;
import org.openslx.dozmod.thrift.cache.MetaDataCache;
import org.openslx.thrifthelper.Comparators;
import org.openslx.util.QuickTimer;
import org.openslx.util.QuickTimer.Task;
import org.openslx.virtualization.configuration.VirtualizationConfigurationDocker;
import org.openslx.virtualization.configuration.VirtualizationConfigurationQemu;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.Collections;
import java.util.List;

/**
 * Page for setting the details of an image.
 */
public class ImageMetaDataPage extends ImageMetaDataPageLayout {

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

	private UploadWizardState state;

	public ImageMetaDataPage(Wizard wizard, UploadWizardState uploadWizardState) {
		super(wizard);
		this.state = uploadWizardState;
		setPageComplete(false);
		// HACK set fixed uploadWizardState to test functions
		uploadWizardState.shareMode = ShareMode.LOCAL;
		chkIsTemplate.setEnabled(Session.isSuperUser());
		// fetch the OS list
		QuickTimer.scheduleOnce(new Task() {
			List<OperatingSystem> osList = null;

			@Override public void fire() {
				osList = MetaDataCache.getOperatingSystems();
				// now send the organizations back to the LoginWindow
				// through populateIdpCombo()
				Gui.asyncExec(new Runnable() {
					@Override public void run() {
						fillOsCombo(osList);
					}
				});
			}
		});

		cboOperatingSystem.addItemListener(new ItemListener() {
			@Override public void itemStateChanged(ItemEvent e) {
				if (e.getStateChange() == ItemEvent.SELECTED) {
					reactToUserInput();
				}
			}
		});

		txtDescription.getDocument().addDocumentListener(new TextChangeListener() {
			@Override public void changed() {
				reactToUserInput();
			}
		});
	}

	@Override protected void onPageEnter() {
		// Preselect OS if possible
		if (state.detectedOs != null) {
			cboOperatingSystem.setSelectedItem(state.detectedOs);
		} else if (state.selectedOs == null) {
			cboOperatingSystem.setSelectedItem(null);
		}
		sCommandCaption.setVisible(false);
		startCommandPane.setVisible(false);
		chkIsTemplate.setSelected(state.isTemplate);
		chkLicenseRestricted.setSelected(state.isRestricted);
		reactToUserInput();
	}

	@Override protected boolean wantNextOrFinish() {
		state.selectedOs = (OperatingSystem) cboOperatingSystem.getSelectedItem();
		state.isTemplate = chkIsTemplate.isSelected();
		state.isRestricted = chkLicenseRestricted.isSelected();

		return state.selectedOs != null && state.description != null;
	}

	/**
	 * @param osList list of OS's to fill the combo with
	 */
	private void fillOsCombo(List<OperatingSystem> osList) {
		Collections.sort(osList, Comparators.operatingSystemByName);
		for (OperatingSystem os : osList) {
			cboOperatingSystem.addItem(os);
		}
	}

	/**
	 * Called by event listeners. This will set guidance message or error message
	 * and call setPageComplete(bool) accordingly.
	 */
	private void reactToUserInput() {
		if (cboOperatingSystem.getSelectedIndex() == -1) {
			// OS empty, description input present
			setWarningMessage(I18n.PAGE.getString("ImageMetaData.WizardPage.warningMessage.noOS"));
			setPageComplete(false);
			return;
		}

		if (state.virtualizationConfig instanceof VirtualizationConfigurationQemu) {
			sCommandCaption.setVisible(true);
			startCommandPane.setVisible(true);
		}

		if (state.virtualizationConfig instanceof VirtualizationConfigurationDocker) {
			// TODO we need Information about a OS in Container? Currently use "Other (32 Bit)" as default
			lblOperatingSystem.setVisible(false);
			cboOperatingSystem.setVisible(false);

			// TODO do we need to check license restrictions in container?
			chkLicenseRestricted.setVisible(false);
			// TODO currently no Container Template!
			chkIsTemplate.setVisible(false);
		}

		// evaluate description field
		state.description = txtDescription.getText();
		if (state.description == null || state.description.isEmpty()) {
			// OS set, no description
			setWarningMessage(I18n.PAGE.getString("ImageMetaData.WizardPage.warningMessage.noDescription"));
			setPageComplete(false);
			return;
		}
		setDescription(I18n.PAGE.getString("ImageMetaData.WizardPage.description"));
		setPageComplete(true);
	}
}