summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/configurator/RunscriptConfigurator.java
blob: 5323a2156a1f4fb615404401095e5a22a1520b63 (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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package org.openslx.dozmod.gui.configurator;

import java.awt.Color;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.EventListener;
import java.util.EventObject;

import javax.swing.Box;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import org.openslx.dozmod.gui.changemonitor.DialogChangeMonitor;
import org.openslx.dozmod.gui.configurator.RunscriptConfigurator.RunscriptType;
import org.openslx.dozmod.gui.configurator.RunscriptConfigurator.RunscriptVisibility;
import org.openslx.dozmod.gui.control.ComboBox;
import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
import org.openslx.dozmod.gui.control.QLabel;
import org.openslx.dozmod.gui.control.WordWrapLabel;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.gui.helper.TextChangeListener;

/**
 * Widget for advanced configuration options for lectures. This handles
 * following options - Network rules - Runscript - USB
 */
public class RunscriptConfigurator extends RunscriptConfiguratorLayout {

	private static final long serialVersionUID = -3497629601818983994L;

	public static enum RunscriptType {
		SHELL("Shellskript", "sh"), BATCH("Windows-Batch", "bat");

		private final String displayName;
		private final String extension;

		private RunscriptType(String name, String extension) {
			this.displayName = name;
			this.extension = extension;
		}

		@Override
		public String toString() {
			return extension + " (" + displayName + ")";
		}
	}

	public static enum RunscriptVisibility {
		NORMAL("Normal", 1), MINIMIZED("Minimiert", 2), HIDDEN("Versteckt", 0);

		private final String displayName;
		private final int flag;

		private RunscriptVisibility(String name, int flag) {
			this.displayName = name;
			this.flag = flag;
		}

		@Override
		public String toString() {
			return displayName;
		}
	}

	public RunscriptConfigurator() {
		super();

		final TextChangeListener docListener = new TextChangeListener() {
			@Override
			public void changed() {
				fireRunscriptConfigurationChangeEvent(new RunscriptConfigurationChangeEvent(
						new Object()));
			}
		};
		taRunScript.getDocument().addDocumentListener(docListener);
		cboRunscriptType.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				fireRunscriptConfigurationChangeEvent(new RunscriptConfigurationChangeEvent(
						new Object()));
			}
		});
		cboRunscriptType.getEditor().getEditorComponent().addKeyListener(new KeyAdapter() {
			@Override
			public void keyReleased(KeyEvent e) {
				fireRunscriptConfigurationChangeEvent(new RunscriptConfigurationChangeEvent(
						new Object()));
			}
		});
	}

	private void setError(final String msg) {
		lblError.setText(msg);
	}
	/**
	 * Gets the runscript as String. The chosen interpreter and visibility flag
	 * will get encoded in the first line of the script.
	 * 
	 * @return runscript as String. If no text was entered, returns a empty
	 *         string.
	 */
	public String getState() {
		setError(""); // fill remove any prior errors, we'll reset them if needed
		// handle user input, this is tricky since
		// * either an item has been selected -> editorContent will be of our enum type
		// * user typed its own interpreter into the box -> editorContent will be a castable String
		String extension = null;
		Object cboContent = cboRunscriptType.getEditor().getItem();
		if (cboContent instanceof RunscriptType) {
			extension = ((RunscriptType) cboContent).extension;
		} else if (cboContent instanceof String) {
			extension = (String) cboContent;
		}
		String taInputText = taRunScript.getText();
		if (taInputText.isEmpty())
			return "";
		if (extension == null || extension.isEmpty()) {
			// this should never happen, so return null to report this invalid state
			setError("Fehlende Dateinamenerweiterung!");
			return null;
		}

		RunscriptVisibility visibility = (RunscriptVisibility) cboRunscriptVisibility.getSelectedItem();
		if (visibility == null) {
			// this should never happen, so return null to report this invalid state
			setError("Fehlendes Sichtbarkeits-Flag!");
			return null;
		}

		setError("");
		return "ext=" + extension + ";" + "visibility=" + visibility.flag + "\n" + taInputText;
	}

	/**
	 * Sets the state of this widget to the given AdvancedConfiguration. Basicly
	 * this sets the content of the text areas to the corresponding network
	 * rules/runscript as given by the AdvancedConfiguration object
	 * 
	 * @param config
	 *            AdvancedConfiguration to set the state to
	 */
	public void setState(final String config) {
		if (config == null || config.isEmpty()) {
			cboRunscriptType.setSelectedItem(null);
			taRunScript.setText("");
			return;
		}
		String header = null;
		try {
			BufferedReader reader = new BufferedReader(new StringReader(config));
			header = reader.readLine();
			reader.close();
		} catch (IOException e) {
			// swallow ...
		}
		if (header != null) {
			// we should have following format: ext=<interpreter>;visibility=<flag>
			// e.g. ext=sh;scriptVisibility=0
			String[] options = header.split(";");
			String extension = null;
			for (String option : options) {
				if(option.startsWith("ext=")) {
					extension = option.replace("ext=", "");
					for (RunscriptType type : RunscriptType.values()) {
						if (type.extension.equals(extension)) {
							cboRunscriptType.setSelectedItem(type);
							// mark that we found it by nulling the shebang...
							extension = null;
							break;
						}
					}
				} else if (option.startsWith("visibility=")) {
					option = option.replace("visibility=", "");
					for (RunscriptVisibility windowFlag : RunscriptVisibility.values()) {
						if (windowFlag.flag == Integer.parseInt(option)) {
							cboRunscriptVisibility.setSelectedItem(windowFlag);
							break;
						}
					}
				}
			}

			if (extension != null) {
				// user specific shebang, so just write the text to the cbo
				cboRunscriptType.getEditor().setItem(extension);
			}
		}
		// finished with the interpreter, remove that line from the given config
		// before setting that text
		taRunScript.setText(config.replaceFirst(header + "\n", ""));
	}

	/**
	 * Custom event mechanism to detect changes to the user list (Mostly needed
	 * for the reactToChange() stuff in LectureDetailsWindow)
	 */
	public class RunscriptConfigurationChangeEvent extends EventObject {

		private static final long serialVersionUID = -8779550754760035845L;

		public RunscriptConfigurationChangeEvent(Object source) {
			super(source);
		}
	}

	public interface RunscriptConfigurationChangeEventListener extends
			EventListener {
		public void stateChanged(RunscriptConfigurationChangeEvent event);
	}

	public void addRunscriptConfigurationChangeEventListener(
			RunscriptConfigurationChangeEventListener listener) {
		listenerList.add(RunscriptConfigurationChangeEventListener.class,
				listener);
	}

	public void removeRunscriptConfigurationChangeEventListener(
			RunscriptConfigurationChangeEventListener listener) {
		listenerList.remove(RunscriptConfigurationChangeEventListener.class,
				listener);
	}

	void fireRunscriptConfigurationChangeEvent(
			RunscriptConfigurationChangeEvent evt) {
		Object[] listeners = listenerList.getListenerList();
		for (int i = 0; i < listeners.length; i++) {
			if (listeners[i] == RunscriptConfigurationChangeEventListener.class) {
				((RunscriptConfigurationChangeEventListener) listeners[i + 1])
						.stateChanged(evt);
			}
		}
	}

	public void addToChangeMonitor(DialogChangeMonitor changeMonitor) {
		changeMonitor.add(taRunScript);
		changeMonitor.addEditableCombo(cboRunscriptType, null);
		changeMonitor.addFixedCombo(cboRunscriptVisibility, null);
	}

}

