Author: mculpepper(a)jboss.com
Date: 2007-05-22 10:54:49 -0400 (Tue, 22 May 2007)
New Revision: 2057
Added:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesBuilder.java
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesNature.java
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ProjectUtils.java
Log:
more refactoring from src/main to src/eclipse
Added:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesBuilder.java
===================================================================
---
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesBuilder.java
(rev 0)
+++
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesBuilder.java 2007-05-22
14:54:49 UTC (rev 2057)
@@ -0,0 +1,179 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.ide.eclipse.archives.core.project;
+
+import java.util.Comparator;
+import java.util.Map;
+import java.util.TreeSet;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.IResourceDelta;
+import org.eclipse.core.resources.IResourceDeltaVisitor;
+import org.eclipse.core.resources.IWorkspaceRoot;
+import org.eclipse.core.resources.IncrementalProjectBuilder;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.jboss.ide.eclipse.archives.core.ArchivesCore;
+import org.jboss.ide.eclipse.archives.core.build.ArchiveBuildDelegate;
+import org.jboss.ide.eclipse.archives.core.model.ArchivesModel;
+import org.jboss.ide.eclipse.archives.core.model.IArchive;
+import org.jboss.ide.eclipse.archives.core.model.IArchiveFileSet;
+import org.jboss.ide.eclipse.archives.core.model.IArchiveModelNode;
+import org.jboss.ide.eclipse.archives.core.model.IArchiveNode;
+import org.jboss.ide.eclipse.archives.core.model.IArchiveNodeVisitor;
+import org.jboss.ide.eclipse.archives.core.util.internal.TrueZipUtil;
+
+/**
+ * This builder is responsible for building the archives
+ * It delegates to a delegate.
+ * @author Rob Stryker (rob.stryker(a)redhat.com)
+ *
+ */
+public class ArchivesBuilder extends IncrementalProjectBuilder {
+
+ public static final String BUILDER_ID =
"org.jboss.ide.eclipse.archives.core.archivesBuilder";
+
+ /**
+ * Build.
+ * @see IncrementalProjectBuilder#build(int, Map, IProgressMonitor)
+ * @see IProject#build(int, String, Map, IProgressMonitor)
+ */
+ protected IProject[] build(int kind, Map args, IProgressMonitor monitor) throws
CoreException {
+
+ // if we're not to build, get out of here
+ if(
!ArchivesCore.getInstance().getPreferenceManager().isBuilderEnabled(getProject().getLocation()))
+ return new IProject[]{};
+
+ IProject[] interestingProjects = getInterestingProjectsInternal();
+
+ final TreeSet addedChanged = createDefaultTreeSet();
+ final TreeSet removed = createDefaultTreeSet();
+
+ ArchiveBuildDelegate delegate = new ArchiveBuildDelegate();
+ if (kind == IncrementalProjectBuilder.INCREMENTAL_BUILD || kind ==
IncrementalProjectBuilder.AUTO_BUILD) {
+ fillDeltas(interestingProjects, addedChanged, removed);
+ delegate.projectIncrementalBuild(addedChanged, removed);
+
+ } else if (kind == IncrementalProjectBuilder.FULL_BUILD){
+ // build each package fully
+ IProject p = getProject();
+ delegate.fullProjectBuild(p.getLocation());
+ }
+
+ return interestingProjects;
+ }
+
+ /**
+ * Delete all archives that were created or represented by this
+ * project's archive model.
+ */
+ protected void clean(IProgressMonitor monitor) throws CoreException {
+ IPath p = getProject().getLocation();
+ IArchiveModelNode root = ArchivesModel.instance().getRoot(p);
+ IArchiveNode[] nodes = root.getChildren(IArchiveNode.TYPE_ARCHIVE);
+ for( int i = 0; i < nodes.length; i++ ) {
+ IPath path = ((IArchive)nodes[i]).getDestinationPath();
+ TrueZipUtil.deleteAll(path);
+ }
+ }
+
+ /**
+ * Browse through the deltas and fill the treesets with
+ * affected resources.
+ * @param projects The interesting projects
+ * @param addedChanged A collection of resources that have been added or changed
+ * @param removed A collection of resources that have been removed.
+ */
+ protected void fillDeltas(IProject[] projects, final TreeSet addedChanged, final TreeSet
removed) {
+ for( int i = 0; i < projects.length; i++ ) {
+ final IProject proj = projects[i];
+ IResourceDelta delta = getDelta(proj);
+ try {
+ delta.accept(new IResourceDeltaVisitor () {
+ public boolean visit(IResourceDelta delta) throws CoreException {
+ if (delta.getResource().getType() == IResource.FILE) {
+ if( (delta.getKind() & IResourceDelta.ADDED) > 0
+ || (delta.getKind() & IResourceDelta.CHANGED) > 0) {
+
+ // ignore the packages project. that will it's own build call,
+ // or will handle the change in some other way
+ if(
!delta.getResource().equals(proj.findMember(ArchivesModel.PROJECT_PACKAGES_FILE)))
+ addedChanged.add(delta.getResource().getLocation());
+ } else if( (delta.getKind() & IResourceDelta.REMOVED ) > 0 ) {
+ removed.add(delta.getResource().getLocation());
+ }
+ }
+ return true;
+ }
+ });
+ } catch( CoreException ce) {
+
+ }
+ }
+ }
+
+ /**
+ * Get any projects that the current project may depend on
+ * (regarding it's archive model, if any filesets match files in
+ * another project).
+ * @return The list of projects that matter
+ */
+ protected IProject[] getInterestingProjectsInternal() {
+ final TreeSet set = createDefaultTreeSet();
+ set.add(getProject());
+
+ final IWorkspaceRoot workspaceRoot = ResourcesPlugin.getWorkspace().getRoot();
+ final int count = workspaceRoot.getLocation().segmentCount();
+
+ ArchivesModel.instance().getRoot(getProject().getLocation()).accept(new
IArchiveNodeVisitor () {
+ public boolean visit (IArchiveNode node) {
+ if (node.getNodeType() == IArchiveNode.TYPE_ARCHIVE_FILESET) {
+ IArchiveFileSet fileset = (IArchiveFileSet)node;
+ IPath p = fileset.getGlobalSourcePath();
+ if( workspaceRoot.getLocation().isPrefixOf(p)) {
+ IProject proj = workspaceRoot.getProject(p.segment(count));
+ set.add(proj);
+ }
+ }
+ return true;
+ }
+ });
+ return (IProject[]) set.toArray(new IProject[set.size()]);
+ }
+
+ /**
+ * Default treeset with default comparator
+ * @return
+ */
+ protected TreeSet createDefaultTreeSet() {
+ return new TreeSet(new Comparator () {
+ public int compare(Object o1, Object o2) {
+ if (o1.equals(o2)) return 0;
+ else return -1;
+ }
+ });
+ }
+
+}
\ No newline at end of file
Added:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesNature.java
===================================================================
---
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesNature.java
(rev 0)
+++
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ArchivesNature.java 2007-05-22
14:54:49 UTC (rev 2057)
@@ -0,0 +1,120 @@
+/*
+ * JBoss, a division of Red Hat
+ * Copyright 2006, Red Hat Middleware, LLC, and individual contributors as indicated
+ * by the @authors tag. See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site:
http://www.fsf.org.
+ */
+package org.jboss.ide.eclipse.archives.core.project;
+
+import java.util.ArrayList;
+
+import org.eclipse.core.resources.ICommand;
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.IProjectNature;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+
+/**
+ * The nature associated with archives, specifically
+ * the builder is added or removed based on this nature.
+ *
+ * @author Rob Stryker (rob.stryker(a)redhat.com)
+ *
+ */
+public class ArchivesNature implements IProjectNature {
+
+ public static final String NATURE_ID =
"org.jboss.ide.eclipse.archives.core.archivesNature";
+
+ private IProject project;
+
+ public void configure() throws CoreException {
+ addProjectBuilder(project, ArchivesBuilder.BUILDER_ID);
+ }
+
+ public void deconfigure() throws CoreException {
+ removeProjectBuilder(project, ArchivesBuilder.BUILDER_ID);
+ }
+
+ public IProject getProject() {
+ return project;
+ }
+
+ public void setProject(IProject project) {
+ this.project = project;
+ }
+
+ public static boolean addProjectBuilder(IProject project, String builderId)
+ {
+ try
+ {
+ IProjectDescription desc = project.getDescription();
+ ICommand[] commands = desc.getBuildSpec();
+
+ //add builders to project
+ ICommand builderCommand = desc.newCommand();
+ builderCommand.setBuilderName(builderId);
+
+ ICommand[] newCommands = new ICommand[commands.length + 1];
+ System.arraycopy(commands, 0, newCommands, 0, commands.length);
+ newCommands[newCommands.length - 1] = builderCommand;
+
+ desc.setBuildSpec(newCommands);
+
+ project.setDescription(desc, new NullProgressMonitor());
+ }
+ catch (CoreException e)
+ {
+ e.printStackTrace();
+ return false;
+ }
+
+ return true;
+ }
+
+ /**
+ * Removes a specific builder from the end of a given project's builder list.
+ * @param project
+ * @param builderId
+ * @return Whether or not the builder was successfully removed
+ */
+ public static boolean removeProjectBuilder(IProject project, String builderId) {
+ try {
+ IProjectDescription desc = project.getDescription();
+ ICommand[] commands = desc.getBuildSpec();
+ ArrayList newCommands = new ArrayList();
+
+ for (int i = 0; i < commands.length; i++) {
+ if (!commands[i].getBuilderName().equals(builderId)) {
+ newCommands.add(commands[i]);
+ }
+ }
+
+ desc.setBuildSpec((ICommand[]) newCommands.toArray(new
ICommand[newCommands.size()]));
+
+ project.setDescription(desc, new NullProgressMonitor());
+ } catch (CoreException e) {
+ e.printStackTrace();
+ return false;
+ }
+
+ return true;
+ }
+
+
+}
Added:
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ProjectUtils.java
===================================================================
---
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ProjectUtils.java
(rev 0)
+++
trunk/core/plugins/org.jboss.ide.eclipse.archives.core/src/eclipse/org/jboss/ide/eclipse/archives/core/project/ProjectUtils.java 2007-05-22
14:54:49 UTC (rev 2057)
@@ -0,0 +1,46 @@
+package org.jboss.ide.eclipse.archives.core.project;
+
+import org.eclipse.core.resources.IProject;
+import org.eclipse.core.resources.IProjectDescription;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IPath;
+import org.eclipse.core.runtime.NullProgressMonitor;
+
+public class ProjectUtils {
+
+ public static boolean addProjectNature(IPath path) {
+ String loc = ResourcesPlugin.getWorkspace().getRoot().getLocation().toOSString();
+ if( path.toOSString().startsWith(loc)) {
+ String proj = path.toOSString().substring(loc.length()+1);
+ IProject p = ResourcesPlugin.getWorkspace().getRoot().getProject(proj);
+ return addProjectNature(p, ArchivesNature.NATURE_ID);
+ }
+ return false;
+ }
+
+ public static boolean addProjectNature(IProject project, String natureId) {
+ boolean added = false;
+ try {
+ if (project != null && natureId != null) {
+ IProjectDescription desc = project.getDescription();
+
+ if (!project.hasNature(natureId)) {
+ String natureIds[] = desc.getNatureIds();
+ String newNatureIds[] = new String[natureIds.length + 1];
+
+ System.arraycopy(natureIds, 0, newNatureIds, 1, natureIds.length);
+ newNatureIds[0] = natureId;
+ desc.setNatureIds(newNatureIds);
+
+ project.getProject().setDescription(desc, new NullProgressMonitor());
+ added = true;
+ }
+ }
+ } catch (CoreException e) {
+ e.printStackTrace();
+ }
+ return added;
+ }
+
+}