1 11 package org.eclipse.jdt.internal.ui.util; 12 13 import java.util.HashSet ; 14 import java.util.Set ; 15 16 import org.eclipse.core.runtime.IAdaptable; 17 import org.eclipse.core.runtime.IStatus; 18 19 import org.eclipse.core.resources.IResource; 20 21 import org.eclipse.swt.widgets.Shell; 22 23 import org.eclipse.jface.dialogs.ErrorDialog; 24 25 import org.eclipse.jdt.core.ICompilationUnit; 26 import org.eclipse.jdt.core.IJavaElement; 27 28 import org.eclipse.jdt.internal.corext.util.Resources; 29 30 import org.eclipse.jdt.internal.ui.JavaUIMessages; 31 32 38 public class ElementValidator { 39 40 private ElementValidator() { 41 } 43 44 55 public static boolean checkInSync(IAdaptable element, Shell parent,String title) { 56 return checkInSync(new IAdaptable[] {element}, parent, title); 57 } 58 59 71 public static boolean checkInSync(IAdaptable[] elements, Shell parent, String title) { 72 return checkInSync(getResources(elements), parent, title); 73 } 74 75 93 public static boolean checkValidateEdit(IJavaElement element, Shell parent, String title) { 94 return checkValidateEdit(new IJavaElement[] {element}, parent, title); 95 } 96 97 115 public static boolean checkValidateEdit(IJavaElement[] elements, Shell parent, String title) { 116 return checkValidateEdit(getResources(elements), parent, title); 117 } 118 119 138 public static boolean check(IJavaElement element, Shell parent, String title, boolean editor) { 139 return check(new IJavaElement[] {element}, parent, title, editor); 140 } 141 142 161 public static boolean check(IJavaElement[] elements, Shell parent,String title, boolean editor) { 162 IResource[] resources= getResources(elements); 163 if (!editor && !checkInSync(resources, parent, title)) 164 return false; 165 return checkValidateEdit(resources, parent, title); 166 } 167 168 private static boolean checkInSync(IResource[] resources, Shell parent, String title) { 169 IStatus status= Resources.checkInSync(resources); 170 if (status.isOK()) 171 return true; 172 ErrorDialog.openError(parent, title, 173 JavaUIMessages.ElementValidator_cannotPerform, 174 status); 175 return false; 176 } 177 178 private static boolean checkValidateEdit(IResource[] resources, Shell parent, String title) { 179 IStatus status= Resources.makeCommittable(resources, parent); 180 if (!status.isOK()) { 181 ErrorDialog.openError(parent, title, 182 JavaUIMessages.ElementValidator_cannotPerform, 183 status); 184 return false; 185 } 186 return true; 187 } 188 189 private static IResource[] getResources(IAdaptable[] elements) { 190 Set result= new HashSet (); 191 for (int i= 0; i < elements.length; i++) { 192 IAdaptable element= elements[i]; 193 IResource resource= null; 194 if (element instanceof IJavaElement) { 195 IJavaElement je= (IJavaElement)element; 196 ICompilationUnit cu= (ICompilationUnit)je.getAncestor(IJavaElement.COMPILATION_UNIT); 197 if (cu != null) { 198 je= cu.getPrimary(); 199 } 200 resource= je.getResource(); 201 } else { 202 resource= (IResource)element.getAdapter(IResource.class); 203 } 204 if (resource != null) 205 result.add(resource); 206 } 207 return (IResource[]) result.toArray(new IResource[result.size()]); 208 } 209 } 210 | Popular Tags |