package smart_gs.swingui.tree.action;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;

import smart_gs.logical.UsersNoteTree;
import smart_gs.logical.SpreadTree;
import smart_gs.logical.UsersNoteTree;
import smart_gs.swingui.WorkspaceWindow;

public class MakeUsersNoteDirectoryAction implements ActionListener {
	DefaultMutableTreeNode parentNode;
	UsersNoteTree tree;


	public MakeUsersNoteDirectoryAction(DefaultMutableTreeNode selectedNode,
			UsersNoteTree tree) {
		this.tree = tree;
		parentNode = selectedNode;		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String newDirName;
		newDirName = (String)JOptionPane.showInputDialog("Input Directory Name:\n");
		if (newDirName.equals("root")){
			JOptionPane.showMessageDialog(WorkspaceWindow.getInstance(),
					"root is a reserved name. Use another name","root is reserved",JOptionPane.ERROR_MESSAGE);
			return;
		}
		boolean whitespaceOnly=true;
		for (int i=0;i<newDirName.length();i++) {
			if (!Character.isWhitespace(newDirName.charAt(i))) {
				whitespaceOnly = false;
				break;
			}
		}
		if (whitespaceOnly){
			JOptionPane.showMessageDialog(WorkspaceWindow.getInstance(),
					"Names out of only white spaces are not allowed","non white space",JOptionPane.ERROR_MESSAGE);
			return;
		}
		tree.makeUsersNoteDirectory(parentNode, newDirName);
	}
}