KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > antlr > works > plugin > intellij > PIActionNewFile


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 JavaDoc;
17 import java.io.IOException JavaDoc;
18
19 /*
20
21 [The "BSD licence"]
22 Copyright (c) 2005-2006 Jean Bovet
23 All rights reserved.
24
25 Redistribution and use in source and binary forms, with or without
26 modification, are permitted provided that the following conditions
27 are met:
28
29 1. Redistributions of source code must retain the above copyright
30 notice, this list of conditions and the following disclaimer.
31 2. Redistributions in binary form must reproduce the above copyright
32 notice, this list of conditions and the following disclaimer in the
33 documentation and/or other materials provided with the distribution.
34 3. The name of the author may not be used to endorse or promote products
35 derived from this software without specific prior written permission.
36
37 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
38 IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
40 IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
41 INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
43 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
44 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
45 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
46 THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
47
48 */

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 JavaDoc 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 JavaDoc rootFolder;
65         if(XJUtils.getLastPathComponent(selectedFile).contains("."))
66             rootFolder = XJUtils.getPathByDeletingLastComponent(selectedFile);
67         else
68             rootFolder = selectedFile;
69
70         String JavaDoc name = askUserForFile();
71         if(name == null)
72             return;
73
74         String JavaDoc file = XJUtils.concatPath(rootFolder, name);
75         try {
76             XJUtils.writeStringToFile("grammar untitled;\n", file);
77         } catch (IOException JavaDoc 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     /** Returns the first selected file in the IntelliJ project panel
88      *
89      */

90     private String JavaDoc 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     /** Open the file in IntelliJ
99      *
100      */

101     private void openFile(Project project, String JavaDoc file) {
102         ApplicationManager.getApplication().runWriteAction(new MyShowFile(file, project));
103     }
104
105     /** Asks the user for the name of the file
106      *
107      */

108     private String JavaDoc askUserForFile() {
109         String JavaDoc 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 JavaDoc {
124
125         private String JavaDoc file;
126         private final Project project;
127
128         public MyShowFile(String JavaDoc 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