1 19 package org.netbeans.jmi.javamodel.codegen; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import javax.swing.text.StyledDocument ; 24 import org.netbeans.api.mdr.MDRepository; 25 import org.netbeans.jmi.javamodel.*; 26 import org.netbeans.modules.javacore.internalapi.JavaMetamodel; 27 import org.openide.LifecycleManager; 28 import org.openide.cookies.EditorCookie; 29 import org.openide.filesystems.FileStateInvalidException; 30 import org.openide.filesystems.FileSystem; 31 import org.openide.loaders.DataObject; 32 33 34 35 public class Utility { 36 37 public static void beginTrans(boolean writeAccess) { 38 getDefaultRepository().beginTrans(writeAccess); 39 } 40 41 public static void endTrans(boolean rollback) { 42 getDefaultRepository().endTrans(rollback); 43 } 44 45 public static void endTrans() { 46 getDefaultRepository().endTrans(); 47 } 48 49 public static MDRepository getDefaultRepository() { 50 return JavaMetamodel.getDefaultRepository(); 51 } 52 53 public static File getFile(File dataDir, String fileName) throws FileStateInvalidException { 54 String result = dataDir.getAbsolutePath() + "/projects/default/src/" + fileName; 55 System.out.println("looking for file: " + result); 56 return new File (result); 57 } 58 59 public static JavaModelPackage getJavaModelPackage(String pattern) { 60 throw new UnsupportedOperationException ("Method is no longer supported. " + 61 "Rewrite your test to new project infrastructure!"); 62 } 63 64 public static FileSystem findFileSystem(String pattern) { 65 throw new UnsupportedOperationException ("Method is no longer supported. " + 66 "Rewrite your test to new project infrastructure!"); 67 } 68 69 public static JavaClass findClass(String s) { 70 JavaClass result; 71 int i = 20; 72 do { 73 result = (JavaClass) JavaMetamodel.getManager().getDefaultExtent().getType().resolve(s); 74 if (result instanceof UnresolvedClass) { 75 try { 76 Thread.sleep(1000); 77 } catch (InterruptedException e) { 78 e.printStackTrace(); 79 return null; 80 } 81 } 82 i--; 83 } while ((result instanceof UnresolvedClass) && i > 0); 84 if (result instanceof UnresolvedClass) { 85 throw new IllegalStateException ("Class " + s + " not found."); 86 } 87 return result; 88 } 89 90 public static StyledDocument getDocument(Element element) { 91 try { 92 Resource res = element.getResource(); 93 DataObject dob = JavaMetamodel.getManager().getDataObject(res); 94 EditorCookie ec = (EditorCookie) dob.getCookie(EditorCookie.class); 95 return ec.openDocument(); 96 } catch (IOException ioe) { 97 throw new RuntimeException (ioe); 98 } 99 } 100 101 public static void saveAll() { 102 LifecycleManager.getDefault().saveAll(); 103 } 104 105 public static void addModified(Element el) { 106 JavaMetamodel.getManager().addModified(JavaMetamodel.getManager().getFileObject(el.getResource())); 107 } 108 109 110 111 112 } 113 | Popular Tags |