📄 cmdlinecopy.java
字号:
package fileSys.UI;import java.awt.Dimension;import java.awt.TextArea;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.util.List;import javax.swing.JFrame;import javax.swing.JPanel;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import fileSys.service.FileService;import fileSys.util.AFile;import fileSys.util.Command;import fileSys.util.ParseCommand;public class CMDLineCopy { private static String commandStr; private static String currentPath; private static String currentDir; private static String newStr = ""; private static String user; private static boolean right = false; private static Command lastCMD = null; static ApplicationContext context = new ClassPathXmlApplicationContext( "config.xml"); static FileService fileService = (FileService) context .getBean("fileService"); final ParseCommand parseCommand = (ParseCommand) context .getBean("parseCommand"); public CMDLineCopy(final String username, final String currentdir) { currentDir = currentdir; user = username; fileService.init(); if (username.equals("root")) currentPath = "/main"; else currentPath = "/main/" + username; commandStr = "[" + user + " " + currentDir + "]" + ":"; JFrame frame = new JFrame("文件管理系统终端"); frame.setLocation(40, 60); JPanel panel = (JPanel) frame.getContentPane(); panel.setPreferredSize(new Dimension(600, 400)); final TextArea textarea = new TextArea(commandStr); textarea.addKeyListener(new KeyListener() { public void keyTyped(KeyEvent event) { Character tempchar = event.getKeyChar(); String tip = ""; String temp = ""+tempchar; if (temp.matches("^.$")||tempchar=='\n') { if (tempchar == 8) { try { newStr = newStr.substring(0, newStr.length() - 1); } catch (Exception e) { } } else { if (tempchar != '\n') { newStr = newStr + tempchar; } else { if (newStr == "") textarea.append(commandStr); else { commandStr = commandStr + newStr; newStr = ""; Command command = parseCommand .parseCom(commandStr); tip = CMDLineCopy.invokeService(command); commandStr = "[" + user + " " + currentDir + "]" + ":"; if (tip != null) { commandStr = commandStr + tip; textarea.append(commandStr); commandStr = "[" + user + " " + currentDir + "]" + ":"; textarea.append("\n" + commandStr); } else { textarea.append(commandStr); } } } } } } public void keyPressed(KeyEvent event) { } public void keyReleased(KeyEvent event) { } }); panel.add(textarea); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.pack(); frame.setVisible(true); } public static String invokeService(Command command) { String com = command.getCmd(); String tip = null; String result; String dest; if (com.equals("cd")) { //命令为"cd" 时的处理; dest = command.getArg(); result = fileService.changeDir(currentPath, dest); if (result == null) { tip = "The directory \""+ dest + "\" can not be found!"; } else { if (result == FileService.NDIR) { tip = dest + "\" is not a directory"; } else { currentPath = result; currentDir = result.substring(result.lastIndexOf('/') + 1); } } dest = null; } else if (com.equals("md")) { //命令为"md" 时的处理; dest = command.getArg(); String flag = null; if (dest == null) { tip = "The operate arguments lost!"; } else { if (fileService.accessControl(user, currentPath, dest) || right) { right = false; flag = fileService.createDir(currentPath, dest); tip = setTip(dest,flag); } else { lastCMD = command; tip = "You have no right to make directory in this directory! \n Do you have the directory's save code? [y/n]"; } } dest = null; } else if (com.equals("dir")) { //命令为"dir" 时的处理; tip = currentPath; } else if (com.equals("rm")) { //命令为"rm" 时的处理; dest = command.getArg(); String flag = null; if (dest == null) { tip = "The operate arguments lost!"; } else { if (fileService.accessControl(user, currentPath, dest) || right) { right = false; flag = fileService.deleteDir(currentPath, dest); tip = setTip(dest,flag); }else { lastCMD = command; tip = "You have no right to remove \"" + dest + "\" ! \n Do you have the directory's save code? [y/n]"; } } if(FileService.NNULL.equals(tip)){ command.setCmd("rmAll"); lastCMD = command; tip = "The directory is not empty! \n Are you sure you want to delete it? [y/n]"; } dest = null; } else if (com.equals("open")) { // 命令为"open" 时的处理; String option = command.getOption(); dest = command.getArg(); if (dest == null) tip = "The operate arguments lost!"; else { AFile afile = fileService.openFile(currentPath, dest); tip = setTip(dest,afile.getFlag()); if(tip == null){ if (option == null || option.equals("-r")) new Editer(afile, "open", "-r",fileService); else if (option.equals("-w") ) { if (fileService.accessControl(user, currentPath, dest)) new Editer(afile, "open", "-w",fileService); else tip = "You have no right to write this file \""+dest; } } } dest = null; } else if (com.equals("openall")) { //命令为"openall" 时的处理; dest = command.getArg(); if(dest == null) dest = currentPath; List<AFile> fileList = fileService.openFiles(currentPath, dest); if(fileList.size() == 0){ tip = "No file in this directory \""+dest+"\""; }else{ for(AFile file : fileList){ new Editer(file,"open","-r",fileService); } } dest = null; } else if (com.equals("create")) { //命令为"create" 时的处理; dest = command.getArg(); String flag = null; if (dest == null) { tip = "The operate arguments lost!"; } else { if (fileService.accessControl(user, currentPath, dest) || right) { right = false; flag = fileService.createFile(currentPath, dest); tip = setTip(dest,flag); } else { lastCMD = command; tip = "You have no right to create new file in this directory! \n Do you have the directory's save code? [y/n]"; } } dest = null; } else if (com.equals("delete")) { //命令为"delete" 时的处理; dest = command.getArg(); String flag = null; if (dest == null) { tip = "The operate arguments lost!"; } else { if (fileService.accessControl(user, currentPath, dest) || right){ flag = fileService.deleteFile(currentPath, dest); tip = setTip(dest,flag); }else { lastCMD = command; tip = "You have no right to delete " + dest + "\n Do you have this file's save code? [y/n]"; } } dest = null; } else if (com.equals("write")) { //命令为"write" 时的处理; dest = command.getArg(); if (dest == null) tip = "The operate arguments lost!"; else { AFile afile = fileService.openFile(currentPath, dest); if (afile.getFlag() == FileService.ERROR) { tip = "There are something wrong at reading the file \""+dest+"\""; } else if (afile.getFlag() == FileService.NFILE) { tip = "It is not a file"; } else if (afile.getFlag() == FileService.NEXIST) { tip = "The file \""+ dest +"\" is not exist"; } else { if (fileService.accessControl(user, currentPath, dest) || right) { right = false; new Editer(afile, "write", "", fileService); } else { lastCMD = command; tip = "You have no right to create new file in this directory! \n Do you have the directory's save code? [y/n]"; } } } dest = null; } else if (com.equals("read")) { //命令为"read" 时的处理; String option = command.getOption(); dest = command.getArg(); if (dest == null) tip = "The operate arguments lost!"; else { AFile afile = fileService.openFile(currentPath, dest); tip = setTip(dest,afile.getFlag()); if(tip == null){ if (option == null || option.equals("-r")) new Editer(afile, "read", "-r",fileService); else if (option.equals("-w") ) { if (fileService.accessControl(user, currentPath, dest)) new Editer(afile, "read", "-w",fileService); else tip = "You have no right to write this file \""+dest+"\""; } } } dest = null; } else if (com.equals("change")) { // 命令为"change" 时的处理; String flag = null; dest = command.getOption(); String saveCode = command.getArg(); if (dest == null) tip = "The operate arguments lost!"; else { if (fileService.accessControl(user, currentPath, dest)) { flag = fileService.changeSaveCode(currentPath, dest, saveCode); tip = setTip(dest, flag); } else tip = "You have no right to change this save code!"; } dest = null; } else if (com.equals("exit")) { //命令为"exit" 时的处理; System.exit(1); } else if (lastCMD != null && com.equals("n")) { //辅助命令"n" 时的处理; lastCMD = null; } else if (lastCMD != null && com.equals("y")) { // 辅助命令"y" 时的处理; com = lastCMD.getCmd(); if (com.equals("rmAll")) { String destDir = lastCMD.getArg(); fileService.deleteNotNullDir(currentPath, destDir); lastCMD = null; } else { tip = "Please input your saveCode :"; } } else if (lastCMD != null && !com.equals("y") && !com.equals("n")) { String flag = fileService.checkSaveCode(lastCMD.getCmd(), currentPath, lastCMD.getArg(), com); if (flag == FileService.SUCCESS) { right = true; tip = invokeService(lastCMD); if (lastCMD.getCmd() != "rmAll") { lastCMD = null; } } else if (flag == FileService.NEXIST) tip = "The file is not exist"; else tip = "The save code is invailed!"; } else { tip = com + " command not found"; } return tip; } static String setTip(String dest,String flag){ String tip = null; if(FileService.ERROR.equals(flag)) tip = "There is something wrong in this operation !"; else if(FileService.EXIST.equals(flag)){ tip = "\" " + dest + " \" has been exist!"; } else if(FileService.NDIR.equals(flag)) tip = "\" " + dest + " \" is not a directory !"; else if(FileService.NEXIST.equals(flag)) tip = "\" " + dest + " \" is not exist !"; else if(FileService.NFILE.equals(flag)) tip = "\" " + dest + " \" is not a file !"; else if(FileService.NNULL.equals(flag)) tip = flag; else if(FileService.USED.equals(flag)) tip = "\" "+ dest +" \" can not be deleted!"; else if(FileService.SUCCESS.equals(flag)) tip = null; else tip = null; return tip; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -