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

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

import javax.swing.Action;
import javax.swing.text.BadLocationException;
import javax.swing.text.StyledEditorKit;
import javax.swing.text.html.HTML;
import javax.swing.text.html.HTMLDocument;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.io.IOException;

import org.apache.log4j.Logger;
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.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.util.vm.QemuMetaData;

/**
 * Page for setting the details of an image.
 */
@SuppressWarnings("serial")
public class ImageMetaDataPage extends ImageMetaDataPageLayout {

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

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

		cbTxtSize.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int size = Integer.parseInt((String) cbTxtSize.getSelectedItem());
				Action act = new StyledEditorKit.FontSizeAction(String.valueOf(size), size);
				act.actionPerformed(new ActionEvent(act, ActionEvent.ACTION_PERFORMED,
						(String) act.getValue(Action.ACTION_COMMAND_KEY)));
			}
		});

		cbTxtColor.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String color = (String) cbTxtColor.getSelectedItem();
				Action act = null;

				switch (color) {
					case "Black":
						act = new StyledEditorKit.ForegroundAction("Black", Color.black);
						break;
					case "Blue":
						act = new StyledEditorKit.ForegroundAction("Blue", Color.blue);
						break;
					case "Yellow":
						act = new StyledEditorKit.ForegroundAction("Yellow", Color.yellow);
						break;
					case "Red":
						act = new StyledEditorKit.ForegroundAction("Red", Color.red);
						break;
					case "Green":
						act = new StyledEditorKit.ForegroundAction("Green", Color.green);
						break;
				}

				act.actionPerformed(new ActionEvent(act, ActionEvent.ACTION_PERFORMED,
						(String) act.getValue(Action.ACTION_COMMAND_KEY)));
			}
		});

		btnWysiwyg.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				String tmp = txtDescription.getText();
				if (txtDescription.getContentType().equals("text/html")) {
					txtDescription.setContentType("text/plain");
					txtDescription.setText(tmp);
					btnWysiwyg.setText("Wysiwyg");

					btnBold.setEnabled(false);
					btnUnderline.setEnabled(false);
					btnItalic.setEnabled(false);
					cbTxtColor.setEnabled(false);
					cbTxtSize.setEnabled(false);
				} else {
					txtDescription.setContentType("text/html");
					txtDescription.setText(tmp);
					btnWysiwyg.setText("Html");

					btnBold.setEnabled(true);
					btnUnderline.setEnabled(true);
					btnItalic.setEnabled(true);
					cbTxtColor.setEnabled(true);
					cbTxtSize.setEnabled(true);
				}
			}
		});

		txtDescription.addKeyListener(new KeyListener() {
			@Override
			public void keyPressed(KeyEvent e) {
			}

			@Override
			public void keyTyped(KeyEvent e) {
			}

			@Override
			public void keyReleased(KeyEvent e) {
				if (e.getKeyCode() == KeyEvent.VK_ENTER && txtDescription.getContentType().equals("text/html")) {
					try {
						kit.insertHTML((HTMLDocument) txtDescription.getDocument(), txtDescription.getCaretPosition(),
								"<br>", 0, 0, HTML.Tag.BR);
						txtDescription.setCaretPosition(txtDescription.getCaretPosition()); // This moves caret to next
																							// line
					} catch (BadLocationException | IOException ex) {
						ex.printStackTrace();
					}
				}
			}
		});
	}


	@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("Wählen Sie das Betriebssystem aus.");
			setPageComplete(false);
			return;
		}

		if (state.meta instanceof QemuMetaData) {
			sCommandCaption.setVisible(true);
			startCommandPane.setVisible(true);
		}

		// evaluate description field
		state.description = txtDescription.getText();
		if (state.description == null || state.description.isEmpty()) {
			// OS set, no description
			setWarningMessage("Fügen Sie eine Beschreibung hinzu.");
			setPageComplete(false);
			return;
		}
		setDescription("Klicken Sie auf 'Weiter' um Berechtigungen festzulegen oder 'Fertigstellen'");
		setPageComplete(true);
	}
}