Skip to content

Commit

Permalink
Make "Generic information for all REDUCE commands" fields all optiona…
Browse files Browse the repository at this point in the history
…l, resetting to defaults where appropriate. Correct behaviour to allow the reduceRootDir TextField to be empty, and to use the commandRootDir if it is set.
  • Loading branch information
fjwright committed Oct 21, 2020
1 parent 24c7805 commit 6f5066e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 22 deletions.
10 changes: 5 additions & 5 deletions src/fjwright/runreduce/REDUCEConfigDialog.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
<Label style="-fx-font-weight: bold;" text="Generic information for all REDUCE commands:"
GridPane.columnSpan="2147483647"/>
<Label text="REDUCE Root Directory" GridPane.rowIndex="1"/>
<TextField fx:id="reduceRootDirTextField" GridPane.columnIndex="1" GridPane.rowIndex="1">
<TextField fx:id="reduceRootDirTextField" promptText="Optional" GridPane.columnIndex="1" GridPane.rowIndex="1">
<tooltip>
<Tooltip
text="As an optional convenience, specify a root directory path&#10;that provides a default for all command root directories."/>
Expand All @@ -54,23 +54,23 @@
<Button mnemonicParsing="false" onAction="#reduceRootDirDCButtonAction" text="..." GridPane.columnIndex="2"
GridPane.rowIndex="1"/>
<Label text="REDUCE Packages Directory" GridPane.rowIndex="2"/>
<TextField fx:id="packagesDirTextField" GridPane.columnIndex="1" GridPane.rowIndex="2">
<TextField fx:id="packagesDirTextField" promptText="Optional" GridPane.columnIndex="1" GridPane.rowIndex="2">
<tooltip>
<Tooltip text="A directory containing the standard REDUCE packages: alg, algint, arith, etc."/>
</tooltip>
</TextField>
<Button mnemonicParsing="false" onAction="#packagesRootDirDCButtonAction" text="..." GridPane.columnIndex="2"
GridPane.rowIndex="2"/>
<Label text="REDUCE Manual Directory" GridPane.rowIndex="3"/>
<TextField fx:id="manualDirTextField" GridPane.columnIndex="1" GridPane.rowIndex="3">
<TextField fx:id="manualDirTextField" promptText="Optional" GridPane.columnIndex="1" GridPane.rowIndex="3">
<tooltip>
<Tooltip text="A directory containing the REDUCE User's Manual."/>
</tooltip>
</TextField>
<Button mnemonicParsing="false" onAction="#manualDirDCButtonAction" text="..." GridPane.columnIndex="2"
GridPane.rowIndex="3"/>
<Label text="REDUCE Primers Directory" GridPane.rowIndex="4"/>
<TextField fx:id="primersDirTextField" GridPane.columnIndex="1" GridPane.rowIndex="4">
<TextField fx:id="primersDirTextField" promptText="Optional" GridPane.columnIndex="1" GridPane.rowIndex="4">
<tooltip>
<Tooltip
text="A directory containing Inside Reduce, the REDUCE Symbolic Mode Primer, and the Standard Lisp Report."/>
Expand All @@ -79,7 +79,7 @@
<Button mnemonicParsing="false" onAction="#primersDirDCButtonAction" text="..." GridPane.columnIndex="2"
GridPane.rowIndex="4"/>
<Label text="REDUCE Working Directory" GridPane.rowIndex="5"/>
<TextField fx:id="workingDirTextField" GridPane.columnIndex="1" GridPane.rowIndex="5">
<TextField fx:id="workingDirTextField" promptText="Optional" GridPane.columnIndex="1" GridPane.rowIndex="5">
<tooltip>
<Tooltip text="The initial working directory for REDUCE and the file choosers used by the File menu."/>
</tooltip>
Expand Down
42 changes: 25 additions & 17 deletions src/fjwright/runreduce/REDUCEConfigDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,17 +183,24 @@ private void showREDUCECommand(REDUCECommand cmd) {
private void saveButtonAction(ActionEvent actionEvent) {
// Write form data back to REDUCEConfiguration
// after validating directory and file fields:
String s;
try {
// Optional:
RunREDUCE.reduceConfiguration.reduceRootDir =
directoryReadableCheck(reduceRootDirTextField.getText());
directoryTextFieldReadableCheck(reduceRootDirTextField);
// All required:
s = directoryTextFieldReadableCheck(packagesDirTextField);
RunREDUCE.reduceConfiguration.packagesDir =
directoryReadableCheck(packagesDirTextField.getText());
s.isEmpty() ? RunREDUCE.reduceConfigurationDefault.packagesDir : s;
s = directoryTextFieldReadableCheck(manualDirTextField);
RunREDUCE.reduceConfiguration.manualDir =
directoryReadableCheck(manualDirTextField.getText());
s.isEmpty() ? RunREDUCE.reduceConfigurationDefault.manualDir : s;
s = directoryTextFieldReadableCheck(primersDirTextField);
RunREDUCE.reduceConfiguration.primersDir =
directoryReadableCheck(primersDirTextField.getText());
s.isEmpty() ? RunREDUCE.reduceConfigurationDefault.primersDir : s;
s = directoryTextFieldReadableCheck(workingDirTextField);
RunREDUCE.reduceConfiguration.workingDir =
directoryReadableCheck(workingDirTextField.getText());
s.isEmpty() ? RunREDUCE.reduceConfigurationDefault.workingDir : s;
saveREDUCECommand(listView.getSelectionModel().getSelectedItem());
RunREDUCE.reduceConfiguration.reduceCommandList = reduceCommandList;
RunREDUCE.reduceConfiguration.save();
Expand All @@ -209,8 +216,9 @@ private void saveButtonAction(ActionEvent actionEvent) {
/**
* Check that dir is a readable directory and if not throw an exception.
*/
private String directoryReadableCheck(String dir) throws FileNotFoundException {
if (new File(dir).canRead()) return dir;
private String directoryTextFieldReadableCheck(TextField dirTextField) throws FileNotFoundException {
String dir = dirTextField.getText().trim();
if (dir.isEmpty() || new File(dir).canRead()) return dir;
else {
RunREDUCE.alert(Alert.AlertType.ERROR, "Invalid Directory",
"The directory\n" + dir + "\ndoes not exist or is not accessible.");
Expand All @@ -222,12 +230,12 @@ private String directoryReadableCheck(String dir) throws FileNotFoundException {
* Check that fileOrDir is a readable file or directory if it begins with $REDUCE or alwaysCheck == true.
* If not throw an exception.
*/
private String fileOrDirReadableCheck(String fileOrDir, boolean alwaysCheck) throws FileNotFoundException {
String localFileOrDir;
private String fileOrDirTextFieldReadableCheck(TextField fileOrDirTextField, String commandRootDir,
boolean alwaysCheck) throws FileNotFoundException {
String localFileOrDir, fileOrDir = fileOrDirTextField.getText().trim();
// Replace $REDUCE/ or $REDUCE\ in local copy of fileOrDir:
if (fileOrDir.startsWith("$REDUCE")) {
localFileOrDir = Paths.get(RunREDUCE.reduceConfiguration.reduceRootDir).
resolve(fileOrDir.substring(8)).toString();
localFileOrDir = Paths.get(commandRootDir).resolve(fileOrDir.substring(8)).toString();
alwaysCheck = true;
} else localFileOrDir = fileOrDir;
if (alwaysCheck && !new File(localFileOrDir).canRead()) {
Expand All @@ -251,15 +259,15 @@ private void saveREDUCECommand(String commandName) throws FileNotFoundException
break;
}
if (cmd == null) return; // Report an error?
cmd.rootDir = commandRootDirTextField.getText().trim();
// Do not check or save blank arguments:
String field = commandRootDirTextField.getText().trim();
if (!field.isEmpty()) cmd.rootDir = directoryReadableCheck(field);
cmd.rootDir = directoryTextFieldReadableCheck(commandRootDirTextField);
String commandRootDir =
cmd.rootDir.isEmpty() ? reduceRootDirTextField.getText().trim() : cmd.rootDir;
// Must replace the whole command array because its length may have changed:
List<String> commandList = new ArrayList<>();
for (int i = 0; i < commandTextFieldArray.length; i++) {
String element = commandTextFieldArray[i].getText().trim();
if (!element.isEmpty()) commandList.add(fileOrDirReadableCheck(element, i == 0));
String element = fileOrDirTextFieldReadableCheck(
commandTextFieldArray[i], commandRootDir, i == 0);
if (!element.isEmpty()) commandList.add(element);
}
cmd.command = commandList.toArray(new String[0]);
}
Expand Down

0 comments on commit 6f5066e

Please sign in to comment.