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 state of the widget. This will first try to parse the
* tpNetworkRules and taRunScript and build the corresponding
* AdvancedConfiguration Object returned.
*
* @return advanced configuration object composed of the parsed network
* rules as List<NetRule> and the raw runscript text as String
* @see org.openslx.dozmod.gui.control.AdvancedConfigurator.AdvancedConfiguration
*/
public RunscriptConfiguration getState() {
// cleanup the TextPane for network rules if needed
String runscriptString = taRunScript.getText();
if (runscriptString != null) {
return new RunscriptConfiguration(runscriptString);
}
return null;
}
/**
* 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 RunscriptConfiguration config) {
// setText() blanks the text area if null is given, so no null checks
originalRunScript = config.runScriptText != null ? config.runScriptText
: "";
this.taRunScript.setText(originalRunScript);
}
/**
* Wrapper class for the advanced configuration information needed since we
* need to return a single object from the runAndReturn routine. This class
* has two members: the list of NetRule(s) (as List<NetRule>) and the
* runScriptText (as String)
*/
public static class RunscriptConfiguration {
public String runScriptText;
public RunscriptConfiguration(String runScriptText) {
this.runScriptText = runScriptText;
}
}
/**
* 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();
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);
}
}