### Eclipse Workspace Patch 1.0 #P org.eclipse.ui.ide Index: extensions/org/eclipse/ui/dialogs/WizardResourceImportPage.java =================================================================== RCS file: /cvsroot/eclipse/org.eclipse.ui.ide/extensions/org/eclipse/ui/dialogs/WizardResourceImportPage.java,v retrieving revision 1.9 diff -u -r1.9 WizardResourceImportPage.java --- extensions/org/eclipse/ui/dialogs/WizardResourceImportPage.java 21 Nov 2005 17:50:26 -0000 1.9 +++ extensions/org/eclipse/ui/dialogs/WizardResourceImportPage.java 1 Feb 2006 20:24:24 -0000 @@ -11,6 +11,8 @@ package org.eclipse.ui.dialogs; import java.util.ArrayList; +import java.util.Hashtable; +import java.util.Iterator; import java.util.Map; import org.eclipse.core.resources.IContainer; @@ -94,7 +96,9 @@ /** * Creates an import wizard page. If the initial resource selection * contains exactly one container resource then it will be used as the default - * import destination. + * import destination. Otherwise, if the initial resource selection consists of many resources, + * a common container resource will be determined and used as + * the default import destination. * * @param name the name of the page * @param selection the current resource selection @@ -103,27 +107,237 @@ IStructuredSelection selection) { super(name); + //Initialize to null currentResourceSelection = null; - if (selection.size() == 1) { - Object firstElement = selection.getFirstElement(); - if (firstElement instanceof IAdaptable) { - Object resource = ((IAdaptable) firstElement) - .getAdapter(IResource.class); - if (resource != null) - currentResourceSelection = (IResource) resource; - } - } + + if (selection.size() > 0){ + + + if (selection.size() == 1) { + Object firstElement = selection.getFirstElement(); + if (firstElement instanceof IAdaptable) { + Object resource = ((IAdaptable) firstElement) + .getAdapter(IResource.class); + if (resource != null) + currentResourceSelection = (IResource) resource; + } + + if (currentResourceSelection != null) { + if (currentResourceSelection.getType() == IResource.FILE) + currentResourceSelection = currentResourceSelection.getParent(); + + if (!currentResourceSelection.isAccessible()) + currentResourceSelection = null; + } + + } + else { + //selection.size() > 1 + + //attempt to find a common IContainer among all selected resources. Otherwise + //return null + + currentResourceSelection = getCommonIResource(selection); + if (currentResourceSelection != null) { + if (!currentResourceSelection.isAccessible()) + currentResourceSelection = null; + }//if + } + - if (currentResourceSelection != null) { - if (currentResourceSelection.getType() == IResource.FILE) - currentResourceSelection = currentResourceSelection.getParent(); + }//if - if (!currentResourceSelection.isAccessible()) - currentResourceSelection = null; - } + } + + /** + * + * This method will attempt to return an IContainer common to all selected resources. + * If there is no common IContainer, it will return a null + * + * @param selection the current resource selection + */ + + protected IResource getCommonIResource(IStructuredSelection selection) { + + IResource commonIResource = null; + + IResource someIResource = null; + ArrayList resources = new ArrayList(); + Object selected = null; + Object resource = null; + boolean resourcesResideInSameProject = false; + + Hashtable uniqueProjects = new Hashtable(); + String projectName = null; + + if ( (selection != null) && (selection.size() > 1) ) { + + try { + + //First, get the IResource for this selection; if it exists + //Second, if the IResource is an IFile, get its container instead + //Third, determine if these IResources have a common project + + Iterator it = selection.iterator(); + while(it.hasNext()) { + selected = it.next(); + if (selected instanceof IAdaptable) { + resource = ((IAdaptable) selected).getAdapter(IResource.class); + if (resource != null) { + someIResource = (IResource) resource; + if (someIResource.getType() == IResource.FILE){ + resources.add(someIResource.getParent()); + }//if + else { + resources.add(someIResource); + }//endif + + //keep track of unique project names + projectName = someIResource.getProject().getName(); + if (!uniqueProjects.containsKey( projectName )) { + uniqueProjects.put(projectName, projectName); + }//if + }//if + }//if + + }//while + + + if (resources.size() > 1) { + + + // + //figure out if resources all reside in same project + // + resourcesResideInSameProject = (uniqueProjects.size() == 1); + uniqueProjects.clear(); + + if (!resourcesResideInSameProject) { + //resources do reside in the same project, therefore no common IContainer resource is possible + //so return null + commonIResource = null; + }//if + else { + + //all the resources reside in the same project + //now find the longest common path + + + IResource[] theResources = (IResource[])resources.toArray(new IResource[0]); + resources.clear(); + + int i = 0; + int j =0; + IPath currentPath = null; + boolean pathSame = true; + int iSmallestSegmentCount = -1; + int segmentCount = 0; + + + // + //trim the IResource paths to the smallest segment size before the main comparison starts + // + + // + // if the resources already have the same segment count then the following code won't affect + // the resource array elements at all. However, if the segment counts are not identical, then + // this step is necessary. + + iSmallestSegmentCount = Integer.MAX_VALUE; + + //determine the smallest path segment count among all resources + for(i=0;i iSmallestSegmentCount) { + for(j=0;j<(segmentCount-iSmallestSegmentCount);j++){ + //modifying array of resources to get parent of this + //resource and store it in the old position of the child resource + theResources[i] = theResources[i].getParent(); + }//for + }//if + }//for + + + // + // Begin main comparison loop + // + boolean commonContainerPathAchieved = false; + + while(!commonContainerPathAchieved) { + + // + // path segment counts are all the same at this point for all the resources + // + + + //get the path of the first element to use for comparison + currentPath = theResources[0].getFullPath(); + + //determine if the resources' paths are the same + pathSame = true; + for(i=0;iWizardResourceImportPage implementation of this