PHP perspective and new Project Wizard
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / PHPCore.java
1 package net.sourceforge.phpeclipse;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 import org.eclipse.core.resources.IFile;
7 import org.eclipse.core.resources.IProject;
8 import org.eclipse.core.resources.IProjectDescription;
9 import org.eclipse.core.runtime.CoreException;
10 import org.eclipse.core.runtime.IProgressMonitor;
11
12 import net.sourceforge.phpeclipse.resourcesview.*;
13 import net.sourceforge.phpeclipse.resourcesview.PHPFile;
14 import net.sourceforge.phpeclipse.resourcesview.PHPProject;
15
16 public class PHPCore {
17
18   public static IProject[] getPHPProjects() {
19     List phpProjectsList = new ArrayList();
20     IProject[] workspaceProjects = PHPeclipsePlugin.getDefault().getWorkspace().getRoot().getProjects();
21
22     for (int i = 0; i < workspaceProjects.length; i++) {
23       IProject iProject = workspaceProjects[i];
24       if (isPHPProject(iProject))
25         phpProjectsList.add(iProject);
26     }
27
28     IProject[] phpProjects = new IProject[phpProjectsList.size()];
29     return (IProject[]) phpProjectsList.toArray(phpProjects); 
30   }
31
32   public static PHPProject getPHPProject(String name) {
33     IProject aProject = PHPeclipsePlugin.getDefault().getWorkspace().getRoot().getProject(name);
34     if (isPHPProject(aProject)) {
35       PHPProject thePHPProject = new PHPProject();
36       thePHPProject.setProject(aProject);
37       return thePHPProject;
38     }
39     return null;
40   }
41
42   public static boolean isPHPProject(IProject aProject) {
43     try {
44       return aProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID);
45     } catch (CoreException e) {
46     }
47
48     return false;
49   }
50
51   public static PHPFile create(IFile aFile) {
52     if (PHPFile.EXTENSION.equalsIgnoreCase(aFile.getFileExtension()))
53       return new PHPFile(aFile);
54     if (PHPFile.EXTENSION1.equalsIgnoreCase(aFile.getFileExtension()))
55       return new PHPFile(aFile);
56     if (PHPFile.EXTENSION2.equalsIgnoreCase(aFile.getFileExtension()))
57       return new PHPFile(aFile);
58     if (PHPFile.EXTENSION3.equalsIgnoreCase(aFile.getFileExtension()))
59       return new PHPFile(aFile);
60     if (PHPFile.EXTENSION4.equalsIgnoreCase(aFile.getFileExtension()))
61       return new PHPFile(aFile);
62     if (PHPFile.EXTENSION5.equalsIgnoreCase(aFile.getFileExtension()))
63       return new PHPFile(aFile);
64
65     return null;
66   }
67
68   public static PHPProject create(IProject aProject) {
69     try {
70       if (aProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
71         PHPProject project = new PHPProject();
72         project.setProject(aProject);
73         return project;
74       }
75     } catch (CoreException e) {
76       System.err.println("Exception occurred in PHPCore#create(IProject): " + e.toString());
77     }
78
79     return null; 
80   }
81
82   public static void addPHPNature(IProject project, IProgressMonitor monitor) throws CoreException {
83     if (!project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
84       IProjectDescription description = project.getDescription();
85       String[] prevNatures = description.getNatureIds();
86       String[] newNatures = new String[prevNatures.length + 1];
87       System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
88       newNatures[prevNatures.length] = PHPeclipsePlugin.PHP_NATURE_ID;
89       description.setNatureIds(newNatures);
90       project.setDescription(description, monitor);
91     }
92   }
93 }