diff options
author | Alexander Kurtakov | 2018-03-16 07:08:55 +0000 |
---|---|---|
committer | Fred Bricon | 2018-03-16 14:41:41 +0000 |
commit | 2929cbe556350a7728d81c63b429caa6b79b2829 (patch) | |
tree | ef9ae546a5236bb62df011a6245b9436826085e1 | |
parent | 488950756d0040364f3f035ad9082e7b8b5cc9f4 (diff) | |
download | m2e-core-2929cbe556350a7728d81c63b429caa6b79b2829.tar.gz m2e-core-2929cbe556350a7728d81c63b429caa6b79b2829.tar.xz m2e-core-2929cbe556350a7728d81c63b429caa6b79b2829.zip |
Remove unused imports.
Change-Id: Ibdfd32843e5dbeae4b25afe4feb16b6aa9bd6040
Signed-off-by: Alexander Kurtakov <akurtako@redhat.com>
2 files changed, 566 insertions, 577 deletions
diff --git a/org.eclipse.m2e.profiles.core/src/org/eclipse/m2e/profiles/core/internal/management/ProfileManager.java b/org.eclipse.m2e.profiles.core/src/org/eclipse/m2e/profiles/core/internal/management/ProfileManager.java index fac26d7e..04ffae3e 100644 --- a/org.eclipse.m2e.profiles.core/src/org/eclipse/m2e/profiles/core/internal/management/ProfileManager.java +++ b/org.eclipse.m2e.profiles.core/src/org/eclipse/m2e/profiles/core/internal/management/ProfileManager.java @@ -8,18 +8,22 @@ * Contributors: * Fred Bricon / JBoss by Red Hat - Initial implementation. ************************************************************************************/ + package org.eclipse.m2e.profiles.core.internal.management; import java.io.File; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; -import java.util.HashSet; -import java.util.Iterator; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; -import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.osgi.util.NLS; + +import org.codehaus.plexus.component.repository.exception.ComponentLookupException; import org.apache.maven.artifact.Artifact; import org.apache.maven.artifact.InvalidRepositoryException; @@ -32,14 +36,8 @@ import org.apache.maven.project.MavenProject; import org.apache.maven.repository.RepositorySystem; import org.apache.maven.settings.Settings; import org.apache.maven.settings.SettingsUtils; -import org.codehaus.plexus.component.repository.exception.ComponentLookupException; -import org.codehaus.plexus.util.StringUtils; -import org.eclipse.core.resources.IProject; -import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.core.runtime.NullProgressMonitor; + import org.eclipse.m2e.core.MavenPlugin; -import org.eclipse.m2e.core.embedder.ArtifactKey; import org.eclipse.m2e.core.embedder.IMaven; import org.eclipse.m2e.core.internal.MavenPluginActivator; import org.eclipse.m2e.core.internal.NoSuchComponentException; @@ -49,9 +47,9 @@ import org.eclipse.m2e.core.project.MavenUpdateRequest; import org.eclipse.m2e.core.project.ResolverConfiguration; import org.eclipse.m2e.profiles.core.internal.IProfileManager; import org.eclipse.m2e.profiles.core.internal.MavenProfilesCoreActivator; -import org.eclipse.m2e.profiles.core.internal.ProfileState; import org.eclipse.m2e.profiles.core.internal.ProfileData; -import org.eclipse.osgi.util.NLS; +import org.eclipse.m2e.profiles.core.internal.ProfileState; + /** * Maven Profile Manager @@ -61,248 +59,242 @@ import org.eclipse.osgi.util.NLS; */ public class ProfileManager implements IProfileManager { - public void updateActiveProfiles(final IMavenProjectFacade mavenProjectFacade, - final List<String> profiles, - final boolean isOffline, - final boolean isForceUpdate, - IProgressMonitor monitor) throws CoreException { - if (mavenProjectFacade == null) { - return; - } - final IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager(); - - IProject project = mavenProjectFacade.getProject(); - - final ResolverConfiguration configuration =configurationManager.getResolverConfiguration(project); - - final String profilesAsString = getAsString(profiles); - if (profilesAsString.equals(configuration.getSelectedProfiles())) { - //Nothing changed - return; - } - - configuration.setSelectedProfiles(profilesAsString); - boolean isSet = configurationManager.setResolverConfiguration(project, configuration); - if (isSet) { - MavenUpdateRequest request = new MavenUpdateRequest(project, isOffline, isForceUpdate); - configurationManager.updateProjectConfiguration(request, monitor); - } - - } - - private String getAsString(List<String> profiles) { - StringBuilder sb = new StringBuilder(); - boolean addComma = false; - if (profiles != null){ - for (String p : profiles) { - if (addComma) { - sb.append(", "); //$NON-NLS-1$ - } - sb.append(p); - addComma = true; - } - } - return sb.toString(); - } - - public Map<Profile, Boolean> getAvailableSettingsProfiles() throws CoreException { - Map<Profile, Boolean> settingsProfiles = new LinkedHashMap<Profile, Boolean>(); - Settings settings = MavenPlugin.getMaven().getSettings(); - List<String> activeProfiles = settings.getActiveProfiles(); - - for (org.apache.maven.settings.Profile sp : settings.getProfiles()) { - Profile p = SettingsUtils.convertFromSettingsProfile(sp); - boolean isAutomaticallyActivated = isActive(sp, activeProfiles); - settingsProfiles.put(p, isAutomaticallyActivated); - } - return Collections.unmodifiableMap(settingsProfiles); - } - - private boolean isActive(org.apache.maven.settings.Profile p, List<String> activeProfiles) { - if (p.getActivation() != null && p.getActivation().isActiveByDefault()){ - return true; - } - for (String activeProfile : activeProfiles) { - if (activeProfile.equals(p.getId())) { - return true; - } - } - return false; - } - - public List<ProfileData> getProfileDatas( - IMavenProjectFacade facade, - IProgressMonitor monitor - ) throws CoreException { - if (facade == null) { - return Collections.emptyList(); - } - - ResolverConfiguration resolverConfiguration = MavenPlugin.getProjectConfigurationManager() - .getResolverConfiguration(facade.getProject()); - - List<String> configuredProfiles = toList(resolverConfiguration.getSelectedProfiles()); - - MavenProject mavenProject = facade.getMavenProject(monitor); - - List<Model> modelHierarchy = new ArrayList<>(); - - getModelHierarchy(modelHierarchy, mavenProject.getModel(), monitor); - - List<Profile> availableProfiles = collectAvailableProfiles(modelHierarchy, monitor); - - final Map<Profile, Boolean> availableSettingsProfiles = getAvailableSettingsProfiles(); - - availableProfiles.addAll(availableSettingsProfiles.keySet()); - - List<ProfileData> statuses = new ArrayList<ProfileData>(); - - Map<String, List<String>> allActiveProfiles = mavenProject.getInjectedProfileIds(); - - for (Profile p : availableProfiles) { - String pId = p.getId(); - ProfileData status = new ProfileData(pId); - boolean isDisabled = configuredProfiles.contains("!"+pId); - if (isActive(pId, allActiveProfiles)) { - status.setActivationState(ProfileState.Active); - } else if (isDisabled) { - status.setActivationState(ProfileState.Disabled); - } - boolean isUserSelected = isDisabled || configuredProfiles.contains(pId); - - status.setUserSelected(isUserSelected); - - Boolean isAutoActiveSettingProfile = availableSettingsProfiles.get(p); - boolean isAutoActive = (isAutoActiveSettingProfile != null && isAutoActiveSettingProfile) - || (status.getActivationState().isActive() && !isUserSelected); - status.setAutoActive(isAutoActive ); - status.setSource(findSource(p, modelHierarchy)); - statuses.add(status); - } - - return Collections.unmodifiableList(statuses); - } - - private boolean isActive(String profileId, - Map<String, List<String>> profilesMap) { - - for (Map.Entry<String, List<String>> entry : profilesMap.entrySet()) { - for (String pId : entry.getValue()) { - if (pId.equals(profileId)) { - return true; - } - } - } - - return false; - } - - private List<String> toList(String profilesAsText) { - List<String> profiles; - if (profilesAsText != null && profilesAsText.trim().length() > 0) { - String[] profilesArray = profilesAsText.split("[,\\s\\|]"); - profiles = new ArrayList<String>(profilesArray.length); - for (String profile : profilesArray) { - profiles.add(profile); - } - } else { - profiles = new ArrayList<String>(0); - } - return profiles; - } - - private String findSource(Profile profile, List<Model> modelHierarchy) { - if (profile != null) { - if ("settings.xml".equals(profile.getSource())) { //$NON-NLS-1$ - return profile.getSource(); - } - for (Model m : modelHierarchy) { - for (Profile p : m.getProfiles()) { - if(p.equals(profile)) { - return m.getArtifactId(); - } - } - } - } - return "undefined"; //$NON-NLS-1$ - } - - protected List<Profile> collectAvailableProfiles(List<Model> models, IProgressMonitor monitor) throws CoreException { - List<Profile> profiles = new ArrayList<Profile>(); - for (Model m : models) { - profiles.addAll(m.getProfiles()); - } - return profiles; - } - - protected List<Model> getModelHierarchy(List<Model> models, Model projectModel, IProgressMonitor monitor) throws CoreException { - if (projectModel == null) { - return null; - } - models.add(projectModel); - Parent p = projectModel.getParent(); - if (p != null) { - - IMaven maven = MavenPlugin.getMaven(); - - List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>(); - repositories.addAll(getProjectRepositories(projectModel)); - repositories.addAll(maven.getArtifactRepositories()); - - Model parentModel = resolvePomModel(p.getGroupId(), p.getArtifactId(), p.getVersion(), repositories, monitor); - if (parentModel != null) { - getModelHierarchy(models, parentModel, monitor); - } - } - return models; - } - - private List<ArtifactRepository> getProjectRepositories(Model projectModel) { - List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>(); - List<Repository> modelRepos = projectModel.getRepositories(); - if (modelRepos != null && !modelRepos.isEmpty()) { - RepositorySystem repositorySystem = getRepositorySystem(); - for (Repository modelRepo : modelRepos) { - ArtifactRepository ar; - try { - ar = repositorySystem.buildArtifactRepository(modelRepo); - if (ar != null) { - repos.add(ar); - } - } catch (InvalidRepositoryException e) { - MavenProfilesCoreActivator.log(e); - } - } - } - return repos; - } - - private RepositorySystem getRepositorySystem() { - try { - //TODO find an alternative way to get the Maven RepositorySystem, or use Aether directly to resolve models?? - return MavenPluginActivator.getDefault().getPlexusContainer().lookup(RepositorySystem.class); - } catch (ComponentLookupException e) { - throw new NoSuchComponentException(e); - } - } - - private Model resolvePomModel(String groupId, String artifactId, String version, List<ArtifactRepository> repositories, IProgressMonitor monitor) - throws CoreException { - monitor.subTask(NLS.bind("Resolving {0}:{1}:{2}", new Object[] { groupId, artifactId, version})); - - IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(groupId, artifactId, version); - IMaven maven = MavenPlugin.getMaven(); - - if (facade != null) { - return facade.getMavenProject(monitor).getModel(); - } - - Artifact artifact = maven.resolve(groupId, artifactId, version, "pom", null, repositories, monitor); //$NON-NLS-1$ - File file = artifact.getFile(); - if(file == null) { - return null; - } - - return maven.readModel(file); - } + public void updateActiveProfiles(final IMavenProjectFacade mavenProjectFacade, final List<String> profiles, + final boolean isOffline, final boolean isForceUpdate, IProgressMonitor monitor) throws CoreException { + if(mavenProjectFacade == null) { + return; + } + final IProjectConfigurationManager configurationManager = MavenPlugin.getProjectConfigurationManager(); + + IProject project = mavenProjectFacade.getProject(); + + final ResolverConfiguration configuration = configurationManager.getResolverConfiguration(project); + + final String profilesAsString = getAsString(profiles); + if(profilesAsString.equals(configuration.getSelectedProfiles())) { + //Nothing changed + return; + } + + configuration.setSelectedProfiles(profilesAsString); + boolean isSet = configurationManager.setResolverConfiguration(project, configuration); + if(isSet) { + MavenUpdateRequest request = new MavenUpdateRequest(project, isOffline, isForceUpdate); + configurationManager.updateProjectConfiguration(request, monitor); + } + + } + + private String getAsString(List<String> profiles) { + StringBuilder sb = new StringBuilder(); + boolean addComma = false; + if(profiles != null) { + for(String p : profiles) { + if(addComma) { + sb.append(", "); //$NON-NLS-1$ + } + sb.append(p); + addComma = true; + } + } + return sb.toString(); + } + + public Map<Profile, Boolean> getAvailableSettingsProfiles() throws CoreException { + Map<Profile, Boolean> settingsProfiles = new LinkedHashMap<Profile, Boolean>(); + Settings settings = MavenPlugin.getMaven().getSettings(); + List<String> activeProfiles = settings.getActiveProfiles(); + + for(org.apache.maven.settings.Profile sp : settings.getProfiles()) { + Profile p = SettingsUtils.convertFromSettingsProfile(sp); + boolean isAutomaticallyActivated = isActive(sp, activeProfiles); + settingsProfiles.put(p, isAutomaticallyActivated); + } + return Collections.unmodifiableMap(settingsProfiles); + } + + private boolean isActive(org.apache.maven.settings.Profile p, List<String> activeProfiles) { + if(p.getActivation() != null && p.getActivation().isActiveByDefault()) { + return true; + } + for(String activeProfile : activeProfiles) { + if(activeProfile.equals(p.getId())) { + return true; + } + } + return false; + } + + public List<ProfileData> getProfileDatas(IMavenProjectFacade facade, IProgressMonitor monitor) throws CoreException { + if(facade == null) { + return Collections.emptyList(); + } + + ResolverConfiguration resolverConfiguration = MavenPlugin.getProjectConfigurationManager() + .getResolverConfiguration(facade.getProject()); + + List<String> configuredProfiles = toList(resolverConfiguration.getSelectedProfiles()); + + MavenProject mavenProject = facade.getMavenProject(monitor); + + List<Model> modelHierarchy = new ArrayList<>(); + + getModelHierarchy(modelHierarchy, mavenProject.getModel(), monitor); + + List<Profile> availableProfiles = collectAvailableProfiles(modelHierarchy, monitor); + + final Map<Profile, Boolean> availableSettingsProfiles = getAvailableSettingsProfiles(); + + availableProfiles.addAll(availableSettingsProfiles.keySet()); + + List<ProfileData> statuses = new ArrayList<ProfileData>(); + + Map<String, List<String>> allActiveProfiles = mavenProject.getInjectedProfileIds(); + + for(Profile p : availableProfiles) { + String pId = p.getId(); + ProfileData status = new ProfileData(pId); + boolean isDisabled = configuredProfiles.contains("!" + pId); + if(isActive(pId, allActiveProfiles)) { + status.setActivationState(ProfileState.Active); + } else if(isDisabled) { + status.setActivationState(ProfileState.Disabled); + } + boolean isUserSelected = isDisabled || configuredProfiles.contains(pId); + + status.setUserSelected(isUserSelected); + + Boolean isAutoActiveSettingProfile = availableSettingsProfiles.get(p); + boolean isAutoActive = (isAutoActiveSettingProfile != null && isAutoActiveSettingProfile) + || (status.getActivationState().isActive() && !isUserSelected); + status.setAutoActive(isAutoActive); + status.setSource(findSource(p, modelHierarchy)); + statuses.add(status); + } + + return Collections.unmodifiableList(statuses); + } + + private boolean isActive(String profileId, Map<String, List<String>> profilesMap) { + + for(Map.Entry<String, List<String>> entry : profilesMap.entrySet()) { + for(String pId : entry.getValue()) { + if(pId.equals(profileId)) { + return true; + } + } + } + + return false; + } + + private List<String> toList(String profilesAsText) { + List<String> profiles; + if(profilesAsText != null && profilesAsText.trim().length() > 0) { + String[] profilesArray = profilesAsText.split("[,\\s\\|]"); + profiles = new ArrayList<String>(profilesArray.length); + for(String profile : profilesArray) { + profiles.add(profile); + } + } else { + profiles = new ArrayList<String>(0); + } + return profiles; + } + + private String findSource(Profile profile, List<Model> modelHierarchy) { + if(profile != null) { + if("settings.xml".equals(profile.getSource())) { //$NON-NLS-1$ + return profile.getSource(); + } + for(Model m : modelHierarchy) { + for(Profile p : m.getProfiles()) { + if(p.equals(profile)) { + return m.getArtifactId(); + } + } + } + } + return "undefined"; //$NON-NLS-1$ + } + + protected List<Profile> collectAvailableProfiles(List<Model> models, IProgressMonitor monitor) throws CoreException { + List<Profile> profiles = new ArrayList<Profile>(); + for(Model m : models) { + profiles.addAll(m.getProfiles()); + } + return profiles; + } + + protected List<Model> getModelHierarchy(List<Model> models, Model projectModel, IProgressMonitor monitor) + throws CoreException { + if(projectModel == null) { + return null; + } + models.add(projectModel); + Parent p = projectModel.getParent(); + if(p != null) { + + IMaven maven = MavenPlugin.getMaven(); + + List<ArtifactRepository> repositories = new ArrayList<ArtifactRepository>(); + repositories.addAll(getProjectRepositories(projectModel)); + repositories.addAll(maven.getArtifactRepositories()); + + Model parentModel = resolvePomModel(p.getGroupId(), p.getArtifactId(), p.getVersion(), repositories, monitor); + if(parentModel != null) { + getModelHierarchy(models, parentModel, monitor); + } + } + return models; + } + + private List<ArtifactRepository> getProjectRepositories(Model projectModel) { + List<ArtifactRepository> repos = new ArrayList<ArtifactRepository>(); + List<Repository> modelRepos = projectModel.getRepositories(); + if(modelRepos != null && !modelRepos.isEmpty()) { + RepositorySystem repositorySystem = getRepositorySystem(); + for(Repository modelRepo : modelRepos) { + ArtifactRepository ar; + try { + ar = repositorySystem.buildArtifactRepository(modelRepo); + if(ar != null) { + repos.add(ar); + } + } catch(InvalidRepositoryException e) { + MavenProfilesCoreActivator.log(e); + } + } + } + return repos; + } + + private RepositorySystem getRepositorySystem() { + try { + //TODO find an alternative way to get the Maven RepositorySystem, or use Aether directly to resolve models?? + return MavenPluginActivator.getDefault().getPlexusContainer().lookup(RepositorySystem.class); + } catch(ComponentLookupException e) { + throw new NoSuchComponentException(e); + } + } + + private Model resolvePomModel(String groupId, String artifactId, String version, + List<ArtifactRepository> repositories, IProgressMonitor monitor) throws CoreException { + monitor.subTask(NLS.bind("Resolving {0}:{1}:{2}", new Object[] {groupId, artifactId, version})); + + IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().getMavenProject(groupId, artifactId, version); + IMaven maven = MavenPlugin.getMaven(); + + if(facade != null) { + return facade.getMavenProject(monitor).getModel(); + } + + Artifact artifact = maven.resolve(groupId, artifactId, version, "pom", null, repositories, monitor); //$NON-NLS-1$ + File file = artifact.getFile(); + if(file == null) { + return null; + } + + return maven.readModel(file); + } } diff --git a/org.eclipse.m2e.profiles.ui/src/org/eclipse/m2e/profiles/ui/internal/actions/ProfileSelectionHandler.java b/org.eclipse.m2e.profiles.ui/src/org/eclipse/m2e/profiles/ui/internal/actions/ProfileSelectionHandler.java index e079831b..5009e628 100644 --- a/org.eclipse.m2e.profiles.ui/src/org/eclipse/m2e/profiles/ui/internal/actions/ProfileSelectionHandler.java +++ b/org.eclipse.m2e.profiles.ui/src/org/eclipse/m2e/profiles/ui/internal/actions/ProfileSelectionHandler.java @@ -1,5 +1,5 @@ /************************************************************************************* - * Copyright (c) 2011-2014 Red Hat, Inc. and others. + * Copyright (c) 2011-2018 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 @@ -8,6 +8,7 @@ * Contributors: * Fred Bricon / JBoss by Red Hat - Initial implementation. ************************************************************************************/ + package org.eclipse.m2e.profiles.ui.internal.actions; import java.util.ArrayList; @@ -19,15 +20,16 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import org.eclipse.core.commands.AbstractHandler; import org.eclipse.core.commands.ExecutionEvent; import org.eclipse.core.commands.ExecutionException; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; -import org.eclipse.core.resources.IResource; import org.eclipse.core.resources.WorkspaceJob; import org.eclipse.core.runtime.CoreException; -import org.eclipse.core.runtime.IAdaptable; import org.eclipse.core.runtime.IProgressMonitor; import org.eclipse.core.runtime.IStatus; import org.eclipse.core.runtime.NullProgressMonitor; @@ -41,27 +43,25 @@ import org.eclipse.core.runtime.jobs.JobChangeAdapter; import org.eclipse.jface.dialogs.Dialog; import org.eclipse.jface.dialogs.MessageDialog; import org.eclipse.jface.viewers.ISelection; -import org.eclipse.jface.viewers.IStructuredSelection; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.ui.IEditorInput; +import org.eclipse.ui.IFileEditorInput; +import org.eclipse.ui.IWorkbenchWindow; +import org.eclipse.ui.handlers.HandlerUtil; + import org.eclipse.m2e.core.MavenPlugin; import org.eclipse.m2e.core.internal.IMavenConstants; import org.eclipse.m2e.core.project.IMavenProjectFacade; import org.eclipse.m2e.core.ui.internal.actions.SelectionUtil; import org.eclipse.m2e.profiles.core.internal.IProfileManager; import org.eclipse.m2e.profiles.core.internal.MavenProfilesCoreActivator; -import org.eclipse.m2e.profiles.core.internal.ProfileState; import org.eclipse.m2e.profiles.core.internal.ProfileData; +import org.eclipse.m2e.profiles.core.internal.ProfileState; import org.eclipse.m2e.profiles.ui.internal.MavenProfilesUIActivator; import org.eclipse.m2e.profiles.ui.internal.Messages; import org.eclipse.m2e.profiles.ui.internal.dialog.ProfileSelection; import org.eclipse.m2e.profiles.ui.internal.dialog.SelectProfilesDialog; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.ui.IEditorInput; -import org.eclipse.ui.IFileEditorInput; -import org.eclipse.ui.IWorkbenchWindow; -import org.eclipse.ui.IWorkingSet; -import org.eclipse.ui.handlers.HandlerUtil; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; + /** * Handles profile selection commands. @@ -71,311 +71,308 @@ import org.slf4j.LoggerFactory; */ public class ProfileSelectionHandler extends AbstractHandler { - private static final Logger log = LoggerFactory.getLogger(ProfileSelectionHandler.class); - - /** - * Opens the Maven profile selection Dialog window. - */ - public Object execute(ExecutionEvent event) throws ExecutionException { - IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); - IProject[] projects = getSelectedProjects(event); - return execute(window.getShell(), projects); - } - - private IProject[] getSelectedProjects(ExecutionEvent event) { - ISelection selection = HandlerUtil.getCurrentSelection(event); - IProject[] projects = SelectionUtil.getProjects(selection, false); - if (projects.length == 0) { - IEditorInput input = HandlerUtil.getActiveEditorInput(event); - if (input instanceof IFileEditorInput) { - IFileEditorInput fileInput = (IFileEditorInput) input; - projects = new IProject[] { fileInput.getFile().getProject() }; - } - } - return projects; - } - - public IStatus execute(Shell shell, IProject... projects) throws ExecutionException { - Set<IMavenProjectFacade> facades = getMavenProjects(projects); - if (facades.isEmpty()) { - display(shell, Messages.ProfileSelectionHandler_Select_some_maven_projects); - return null; - } - - final IProfileManager profileManager = MavenProfilesCoreActivator.getDefault().getProfileManager(); - - GetProfilesJob getProfilesJob = new GetProfilesJob(facades, profileManager); - getProfilesJob.addJobChangeListener(onProfilesFetched(getProfilesJob, facades, profileManager, shell)); - getProfilesJob.setUser(true); - getProfilesJob.schedule(); - return Status.OK_STATUS; - } - - private IJobChangeListener onProfilesFetched(final GetProfilesJob getProfilesJob, final Set<IMavenProjectFacade> facades, final IProfileManager profileManager, final Shell shell) { - - return new JobChangeAdapter() { - - @Override - public void done(IJobChangeEvent event) { - if (getProfilesJob.getResult().isOK()) { - shell.getDisplay().syncExec(new Runnable() { - - public void run() { - List<ProfileSelection> sharedProfiles = getProfilesJob.getSharedProfiles(); - Map<IMavenProjectFacade, List<ProfileData>> allProfiles = getProfilesJob.getAllProfiles(); - final SelectProfilesDialog dialog = new SelectProfilesDialog(shell, - facades, - sharedProfiles); - if(dialog.open() == Dialog.OK) { - Job job = new UpdateProfilesJob(allProfiles, sharedProfiles, profileManager, dialog); - job.setRule( MavenPlugin.getProjectConfigurationManager().getRule()); - job.schedule(); - } - } - }); - - } - } - }; - } - - private void display(Shell shell, String message) { - MessageDialog.openInformation( - shell, - Messages.SelectProfilesDialog_Select_Maven_profiles, - message); - } - - /** - * Returns an IMavenProjectFacade from the selected IResource, or from the - * active editor - * - * @param event - * @return the selected IMavenProjectFacade - */ - private Set<IMavenProjectFacade> getMavenProjects(IProject[] projects) { - if (projects == null || projects.length == 0) { - return Collections.emptySet(); - } - Set<IMavenProjectFacade> facades = new HashSet<IMavenProjectFacade>(projects.length); - try { - IProgressMonitor monitor = new NullProgressMonitor(); - for (IProject p : projects) { - if (p != null && p.isAccessible() && p.hasNature(IMavenConstants.NATURE_ID)) { - IFile pom = p.getFile(IMavenConstants.POM_FILE_NAME); - IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(pom, true, monitor); - facades.add(facade); - } - } - } catch (CoreException e) { - log.error("Unable to select Maven projects", e); - } - - return facades; - } - - class GetProfilesJob extends Job { - - private IProfileManager profileManager; - private Set<IMavenProjectFacade> facades; - private Map<IMavenProjectFacade, List<ProfileData>> allProfiles; - private List<ProfileSelection> sharedProfiles; - - private GetProfilesJob(final Set<IMavenProjectFacade> facades, IProfileManager profileManager) { - super(Messages.ProfileSelectionHandler_Loading_maven_profiles); - this.facades = facades; - this.profileManager = profileManager; - } - - @Override - protected IStatus run(IProgressMonitor monitor) { - try { - this.allProfiles = getAllProfiles(facades, profileManager); - this.sharedProfiles = getSharedProfiles(allProfiles); - } catch (CoreException e) { - return new Status(IStatus.ERROR, MavenProfilesUIActivator.PLUGIN_ID, - Messages.ProfileSelectionHandler_Unable_to_open_profile_dialog, e); - } - return Status.OK_STATUS; - } - - private List<ProfileSelection> getSharedProfiles( - Map<IMavenProjectFacade, List<ProfileData>> projectProfilesMap) { - - List<ProfileData> currentSelection = null; - List<List<ProfileData>> projectProfiles = new ArrayList<List<ProfileData>>(projectProfilesMap.values()); - int smallestSize = Integer.MAX_VALUE; - for (List<ProfileData> profiles : projectProfiles) { - int size = profiles.size(); - if (size < smallestSize) { - smallestSize = size; - currentSelection = profiles; - } - } - projectProfiles.remove(currentSelection); - - // Init the smallest profiles selection possible - List<ProfileSelection> selection = new ArrayList<ProfileSelection>(); - for (ProfileData p : currentSelection) { - ProfileSelection ps = new ProfileSelection(); - ps.setId(p.getId()); - ps.setActivationState(p.getActivationState()); - ps.setAutoActive(p.isAutoActive()); - ps.setSource(p.getSource()); - ps.setSelected(p.isUserSelected()); - selection.add(ps); - } - - if (!projectProfiles.isEmpty()) { - // Restrict to the common profiles only - Iterator<ProfileSelection> ite = selection.iterator(); - - while (ite.hasNext()) { - ProfileSelection p = ite.next(); - for (List<ProfileData> statuses : projectProfiles) { - ProfileData s = hasProfile(p.getId(), statuses); - if (s == null) { - // remove any non-common profile selection - ite.remove(); - break; - } - // reset non common settings - if (p.getAutoActive() != null && !p.getAutoActive().equals(s.isAutoActive())) { - p.setAutoActive(null); - } - if (p.getSource() != null && !p.getSource().equals(s.getSource())) { - p.setSource(Messages.ProfileSelectionHandler_multiple_definitions); - } - if (p.getSelected() != null && !p.getSelected().equals(s.isUserSelected())) { - p.setSelected(null); - } - if (p.getActivationState() != null && !p.getActivationState().equals(s.getActivationState())) { - p.setActivationState(null); - p.setAutoActive(null); - } - } - } - } - - return selection; - } - - private ProfileData hasProfile(String id, List<ProfileData> statuses) { - for (ProfileData p : statuses) { - if (id.equals(p.getId())) { - return p; - } - } - return null; - } - - private Map<IMavenProjectFacade, List<ProfileData>> getAllProfiles(final Set<IMavenProjectFacade> facades, - final IProfileManager profileManager) throws CoreException { - Map<IMavenProjectFacade, List<ProfileData>> allProfiles = - new HashMap<IMavenProjectFacade, List<ProfileData>>(facades.size()); - IProgressMonitor monitor = new NullProgressMonitor(); - for (IMavenProjectFacade facade : facades) { - allProfiles.put(facade, profileManager.getProfileDatas(facade, monitor)); - } - return allProfiles; - } - - public List<ProfileSelection> getSharedProfiles() { - return sharedProfiles; - } - - public Map<IMavenProjectFacade, List<ProfileData>> getAllProfiles() { - return allProfiles; - } - } - - class UpdateProfilesJob extends WorkspaceJob { - - private Map<IMavenProjectFacade, List<ProfileData>> allProfiles; - private List<ProfileSelection> sharedProfiles; - private IProfileManager profileManager; - private SelectProfilesDialog dialog; - - private UpdateProfilesJob(Map<IMavenProjectFacade, List<ProfileData>> allProfiles, - List<ProfileSelection> sharedProfiles, IProfileManager profileManager, SelectProfilesDialog dialog) { - super(Messages.ProfileManager_Updating_maven_profiles); - this.allProfiles = allProfiles; - this.sharedProfiles = sharedProfiles; - this.profileManager = profileManager; - this.dialog = dialog; - } - - public IStatus runInWorkspace(IProgressMonitor monitor) { - try { - SubMonitor progress = SubMonitor.convert(monitor, Messages.ProfileManager_Updating_maven_profiles, 100); - SubMonitor subProgress = SubMonitor.convert(progress.newChild(5), allProfiles.size() * 100); - for (Map.Entry<IMavenProjectFacade, List<ProfileData>> entry : allProfiles.entrySet()) { - if (progress.isCanceled()) { - throw new OperationCanceledException(); - } - IMavenProjectFacade facade = entry.getKey(); - List<String> activeProfiles = getActiveProfiles(sharedProfiles, entry.getValue()); - - profileManager.updateActiveProfiles(facade, activeProfiles, - dialog.isOffline(), dialog.isForceUpdate(), subProgress.newChild(100)); - } - } catch (CoreException ex) { - log.error("Unable to update Maven profiles", ex); - return ex.getStatus(); - } - return Status.OK_STATUS; - } - - private List<String> getActiveProfiles( - List<ProfileSelection> sharedProfiles, - List<ProfileData> availableProfiles) { - List<String> ids = new ArrayList<String>(); - - for (ProfileData st : availableProfiles) { - ProfileSelection selection = findSelectedProfile(st.getId(), sharedProfiles); - String id = null; - boolean isDisabled = false; - if (selection == null) { - // was not displayed. Use existing value. - if (st.isUserSelected()) { - id = st.getId(); - isDisabled = st.getActivationState().equals(ProfileState.Disabled); - } - } else { - if (null == selection.getSelected()) { - // Value was displayed but its state is unknown, use - // previous state - if (st.isUserSelected()) { - id = st.getId(); - isDisabled = st.getActivationState().equals(ProfileState.Disabled); - } - } else { - // Value was displayed and is consistent - if (Boolean.TRUE.equals(selection.getSelected())) { - id = selection.getId(); - isDisabled = selection.getActivationState().equals(ProfileState.Disabled); - } - } - } - - if (id != null) { - if (isDisabled) { - id = "!" + id; //$NON-NLS-1$ - } - ids.add(id); - } - } - return ids; - } - - private ProfileSelection findSelectedProfile(String id, - List<ProfileSelection> sharedProfiles) { - for (ProfileSelection sel : sharedProfiles) { - if (id.equals(sel.getId())) { - return sel; - } - } - return null; - } - } + private static final Logger log = LoggerFactory.getLogger(ProfileSelectionHandler.class); + + /** + * Opens the Maven profile selection Dialog window. + */ + public Object execute(ExecutionEvent event) throws ExecutionException { + IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event); + IProject[] projects = getSelectedProjects(event); + return execute(window.getShell(), projects); + } + + private IProject[] getSelectedProjects(ExecutionEvent event) { + ISelection selection = HandlerUtil.getCurrentSelection(event); + IProject[] projects = SelectionUtil.getProjects(selection, false); + if(projects.length == 0) { + IEditorInput input = HandlerUtil.getActiveEditorInput(event); + if(input instanceof IFileEditorInput) { + IFileEditorInput fileInput = (IFileEditorInput) input; + projects = new IProject[] {fileInput.getFile().getProject()}; + } + } + return projects; + } + + public IStatus execute(Shell shell, IProject... projects) throws ExecutionException { + Set<IMavenProjectFacade> facades = getMavenProjects(projects); + if(facades.isEmpty()) { + display(shell, Messages.ProfileSelectionHandler_Select_some_maven_projects); + return null; + } + + final IProfileManager profileManager = MavenProfilesCoreActivator.getDefault().getProfileManager(); + + GetProfilesJob getProfilesJob = new GetProfilesJob(facades, profileManager); + getProfilesJob.addJobChangeListener(onProfilesFetched(getProfilesJob, facades, profileManager, shell)); + getProfilesJob.setUser(true); + getProfilesJob.schedule(); + return Status.OK_STATUS; + } + + private IJobChangeListener onProfilesFetched(final GetProfilesJob getProfilesJob, + final Set<IMavenProjectFacade> facades, final IProfileManager profileManager, final Shell shell) { + + return new JobChangeAdapter() { + + @Override + public void done(IJobChangeEvent event) { + if(getProfilesJob.getResult().isOK()) { + shell.getDisplay().syncExec(new Runnable() { + + public void run() { + List<ProfileSelection> sharedProfiles = getProfilesJob.getSharedProfiles(); + Map<IMavenProjectFacade, List<ProfileData>> allProfiles = getProfilesJob.getAllProfiles(); + final SelectProfilesDialog dialog = new SelectProfilesDialog(shell, facades, sharedProfiles); + if(dialog.open() == Dialog.OK) { + Job job = new UpdateProfilesJob(allProfiles, sharedProfiles, profileManager, dialog); + job.setRule(MavenPlugin.getProjectConfigurationManager().getRule()); + job.schedule(); + } + } + }); + + } + } + }; + } + + private void display(Shell shell, String message) { + MessageDialog.openInformation(shell, Messages.SelectProfilesDialog_Select_Maven_profiles, message); + } + + /** + * Returns an IMavenProjectFacade from the selected IResource, or from the active editor + * + * @param event + * @return the selected IMavenProjectFacade + */ + private Set<IMavenProjectFacade> getMavenProjects(IProject[] projects) { + if(projects == null || projects.length == 0) { + return Collections.emptySet(); + } + Set<IMavenProjectFacade> facades = new HashSet<IMavenProjectFacade>(projects.length); + try { + IProgressMonitor monitor = new NullProgressMonitor(); + for(IProject p : projects) { + if(p != null && p.isAccessible() && p.hasNature(IMavenConstants.NATURE_ID)) { + IFile pom = p.getFile(IMavenConstants.POM_FILE_NAME); + IMavenProjectFacade facade = MavenPlugin.getMavenProjectRegistry().create(pom, true, monitor); + facades.add(facade); + } + } + } catch(CoreException e) { + log.error("Unable to select Maven projects", e); + } + + return facades; + } + + class GetProfilesJob extends Job { + + private IProfileManager profileManager; + + private Set<IMavenProjectFacade> facades; + + private Map<IMavenProjectFacade, List<ProfileData>> allProfiles; + + private List<ProfileSelection> sharedProfiles; + + private GetProfilesJob(final Set<IMavenProjectFacade> facades, IProfileManager profileManager) { + super(Messages.ProfileSelectionHandler_Loading_maven_profiles); + this.facades = facades; + this.profileManager = profileManager; + } + + @Override + protected IStatus run(IProgressMonitor monitor) { + try { + this.allProfiles = getAllProfiles(facades, profileManager); + this.sharedProfiles = getSharedProfiles(allProfiles); + } catch(CoreException e) { + return new Status(IStatus.ERROR, MavenProfilesUIActivator.PLUGIN_ID, + Messages.ProfileSelectionHandler_Unable_to_open_profile_dialog, e); + } + return Status.OK_STATUS; + } + + private List<ProfileSelection> getSharedProfiles(Map<IMavenProjectFacade, List<ProfileData>> projectProfilesMap) { + + List<ProfileData> currentSelection = null; + List<List<ProfileData>> projectProfiles = new ArrayList<List<ProfileData>>(projectProfilesMap.values()); + int smallestSize = Integer.MAX_VALUE; + for(List<ProfileData> profiles : projectProfiles) { + int size = profiles.size(); + if(size < smallestSize) { + smallestSize = size; + currentSelection = profiles; + } + } + projectProfiles.remove(currentSelection); + + // Init the smallest profiles selection possible + List<ProfileSelection> selection = new ArrayList<ProfileSelection>(); + for(ProfileData p : currentSelection) { + ProfileSelection ps = new ProfileSelection(); + ps.setId(p.getId()); + ps.setActivationState(p.getActivationState()); + ps.setAutoActive(p.isAutoActive()); + ps.setSource(p.getSource()); + ps.setSelected(p.isUserSelected()); + selection.add(ps); + } + + if(!projectProfiles.isEmpty()) { + // Restrict to the common profiles only + Iterator<ProfileSelection> ite = selection.iterator(); + + while(ite.hasNext()) { + ProfileSelection p = ite.next(); + for(List<ProfileData> statuses : projectProfiles) { + ProfileData s = hasProfile(p.getId(), statuses); + if(s == null) { + // remove any non-common profile selection + ite.remove(); + break; + } + // reset non common settings + if(p.getAutoActive() != null && !p.getAutoActive().equals(s.isAutoActive())) { + p.setAutoActive(null); + } + if(p.getSource() != null && !p.getSource().equals(s.getSource())) { + p.setSource(Messages.ProfileSelectionHandler_multiple_definitions); + } + if(p.getSelected() != null && !p.getSelected().equals(s.isUserSelected())) { + p.setSelected(null); + } + if(p.getActivationState() != null && !p.getActivationState().equals(s.getActivationState())) { + p.setActivationState(null); + p.setAutoActive(null); + } + } + } + } + + return selection; + } + + private ProfileData hasProfile(String id, List<ProfileData> statuses) { + for(ProfileData p : statuses) { + if(id.equals(p.getId())) { + return p; + } + } + return null; + } + + private Map<IMavenProjectFacade, List<ProfileData>> getAllProfiles(final Set<IMavenProjectFacade> facades, + final IProfileManager profileManager) throws CoreException { + Map<IMavenProjectFacade, List<ProfileData>> allProfiles = new HashMap<IMavenProjectFacade, List<ProfileData>>( + facades.size()); + IProgressMonitor monitor = new NullProgressMonitor(); + for(IMavenProjectFacade facade : facades) { + allProfiles.put(facade, profileManager.getProfileDatas(facade, monitor)); + } + return allProfiles; + } + + public List<ProfileSelection> getSharedProfiles() { + return sharedProfiles; + } + + public Map<IMavenProjectFacade, List<ProfileData>> getAllProfiles() { + return allProfiles; + } + } + + class UpdateProfilesJob extends WorkspaceJob { + + private Map<IMavenProjectFacade, List<ProfileData>> allProfiles; + + private List<ProfileSelection> sharedProfiles; + + private IProfileManager profileManager; + + private SelectProfilesDialog dialog; + + private UpdateProfilesJob(Map<IMavenProjectFacade, List<ProfileData>> allProfiles, + List<ProfileSelection> sharedProfiles, IProfileManager profileManager, SelectProfilesDialog dialog) { + super(Messages.ProfileManager_Updating_maven_profiles); + this.allProfiles = allProfiles; + this.sharedProfiles = sharedProfiles; + this.profileManager = profileManager; + this.dialog = dialog; + } + + public IStatus runInWorkspace(IProgressMonitor monitor) { + try { + SubMonitor progress = SubMonitor.convert(monitor, Messages.ProfileManager_Updating_maven_profiles, 100); + SubMonitor subProgress = SubMonitor.convert(progress.newChild(5), allProfiles.size() * 100); + for(Map.Entry<IMavenProjectFacade, List<ProfileData>> entry : allProfiles.entrySet()) { + if(progress.isCanceled()) { + throw new OperationCanceledException(); + } + IMavenProjectFacade facade = entry.getKey(); + List<String> activeProfiles = getActiveProfiles(sharedProfiles, entry.getValue()); + + profileManager.updateActiveProfiles(facade, activeProfiles, dialog.isOffline(), dialog.isForceUpdate(), + subProgress.newChild(100)); + } + } catch(CoreException ex) { + log.error("Unable to update Maven profiles", ex); + return ex.getStatus(); + } + return Status.OK_STATUS; + } + + private List<String> getActiveProfiles(List<ProfileSelection> sharedProfiles, List<ProfileData> availableProfiles) { + List<String> ids = new ArrayList<String>(); + + for(ProfileData st : availableProfiles) { + ProfileSelection selection = findSelectedProfile(st.getId(), sharedProfiles); + String id = null; + boolean isDisabled = false; + if(selection == null) { + // was not displayed. Use existing value. + if(st.isUserSelected()) { + id = st.getId(); + isDisabled = st.getActivationState().equals(ProfileState.Disabled); + } + } else { + if(null == selection.getSelected()) { + // Value was displayed but its state is unknown, use + // previous state + if(st.isUserSelected()) { + id = st.getId(); + isDisabled = st.getActivationState().equals(ProfileState.Disabled); + } + } else { + // Value was displayed and is consistent + if(Boolean.TRUE.equals(selection.getSelected())) { + id = selection.getId(); + isDisabled = selection.getActivationState().equals(ProfileState.Disabled); + } + } + } + + if(id != null) { + if(isDisabled) { + id = "!" + id; //$NON-NLS-1$ + } + ids.add(id); + } + } + return ids; + } + + private ProfileSelection findSelectedProfile(String id, List<ProfileSelection> sharedProfiles) { + for(ProfileSelection sel : sharedProfiles) { + if(id.equals(sel.getId())) { + return sel; + } + } + return null; + } + } } |