1 19 package org.netbeans.modules.apisupport.metainfservices; 20 21 import com.sun.source.tree.Tree; 22 import com.sun.source.util.TreePath; 23 import java.awt.Dialog ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.io.InputStream ; 27 import java.io.OutputStream ; 28 import java.util.Collection ; 29 import java.util.List ; 30 import java.util.TreeSet ; 31 import java.util.logging.Level ; 32 import java.util.logging.Logger ; 33 import javax.lang.model.element.Element; 34 import javax.lang.model.element.TypeElement; 35 import javax.lang.model.type.TypeMirror; 36 import org.netbeans.api.java.project.JavaProjectConstants; 37 import org.netbeans.api.java.source.CancellableTask; 38 import org.netbeans.api.java.source.CompilationController; 39 import org.netbeans.api.java.source.JavaSource; 40 import org.netbeans.api.project.FileOwnerQuery; 41 import org.netbeans.api.project.Project; 42 import org.netbeans.api.project.ProjectUtils; 43 import org.netbeans.api.project.SourceGroup; 44 import org.netbeans.api.project.Sources; 45 import org.openide.DialogDisplayer; 46 import org.openide.ErrorManager; 47 import org.openide.NotifyDescriptor; 48 import org.openide.WizardDescriptor; 49 import org.openide.filesystems.FileLock; 50 import org.openide.filesystems.FileObject; 51 import org.openide.filesystems.FileUtil; 52 import org.openide.loaders.DataObject; 53 import org.openide.nodes.Node; 54 import org.openide.util.HelpCtx; 55 import org.openide.util.NbBundle; 56 import org.openide.util.actions.CookieAction; 57 58 public final class ExportAction extends CookieAction { 59 private static final Logger LOG = Logger.getLogger(ExportAction.class.getName()); 60 61 protected void performAction(Node[] activatedNodes) { 62 MyTask task = new ExportAction.MyTask(); 63 FileObject fo = activatedNodes[0].getLookup().lookup(org.openide.filesystems.FileObject.class); 64 if (fo == null) { 65 return; 66 } 67 try { 68 JavaSource source = JavaSource.forFileObject(fo); 69 source.runUserActionTask(task, true); 70 } catch (IOException ex) { 71 LOG.log(Level.SEVERE, ex.getMessage(), ex); 72 } 73 74 task.postProcess(fo); 75 } 76 77 protected int mode() { 78 return CookieAction.MODE_EXACTLY_ONE; 79 } 80 81 public String getName() { 82 return NbBundle.getMessage(ExportAction.class, "CTL_ExportAction"); 83 } 84 85 protected Class [] cookieClasses() { 86 return new Class [] { 87 DataObject.class 88 }; 89 } 90 91 protected void initialize() { 92 super.initialize(); 93 putValue("noIconInMenu", Boolean.TRUE); 95 } 96 97 public HelpCtx getHelpCtx() { 98 return HelpCtx.DEFAULT_HELP; 99 } 100 101 protected boolean asynchronous() { 102 return false; 103 } 104 105 private static final class MyTask implements CancellableTask<CompilationController> { 106 Collection <String > allInterfaces = new TreeSet <String >(); 107 String clazzName; 108 109 public void cancel() { 110 } 111 112 public void run(CompilationController cont) throws Exception { 113 cont.toPhase(JavaSource.Phase.ELEMENTS_RESOLVED); 114 for (Tree t : cont.getCompilationUnit().getTypeDecls()) { 115 if (t.getKind() == Tree.Kind.CLASS) { 116 TreePath path = cont.getTrees().getPath(cont.getCompilationUnit(), t); 117 Element e = cont.getTrees().getElement(path); 118 if (e instanceof TypeElement) { 119 TypeElement te = (TypeElement)e; 120 clazzName = te.getQualifiedName().toString(); 121 } 122 findInterfaces(cont, e); 123 } 124 } 125 } 126 127 private void findInterfaces(CompilationController cont, Element e) { 128 if (e == null) { 129 return; 130 } 131 if (!e.getKind().isClass() && !e.getKind().isInterface()) { 132 return; 133 } 134 TypeElement type = (TypeElement)e; 135 allInterfaces.add(type.getQualifiedName().toString()); 136 137 findInterfaces(cont, type.getSuperclass()); 138 for (TypeMirror m : type.getInterfaces()) { 139 findInterfaces(cont, m); 140 } 141 } 142 143 private void findInterfaces(CompilationController cont, TypeMirror m) { 144 findInterfaces(cont, cont.getTypes().asElement(m)); 145 } 146 147 public void postProcess(FileObject fo) { 148 FileObject target = null; 149 Project p = FileOwnerQuery.getOwner(fo); 150 151 if (p != null) { 152 Sources s = ProjectUtils.getSources(p); 153 SourceGroup[] arr = s.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA); 154 if (arr != null && arr.length > 0) { 155 target = arr[0].getRootFolder(); 156 } 157 } 158 159 160 if (allInterfaces.isEmpty() || clazzName == null) { 161 NotifyDescriptor d = new NotifyDescriptor.Message( 162 NbBundle.getMessage(ExportAction.class, "MSG_CannotFindClass", fo), 163 NotifyDescriptor.WARNING_MESSAGE 164 ); 165 DialogDisplayer.getDefault().notify(d); 166 return; 167 } 168 169 WizardDescriptor wd = new WizardDescriptor(new ExportWizardIterator()); 170 171 wd.putProperty("implName", clazzName); wd.putProperty("interfaceNames", allInterfaces); wd.putProperty("target", target); 174 175 Dialog d = DialogDisplayer.getDefault().createDialog(wd); 176 d.setVisible(true); 177 178 if (wd.FINISH_OPTION == wd.getValue()) { 179 try { 180 createFiles(clazzName, wd, target); 181 } catch (IOException ex) { 182 ErrorManager.getDefault().notify(ex); 183 } 184 } 185 } 186 } 187 188 @SuppressWarnings ("unchecked") 189 private static void createFiles(String implName, WizardDescriptor wd, FileObject target) 190 throws IOException , FileNotFoundException { 191 List <String > files = (List <String >)wd.getProperty("files"); createFiles(implName, files, target); 193 } 194 195 static void createFiles(String implName, List <String > files, FileObject target) 196 throws IOException , FileNotFoundException { 197 for (String s : files) { 199 FileObject f = FileUtil.createData(target, s); 200 byte[] exist = new byte[(int)f.getSize()]; 201 InputStream is = f.getInputStream(); 202 int len = is.read(exist); 203 is.close(); 204 206 String content = new String (exist); 207 if (content.length() > 0 && !content.endsWith("\n")) { content = content + "\n"; } 210 211 content = content + implName + "\n"; 213 FileLock lock = f.lock(); 214 OutputStream os = f.getOutputStream(lock); 215 os.write(content.getBytes()); 216 os.close(); 217 lock.releaseLock(); 218 } 219 } 220 } 221 222 | Popular Tags |