Author: adietish
Date: 2011-08-08 11:32:36 -0400 (Mon, 08 Aug 2011)
New Revision: 33678
Modified:
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/internal/profiles/ProfileSelectionHandler.java
Log:
[JBIDE-8969] moved profile fetching to background job
Modified:
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/internal/profiles/ProfileSelectionHandler.java
===================================================================
---
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/internal/profiles/ProfileSelectionHandler.java 2011-08-08
14:24:08 UTC (rev 33677)
+++
trunk/maven/plugins/org.jboss.tools.maven.ui/src/org/jboss/tools/maven/ui/internal/profiles/ProfileSelectionHandler.java 2011-08-08
15:32:36 UTC (rev 33678)
@@ -33,13 +33,18 @@
import org.eclipse.core.runtime.OperationCanceledException;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
+import org.eclipse.core.runtime.jobs.IJobChangeEvent;
+import org.eclipse.core.runtime.jobs.IJobChangeListener;
+import org.eclipse.core.runtime.jobs.Job;
+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.jface.window.Window;
import org.eclipse.m2e.core.MavenPlugin;
import org.eclipse.m2e.core.internal.IMavenConstants;
import org.eclipse.m2e.core.project.IMavenProjectFacade;
+import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IFileEditorInput;
import org.eclipse.ui.IWorkbenchWindow;
@@ -53,116 +58,58 @@
import org.jboss.tools.maven.ui.Messages;
/**
- * Handles profile selection commands.
+ * Handles profile selection commands.
*/
public class ProfileSelectionHandler extends AbstractHandler {
/**
- * Opens the Maven profile selection Dialog window.
+ * Opens the Maven profile selection Dialog window.
*/
public Object execute(ExecutionEvent event) throws ExecutionException {
IWorkbenchWindow window = HandlerUtil.getActiveWorkbenchWindowChecked(event);
- final Set<IMavenProjectFacade> facades = getSelectedMavenProjects(event);
-
+ Set<IMavenProjectFacade> facades = getSelectedMavenProjects(event);
+
if (facades.isEmpty()) {
display(window, Messages.ProfileSelectionHandler_Select_some_maven_projects);
return null;
}
- System.out.print("Select projects "+facades); //$NON-NLS-1$
-
+ System.out.print("Select projects " + facades); //$NON-NLS-1$
+
final IProfileManager profileManager =
MavenCoreActivator.getDefault().getProfileManager();
- final List<ProfileSelection> sharedProfiles;
- final Map<IMavenProjectFacade, List<ProfileStatus>> allProfiles;
- try {
- allProfiles = getAllProfiles(facades, profileManager);
- sharedProfiles = getSharedProfiles(allProfiles);
- } catch (CoreException e) {
- throw new
ExecutionException(Messages.ProfileSelectionHandler_Unable_to_open_profile_dialog, e);
- }
- final SelectProfilesDialog dialog = new SelectProfilesDialog(window.getShell(),
- facades,
- sharedProfiles);
- if(dialog.open() == Window.OK) {
-
- WorkspaceJob job = new WorkspaceJob(Messages.ProfileManager_Updating_maven_profiles)
{
- 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<ProfileStatus>> 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) {
- Activator.log(ex);
- return ex.getStatus();
- }
- return Status.OK_STATUS;
- }
+ GetProfilesJob getProfilesJob = new GetProfilesJob(facades, profileManager);
+ getProfilesJob.addJobChangeListener(onProfilesFetched(getProfilesJob, facades,
profileManager, window.getShell()));
+ getProfilesJob.setUser(true);
+ getProfilesJob.schedule();
+ return Status.OK_STATUS;
+ }
- private List<String> getActiveProfiles(
- List<ProfileSelection> sharedProfiles,
- List<ProfileStatus> availableProfiles) {
- List<String> ids = new ArrayList<String>();
-
- for (ProfileStatus 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);
- }
- }
- }
+ 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() {
- if (id != null) {
- if (isDisabled) {
- id = "!"+id; //$NON-NLS-1$
- }
- ids.add(id);
+ public void run() {
+ List<ProfileSelection> sharedProfiles = getProfilesJob.getSharedProfiles();
+ Map<IMavenProjectFacade, List<ProfileStatus>> 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();
+ }
}
- }
- return ids;
- }
+ });
- private ProfileSelection findSelectedProfile(String id,
- List<ProfileSelection> sharedProfiles) {
- for (ProfileSelection sel : sharedProfiles) {
- if (id.equals(sel.getId())) {
- return sel;
- }
- }
- return null;
}
- };
- job.setRule( MavenPlugin.getProjectConfigurationManager().getRule());
- job.schedule();
- }
- return null;
+ }
+ };
}
private void display(IWorkbenchWindow window, String message) {
@@ -172,89 +119,10 @@
message);
}
- private List<ProfileSelection> getSharedProfiles(
- Map<IMavenProjectFacade, List<ProfileStatus>> projectProfilesMap) {
-
- List<ProfileStatus> currentSelection = null;
- List<List<ProfileStatus>> projectProfiles = new
ArrayList<List<ProfileStatus>>(projectProfilesMap.values());
- int smallestSize = Integer.MAX_VALUE;
- for(List<ProfileStatus> 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(ProfileStatus 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<ProfileStatus> statuses : projectProfiles) {
- ProfileStatus 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(null);
- }
- 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 ProfileStatus hasProfile(String id, List<ProfileStatus> statuses) {
- for (ProfileStatus p : statuses){
- if (id.equals(p.getId())){
- return p;
- }
- }
- return null;
- }
-
- private Map<IMavenProjectFacade, List<ProfileStatus>> getAllProfiles(final
Set<IMavenProjectFacade> facades,
- final IProfileManager profileManager) throws CoreException {
- Map<IMavenProjectFacade, List<ProfileStatus>> allProfiles =
- new HashMap<IMavenProjectFacade, List<ProfileStatus>>(facades.size());
- IProgressMonitor monitor = new NullProgressMonitor();
- for (IMavenProjectFacade facade : facades) {
- allProfiles.put(facade, profileManager.getProfilesStatuses(facade, monitor));
- }
- return allProfiles;
- }
-
/**
- * Returns an IMavenProjectFacade from the selected IResource, or from the active editor
+ * Returns an IMavenProjectFacade from the selected IResource, or from the
+ * active editor
+ *
* @param event
* @return the selected IMavenProjectFacade
*/
@@ -264,11 +132,11 @@
Set<IMavenProjectFacade> facades = new HashSet<IMavenProjectFacade>();
try {
if (projects.length == 0) {
- IEditorInput input = HandlerUtil.getActiveEditorInput(event);
- if(input instanceof IFileEditorInput) {
- IFileEditorInput fileInput = (IFileEditorInput) input;
- projects = new IProject[]{fileInput.getFile().getProject()};
- }
+ IEditorInput input = HandlerUtil.getActiveEditorInput(event);
+ if (input instanceof IFileEditorInput) {
+ IFileEditorInput fileInput = (IFileEditorInput) input;
+ projects = new IProject[] { fileInput.getFile().getProject() };
+ }
}
IProgressMonitor monitor = new NullProgressMonitor();
for (IProject p : projects) {
@@ -293,14 +161,14 @@
while (it.hasNext()) {
Object o = it.next();
if (o instanceof IResource) {
- projects.add(((IResource) o).getProject());
+ projects.add(((IResource) o).getProject());
} else if (o instanceof IWorkingSet) {
- IAdaptable[] elements = ((IWorkingSet)o).getElements();
+ IAdaptable[] elements = ((IWorkingSet) o).getElements();
if (elements != null) {
for (IAdaptable e : elements) {
IProject p = (IProject) e.getAdapter(IProject.class);
if (p != null) {
- projects.add(p);
+ projects.add(p);
}
}
}
@@ -311,5 +179,209 @@
projects.toArray(array);
return array;
}
-
+
+ class GetProfilesJob extends Job {
+
+ private IProfileManager profileManager;
+ private Set<IMavenProjectFacade> facades;
+ private Map<IMavenProjectFacade, List<ProfileStatus>> allProfiles;
+ private List<ProfileSelection> sharedProfiles;
+
+ private GetProfilesJob(final Set<IMavenProjectFacade> facades, IProfileManager
profileManager) {
+ super("get 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, Activator.PLUGIN_ID,
+ Messages.ProfileSelectionHandler_Unable_to_open_profile_dialog, e);
+ }
+ return Status.OK_STATUS;
+ }
+
+ private List<ProfileSelection> getSharedProfiles(
+ Map<IMavenProjectFacade, List<ProfileStatus>> projectProfilesMap) {
+
+ List<ProfileStatus> currentSelection = null;
+ List<List<ProfileStatus>> projectProfiles = new
ArrayList<List<ProfileStatus>>(projectProfilesMap.values());
+ int smallestSize = Integer.MAX_VALUE;
+ for (List<ProfileStatus> 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 (ProfileStatus 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<ProfileStatus> statuses : projectProfiles) {
+ ProfileStatus 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(null);
+ }
+ 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 ProfileStatus hasProfile(String id, List<ProfileStatus> statuses) {
+ for (ProfileStatus p : statuses) {
+ if (id.equals(p.getId())) {
+ return p;
+ }
+ }
+ return null;
+ }
+
+ private Map<IMavenProjectFacade, List<ProfileStatus>> getAllProfiles(final
Set<IMavenProjectFacade> facades,
+ final IProfileManager profileManager) throws CoreException {
+ Map<IMavenProjectFacade, List<ProfileStatus>> allProfiles =
+ new HashMap<IMavenProjectFacade, List<ProfileStatus>>(facades.size());
+ IProgressMonitor monitor = new NullProgressMonitor();
+ for (IMavenProjectFacade facade : facades) {
+ allProfiles.put(facade, profileManager.getProfilesStatuses(facade, monitor));
+ }
+ return allProfiles;
+ }
+
+ public List<ProfileSelection> getSharedProfiles() {
+ return sharedProfiles;
+ }
+
+ public Map<IMavenProjectFacade, List<ProfileStatus>> getAllProfiles() {
+ return allProfiles;
+ }
+ }
+
+ class UpdateProfilesJob extends WorkspaceJob {
+
+ private Map<IMavenProjectFacade, List<ProfileStatus>> allProfiles;
+ private List<ProfileSelection> sharedProfiles;
+ private IProfileManager profileManager;
+ private SelectProfilesDialog dialog;
+
+ private UpdateProfilesJob(Map<IMavenProjectFacade, List<ProfileStatus>>
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<ProfileStatus>> 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) {
+ Activator.log(ex);
+ return ex.getStatus();
+ }
+ return Status.OK_STATUS;
+ }
+
+ private List<String> getActiveProfiles(
+ List<ProfileSelection> sharedProfiles,
+ List<ProfileStatus> availableProfiles) {
+ List<String> ids = new ArrayList<String>();
+
+ for (ProfileStatus 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;
+ }
+ }
}