diff --git a/src/main/java/com/neuronrobotics/bowlerstudio/BowlerStudioController.java b/src/main/java/com/neuronrobotics/bowlerstudio/BowlerStudioController.java index 92cc90db9..6d7bc1202 100644 --- a/src/main/java/com/neuronrobotics/bowlerstudio/BowlerStudioController.java +++ b/src/main/java/com/neuronrobotics/bowlerstudio/BowlerStudioController.java @@ -158,6 +158,8 @@ public void run() { }.start(); String key = t.getScripting().getGitRepo() + ":" + t.getScripting().getGitFile(); + if(key.length()==1) + throw new RuntimeException("Failed to create a file key"); ArrayList files = new ArrayList<>(); files.add(t.getScripting().getGitRepo()); files.add(t.getScripting().getGitFile()); @@ -174,12 +176,15 @@ public void run() { addTab(fileTab, true); widgets.put(file.getAbsolutePath(), t); + System.err.println("Open Tab " + file.getAbsolutePath()); + fileTab.setOnCloseRequest(event -> { + widgets.remove(file.getAbsolutePath()); openFiles.remove(file.getAbsolutePath()); ConfigurationDatabase.removeObject("studio-open-git", key); t.getScripting().close(); - com.neuronrobotics.sdk.common.Log.error("Closing " + file.getAbsolutePath()); + System.err.println("Closing " + file.getAbsolutePath()); }); FileChangeWatcher watcher = FileChangeWatcher.watch(file); watcher.addIFileChangeListener(new IFileChangeListener() { diff --git a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/ScriptingFileWidget.java b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/ScriptingFileWidget.java index 8d6a9774e..939f7816e 100644 --- a/src/main/java/com/neuronrobotics/bowlerstudio/scripting/ScriptingFileWidget.java +++ b/src/main/java/com/neuronrobotics/bowlerstudio/scripting/ScriptingFileWidget.java @@ -1,4 +1,5 @@ package com.neuronrobotics.bowlerstudio.scripting; + import javafx.scene.Node; import com.neuronrobotics.bowlerstudio.BowlerStudio; import com.neuronrobotics.bowlerstudio.BowlerStudioController; @@ -56,6 +57,7 @@ import javafx.scene.control.Alert; import javafx.scene.control.ButtonType; import java.util.concurrent.CompletableFuture; + @SuppressWarnings("unused") public class ScriptingFileWidget extends BorderPane implements IFileChangeListener { @@ -73,17 +75,16 @@ public class ScriptingFileWidget extends BorderPane implements IFileChangeListen private Button publish = new Button("Save"); private Button convert = new Button("Convert..."); - private CheckBox autoRun = new CheckBox(); private String addr; boolean loadGist = false; - private PrintBedManager manager=null; + private PrintBedManager manager = null; private ScriptingWidgetType type; - final TextField fileListBox = new TextField(); + final TextField gitField = new TextField(); final TextField fileNameBox = new TextField(); private File currentFile = null; ExternalEditorController externalEditorController; @@ -97,11 +98,12 @@ public class ScriptingFileWidget extends BorderPane implements IFileChangeListen private boolean isArrange = false; private Button printbed; - private FileChangeWatcher watch;; + private FileChangeWatcher watch; + private String findLocalPath;; + public ScriptingFileWidget(File currentFile) throws IOException { load(ScriptingWidgetType.FILE, currentFile); - } private void startStopAction() { @@ -139,17 +141,17 @@ private void load(ScriptingWidgetType type, File currentFile) { }); publish.setTooltip(new Tooltip("Save this code to Git")); - + convert.setOnAction(e -> { launchConvert(currentFile); }); - convert.setTooltip(new Tooltip("Convert the output of this file to a different file type. For instance convert each of the CSG's from a script into STL's or Blender Files in the working repo.")); + convert.setTooltip(new Tooltip( + "Convert the output of this file to a different file type. For instance convert each of the CSG's from a script into STL's or Blender Files in the working repo.")); autoRun.setTooltip(new Tooltip("Check to auto-run files on file change")); - + // arrange.setMinWidth(80); // publish.setMinWidth(80); - // Set up the run controls and the code area // The BorderPane has the same areas laid out as the @@ -158,13 +160,13 @@ private void load(ScriptingWidgetType type, File currentFile) { controlPane = new HBox(20); double lengthScalar = fileNameBox.getFont().getSize() * 1.5; - //fileNameBox.prefColumnCountProperty().bind(fileNameBox.textProperty().length()); - //fileListBox.prefColumnCountProperty().bind(fileListBox.textProperty().length()); - + // fileNameBox.prefColumnCountProperty().bind(fileNameBox.textProperty().length()); + // fileListBox.prefColumnCountProperty().bind(fileListBox.textProperty().length()); + HBox.setHgrow(fileNameBox, Priority.ALWAYS); fileNameBox.setMaxWidth(Double.MAX_VALUE); - HBox.setHgrow(fileListBox, Priority.ALWAYS); - fileListBox.setMaxWidth(Double.MAX_VALUE); + HBox.setHgrow(gitField, Priority.ALWAYS); + gitField.setMaxWidth(Double.MAX_VALUE); // fileNameBox.textProperty().addListener((ov, prevText, currText) -> { // // Do this in a BowlerStudio.runLater because of Textfield has no padding at @@ -198,11 +200,12 @@ private void load(ScriptingWidgetType type, File currentFile) { // // little bit // }); // }); - - //com.neuronrobotics.sdk.common.Log.error("\n\n\nScriptingFileWidget loading the editor loader:\n\n\n"); + + // com.neuronrobotics.sdk.common.Log.error("\n\n\nScriptingFileWidget loading + // the editor loader:\n\n\n"); try { externalEditorController = new ExternalEditorController(currentFile, autoRun); - }catch(Throwable t) { + } catch (Throwable t) { t.printStackTrace(); } @@ -222,14 +225,14 @@ private void load(ScriptingWidgetType type, File currentFile) { }).start(); }); - printbed = new Button("STL"); - printbed.setMinWidth(80); - printbed.setGraphic(AssetFactory.loadIcon("Edit-CAD-Engine.png")); - BowlerStudio.runLater(() -> { - printbed.setDisable(true); - }); - printbed.setOnAction(event -> { - if (manager!=null) { + printbed = new Button("STL"); + printbed.setMinWidth(80); + printbed.setGraphic(AssetFactory.loadIcon("Edit-CAD-Engine.png")); + BowlerStudio.runLater(() -> { + printbed.setDisable(true); + }); + printbed.setOnAction(event -> { + if (manager != null) { exportAll(true); BowlerStudio.runLater(() -> { printbed.setDisable(true); @@ -239,15 +242,16 @@ private void load(ScriptingWidgetType type, File currentFile) { } }); final Tooltip tooltip = new Tooltip(); - tooltip.setText("\nMake a print bed and export all of the parts on the screen\n" + "to manufacturing. STL and SVG\n"); + tooltip.setText( + "\nMake a print bed and export all of the parts on the screen\n" + "to manufacturing. STL and SVG\n"); printbed.setTooltip(tooltip); - + controlPane.getChildren().add(runfx); if (isOwnedByLoggedInUser) { controlPane.getChildren().add(arrange); controlPane.getChildren().add(printbed); } - if(externalEditorController!=null) + if (externalEditorController != null) controlPane.getChildren().add(externalEditorController.getControl()); controlPane.getChildren().add(autoRun); controlPane.getChildren().add(publish); @@ -260,22 +264,22 @@ private void load(ScriptingWidgetType type, File currentFile) { Label e2 = new Label("git:"); e2.setMinWidth(30); controlPane.getChildren().add(e2); - controlPane.getChildren().add(fileListBox); - fileListBox.setMaxWidth(Double.MAX_VALUE); + controlPane.getChildren().add(gitField); + gitField.setMaxWidth(Double.MAX_VALUE); controlPane.setMaxWidth(Double.MAX_VALUE); - //convert + // convert controlPane.getChildren().add(convert); // put the flowpane in the top area of the BorderPane setTop(controlPane); addIScriptEventListener(BowlerStudioController.getBowlerStudio()); - + try { loadCodeFromFile(currentFile); } catch (IOException e1) { // file has no git - isOwnedByLoggedInUser=false; + isOwnedByLoggedInUser = false; } // publish.setDisable(!isOwnedByLoggedInUser); runfx.setGraphic(AssetFactory.loadIcon("Run.png")); @@ -289,9 +293,10 @@ private void load(ScriptingWidgetType type, File currentFile) { convert.setDisable(true); reset(); } + private void launchConvert(File currentFile2) { // Auto-generated method stub - new Thread(()->{ + new Thread(() -> { String fileType = chooseFileType(); if (fileType != null) { try { @@ -309,56 +314,55 @@ private void launchConvert(File currentFile2) { private void convertResults(String fileType) throws Exception { Object obj = ScriptingEngine.inlineFileScriptRun(currentFile, null); ArrayList cache = new ArrayList<>(); - addObject(obj,cache); - int index=0; + addObject(obj, cache); + int index = 0; String url = getURL(); - boolean useSingleFileFOrImports= - fileType.toLowerCase().contains("fcstd")|| - fileType.toLowerCase().contains("blend"); + boolean useSingleFileFOrImports = fileType.toLowerCase().contains("fcstd") + || fileType.toLowerCase().contains("blend"); File newFile = null; - if(useSingleFileFOrImports) { - String file = fileNameBox.getText()+"."+fileType; + if (useSingleFileFOrImports) { + String file = fileNameBox.getText() + "." + fileType; newFile = ScriptingEngine.fileFromGit(url, file); ScriptingEngine.ignore(url, "/**.FCBak"); ScriptingEngine.ignore(url, "**.blend1"); - if(newFile.exists() ) { - if(AskToDeleteWidget.askToDeleteFile(file)) { + if (newFile.exists()) { + if (AskToDeleteWidget.askToDeleteFile(file)) { newFile.delete(); } } } - for(CSG c:cache) { - String objectName = c.getName().length()>0?"_"+c.getName():""; - String indexString = cache.size()>1?"_"+index:""; - if(useSingleFileFOrImports) { + for (CSG c : cache) { + String objectName = c.getName().length() > 0 ? "_" + c.getName() : ""; + String indexString = cache.size() > 1 ? "_" + index : ""; + if (useSingleFileFOrImports) { // In freeecad we add each item to the same model - objectName=""; - indexString=""; + objectName = ""; + indexString = ""; } - String file = fileNameBox.getText()+objectName+indexString+"."+fileType; - com.neuronrobotics.sdk.common.Log.error("File Name "+file); - com.neuronrobotics.sdk.common.Log.error("Placing file in "+url); + String file = fileNameBox.getText() + objectName + indexString + "." + fileType; + com.neuronrobotics.sdk.common.Log.error("File Name " + file); + com.neuronrobotics.sdk.common.Log.error("Placing file in " + url); try { newFile = ScriptingEngine.fileFromGit(url, file); - if(newFile.exists() && !useSingleFileFOrImports) { - if(AskToDeleteWidget.askToDeleteFile(file)) { + if (newFile.exists() && !useSingleFileFOrImports) { + if (AskToDeleteWidget.askToDeleteFile(file)) { newFile.delete(); - }else + } else continue; } // do the conversion now - if(fileType.toLowerCase().contains("stl")) { + if (fileType.toLowerCase().contains("stl")) { FileUtil.write(Paths.get(newFile.getAbsolutePath()), c.toStlString()); } - if(fileType.toLowerCase().contains("blend")) { + if (fileType.toLowerCase().contains("blend")) { BlenderLoader.toBlenderFile(c, newFile); - com.neuronrobotics.sdk.common.Log.error("Added mesh to "+newFile); + com.neuronrobotics.sdk.common.Log.error("Added mesh to " + newFile); } - if(fileType.toLowerCase().contains("fcstd")) { + if (fileType.toLowerCase().contains("fcstd")) { FreecadLoader.addCSGToFreeCAD(newFile, c); } - if(!useSingleFileFOrImports) + if (!useSingleFileFOrImports) BowlerStudio.createFileTab(newFile); } catch (GitAPIException | IOException e) { // Auto-generated catch block @@ -366,10 +370,10 @@ private void convertResults(String fileType) throws Exception { } index++; } - if(useSingleFileFOrImports && newFile!=null) + if (useSingleFileFOrImports && newFile != null) BowlerStudio.createFileTab(newFile); } - + public String chooseFileType() { CompletableFuture future = new CompletableFuture<>(); @@ -384,18 +388,18 @@ public String chooseFileType() { ButtonType freecad = new ButtonType("FreeCAD"); ButtonType cancelButton = new ButtonType("Cancel"); - alert.getButtonTypes().setAll(stlButton, blenderButton,freecad, cancelButton); + alert.getButtonTypes().setAll(stlButton, blenderButton, freecad, cancelButton); Stage stage = (Stage) alert.getDialogPane().getScene().getWindow(); stage.setOnCloseRequest(event -> alert.hide()); Node root = alert.getDialogPane(); - FontSizeManager.addListener(fontNum->{ - int tmp = fontNum-10; - if(tmp<12) - tmp=12; - root.setStyle("-fx-font-size: "+tmp+"pt"); - alert.getDialogPane().applyCss(); - alert.getDialogPane().layout(); - stage.sizeToScene(); + FontSizeManager.addListener(fontNum -> { + int tmp = fontNum - 10; + if (tmp < 12) + tmp = 12; + root.setStyle("-fx-font-size: " + tmp + "pt"); + alert.getDialogPane().applyCss(); + alert.getDialogPane().layout(); + stage.sizeToScene(); }); String result = alert.showAndWait().map(response -> { if (response == stlButton) @@ -419,16 +423,16 @@ public String chooseFileType() { } private String getURL() { - return fileListBox.getText(); + return gitField.getText(); } private void exportAll(boolean makePrintBed) { new Thread() { public void run() { setName("Exporting the CAD objects"); - ArrayList csgs =manager.makePrintBeds(); - if(makePrintBed) { - + ArrayList csgs = manager.makePrintBeds(); + if (makePrintBed) { + } com.neuronrobotics.sdk.common.Log.error("Exporting " + csgs.size() + " parts"); File baseDirForFiles = FileSelectionFactory.GetDirectory(MobleBaseMenueFactory.getBaseDirForFiles()); @@ -452,13 +456,12 @@ public void run() { } }.start(); } - + private void run() { new Thread() { public void run() { - - save(); + save(); // do not attempt to save no binary files startStopAction(); } @@ -469,10 +472,10 @@ public void saveTheFile(File currentFile) { new Thread(() -> { if (isOwnedByLoggedInUser) { save(); - CommitWidget.commit(currentFile, langaugeType.getIsTextFile()?getCode():null); + CommitWidget.commit(currentFile, langaugeType.getIsTextFile() ? getCode() : null); } else { String reponame = currentFile.getName().split("\\.")[0] + "_" + PasswordManager.getLoginID(); - String content = langaugeType.getIsTextFile()?getCode():null; + String content = langaugeType.getIsTextFile() ? getCode() : null; String newGit; try { newGit = ScriptingEngine.fork(remote, reponame, "Making fork from git: " + remote); @@ -557,7 +560,7 @@ private void start() { } } catch (Exception e) { // ignore CSG database - //e.printStackTrace(); + // e.printStackTrace(); } BowlerStudio.clearConsole(); BowlerStudioController.clearHighlight(); @@ -565,12 +568,10 @@ private void start() { running = true; BowlerStudio.runLater(() -> { BowlerStudio.setToStopButton(runfx); - + }); scriptRunner = new Thread() { - - public void run() { // String name; // try{ @@ -581,29 +582,33 @@ public void run() { try { Object obj = ScriptingEngine.inlineFileScriptRun(currentFile, null); ArrayList cache = new ArrayList<>(); - addObject(obj,cache); + addObject(obj, cache); String git; git = ScriptingEngine.locateGitUrl(currentFile); if (cache.size() > 0) { - BowlerStudio.runLater(()->{convert.setDisable(false);}); + BowlerStudio.runLater(() -> { + convert.setDisable(false); + }); boolean enableArraange = false; - for(CSG c:cache) { - if(c.getName().length()>0) { + for (CSG c : cache) { + if (c.getName().length() > 0) { enableArraange = true; } } - if(enableArraange) { - BowlerStudio.runLater(()->{arrange.setDisable(false);}); + if (enableArraange) { + BowlerStudio.runLater(() -> { + arrange.setDisable(false); + }); if (git != null && isArrange) { manager = new PrintBedManager(git, cache); - obj=manager.get(); + obj = manager.get(); BowlerStudio.runLater(() -> { printbed.setDisable(false); }); } } } - + for (int i = 0; i < listeners.size(); i++) { IScriptEventListener l = listeners.get(i); l.onScriptFinished(obj, scriptResult, currentFile); @@ -650,6 +655,7 @@ public void run() { } } + void addObject(Object o, ArrayList cache) { if (List.class.isInstance(o)) { List c = (List) o; @@ -665,20 +671,21 @@ void addObject(Object o, ArrayList cache) { return; } } + private void append(String s) { com.neuronrobotics.sdk.common.Log.error(s); } public String getGitRepo() { - return fileListBox.getText(); + return remote; } public String getGitFile() { - return fileNameBox.getText(); + return findLocalPath; } private void setUpFile(File f) { - com.neuronrobotics.sdk.common.Log.error("Setup ScriptingFileWidget "+f.getAbsolutePath()); + com.neuronrobotics.sdk.common.Log.error("Setup ScriptingFileWidget " + f.getAbsolutePath()); currentFile = f; try { watch = FileChangeWatcher.watch(currentFile); @@ -697,13 +704,13 @@ private void setUpFile(File f) { // ScriptingEngine.setLastFile(f); try { - ScriptingEngine.locateGit(currentFile,git->{ + ScriptingEngine.locateGit(currentFile, git -> { remote = git.getRepository().getConfig().getString("remote", "origin", "url"); - String findLocalPath = ScriptingEngine.findLocalPath(f, git); + findLocalPath = ScriptingEngine.findLocalPath(f, git); BowlerStudio.runLater(() -> { // fileListBox.setMinWidth(remote.getBytes().length*10); - fileListBox.setText(remote); - fileListBox.setTooltip(new Tooltip(remote)); + gitField.setText(remote); + gitField.setTooltip(new Tooltip(remote)); // fileListBox.res fileNameBox.setText(findLocalPath); @@ -715,16 +722,16 @@ private void setUpFile(File f) { fileNameBox.textProperty().addListener((observable, oldValue, newValue) -> { fileNameBox.setText(findLocalPath); }); - fileListBox.textProperty().addListener((observable, oldValue, newValue) -> { - fileListBox.setText(remote); + gitField.textProperty().addListener((observable, oldValue, newValue) -> { + gitField.setText(remote); }); }); }); - + } catch (Exception e1) { BowlerStudio.runLater(() -> { - fileListBox.setText("none"); - fileListBox.setMinWidth(40); + gitField.setText("none"); + gitField.setMinWidth(40); fileNameBox.setText(f.getAbsolutePath()); // These values are display only, so if hte user tries to change them, they // reset @@ -733,8 +740,8 @@ private void setUpFile(File f) { fileNameBox.textProperty().addListener((observable, oldValue, newValue) -> { fileNameBox.setText(f.getAbsolutePath()); }); - fileListBox.textProperty().addListener((observable, oldValue, newValue) -> { - fileListBox.setText("none"); + gitField.textProperty().addListener((observable, oldValue, newValue) -> { + gitField.setText("none"); }); }); @@ -780,7 +787,7 @@ public void save() { if (!langaugeType.getIsTextFile()) return; try { - + String content = new String(Files.readAllBytes(Paths.get(currentFile.getAbsolutePath()))); String ineditor = getCode(); if (content.contentEquals(ineditor)) { @@ -806,37 +813,38 @@ public void onFileChange(File fileThatChanged, @SuppressWarnings("rawtypes") Wat // BowlerStudio.runLater( new Runnable() { // @Override // public void run() { - //updateneeded = false; - // Auto-generated method stub - String absolutePath = fileThatChanged.getAbsolutePath(); - String absolutePath2 = currentFile.getAbsolutePath(); - //com.neuronrobotics.sdk.common.Log.error(absolutePath+" "+absolutePath2); - if (absolutePath.contains(absolutePath2)) { - - //com.neuronrobotics.sdk.common.Log.error("Code in " + absolutePath + " changed"); - String content = new String( - Files.readAllBytes(Paths.get(absolutePath))); - BowlerStudio.runLater(() -> { - if (content.length() > 2)// ensures tha the file contents never get wiped out on the - // user - setCode(content); - if (autoRun.isSelected()) { - new Thread(() -> { - stop(); - start(); - }).start(); - - } - watch.addIFileChangeListener(this); - //watch.addIFileChangeListener(ScriptingFileWidget.this); - - }); + // updateneeded = false; + // Auto-generated method stub + String absolutePath = fileThatChanged.getAbsolutePath(); + String absolutePath2 = currentFile.getAbsolutePath(); + // com.neuronrobotics.sdk.common.Log.error(absolutePath+" "+absolutePath2); + if (absolutePath.contains(absolutePath2)) { + + // com.neuronrobotics.sdk.common.Log.error("Code in " + absolutePath + " + // changed"); + String content = new String(Files.readAllBytes(Paths.get(absolutePath))); + BowlerStudio.runLater(() -> { + if (content.length() > 2)// ensures tha the file contents never get wiped out on the + // user + setCode(content); + if (autoRun.isSelected()) { + new Thread(() -> { + stop(); + start(); + }).start(); - } else { - // com.neuronrobotics.sdk.common.Log.error("Othr Code in "+fileThatChanged.getAbsolutePath()+" - // changed"); - watch.addIFileChangeListener(this); } + watch.addIFileChangeListener(this); + // watch.addIFileChangeListener(ScriptingFileWidget.this); + + }); + + } else { + // com.neuronrobotics.sdk.common.Log.error("Othr Code in + // "+fileThatChanged.getAbsolutePath()+" + // changed"); + watch.addIFileChangeListener(this); + } // } // }); } catch (Throwable e) { @@ -867,12 +875,11 @@ public String getFileName() { return "Web"; } - public void close() { - - watch.removeIFileChangeListener(this); - watch.close(); - watch=null; + + watch.removeIFileChangeListener(this); + watch.close(); + watch = null; }