summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java
blob: b4c375d2a104121b9d8f4d27d6661d0ba46331ab (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
package org.openslx.dozmod.gui.control;

import java.awt.Insets;
import java.util.EventListener;
import java.util.EventObject;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.EventListenerList;

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;
	private String originalRunScript = null;

	public RunscriptConfigurator() {
		super();

		final TextChangeListener docListener = new TextChangeListener() {
			@Override
			public void changed() {
				fireRunscriptConfigurationChangeEvent(new RunscriptConfigurationChangeEvent(
						new Object()));
			}
		};
		taRunScript.getDocument().addDocumentListener(docListener);

	}

	public boolean hasChanged() {
		return !originalRunScript.equalsIgnoreCase(taRunScript.getText());
	}

	/**
	 * Gets the runscript as String.
	 * 
	 * @return runscript as String. If not text was entered, returns a empty string.
	 */
	public String getState() {
		String input = taRunScript.getText();
		return input != null ? input : "";
	}

	/**
	 * 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) {
		this.taRunScript.setText(config != null ? config : "" );
	}


	/**
	 * Custom event mechanism to detect changes to the user list (Mostly needed
	 * for the reactToChange() stuff in LectureDetailsWindow)
	 */
	protected EventListenerList listenerList = new EventListenerList();

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

/**
 * 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 txtRunScriptTitle = "Startskript";
	private final static String txtRunScriptDesc = "Ein hier eingetragenes Skript wird nach dem Start dieser VM automatisch ausgeführt.";

	private final JPanel pnlRunScript;
	protected final JTextArea taRunScript;

	public RunscriptConfiguratorLayout() {

		GridManager grid = new GridManager(this, 1, true,
				new Insets(5, 5, 5, 5));

		// middle panel for the run script textpane
		pnlRunScript = new JPanel();
		GridManager gridRunScript = new GridManager(pnlRunScript, 1, true,
				new Insets(2, 2, 2, 2));
		taRunScript = new JTextArea("", 5, 20);
		taRunScript.setLineWrap(true);
		taRunScript.setWrapStyleWord(true);
		JScrollPane scpRunScript = new JScrollPane(taRunScript,
				JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED,
				JScrollPane.HORIZONTAL_SCROLLBAR_NEVER);
		pnlRunScript.setBorder(BorderFactory
				.createTitledBorder(txtRunScriptTitle));
		gridRunScript
				.add(new WordWrapLabel(txtRunScriptDesc, false, true))
				.fill(true, false).expand(true, false);
		gridRunScript.nextRow();
		// TODO add checkbox for script's interpreter type: sh, bash, powershell...
		
		gridRunScript.add(scpRunScript).fill(true, true).expand(true, true);
		gridRunScript.finish(false);

		// build the final grid
		grid.add(pnlRunScript).fill(true, true).expand(true, true);
		grid.finish(false);
	}
}