PHP perspective and new Project Wizard
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpeclipse / PHPCore.java
diff --git a/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPCore.java b/net.sourceforge.phpeclipse/src/net/sourceforge/phpeclipse/PHPCore.java
new file mode 100644 (file)
index 0000000..81943ac
--- /dev/null
@@ -0,0 +1,93 @@
+package net.sourceforge.phpeclipse;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IProgressMonitor;
+
+import net.sourceforge.phpeclipse.resourcesview.*;
+import net.sourceforge.phpeclipse.resourcesview.PHPFile;
+import net.sourceforge.phpeclipse.resourcesview.PHPProject;
+
+public class PHPCore {
+
+  public static IProject[] getPHPProjects() {
+    List phpProjectsList = new ArrayList();
+    IProject[] workspaceProjects = PHPeclipsePlugin.getDefault().getWorkspace().getRoot().getProjects();
+
+    for (int i = 0; i < workspaceProjects.length; i++) {
+      IProject iProject = workspaceProjects[i];
+      if (isPHPProject(iProject))
+        phpProjectsList.add(iProject);
+    }
+
+    IProject[] phpProjects = new IProject[phpProjectsList.size()];
+    return (IProject[]) phpProjectsList.toArray(phpProjects); 
+  }
+
+  public static PHPProject getPHPProject(String name) {
+    IProject aProject = PHPeclipsePlugin.getDefault().getWorkspace().getRoot().getProject(name);
+    if (isPHPProject(aProject)) {
+      PHPProject thePHPProject = new PHPProject();
+      thePHPProject.setProject(aProject);
+      return thePHPProject;
+    }
+    return null;
+  }
+
+  public static boolean isPHPProject(IProject aProject) {
+    try {
+      return aProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID);
+    } catch (CoreException e) {
+    }
+
+    return false;
+  }
+
+  public static PHPFile create(IFile aFile) {
+    if (PHPFile.EXTENSION.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+    if (PHPFile.EXTENSION1.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+    if (PHPFile.EXTENSION2.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+    if (PHPFile.EXTENSION3.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+    if (PHPFile.EXTENSION4.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+    if (PHPFile.EXTENSION5.equalsIgnoreCase(aFile.getFileExtension()))
+      return new PHPFile(aFile);
+
+    return null;
+  }
+
+  public static PHPProject create(IProject aProject) {
+    try {
+      if (aProject.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
+        PHPProject project = new PHPProject();
+        project.setProject(aProject);
+        return project;
+      }
+    } catch (CoreException e) {
+      System.err.println("Exception occurred in PHPCore#create(IProject): " + e.toString());
+    }
+
+    return null; 
+  }
+
+  public static void addPHPNature(IProject project, IProgressMonitor monitor) throws CoreException {
+    if (!project.hasNature(PHPeclipsePlugin.PHP_NATURE_ID)) {
+      IProjectDescription description = project.getDescription();
+      String[] prevNatures = description.getNatureIds();
+      String[] newNatures = new String[prevNatures.length + 1];
+      System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length);
+      newNatures[prevNatures.length] = PHPeclipsePlugin.PHP_NATURE_ID;
+      description.setNatureIds(newNatures);
+      project.setDescription(description, monitor);
+    }
+  }
+}
\ No newline at end of file