-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/org/chrisle/netbeans/plugins/nbscratchfile/model/NbScratchFileViewModel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |