diff --git a/src/org/chrisle/netbeans/plugins/nbscratchfile/CreateScratchFile.java b/src/org/chrisle/netbeans/plugins/nbscratchfile/CreateScratchFile.java index 396c309..f85c3eb 100644 --- a/src/org/chrisle/netbeans/plugins/nbscratchfile/CreateScratchFile.java +++ b/src/org/chrisle/netbeans/plugins/nbscratchfile/CreateScratchFile.java @@ -1,10 +1,14 @@ package org.chrisle.netbeans.plugins.nbscratchfile; +import org.chrisle.netbeans.plugins.nbscratchfile.model.NbScratchFileViewModel; import java.awt.Component; +import java.awt.Dimension; import java.awt.KeyboardFocusManager; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.KeyEvent; +import java.awt.event.WindowEvent; +import java.awt.event.WindowFocusListener; import javafx.application.Platform; import javafx.beans.value.ObservableValue; import javafx.concurrent.Worker.State; @@ -15,6 +19,7 @@ import javax.swing.JComponent; import javax.swing.JDialog; import javax.swing.KeyStroke; +import javax.swing.SwingUtilities; import netscape.javascript.JSObject; import org.openide.awt.ActionID; import org.openide.awt.ActionReference; @@ -54,6 +59,19 @@ public CreateScratchFile() { dialog.setResizable(false); dialog.setAlwaysOnTop(true); dialog.setUndecorated(true); + dialog.addWindowFocusListener(new WindowFocusListener() { + @Override + public void windowGainedFocus(WindowEvent e) {} + + @Override + public void windowLostFocus(WindowEvent e) { + if (SwingUtilities.isDescendingFrom(e.getOppositeWindow(), dialog)) { + return; + } + + dialog.setVisible(false); + } + }); dialog.getRootPane().registerKeyboardAction((ActionEvent e1) -> { dialog.setVisible(false); @@ -75,7 +93,7 @@ public void actionPerformed(ActionEvent e) { }); try { - webEngine.load(CreateScratchFile.class.getResource("/org/chrisle/netbeans/plugins/nbscratchfile/components/filetypewindow/ui/index.html").toExternalForm()); + webEngine.load(CreateScratchFile.class.getResource("/org/chrisle/netbeans/plugins/nbscratchfile/components/filetypewindow/dist/index.html").toExternalForm()); } catch (Exception ex) { Exceptions.printStackTrace(ex); } diff --git a/src/org/chrisle/netbeans/plugins/nbscratchfile/model/NbScratchFileViewModel.java b/src/org/chrisle/netbeans/plugins/nbscratchfile/model/NbScratchFileViewModel.java new file mode 100644 index 0000000..1376e33 --- /dev/null +++ b/src/org/chrisle/netbeans/plugins/nbscratchfile/model/NbScratchFileViewModel.java @@ -0,0 +1,44 @@ +package org.chrisle.netbeans.plugins.nbscratchfile.model; + +import java.io.IOException; +import java.nio.file.FileAlreadyExistsException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import javax.swing.JDialog; +import javax.swing.JOptionPane; +import org.netbeans.api.actions.Openable; +import org.openide.filesystems.FileObject; +import org.openide.filesystems.FileUtil; +import org.openide.loaders.DataObject; +import org.openide.util.Exceptions; + +public class NbScratchFileViewModel { + private int counter = 1; + private final JDialog dialog; + + public NbScratchFileViewModel(JDialog dialog) { + this.dialog = dialog; + } + + public void setExt(String ext, String languageName) { + try { + Path path = Paths.get(String.format("%s/.netbeans/scratches/%s/scratch%d.%s", System.getProperty("user.home"), languageName, this.counter++, ext)); + Files.createDirectories(path.getParent()); + Files.createFile(path); + + FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(path.toFile())); + DataObject dataObject = DataObject.find(fo); + Openable openable = dataObject.getLookup().lookup(Openable.class); + + dialog.setVisible(false); + openable.open(); + } catch (FileAlreadyExistsException e) { + this.setExt(ext, languageName); + System.err.println("already exists: " + e.getMessage()); + } catch (IOException ex) { + JOptionPane.showMessageDialog(null, ex.getMessage()); + Exceptions.printStackTrace(ex); + } + } +}