summaryrefslogtreecommitdiffstats
path: root/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control
diff options
context:
space:
mode:
authorJonathan Bauer2016-09-07 13:22:01 +0200
committerJonathan Bauer2016-09-07 13:22:01 +0200
commitb23d9c81520116633e40d1e445f578ba4633c1e1 (patch)
tree4cd2532c264d57822d6812869ccaab904d85dc77 /dozentenmodul/src/main/java/org/openslx/dozmod/gui/control
parent[client] make image details editable only when working with local images (diff)
downloadtutor-module-b23d9c81520116633e40d1e445f578ba4633c1e1.tar.gz
tutor-module-b23d9c81520116633e40d1e445f578ba4633c1e1.tar.xz
tutor-module-b23d9c81520116633e40d1e445f578ba4633c1e1.zip
[client] added editable interpreter dropdown
Diffstat (limited to 'dozentenmodul/src/main/java/org/openslx/dozmod/gui/control')
-rw-r--r--dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java149
1 files changed, 124 insertions, 25 deletions
diff --git a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java
index b4c375d2..6cccb136 100644
--- a/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java
+++ b/dozentenmodul/src/main/java/org/openslx/dozmod/gui/control/RunscriptConfigurator.java
@@ -1,15 +1,26 @@
package org.openslx.dozmod.gui.control;
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.BorderFactory;
+import javax.swing.DefaultComboBoxModel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.event.EventListenerList;
+import org.apache.log4j.Logger;
+import org.openslx.dozmod.gui.control.ComboBox.ComboBoxRenderer;
+import org.openslx.dozmod.gui.control.RunscriptConfigurator.RunscriptType;
import org.openslx.dozmod.gui.helper.GridManager;
import org.openslx.dozmod.gui.helper.TextChangeListener;
@@ -20,7 +31,25 @@ import org.openslx.dozmod.gui.helper.TextChangeListener;
public class RunscriptConfigurator extends RunscriptConfiguratorLayout {
private static final long serialVersionUID = -3497629601818983994L;
- private String originalRunScript = null;
+ private static final Logger LOGGER = Logger
+ .getLogger(RunscriptConfigurator.class);
+
+ 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 RunscriptConfigurator() {
super();
@@ -33,21 +62,47 @@ public class RunscriptConfigurator extends RunscriptConfiguratorLayout {
}
};
taRunScript.getDocument().addDocumentListener(docListener);
-
- }
-
- public boolean hasChanged() {
- return !originalRunScript.equalsIgnoreCase(taRunScript.getText());
+ 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()));
+ }
+ });
}
/**
- * Gets the runscript as String.
+ * Gets the runscript as String. The chosen interpreter will get encoded as
+ * the first line of the script.
*
- * @return runscript as String. If not text was entered, returns a empty 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 : "";
+ if (input == null)
+ return "";
+
+ // 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 interpreter = "";
+ Object editorContent = cboRunscriptType.getEditor().getItem();
+ if (editorContent instanceof RunscriptType)
+ interpreter = ((RunscriptType) editorContent).extension;
+ else if (input instanceof String)
+ interpreter = (String) editorContent;
+ else
+ return "";
+
+ return "ext=" + interpreter + "\n" + input;
}
/**
@@ -59,10 +114,40 @@ public class RunscriptConfigurator extends RunscriptConfiguratorLayout {
* AdvancedConfiguration to set the state to
*/
public void setState(final String config) {
- this.taRunScript.setText(config != null ? config : "" );
+ String shebang = null;
+ if (config == null) {
+ cboRunscriptType.setSelectedItem(null);
+ taRunScript.setText("");
+ }
+ try {
+ BufferedReader reader = new BufferedReader(new StringReader(config));
+ shebang = reader.readLine();
+ reader.close();
+ } catch (IOException e) {
+ // swallow ...
+ }
+ if (shebang != null) {
+ // we should have following format: ext=<interpreter>
+ // e.g. ext=sh
+ shebang = shebang.replace("ext=", "");
+ for (RunscriptType type : RunscriptType.values()) {
+ if (type.extension.equals(shebang)) {
+ cboRunscriptType.setSelectedItem(type);
+ // mark that we found it by nulling the shebang...
+ shebang = null;
+ continue;
+ }
+ }
+ if (shebang != null) {
+ // user specific shebang, so just write the text to the cbo
+ cboRunscriptType.getEditor().setItem(shebang);
+ }
+ }
+ // finished with the interpreter, remove that line from the given config
+ // before setting that text
+ taRunScript.setText(config.replaceFirst("^ext=.*\n", ""));
}
-
/**
* Custom event mechanism to detect changes to the user list (Mostly needed
* for the reactToChange() stuff in LectureDetailsWindow)
@@ -116,35 +201,49 @@ 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 static String txtRunScriptDesc = "Ein hier eingetragenes Skript wird nach dem Start dieser VM automatisch mit dem ausgewählten Interpreter ausgeführt.";
- private final JPanel pnlRunScript;
protected final JTextArea taRunScript;
+ protected final ComboBox<RunscriptType> cboRunscriptType;
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));
+ JPanel pnlRunScript = new JPanel();
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))
+
+ GridManager gridRunScript = new GridManager(pnlRunScript, 2, true,
+ new Insets(2, 2, 2, 2));
+ taRunScript.setLineWrap(true);
+ taRunScript.setWrapStyleWord(true);
+ gridRunScript.add(new WordWrapLabel(txtRunScriptDesc, false, true), 2)
.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);
+ 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);
+ gridRunScript.add(new QLabel("Interpreter: ")).fill(false, false)
+ .expand(false, false);
+ gridRunScript.add(cboRunscriptType).fill(true, false)
+ .expand(true, false);
+ gridRunScript.nextRow();
+ gridRunScript.add(scpRunScript, 2).fill(true, true).expand(true, true);
gridRunScript.finish(false);
// build the final grid