/**
 * Internal layout class for the advanced configurator (to keep it clean even
 * for widgets)
 */
class RunscriptConfiguratorLayout extends JPanel {

	private static final long serialVersionUID = 648729071828404053L;

	private final static String txtRunScriptDesc = "Ein hier eingetragenes Skript wird nach dem Start dieser VM automatisch ausgeführt.";
	protected final QLabel lblError;
	protected final JTextArea taRunScript;
	protected final ComboBox<RunscriptType> cboRunscriptType;
	protected final ComboBox<RunscriptVisibility> cboRunscriptVisibility;

	public RunscriptConfiguratorLayout() {

		GridManager grid = new GridManager(this, 2, true, new Insets(5, 5, 5, 5));
		taRunScript = new JTextArea("", 5, 20);
		JScrollPane scpRunScript = new JScrollPane(taRunScript,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		taRunScript.setLineWrap(true);
		taRunScript.setWrapStyleWord(true);
		grid.add(new WordWrapLabel(txtRunScriptDesc, false, true), 2)
				.fill(true, false).expand(true, false);
		grid.nextRow();
		cboRunscriptType = new ComboBox<RunscriptType>(
				new ComboBoxRenderer<RunscriptType>() {
					@Override
					public String renderItem(RunscriptType item) {
						if (item == null)
							return null;
						return item.toString();
					}
				});
		cboRunscriptType.setModel(new DefaultComboBoxModel<RunscriptType>(
				RunscriptType.values()));
		cboRunscriptType.setEditable(true);
		grid.add(new QLabel("Dateinamenserweiterung: ")).fill(false, false)
				.expand(false, false);
		grid.add(cboRunscriptType).fill(true, false)
				.expand(true, false);
		grid.nextRow();
		cboRunscriptVisibility = new ComboBox<RunscriptVisibility>(new ComboBoxRenderer<RunscriptVisibility>() {
			@Override
			public String renderItem(RunscriptVisibility item) {
				if (item == null)
					return null;
				return item.toString();
			}
		});
		cboRunscriptVisibility.setModel(new DefaultComboBoxModel<RunscriptVisibility>(RunscriptVisibility.values()));
		grid.add(new QLabel("Sichtbarkeit: ")).fill(false, false)
				.expand(false, false);
		grid.add(cboRunscriptVisibility).fill(true, false)
				.expand(true, false);
		grid.nextRow();
		grid.add(scpRunScript, 2).fill(true, true).expand(true, true);
		grid.nextRow();
		lblError = new QLabel("");
		lblError.setForeground(Color.RED);
		JPanel pnlError = new JPanel();
		pnlError.add(Box.createGlue());
		pnlError.add(lblError);
		pnlError.add(Box.createGlue());
		grid.add(pnlError, 2).fill(true, false).expand(true, false);
		grid.finish(false);

	}
}