KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > jmi > javamodel > codegen > Utility


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19 package org.netbeans.jmi.javamodel.codegen;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import javax.swing.text.StyledDocument JavaDoc;
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 JavaDoc getFile(File JavaDoc dataDir, String JavaDoc fileName) throws FileStateInvalidException {
54         String JavaDoc result = dataDir.getAbsolutePath() + "/projects/default/src/" + fileName;
55         System.out.println("looking for file: " + result);
56         return new File JavaDoc(result);
57     }
58
59     public static JavaModelPackage getJavaModelPackage(String JavaDoc pattern) {
60         throw new UnsupportedOperationException JavaDoc("Method is no longer supported. " +
61             "Rewrite your test to new project infrastructure!");
62     }
63     
64     public static FileSystem findFileSystem(String JavaDoc pattern) {
65         throw new UnsupportedOperationException JavaDoc("Method is no longer supported. " +
66             "Rewrite your test to new project infrastructure!");
67     }
68     
69     public static JavaClass findClass(String JavaDoc 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 JavaDoc 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 JavaDoc("Class " + s + " not found.");
86         }
87         return result;
88     }
89     
90     public static StyledDocument JavaDoc 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 JavaDoc ioe) {
97             throw new RuntimeException JavaDoc(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