Eugene Goldfarb
+ *
+ */
+public class WorkspaceResourceChangeListener implements IResourceChangeListener {
+
+ public void resourceChanged(IResourceChangeEvent event) {
+
+ try {
+ if (event.getType() == IResourceChangeEvent.PRE_CLOSE
+ || event.getType() == IResourceChangeEvent.PRE_DELETE) {
+
+ if (IvyPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.AUTO_RESOLVE_ON_CLOSE)) {
+
+ // Check if one of Ivy projects is being removed
+ final IResource res = event.getResource();
+ final IJavaProject javaProject = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProject(res);
+ if (javaProject != null) {
+ IPath path = null;
+ IClasspathEntry[] entries = javaProject.getRawClasspath();
+ for (int i= 0; i < entries.length; i++) {
+ IClasspathEntry entry= entries[i];
+ if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER &&
+ IvyClasspathContainer.isIvyClasspathContainer(entry.getPath())) {
+ path = entry.getPath();
+ break;
+ }
+ }
+
+ if (path != null) {
+ // Found an Ivy container in this project -- notify dependent projects to perform fresh resolve
+
+ // Let's try to be nice and use the workspace method
+ // to schedule resolves in dependent projects after the close operation
+ // has finished.
+ IWorkspace workspace = ResourcesPlugin.getWorkspace();
+ IResourceRuleFactory ruleFactory = workspace.getRuleFactory();
+ ISchedulingRule modifyRule = ruleFactory.modifyRule(res);
+ class IvyClosedProjectJob extends WorkspaceJob {
+
+ public IvyClosedProjectJob() {
+ super("IvyClosedProjectJob");
+ }
+ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
+ resolveAffectedProjects(javaProject.getPath());
+ return Status.OK_STATUS;
+ }
+ };
+ IvyClosedProjectJob job = new IvyClosedProjectJob();
+ job.setRule(modifyRule);
+ job.schedule();
+
+ }
+ }
+ }
+ } else if (event.getType() == IResourceChangeEvent.POST_CHANGE) {
+
+ if (IvyPlugin.getDefault().getPreferenceStore().getBoolean(PreferenceConstants.AUTO_RESOLVE_ON_OPEN)) {
+
+ /* FIXME: This is not working yet, it triggers too many events
+ // Find out if a project was opened.
+ IResourceDelta delta = event.getDelta();
+
+ if (delta != null) {
+ final IResourceDelta[] projDeltas = delta.getAffectedChildren(IResourceDelta.CHANGED);
+ String deltaString = ((ResourceDelta)delta).toDeepDebugString();
+ if (projDeltas.length > 0) {
+ final Collection projects = new LinkedHashSet(projDeltas.length);
+ for (int i =0; i < projDeltas.length; i++) {
+ if ((projDeltas[i].getFlags() & IResourceDelta.OPEN) != 0) {
+ IResourceDelta rootDelta = projDeltas[i];
+ IResourceDelta[] childDeltas = rootDelta.getAffectedChildren();
+ if (childDeltas.length>0 && (childDeltas[0].getFlags() & IResourceDelta.ADDED) != 0)
+ projects.add(projDeltas[i].getResource());
+ }
+ }
+
+ if (projects.size() > 0) {
+ // Found an Ivy container in this project -- notify dependent projects to perform fresh resolve
+ // Let's try to be nice and use the workspace method
+ // to schedule resolves in dependent projects after the close operation
+ // has finished.
+ class IvyOpenProjectJob extends WorkspaceJob {
+
+ public IvyOpenProjectJob() {
+ super("IvyOpenProjectJob");
+ }
+ public IStatus runInWorkspace(IProgressMonitor monitor) throws CoreException {
+ resolveAllProjectsExcept(projects);
+ return Status.OK_STATUS;
+ }
+ };
+ IvyOpenProjectJob job = new IvyOpenProjectJob();
+ job.schedule();
+ }
+ }
+ }
+ */
+ }
+ }
+ } catch (JavaModelException jme) {
+ IvyPlugin.log(IStatus.ERROR, "Errors occurred trying to find projects affected by closure", jme);
+ } catch (OperationCanceledException oce) {
+ IvyPlugin.log(IStatus.CANCEL, "Ivy update of dependent proejects affected by project close operation canceled", null);
+ } catch (CoreException ce) {
+ IvyPlugin.log(IStatus.ERROR, "Errors occurred trying to find projects affected by closure", ce);
+
+ }
+ }
+
+ /*
+ * Only resolve those projects which include the specified project path as ivy dependency
+ */
+ private void resolveAffectedProjects (IPath projectPath) {
+
+ try {
+ IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
+ IJavaProject[] projects= JavaCore.create(root).getJavaProjects();
+
+ for (int i= 0; i < projects.length; i++) {
+ IJavaProject javaProject= projects[i];
+ IClasspathEntry[] entries= javaProject.getRawClasspath();
+ for (int k= 0; k < entries.length; k++) {
+ IClasspathEntry entry= entries[k];
+ if (entry != null && entry.getEntryKind() == IClasspathEntry.CPE_CONTAINER) {
+ IPath path = entry.getPath();
+ if (IvyClasspathContainer.isIvyClasspathContainer(path)) {
+ IClasspathContainer cp = JavaCore.getClasspathContainer(path, javaProject);
+ if (cp instanceof IvyClasspathContainer) {
+ IvyClasspathContainer c = (IvyClasspathContainer)cp;
+ IClasspathEntry[] containerEntries = c.getClasspathEntries();
+ for (int j=0; j < containerEntries.length; j++) {
+ IClasspathEntry containerEntry = containerEntries[j];
+ if (containerEntry!=null &&
+ containerEntry.getEntryKind() == IClasspathEntry.CPE_PROJECT &&
+ containerEntry.getPath().equals(projectPath)) {
+ c.resolve();
+ break;
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+ } catch (JavaModelException e) {
+ IvyPlugin.log(IStatus.ERROR, "Errors occurred trying to find dependent projects", e);
+ }
+ }
+
+ private void resolveAllProjectsExcept (Collection sourceProjects) {
+ try {
+ IJavaProject[] projects = JavaModelManager.getJavaModelManager().getJavaModel().getJavaProjects();
+ for (int i = 0; i < projects.length; i++) {
+ if (!sourceProjects.contains(projects[i])) {
+ IvyClasspathContainer.resolve(projects[i]);
+ }
+ }
+ } catch (JavaModelException e) {
+ IvyPlugin.log(IStatus.ERROR, "Errors occurred trying to resolve all projects", e);
+ }
+ }
+
+}
Property changes on: src\java\org\jayasoft\ivyde\eclipse\resolver\WorkspaceResourceChangeListener.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Author Date Revision Id URL
Name: svn:eol-style
+ native
Index: src/java/org/jayasoft/ivyde/eclipse/ui/preferences/PreferenceConstants.java
===================================================================
--- src/java/org/jayasoft/ivyde/eclipse/ui/preferences/PreferenceConstants.java (revision 608)
+++ src/java/org/jayasoft/ivyde/eclipse/ui/preferences/PreferenceConstants.java (working copy)
@@ -20,5 +20,8 @@
public static final String P_STRING = "stringPreference";
public static final String DO_RETRIEVE = "do.retreive";
public static final String RETRIEVE_PATTERN = "retreive.pattern";
-
+
+ public static final String AUTO_RESOLVE_ON_OPEN = "autoResolve.open";
+ public static final String AUTO_RESOLVE_ON_CLOSE = "autoResolve.close";
+
}
Index: src/java/org/jayasoft/ivyde/eclipse/ui/preferences/WorkspaceResolverPreferencePage.java
===================================================================
--- src/java/org/jayasoft/ivyde/eclipse/ui/preferences/WorkspaceResolverPreferencePage.java (revision 0)
+++ src/java/org/jayasoft/ivyde/eclipse/ui/preferences/WorkspaceResolverPreferencePage.java (revision 0)
@@ -0,0 +1,88 @@
+package org.jayasoft.ivyde.eclipse.ui.preferences;
+
+import java.io.File;
+import java.net.MalformedURLException;
+import org.eclipse.jface.preference.BooleanFieldEditor;
+import org.eclipse.jface.preference.FieldEditorPreferencePage;
+import org.eclipse.jface.preference.FileFieldEditor;
+import org.eclipse.jface.preference.StringFieldEditor;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.events.SelectionAdapter;
+import org.eclipse.swt.events.SelectionEvent;
+import org.eclipse.swt.layout.GridData;
+import org.eclipse.swt.widgets.Button;
+import org.eclipse.swt.widgets.Composite;
+import org.eclipse.swt.widgets.Label;
+import org.eclipse.ui.IWorkbench;
+import org.eclipse.ui.IWorkbenchPreferencePage;
+import org.jayasoft.ivyde.eclipse.IvyPlugin;
+
+
+/**
+ * This class represents a preference page that
+ * is contributed to the Preferences dialog. By
+ * subclassing FieldEditorPreferencePage, we
+ * can use the field support built into JFace that allows
+ * us to create a page that is small and knows how to
+ * save, restore and apply itself.
+ *
+ * This page is used to modify preferences only. They
+ * are stored in the preference store that belongs to
+ * the main plug-in class. That way, preferences can
+ * be accessed directly via the preference store.
+ */
+
+public class WorkspaceResolverPreferencePage
+ extends FieldEditorPreferencePage
+ implements IWorkbenchPreferencePage {
+
+ public WorkspaceResolverPreferencePage() {
+ super(GRID);
+ setPreferenceStore(IvyPlugin.getDefault().getPreferenceStore());
+ setDescription("");
+ }
+
+ /**
+ * Creates the field editors. Field editors are abstractions of
+ * the common GUI blocks needed to manipulate various types
+ * of preferences. Each field editor knows how to save and
+ * restore itself.
+ */
+ public void createFieldEditors() {
+ final Composite fieldParent = getFieldEditorParent();
+
+ Label spacer = new Label(fieldParent, SWT.NONE);
+ GridData spacerData = new GridData();
+ spacerData.horizontalSpan = 3;
+ spacer.setLayoutData(spacerData);
+ spacer.setText("Workspace Resolver");
+ spacer = new Label(fieldParent, SWT.SEPARATOR | SWT.HORIZONTAL );
+ spacer.setLayoutData(new GridData(GridData.FILL, GridData.CENTER, true, false, 3, 1));
+
+ BooleanFieldEditor aro = new BooleanFieldEditor(PreferenceConstants.AUTO_RESOLVE_ON_OPEN, "Auto resolve on project open", fieldParent);
+ aro.setEnabled(false, fieldParent); // TODO: enable when it works
+ addField(aro);
+
+ new Label(fieldParent, SWT.NONE); // space
+ Label explanation = new Label(fieldParent, SWT.NONE);
+ explanation.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, 2, 1));
+ explanation.setText("Will automatically resolve projects in the workspace and link open project where necessary");
+ new Label(fieldParent, SWT.NONE).setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, 3, 1)); // space
+
+ addField(new BooleanFieldEditor(PreferenceConstants.AUTO_RESOLVE_ON_CLOSE, "Auto resolve on project close", fieldParent));
+
+ explanation = new Label(fieldParent, SWT.NONE);
+ explanation.setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, 2, 1));
+ explanation.setText("Will automatically resolve projects in the workspace after dependent project is closed");
+ new Label(fieldParent, SWT.NONE).setLayoutData(new GridData(GridData.FILL, GridData.BEGINNING, false, false, 3, 1)); // space
+
+ }
+
+
+ /* (non-Javadoc)
+ * @see org.eclipse.ui.IWorkbenchPreferencePage#init(org.eclipse.ui.IWorkbench)
+ */
+ public void init(IWorkbench workbench) {
+ }
+
+}
\ No newline at end of file
Property changes on: src\java\org\jayasoft\ivyde\eclipse\ui\preferences\WorkspaceResolverPreferencePage.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Name: svn:keywords
+ Author Date Revision Id URL
Name: svn:eol-style
+ native