KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > projects > APITests > utils > Utilities


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-2006s Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20
21 package projects.APITests.utils;
22
23 import java.io.File JavaDoc;
24 import org.netbeans.api.project.FileOwnerQuery;
25 import org.netbeans.api.project.Project;
26 import org.netbeans.api.project.ProjectUtils;
27 import org.netbeans.api.project.ui.OpenProjects;
28 import org.openide.ErrorManager;
29 import org.openide.filesystems.FileUtil;
30 import org.openide.util.Mutex;
31
32 /**
33  *
34  * @author jaromiruhrik
35  */

36 public class Utilities {
37     
38     /** Creates a new instance of Utilities */
39     public Utilities() {
40     }
41     /** Waits until metadata scanning is finished. */
42     public static boolean waitScanFinished() {
43         return true;
44     }
45     
46     /** Opens project in specified directory.
47      * @param projectDir a directory with project to open
48      * @return Project instance of opened project
49      */

50     public static Object JavaDoc openProject(File JavaDoc projectDir) {
51         final ProjectOpenListener listener = new ProjectOpenListener();
52         try {
53             // open project
54
final Project project = FileOwnerQuery.getOwner(FileUtil.toFileObject(projectDir));
55             // posting the to AWT event thread
56
Mutex.EVENT.writeAccess(new Runnable JavaDoc() {
57                 public void run() {
58                     OpenProjects.getDefault().addPropertyChangeListener(listener);
59                     OpenProjects.getDefault().open(new Project[]{project},false);
60                     // Set main? Probably user should do this if he wants.
61
// OpenProjectList.getDefault().setMainProject(project);
62
}
63             });
64             // WAIT PROJECT OPEN - start
65
// We need to wait until project is open and then we can start to
66
// wait when scanning finishes. If we don't wait, scanning is started
67
// too early and finishes immediatelly.
68
Thread JavaDoc waitThread = new Thread JavaDoc(new Runnable JavaDoc() {
69                 public void run() {
70                     while(!listener.projectOpened) {
71                         try {
72                             Thread.sleep(50);
73                         } catch (Exception JavaDoc e) {
74                             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, e);
75                         }
76                     }
77                 }
78             });
79             waitThread.start();
80             try {
81                 waitThread.join(60000L); // wait 1 minute at the most
82
} catch (InterruptedException JavaDoc iex) {
83                 ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, iex);
84             }
85             if (waitThread.isAlive()) {
86                 // time-out expired, project not opened -> interrupt the wait thread
87
ErrorManager.getDefault().log(ErrorManager.USER, "Project not opened in 60 second.");
88                 waitThread.interrupt();
89             }
90             // WAIT PROJECT OPEN - end
91
// wait until metadata scanning is finished
92
waitScanFinished();
93             return project;
94         } catch (Exception JavaDoc ex) {
95             ErrorManager.getDefault().notify(ErrorManager.EXCEPTION, ex);
96             return null;
97         } finally {
98             OpenProjects.getDefault().removePropertyChangeListener(listener);
99         }
100     }
101     /** Opens project on specified path.
102      * @param projectPath path to a directory with project to open
103      * @return Project instance of opened project
104      */

105     public static Object JavaDoc openProject(String JavaDoc projectPath) {
106         return openProject(new File JavaDoc(projectPath));
107     }
108     
109     /** Closes project with given name.
110      * @param name system or display name of project to be closed.
111      * @return true if project is closed, false in other cases.
112      */

113     public static boolean closeProject(String JavaDoc name){
114         Project[] projectList = OpenProjects.getDefault().getOpenProjects();
115         for (int i = 0; i < projectList.length; i++) {
116             final Project project = projectList[i];
117             if(ProjectUtils.getInformation(project).getDisplayName().equals(name) || ProjectUtils.getInformation(project).getName().equals(name)){
118                 Mutex.EVENT.writeAccess(new Runnable JavaDoc(){
119                     public void run(){
120                         OpenProjects.getDefault().close(new Project[] {project});
121                     }
122                 });
123                 return true;
124             }
125         }
126         return false;
127     }
128     
129     public static boolean deleteProjectFolder(String JavaDoc projectFolder){
130         File JavaDoc folder = new File JavaDoc(projectFolder);
131         return deleteDirContent(folder);
132     }
133     
134     private static boolean deleteDirContent(File JavaDoc dir){
135         File JavaDoc[] files = dir.listFiles();
136         if(files != null){
137             for (int i = 0; i < files.length; i++) {
138                 if(files[i].isFile()){
139                     files[i].delete();
140                 }else{
141                     deleteDirContent(files[i]);
142                 }
143             }
144         }
145         return dir.delete();
146     }
147 }
148
Popular Tags