View | Details | Raw Unified | Return to bug 125582
Collapse All | Expand All

(-)extensions/org/eclipse/ui/dialogs/WizardResourceImportPage.java (-16 / +230 lines)
Lines 11-16 Link Here
11
package org.eclipse.ui.dialogs;
11
package org.eclipse.ui.dialogs;
12
12
13
import java.util.ArrayList;
13
import java.util.ArrayList;
14
import java.util.Hashtable;
15
import java.util.Iterator;
14
import java.util.Map;
16
import java.util.Map;
15
17
16
import org.eclipse.core.resources.IContainer;
18
import org.eclipse.core.resources.IContainer;
Lines 94-100 Link Here
94
    /**
96
    /**
95
     * Creates an import wizard page. If the initial resource selection 
97
     * Creates an import wizard page. If the initial resource selection 
96
     * contains exactly one container resource then it will be used as the default
98
     * contains exactly one container resource then it will be used as the default
97
     * import destination.
99
     * import destination. Otherwise, if the initial resource selection consists of many resources,
100
     * a common container resource will be determined and used as
101
     * the default import destination.
98
     *
102
     *
99
     * @param name the name of the page
103
     * @param name the name of the page
100
     * @param selection the current resource selection
104
     * @param selection the current resource selection
Lines 103-129 Link Here
103
            IStructuredSelection selection) {
107
            IStructuredSelection selection) {
104
        super(name);
108
        super(name);
105
109
110
        
106
        //Initialize to null
111
        //Initialize to null
107
        currentResourceSelection = null;
112
        currentResourceSelection = null;
108
        if (selection.size() == 1) {
113
        
109
            Object firstElement = selection.getFirstElement();
114
        if (selection.size() > 0){
110
            if (firstElement instanceof IAdaptable) {
115
        	
111
                Object resource = ((IAdaptable) firstElement)
116
        
112
                        .getAdapter(IResource.class);
117
	        if (selection.size() == 1) {
113
                if (resource != null)
118
	            Object firstElement = selection.getFirstElement();
114
                    currentResourceSelection = (IResource) resource;
119
	            if (firstElement instanceof IAdaptable) {
115
            }
120
	                Object resource = ((IAdaptable) firstElement)
116
        }
121
	                        .getAdapter(IResource.class);
122
	                if (resource != null)
123
	                    currentResourceSelection = (IResource) resource;
124
	            }
125
	            
126
		        if (currentResourceSelection != null) {
127
		            if (currentResourceSelection.getType() == IResource.FILE)
128
		                currentResourceSelection = currentResourceSelection.getParent();
129
		
130
		            if (!currentResourceSelection.isAccessible())
131
		                currentResourceSelection = null;
132
		        }
133
	            
134
	        }
135
	        else {
136
	        	//selection.size() > 1
137
	   
138
	        	//attempt to find a common IContainer among all selected resources. Otherwise
139
	        	//return null
140
	        	
141
	        	currentResourceSelection = getCommonIResource(selection);
142
	        	if (currentResourceSelection != null) {
143
		            if (!currentResourceSelection.isAccessible())
144
		                currentResourceSelection = null;
145
	        	}//if
146
	        }
147
	
117
148
118
        if (currentResourceSelection != null) {
149
        }//if
119
            if (currentResourceSelection.getType() == IResource.FILE)
120
                currentResourceSelection = currentResourceSelection.getParent();
121
150
122
            if (!currentResourceSelection.isAccessible())
151
        
123
                currentResourceSelection = null;
124
        }
125
152
126
    }
153
    }
154
    
155
    /**
156
     *  
157
     * This method will attempt to return an IContainer common to all selected resources.
158
     * If there is no common IContainer, it will return a null
159
     * 
160
     * @param selection the current resource selection
161
     */
162
    
