break;
// create a new file at the current node
case KeyEvent.VK_INSERT:
e.consume();
FileTreeNode selectedNode = (FileTreeNode) scalaLabPathsListener.selectedNode;
if (selectedNode == null) return; // selected node not exists
FileTreeNode parent = (FileTreeNode) selectedNode.getParent();
String newFileName = JOptionPane.showInputDialog(null, "Name for your new file?", JOptionPane.QUESTION_MESSAGE);
// create the specified file actually at the filesystem
String filePath = scalaLabPathsListener.selectedPath; // the path of the selected node
String newFileFullPathName = filePath+File.separator+newFileName;
File newFile = new File(newFileFullPathName);
boolean OKforNewFile = true;
int userResponse = JOptionPane.YES_OPTION; // allows further processing if file either not exists or user responds to overwrite
if (newFile.exists()) { // file already exists
userResponse = JOptionPane.showConfirmDialog(null, "File: "+newFileFullPathName+" already exists. Overwrite? ", "File already exists",
JOptionPane.YES_NO_OPTION);
OKforNewFile = (userResponse == JOptionPane.YES_OPTION);
if (OKforNewFile) {
boolean deleteSuccess = newFile.delete();
if (deleteSuccess == false)
JOptionPane.showMessageDialog(null, "Failing to delete file: "+newFileFullPathName, "File delete failed", JOptionPane.INFORMATION_MESSAGE);
}
}
if (OKforNewFile) { // create new file and update tree
// create new file
try {
newFile.createNewFile();
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(null, "IOException trying to create file"+newFileFullPathName, "IO Exception", JOptionPane.INFORMATION_MESSAGE );
}
// update tree
int selectedIndex = 0;
if (parent != null) {
selectedIndex = parent.getIndex(selectedNode);
}
// update the tree model
FileTreeNode newNode = null;
try {
newNode = new FileTreeNode(new File(filePath), newFileName);
newNode.setUserObject(filePath+File.separator+newFileName);
// now display the new node
GlobalValues.currentFileExplorer.model.insertNodeInto(newNode, parent, selectedIndex+1);
TreeNode [] nodes = GlobalValues.currentFileExplorer.model.getPathToRoot(newNode);
TreePath pathScroll = new TreePath(nodes);
GlobalValues.currentFileExplorer.pathsTree.expandPath(pathScroll);
}
catch (FileNotFoundException ex) { System.out.println("File not found exception in creating new File"); ex.printStackTrace();}
catch (SecurityException ex) { System.out.println("Security exception in creating new File"); ex.printStackTrace();}
}
break;
case KeyEvent.VK_UP: // Up Folder
e.consume();
selectedNode = (FileTreeNode) scalaLabPathsListener.selectedNode;
if (selectedNode == null) return;
String selectedPath = selectedNode.toString();
int idxLastPath = selectedPath.lastIndexOf(File.separatorChar);
if (idxLastPath==-1) {
JOptionPane.showMessageDialog(null, "Already in root level", "There is no parent folder", JOptionPane.INFORMATION_MESSAGE);
return;
}
String pathComponent = selectedPath.substring(0, idxLastPath);
GlobalValues.selectedExplorerPath = pathComponent;
GlobalValues.scalalabMainFrame.explorerPanel.updatePaths();
break;
case KeyEvent.VK_F2: // New File within the directory
System.out.println("ALT-INSERT");
e.consume();
selectedNode = (FileTreeNode) scalaLabPathsListener.selectedNode;
if (selectedNode == null) return;
parent = (FileTreeNode) selectedNode.getParent();
newFileName = JOptionPane.showInputDialog(null, "Name for your new file?", JOptionPane.QUESTION_MESSAGE);
// create the specified file actually at the filesystem
filePath = scalaLabPathsListener.selectedValue; // the path of the selected node
File directoryOfNewFile = new File(filePath);
if (directoryOfNewFile.isDirectory()==false) {
JOptionPane.showMessageDialog(null, "Cannot place a file within another file!!", "Improper attempt to create a file within a directory", JOptionPane.INFORMATION_MESSAGE);
return;
}
newFileFullPathName = directoryOfNewFile+File.separator+newFileName;
newFile = new File(newFileFullPathName);
OKforNewFile = true;
userResponse = JOptionPane.YES_OPTION; // allows further processing if file either not exists or user responds to overwrite
if (newFile.exists()) { // file already exists
userResponse = JOptionPane.showConfirmDialog(null, "File: "+newFileFullPathName+" already exists. Overwrite? ", "File already exists",
JOptionPane.YES_NO_OPTION);
OKforNewFile = (userResponse == JOptionPane.YES_OPTION);
if (OKforNewFile) {
boolean deleteSuccess = newFile.delete();
if (deleteSuccess == false)
JOptionPane.showMessageDialog(null, "Failing to delete file: "+newFileFullPathName, "File delete failed", JOptionPane.INFORMATION_MESSAGE);
}
}
if (OKforNewFile) { // create new file and update tree
// create new file
try {
newFile.createNewFile();
}
catch (IOException ioEx) {
JOptionPane.showMessageDialog(null, "IOException trying to create file"+newFileFullPathName, "IO Exception", JOptionPane.INFORMATION_MESSAGE );
}
// update tree
int selectedIndex = 0;
if (parent != null)
selectedIndex = parent.getIndex(selectedNode);
// update the tree model
FileTreeNode newNode = null;
try {
newNode = new FileTreeNode(new File(filePath), newFileName);
newNode.setUserObject(filePath+File.separator+newFileName); // sets the user object for this node
// now display the new node
DefaultTreeModel model = GlobalValues.currentFileExplorer.model;
model.insertNodeInto(newNode, selectedNode, 0);
TreeNode [] nodes = model.getPathToRoot(newNode);
TreePath pathScroll = new TreePath(nodes);