1 11 package org.eclipse.jdt.internal.ui.wizards.buildpaths.newsourcepage; 12 13 import java.util.Collections ; 14 import java.util.List ; 15 16 import org.eclipse.core.runtime.IAdaptable; 17 18 import org.eclipse.core.resources.IResource; 19 20 import org.eclipse.jface.viewers.IStructuredSelection; 21 22 import org.eclipse.jdt.core.IJavaElement; 23 import org.eclipse.jdt.core.IJavaProject; 24 import org.eclipse.jdt.core.JavaCore; 25 import org.eclipse.jdt.core.JavaModelException; 26 27 import org.eclipse.jdt.internal.ui.JavaPlugin; 28 import org.eclipse.jdt.internal.ui.packageview.ClassPathContainer; 29 30 public class BuildActionSelectionContext { 31 32 private IStructuredSelection fSelection; 33 private IJavaProject fJavaProject; 34 private List fElements; 35 private int[] fTypes; 36 37 public BuildActionSelectionContext() { 38 fSelection= null; 39 fElements= null; 40 fTypes= null; 41 } 42 43 public void init(IStructuredSelection selection) { 44 if (selection == null || !selection.equals(fSelection)) { 45 initContextValues(selection); 46 } 47 } 48 49 private void initContextValues(IStructuredSelection selection) { 50 fSelection= selection; 51 52 fJavaProject= null; 53 fElements= Collections.EMPTY_LIST; 54 fTypes= new int[0]; 55 56 IJavaProject project= getJavaProjectFromSelection(selection); 57 if (project != null && project.exists()) { 58 List elements= selection.toList(); 59 try { 60 int[] types= new int[elements.size()]; 61 for (int i= 0; i < elements.size(); i++) { 62 Object curr= elements.get(i); 63 if (i > 0 && !project.equals(getJavaProjectFromSelectedElement(curr))) { 64 return; 65 } 66 67 types[i]= DialogPackageExplorerActionGroup.getType(elements.get(i), project); 68 } 69 70 fJavaProject= project; 71 fElements= elements; 72 fTypes= types; 73 } catch (JavaModelException e) { 74 JavaPlugin.log(e); 75 } 76 } 77 78 } 79 80 81 89 private IJavaProject getJavaProjectFromSelection(IStructuredSelection selection) { 90 if (selection.isEmpty()) 91 return null; 92 Object element= selection.getFirstElement(); 93 return getJavaProjectFromSelectedElement(element); 94 } 95 96 104 private IJavaProject getJavaProjectFromSelectedElement(Object element) { 105 106 if (element instanceof IJavaElement) 107 return ((IJavaElement) element).getJavaProject(); 108 if (element instanceof ClassPathContainer) 109 return ((ClassPathContainer) element).getJavaProject(); 110 if (element instanceof IResource) 111 return JavaCore.create(((IResource) element).getProject()); 112 113 if (element instanceof IAdaptable) { 114 IResource resource= (IResource) ((IAdaptable) element).getAdapter(IResource.class); 115 if (resource != null) { 116 return JavaCore.create(resource.getProject()); 117 } 118 } 119 return null; 120 } 121 122 public List getElements() { 123 return fElements; 124 } 125 126 127 public IJavaProject getJavaProject() { 128 return fJavaProject; 129 } 130 131 132 public IStructuredSelection getSelection() { 133 return fSelection; 134 } 135 136 137 public int[] getTypes() { 138 return fTypes; 139 } 140 141 } 142 | Popular Tags |