163
    protected IResource getCommonIResource(IStructuredSelection selection) {
164
    	
165
    	IResource commonIResource = null;
166
    	
167
    	IResource someIResource = null;
168
    	ArrayList resources = new ArrayList();
169
        Object selected = null;
170
        Object resource = null;
171
        boolean resourcesResideInSameProject = false;
172
173
        Hashtable uniqueProjects = new Hashtable();
174
        String projectName = null;
175
        
176
        if ( (selection != null) && (selection.size() > 1) ) {
177
        	
178
	     try {
179
180
	    	  //First, get the IResource for this selection; if it exists
181
	    	  //Second, if the IResource is an IFile, get its container instead
182
	    	  //Third, determine if these IResources have a common project
183
		
184
	    	  Iterator it = selection.iterator();
185
	    	  while(it.hasNext()) {
186
	    		  selected = it.next();
187
		          if (selected instanceof IAdaptable) {
188
		              resource = ((IAdaptable) selected).getAdapter(IResource.class);
189
		              if (resource != null) {
190
		                  someIResource = (IResource) resource;
191
		                  if (someIResource.getType() == IResource.FILE){
192
		                	  resources.add(someIResource.getParent());
193
		                  }//if
194
		                  else {
195
		                	  resources.add(someIResource);
196
		                  }//endif
197
		                  
198
		                  //keep track of unique project names
199
		                  projectName = someIResource.getProject().getName();
200
		                  if (!uniqueProjects.containsKey( projectName )) {
201
		                	  uniqueProjects.put(projectName, projectName);
202
		                  }//if
203
		              }//if
204
		          }//if
205
		          
206
	    	  }//while
207
	    	  
208
	    	  
209
	    	  if (resources.size() > 1) {
210
	    		  
211
	    		  
212
		    	  //
213
		    	  //figure out if resources all reside in same project
214
		    	  //
215
		    	  resourcesResideInSameProject = (uniqueProjects.size() == 1);
216
		    	  uniqueProjects.clear();
217
		    	  
218
		    	  if (!resourcesResideInSameProject) {
219
		    		  //resources do reside in the same project, therefore no common IContainer resource is possible 
220
	    			  //so return null
221
	    			  commonIResource = null;
222
		    	  }//if
223
		    	  else {
224
		    		  
225
		    		  //all the resources reside in the same project
226
		    		  //now find the longest common path
227
		    		  
228
		    		  
229
		    		  IResource[] theResources = (IResource[])resources.toArray(new IResource[0]);
230
		    		  resources.clear();
231
		    		  
232
		    		  int i = 0;
233
		    		  int j =0;
234
		    		  IPath currentPath = null;
235
		    		  boolean pathSame = true;
236
		    		  int iSmallestSegmentCount = -1;
237
		    		  int segmentCount = 0;
238
		    		  
239
240
	    			   //
241
	    			   //trim the IResource  paths to the smallest segment size before the main comparison starts
242
	    			   //
243
	    			   
244
	    			   //
245
	    			   // if the resources already have the same segment count then the following code won't affect
246
	    			   // the resource array elements at all. However, if the segment counts are not identical, then
247
	    			   // this step is necessary.
248
	    			  
249
	    				iSmallestSegmentCount = Integer.MAX_VALUE;
250
251
	    				//determine the smallest path segment count among all resources
252
		    			for(i=0;i<theResources.length;i++){
253
		    				segmentCount = theResources[i].getFullPath().segmentCount();
254
		    				if (segmentCount < iSmallestSegmentCount) {
255
		    					iSmallestSegmentCount = segmentCount;
256
		    				}//if
257
		    			}//for
258
259
260
		    		   //reduce all resources to the smallest segment size
261
		    		   for(i=0;i<theResources.length;i++){
262
		    			   segmentCount = theResources[i].getFullPath().segmentCount();
263
		    			   if (segmentCount > iSmallestSegmentCount) {
264
		    				   for(j=0;j<(segmentCount-iSmallestSegmentCount);j++){
265
		    					   //modifying array of resources to get parent of this
266
		    					   //resource and store it in the old position of the child resource
267
		    					   theResources[i] = theResources[i].getParent();
268
		    				   }//for
269
		    			   }//if
270
		    		   }//for
271
		    		
272
		    		   
273
		    		   //
274
		    		   // Begin main comparison loop
275
		    		   //
276
			    	   boolean commonContainerPathAchieved = false;
277
278
			    	   while(!commonContainerPathAchieved) {
279
280
		    			   //
281
		    			   // path segment counts are all the same at this point for all the resources
282
		    			   //
283
			    		   
284
			    		   
285
	 		    		    //get the path of the first element to use for comparison  
286
	 		    		    currentPath = theResources[0].getFullPath();
287
	    				   
288
				    		//determine if the resources' paths are the same
289
				    		pathSame = true;
290
				    		for(i=0;i<theResources.length;i++){
291
				    				if (!currentPath.equals(theResources[i].getFullPath())) {
292
				    					pathSame = false;
293
				    					break;
294
				    				}//if
295
				    		}//for
296
			    		   
297
298
			    			if (pathSame == false) {					    		
299
			    		   
300
			    				   //
301
			    				   // get the parents of all the resources and store them in resource array
302
			    				   //
303
			    				   for(i=0;i<theResources.length;i++){
304
			    					   //modifying array of resources to get parent of this
305
			    					   //resource and store it in the old position of the child resource
306
			    					   theResources[i] = theResources[i].getParent();
307
			    				   }//for
308
			    				   
309
			    			}//if
310
			    			else {
311
			    				//path is finally common across all resources
312
			    				
313
			    				//break out of loop
314
			    				commonContainerPathAchieved = true;
315
			    			}//endif
316
			    			
317
			    		}//while
318
			    		   
319
		    		  
320
		    		  //
321
		    		  // At this point, all the IContainer elements in the array have the same path
322
		    		  //
323
		    		  
324
    				  //set the commonIResource
325
	    			  commonIResource = theResources[0];
326
		    		  
327
		    		  
328
		    	  }//endif
329
		    	  
330
	    	  }//if
331
	    	  
332
	    	}//try
333
	    	catch(Exception e){
334
	    		e.printStackTrace();
335
	    	}//catch
336
        }//if
337
        
338
    	return commonIResource;
339
    }
340
    
127
341
128
    /**
342
    /**
129
     * The <code>WizardResourceImportPage</code> implementation of this 
343
     * The <code>WizardResourceImportPage</code> implementation of this 

Return to bug 125582