Skip to content

Commit

Permalink
Refactored stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris2011 committed Sep 15, 2017
1 parent 0fec25c commit eefc34b
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit eefc34b

Please sign in to comment.