1 11 package org.eclipse.jdt.internal.ui.browsing; 12 13 import java.util.ArrayList ; 14 import java.util.List ; 15 16 import org.eclipse.swt.widgets.Control; 17 import org.eclipse.swt.widgets.Display; 18 19 import org.eclipse.jface.viewers.IStructuredContentProvider; 20 import org.eclipse.jface.viewers.StructuredViewer; 21 import org.eclipse.jface.viewers.TableViewer; 22 23 import org.eclipse.jdt.core.IJavaElement; 24 import org.eclipse.jdt.core.IJavaElementDelta; 25 import org.eclipse.jdt.core.IJavaProject; 26 import org.eclipse.jdt.core.IPackageFragment; 27 import org.eclipse.jdt.core.IPackageFragmentRoot; 28 import org.eclipse.jdt.core.JavaModelException; 29 30 38 class PackagesViewFlatContentProvider extends LogicalPackagesProvider implements IStructuredContentProvider { 39 PackagesViewFlatContentProvider(StructuredViewer viewer) { 40 super(viewer); 41 } 42 43 46 public Object [] getChildren(Object parentElement) { 47 48 if(parentElement instanceof IJavaElement){ 49 IJavaElement element= (IJavaElement) parentElement; 50 51 int type= element.getElementType(); 52 53 try { 54 switch (type) { 55 case IJavaElement.JAVA_PROJECT : 56 IJavaProject project= (IJavaProject) element; 57 IPackageFragment[] children= getPackageFragments(project.getPackageFragments()); 58 if(isInCompoundState()) { 59 fMapToLogicalPackage.clear(); 60 fMapToPackageFragments.clear(); 61 return combineSamePackagesIntoLogialPackages(children); 62 } else 63 return children; 64 65 case IJavaElement.PACKAGE_FRAGMENT_ROOT : 66 fMapToLogicalPackage.clear(); 67 fMapToPackageFragments.clear(); 68 IPackageFragmentRoot root= (IPackageFragmentRoot) element; 69 return root.getChildren(); 70 71 case IJavaElement.PACKAGE_FRAGMENT : 72 break; 74 75 default : 76 } 78 } catch (JavaModelException e) { 79 return NO_CHILDREN; 80 } 81 82 } 83 return NO_CHILDREN; 84 } 85 86 89 private IPackageFragment[] getPackageFragments(IPackageFragment[] iPackageFragments) { 90 List list= new ArrayList (); 91 for (int i= 0; i < iPackageFragments.length; i++) { 92 IPackageFragment fragment= iPackageFragments[i]; 93 IJavaElement el= fragment.getParent(); 94 if (el instanceof IPackageFragmentRoot) { 95 IPackageFragmentRoot root= (IPackageFragmentRoot) el; 96 if(root.isArchive() && root.isExternal()) 97 continue; 98 } 99 list.add(fragment); 100 } 101 return (IPackageFragment[]) list.toArray(new IPackageFragment[list.size()]); 102 } 103 104 107 public Object [] getElements(Object inputElement) { 108 return getChildren(inputElement); 109 } 110 111 protected void processDelta(IJavaElementDelta delta) throws JavaModelException { 112 113 int kind= delta.getKind(); 114 final IJavaElement element= delta.getElement(); 115 116 if (isClassPathChange(delta)) { 117 Object input= fViewer.getInput(); 118 if (input != null) { 119 if (fInputIsProject && input.equals(element.getJavaProject())) { 120 postRefresh(input); 121 return; 122 } else if (!fInputIsProject && input.equals(element)) { 123 if (element.exists()) 124 postRefresh(input); 125 else 126 postRemove(input); 127 return; 128 } 129 } 130 } 131 132 if (kind == IJavaElementDelta.REMOVED) { 133 Object input= fViewer.getInput(); 134 if (input != null && input.equals(element)) { 135 postRemove(input); 136 return; 137 } 138 } 139 140 if (element instanceof IPackageFragment) { 141 final IPackageFragment frag= (IPackageFragment) element; 142 143 if (kind == IJavaElementDelta.REMOVED) { 144 removeElement(frag); 145 146 } else if (kind == IJavaElementDelta.ADDED) { 147 addElement(frag); 148 149 } else if (kind == IJavaElementDelta.CHANGED) { 150 Object toBeRefreshed= element; 152 153 IPackageFragment pkgFragment= (IPackageFragment) element; 154 LogicalPackage logicalPkg= findLogicalPackage(pkgFragment); 155 if (logicalPkg != null) 157 toBeRefreshed= findElementToRefresh(logicalPkg); 158 else 159 toBeRefreshed= findElementToRefresh(pkgFragment); 160 161 postRefresh(toBeRefreshed); 162 } 163 return; 165 } 166 processAffectedChildren(delta); 167 } 168 169 private Object findElementToRefresh(IPackageFragment fragment) { 172 if (fViewer.testFindItem(fragment) == null) { 173 if(fInputIsProject) 174 return fragment.getJavaProject(); 175 else return fragment.getParent(); 176 } 177 return fragment; 178 } 179 180 private Object findElementToRefresh(LogicalPackage logicalPackage) { 183 if (fViewer.testFindItem(logicalPackage) == null) { 184 IPackageFragment fragment= logicalPackage.getFragments()[0]; 185 return fragment.getJavaProject(); 186 } 187 return logicalPackage; 188 } 189 190 191 private void processAffectedChildren(IJavaElementDelta delta) throws JavaModelException { 192 IJavaElementDelta[] children= delta.getAffectedChildren(); 193 for (int i= 0; i < children.length; i++) { 194 IJavaElementDelta elementDelta= children[i]; 195 processDelta(elementDelta); 196 } 197 } 198 199 private void postAdd(final Object child) { 200 postRunnable(new Runnable () { 201 public void run() { 202 Control ctrl = fViewer.getControl(); 203 if (ctrl != null && !ctrl.isDisposed()) { 204 ((TableViewer)fViewer).add(child); 205 } 206 } 207 }); 208 } 209 210 211 private void postRemove(final Object object) { 212 postRunnable(new Runnable () { 213 public void run() { 214 Control ctrl = fViewer.getControl(); 215 if (ctrl != null && !ctrl.isDisposed()) { 216 ((TableViewer)fViewer).remove(object); 217 } 218 } 219 }); 220 } 221 222 private void postRunnable(final Runnable r) { 223 Control ctrl= fViewer.getControl(); 224 if (ctrl != null && !ctrl.isDisposed()) { 225 try { 227 Display currentDisplay= Display.getCurrent(); 228 if (currentDisplay != null && currentDisplay.equals(ctrl.getDisplay())) 229 ctrl.getDisplay().syncExec(r); 230 else 231 ctrl.getDisplay().asyncExec(r); 232 } finally { 233 } 235 } 236 } 237 238 private void removeElement(IPackageFragment frag) { 239 String key= getKey(frag); 240 LogicalPackage lp= (LogicalPackage)fMapToLogicalPackage.get(key); 241 242 if(lp != null){ 243 lp.remove(frag); 244 if(lp.getFragments().length == 1){ 246 IPackageFragment fragment= lp.getFragments()[0]; 247 fMapToLogicalPackage.remove(key); 248 fMapToPackageFragments.put(key,fragment); 249 250 postRemove(lp); 252 postAdd(fragment); 253 } return; 254 } else { 255 fMapToPackageFragments.remove(key); 256 postRemove(frag); 257 } 258 } 259 260 261 private void postRefresh(final Object element) { 262 postRunnable(new Runnable () { 263 public void run() { 264 Control ctrl= fViewer.getControl(); 265 if (ctrl != null && !ctrl.isDisposed()) { 266 fViewer.refresh(element); 267 } 268 } 269 }); 270 } 271 272 private void addElement(IPackageFragment frag) { 273 String key= getKey(frag); 274 LogicalPackage lp= (LogicalPackage)fMapToLogicalPackage.get(key); 275 276 if(lp != null && lp.belongs(frag)){ 277 lp.add(frag); 278 return; 279 } 280 281 IPackageFragment fragment= (IPackageFragment)fMapToPackageFragments.get(key); 282 if(fragment != null){ 283 if(!fragment.equals(frag)){ 285 lp= new LogicalPackage(fragment); 286 lp.add(frag); 287 fMapToLogicalPackage.put(key, lp); 288 289 postRemove(fragment); 291 postAdd(lp); 292 293 return; 294 } 295 } 296 297 else { 298 fMapToPackageFragments.put(key, frag); 299 postAdd(frag); 300 } 301 } 302 } 303 | Popular Tags |