diff options
author | Fred Bricon | 2015-03-13 11:50:02 +0000 |
---|---|---|
committer | Fred Bricon | 2015-03-18 06:11:48 +0000 |
commit | 6eb6db0ce9f1f9169406f2d9846c09b84477a909 (patch) | |
tree | 65444154a199db9d4e42b12a753dbb84a4e1329f /org.eclipse.m2e.core.ui | |
parent | fb1b637af59064991edcf6ea737e5d200bfa9ab9 (diff) | |
download | m2e-core-6eb6db0ce9f1f9169406f2d9846c09b84477a909.tar.gz m2e-core-6eb6db0ce9f1f9169406f2d9846c09b84477a909.tar.xz m2e-core-6eb6db0ce9f1f9169406f2d9846c09b84477a909.zip |
335720 : Add a preference to automatically update configuration
The preference is set to true by default.
Change-Id: I74b4fbc1582868d18939b36f0c87cc7617f8b938
Task-Url: https://bugs.eclipse.org/bugs/show_bug.cgi?id=335720
Signed-off-by: Fred Bricon <fbricon@gmail.com>
Diffstat (limited to 'org.eclipse.m2e.core.ui')
6 files changed, 181 insertions, 3 deletions
diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/M2EUIPluginActivator.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/M2EUIPluginActivator.java index 8741f64f..8245fd0e 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/M2EUIPluginActivator.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/M2EUIPluginActivator.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008-2010 Sonatype, Inc. + * Copyright (c) 2008-2015 Sonatype, Inc. and others * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -7,6 +7,7 @@ * * Contributors: * Sonatype, Inc. - initial API and implementation + * Fred Bricon (Red Hat, Inc.) - auto update project configuration *******************************************************************************/ package org.eclipse.m2e.core.ui.internal; @@ -15,6 +16,9 @@ import org.osgi.framework.BundleContext; import org.osgi.framework.ServiceReference; import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResourceChangeEvent; +import org.eclipse.core.resources.IWorkspace; +import org.eclipse.core.resources.ResourcesPlugin; import org.eclipse.core.runtime.CoreException; import org.eclipse.core.runtime.preferences.InstanceScope; import org.eclipse.jface.preference.IPreferenceStore; @@ -29,6 +33,7 @@ import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.internal.index.filter.FilteredIndex; import org.eclipse.m2e.core.internal.lifecyclemapping.discovery.IMavenDiscovery; import org.eclipse.m2e.core.ui.internal.console.MavenConsoleImpl; +import org.eclipse.m2e.core.ui.internal.project.MavenUpdateConfigurationChangeListener; import org.eclipse.m2e.core.ui.internal.search.util.IndexSearchEngine; import org.eclipse.m2e.core.ui.internal.search.util.SearchEngine; import org.eclipse.m2e.core.ui.internal.wizards.IMavenDiscoveryUI; @@ -63,14 +68,26 @@ public class M2EUIPluginActivator extends AbstractUIPlugin { private MavenConsoleImpl console; + private MavenUpdateConfigurationChangeListener mavenUpdateConfigurationChangeListener; + @Override public void start(BundleContext context) throws Exception { super.start(context); + + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + mavenUpdateConfigurationChangeListener = new MavenUpdateConfigurationChangeListener(); + workspace.addResourceChangeListener(mavenUpdateConfigurationChangeListener, IResourceChangeEvent.POST_CHANGE); + } @Override public void stop(BundleContext context) throws Exception { super.stop(context); + + IWorkspace workspace = ResourcesPlugin.getWorkspace(); + workspace.removeResourceChangeListener(this.mavenUpdateConfigurationChangeListener); + this.mavenUpdateConfigurationChangeListener = null; + } public static M2EUIPluginActivator getDefault() { diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java index c953a501..cd06f8a1 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/Messages.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008-2014 Sonatype, Inc. and others. + * Copyright (c) 2008-2015 Sonatype, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -389,6 +389,8 @@ public class Messages extends NLS { public static String MavenPreferencePage_update; + public static String MavenPreferencePage_autoUpdateProjectConfiguration; + public static String MavenPreferencePage_updateProjectRequired_title; public static String MavenProjectPreferencePage_btnResolve; diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties index 05d56ce4..bcfc3c21 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/messages.properties @@ -224,6 +224,7 @@ MavenPomWizard_title=Maven POM wizard MavenPreferencePage_download=Download repository index updates on startup MavenPreferencePage_hide=Hide folders of physically nested modules (experimental) MavenPreferencePage_update=Update Maven projects on startup +MavenPreferencePage_autoUpdateProjectConfiguration=Automatically update Maven projects configuration (experimental) MavenPreferencePage_updateProjectRequired_title=Update project required MavenPreferencePage_warnIncompleteMapping=Hide warning for incomplete mapping MavenPreferencePage_changingPreferencesRequiresProjectUpdate=Changing the checksum policy requires updating the Maven projects for the changes to take effect. Do you want to update projects now? diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenPreferencePage.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenPreferencePage.java index 75d57d5d..ec256cdc 100644 --- a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenPreferencePage.java +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/preferences/MavenPreferencePage.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008-2014 Sonatype, Inc. and others. + * Copyright (c) 2008-2015 Sonatype, Inc. and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -82,6 +82,10 @@ public class MavenPreferencePage extends FieldEditorPreferencePage implements IW Messages.MavenPreferencePage_update, // getFieldEditorParent())); + addField(new BooleanFieldEditor(MavenPreferenceConstants.P_AUTO_UPDATE_CONFIGURATION, // + Messages.MavenPreferencePage_autoUpdateProjectConfiguration, // + getFieldEditorParent())); + addField(new BooleanFieldEditor(MavenPreferenceConstants.P_HIDE_FOLDERS_OF_NESTED_PROJECTS, // Messages.MavenPreferencePage_hide, getFieldEditorParent())); diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/MavenUpdateConfigurationChangeListener.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/MavenUpdateConfigurationChangeListener.java new file mode 100644 index 00000000..c75bfc37 --- /dev/null +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/MavenUpdateConfigurationChangeListener.java @@ -0,0 +1,68 @@ +/************************************************************************************* + * Copyright (c) 2014-2015 Red Hat, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ + +package org.eclipse.m2e.core.ui.internal.project; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IResourceChangeEvent; +import org.eclipse.core.resources.IResourceChangeListener; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.jobs.Job; + +import org.eclipse.m2e.core.MavenPlugin; +import org.eclipse.m2e.core.ui.internal.UpdateMavenProjectJob; + + +/** + * Listens for Out-Of-Date project configuration {@link IMarker}s added to Maven projects. Triggers an + * {@link UpdateMavenProjectJob} when such markers are found. + * + * @author Fred Bricon + */ +public class MavenUpdateConfigurationChangeListener implements IResourceChangeListener { + + private static Logger LOG = LoggerFactory.getLogger(MavenUpdateConfigurationChangeListener.class); + + @Override + public void resourceChanged(IResourceChangeEvent event) { + if(isDisabled()) { + return; + } + List<IProject> outOfDateProjects = null; + try { + OutOfDateConfigurationDeltaVisitor visitor = new OutOfDateConfigurationDeltaVisitor(); + event.getDelta().accept(visitor); + outOfDateProjects = visitor.outOfDateProjects; + } catch(CoreException e) { + LOG.error("An error occurred while checking for out-of-date configuration markers", e); + return; + } + updateProjectConfiguration(outOfDateProjects); + } + + private boolean isDisabled() { + return !MavenPlugin.getMavenConfiguration().isAutomaticallyUpdateConfiguration(); + } + + protected void updateProjectConfiguration(List<IProject> outOfDateProjects) { + if(outOfDateProjects != null && !outOfDateProjects.isEmpty()) { + LOG.debug("Automatic update of {}", outOfDateProjects); + Job updateJob = new UpdateMavenProjectJob(outOfDateProjects.toArray(new IProject[outOfDateProjects.size()])); + updateJob.schedule(); + } + } +} diff --git a/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/OutOfDateConfigurationDeltaVisitor.java b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/OutOfDateConfigurationDeltaVisitor.java new file mode 100644 index 00000000..145e120b --- /dev/null +++ b/org.eclipse.m2e.core.ui/src/org/eclipse/m2e/core/ui/internal/project/OutOfDateConfigurationDeltaVisitor.java @@ -0,0 +1,86 @@ +/************************************************************************************* + * Copyright (c) 2014-2015 Red Hat, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * JBoss by Red Hat - Initial implementation. + ************************************************************************************/ + +package org.eclipse.m2e.core.ui.internal.project; + +import java.util.ArrayList; +import java.util.List; + +import org.eclipse.core.resources.IMarker; +import org.eclipse.core.resources.IMarkerDelta; +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.runtime.CoreException; + +import org.eclipse.m2e.core.internal.IMavenConstants; +import org.eclipse.m2e.core.internal.Messages; + + +/** + * Detects Out-Of-Date project configuration {@link IMarker}s on projects + * + * @author Fred Bricon + */ +public class OutOfDateConfigurationDeltaVisitor implements IResourceDeltaVisitor { + + List<IProject> outOfDateProjects = new ArrayList<>(); + + @Override + public boolean visit(IResourceDelta delta) throws CoreException { + switch(delta.getKind()) { + case IResourceDelta.CHANGED: + int flags = delta.getFlags(); + if((flags & IResourceDelta.MARKERS) != 0) { + IResource res = delta.getResource(); + if(res == null) { + return true; + } + IProject project = res.getProject(); + if(project == null || !isMavenProject(project) || outOfDateProjects.contains(project)) { + return false; + } + + IMarkerDelta[] markers = delta.getMarkerDeltas(); + boolean hasOutOfDateMarker = containsOutOfDateMarkers(markers); + if(hasOutOfDateMarker) { + outOfDateProjects.add(project); + } + return false; + } + return true; + default: + break; + } + return false; // visit the children + } + + private boolean containsOutOfDateMarkers(IMarkerDelta[] markers) throws CoreException { + if(markers != null && markers.length > 0) { + for(IMarkerDelta markerDelta : markers) { + IMarker marker = markerDelta.getMarker(); + if(marker.exists()) { + String message = (String) marker.getAttribute(IMarker.MESSAGE); + if(Messages.ProjectConfigurationUpdateRequired.equals(message)) { + return true; + } + } + } + } + return false; + } + + private boolean isMavenProject(IProject project) throws CoreException { + return project != null && project.isAccessible() && project.hasNature(IMavenConstants.NATURE_ID); + } + +} |