Search in sources :

Example 1 with IFileChangeListener

use of com.neuronrobotics.bowlerstudio.util.IFileChangeListener in project BowlerStudio by CommonWealthRobotics.

the class BowlerStudioController method createFileTab.

// Custom function for creation of New Tabs.
public ScriptingFileWidget createFileTab(File file) {
    if (openFiles.get(file.getAbsolutePath()) != null && widgets.get(file.getAbsolutePath()) != null) {
        BowlerStudioModularFrame.getBowlerStudioModularFrame().setSelectedTab(openFiles.get(file.getAbsolutePath()));
        return widgets.get(file.getAbsolutePath()).getScripting();
    }
    Tab fileTab = new Tab(file.getName());
    openFiles.put(file.getAbsolutePath(), fileTab);
    try {
        System.err.println("Loading local file from: " + file.getAbsolutePath());
        LocalFileScriptTab t = new LocalFileScriptTab(file);
        new Thread() {

            public void run() {
                String gitRepo = t.getScripting().getGitRepo();
                if (gitRepo != null) {
                    String message = BowlerStudioMenu.gitURLtoMessage(gitRepo);
                    if (gitRepo.length() < 5 || (message == null))
                        message = "Project " + gitRepo;
                    BowlerStudioMenuWorkspace.add(gitRepo, message);
                }
            }
        }.start();
        String key = t.getScripting().getGitRepo() + ":" + t.getScripting().getGitFile();
        ArrayList<String> files = new ArrayList<>();
        files.add(t.getScripting().getGitRepo());
        files.add(t.getScripting().getGitFile());
        try {
            if (// catch degenerates
            key.length() > 3 && files.get(0).length() > 0 && files.get(1).length() > 0)
                ConfigurationDatabase.setObject("studio-open-git", key, files);
        } catch (java.lang.NullPointerException ex) {
        // file can not be opened
        }
        fileTab.setContent(t);
        fileTab.setGraphic(AssetFactory.loadIcon("Script-Tab-" + ScriptingEngine.getShellType(file.getName()) + ".png"));
        addTab(fileTab, true);
        widgets.put(file.getAbsolutePath(), t);
        fileTab.setOnCloseRequest(event -> {
            widgets.remove(file.getAbsolutePath());
            openFiles.remove(file.getAbsolutePath());
            ConfigurationDatabase.removeObject("studio-open-git", key);
            t.getScripting().close();
            System.out.println("Closing " + file.getAbsolutePath());
        });
        FileChangeWatcher watcher = FileChangeWatcher.watch(file);
        watcher.addIFileChangeListener(new IFileChangeListener() {

            @Override
            public void onFileDelete(File fileThatIsDeleted) {
                BowlerStudioModularFrame.getBowlerStudioModularFrame().closeTab(fileTab);
            }

            @Override
            public void onFileChange(File fileThatChanged, WatchEvent event) {
            }
        });
        t.setFontSize(size);
        return t.getScripting();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}
Also used : ArrayList(java.util.ArrayList) FileChangeWatcher(com.neuronrobotics.bowlerstudio.util.FileChangeWatcher) IOException(java.io.IOException) LocalFileScriptTab(com.neuronrobotics.bowlerstudio.tabs.LocalFileScriptTab) LocalFileScriptTab(com.neuronrobotics.bowlerstudio.tabs.LocalFileScriptTab) Tab(javafx.scene.control.Tab) IFileChangeListener(com.neuronrobotics.bowlerstudio.util.IFileChangeListener) WatchEvent(java.nio.file.WatchEvent) File(java.io.File)

Aggregations

LocalFileScriptTab (com.neuronrobotics.bowlerstudio.tabs.LocalFileScriptTab)1 FileChangeWatcher (com.neuronrobotics.bowlerstudio.util.FileChangeWatcher)1 IFileChangeListener (com.neuronrobotics.bowlerstudio.util.IFileChangeListener)1 File (java.io.File)1 IOException (java.io.IOException)1 WatchEvent (java.nio.file.WatchEvent)1 ArrayList (java.util.ArrayList)1 Tab (javafx.scene.control.Tab)1