1 package org.antlr.works.plugin.intellij; 2 3 import com.intellij.openapi.actionSystem.AnAction; 4 import com.intellij.openapi.actionSystem.AnActionEvent; 5 import com.intellij.openapi.actionSystem.DataConstants; 6 import com.intellij.openapi.application.ApplicationManager; 7 import com.intellij.openapi.fileEditor.FileEditorManager; 8 import com.intellij.openapi.project.Project; 9 import com.intellij.openapi.ui.Messages; 10 import com.intellij.openapi.vfs.VirtualFile; 11 import com.intellij.openapi.vfs.VirtualFileManager; 12 import org.antlr.xjlib.appkit.utils.XJAlertInput; 13 import org.antlr.xjlib.foundation.XJUtils; 14 import org.antlr.works.utils.IconManager; 15 16 import java.io.File ; 17 import java.io.IOException ; 18 19 49 50 public class PIActionNewFile extends AnAction { 51 52 public PIActionNewFile() { 53 super("Grammar File", "Create New ANTLR 3 Grammar File", IconManager.shared().getIconApplication16x16()); 54 } 55 56 public void actionPerformed(AnActionEvent e) { 57 String selectedFile = getSelectedFile(e); 58 if(selectedFile == null) { 59 Messages.showMessageDialog("Cannot get the selected file in IntelliJ!", "ANTLRWorks Plugin", 60 IconManager.shared().getIconApplication32x32()); 61 return; 62 } 63 64 String rootFolder; 65 if(XJUtils.getLastPathComponent(selectedFile).contains(".")) 66 rootFolder = XJUtils.getPathByDeletingLastComponent(selectedFile); 67 else 68 rootFolder = selectedFile; 69 70 String name = askUserForFile(); 71 if(name == null) 72 return; 73 74 String file = XJUtils.concatPath(rootFolder, name); 75 try { 76 XJUtils.writeStringToFile("grammar untitled;\n", file); 77 } catch (IOException e1) { 78 Messages.showMessageDialog("Cannot create the new grammar file:\n"+e1.toString(), "ANTLRWorks Plugin", 79 IconManager.shared().getIconApplication32x32()); 80 return; 81 } 82 83 Project project = (Project) e.getDataContext().getData(DataConstants.PROJECT); 84 openFile(project, file); 85 } 86 87 90 private String getSelectedFile(AnActionEvent e) { 91 VirtualFile[] files = (VirtualFile[]) e.getDataContext().getData(DataConstants.VIRTUAL_FILE_ARRAY); 92 if(files != null && files.length > 0) 93 return files[0].getPath(); 94 else 95 return null; 96 } 97 98 101 private void openFile(Project project, String file) { 102 ApplicationManager.getApplication().runWriteAction(new MyShowFile(file, project)); 103 } 104 105 108 private String askUserForFile() { 109 String name = XJAlertInput.showInputDialog(null, "New Grammar File", 110 "Enter a new file name:", ""); 111 if(name == null) 112 return name; 113 114 if(name.contains(".")) { 115 if(name.endsWith(".g")) 116 return name; 117 else 118 return name+".g"; 119 } else 120 return name+".g"; 121 } 122 123 private static class MyShowFile implements Runnable { 124 125 private String file; 126 private final Project project; 127 128 public MyShowFile(String file, Project project) { 129 this.file = file; 130 this.project = project; 131 } 132 133 public void run() { 134 VirtualFile vfile = VirtualFileManager.getInstance().refreshAndFindFileByUrl("file://"+file.replace(File.separatorChar, '/')); 135 if(vfile == null) { 136 Messages.showMessageDialog("Cannot find the newly created grammar file:\n"+file, 137 "ANTLRWorks Plugin", 138 IconManager.shared().getIconApplication32x32()); 139 return; 140 } 141 FileEditorManager fem = FileEditorManager.getInstance(project); 142 fem.openFile(vfile, true); 143 } 144 } 145 } 146 | Popular Tags |