1 11 package org.eclipse.jdt.internal.ui.browsing; 12 13 import java.util.ArrayList ; 14 import java.util.HashMap ; 15 import java.util.List ; 16 import java.util.Map ; 17 18 import org.eclipse.core.runtime.Assert; 19 20 import org.eclipse.jface.util.IPropertyChangeListener; 21 import org.eclipse.jface.util.PropertyChangeEvent; 22 import org.eclipse.jface.viewers.StructuredViewer; 23 import org.eclipse.jface.viewers.TreeViewer; 24 import org.eclipse.jface.viewers.Viewer; 25 26 import org.eclipse.jdt.core.ElementChangedEvent; 27 import org.eclipse.jdt.core.IElementChangedListener; 28 import org.eclipse.jdt.core.IJavaElement; 29 import org.eclipse.jdt.core.IJavaElementDelta; 30 import org.eclipse.jdt.core.IJavaProject; 31 import org.eclipse.jdt.core.IPackageFragment; 32 import org.eclipse.jdt.core.JavaCore; 33 import org.eclipse.jdt.core.JavaModelException; 34 35 import org.eclipse.jdt.internal.ui.JavaPlugin; 36 37 abstract class LogicalPackagesProvider implements IPropertyChangeListener, IElementChangedListener { 38 39 protected static final Object [] NO_CHILDREN= new Object [0]; 40 41 protected Map fMapToLogicalPackage; 42 protected Map fMapToPackageFragments; 43 protected boolean fCompoundState; 44 protected StructuredViewer fViewer; 45 protected boolean fInputIsProject; 46 47 public LogicalPackagesProvider(StructuredViewer viewer){ 48 fViewer= viewer; 49 fCompoundState= isInCompoundState(); 50 fInputIsProject= true; 51 fMapToLogicalPackage= new HashMap (); 52 fMapToPackageFragments= new HashMap (); 53 JavaPlugin.getDefault().getPreferenceStore().addPropertyChangeListener(this); 54 } 55 56 62 protected void addFragmentsToMap(IPackageFragment[] packageFragments) { 63 for (int i= 0; i < packageFragments.length; i++) { 64 IPackageFragment fragment= packageFragments[i]; 65 String key= getKey(fragment); 66 fMapToPackageFragments.put(key, fragment); 67 } 68 } 69 70 protected String getKey(IPackageFragment fragment) { 71 return fragment.getElementName() + fragment.getJavaProject().getElementName(); 72 } 73 74 81 public LogicalPackage findLogicalPackage(IPackageFragment fragment) { 82 Assert.isNotNull(fragment); 83 if (isInCompoundState()) 84 return (LogicalPackage)fMapToLogicalPackage.get(getKey(fragment)); 85 else 86 return null; 87 } 88 89 97 protected Object [] combineSamePackagesIntoLogialPackages(IPackageFragment[] packageFragments) { 98 99 if (!fCompoundState) 100 return packageFragments; 101 102 List newChildren= new ArrayList (); 103 104 for (int i= 0; i < packageFragments.length; i++) { 105 IPackageFragment fragment= packageFragments[i]; 106 107 if (fragment == null) 108 continue; 109 110 LogicalPackage lp= findLogicalPackage(fragment); 111 112 if (lp != null) { 113 if (lp.belongs(fragment)) { 114 lp.add(fragment); 115 } 116 if(!newChildren.contains(lp)) 117 newChildren.add(lp); 118 119 } else { 120 String key= getKey(fragment); 121 IPackageFragment frag= (IPackageFragment)fMapToPackageFragments.get(key); 122 if (frag != null && !fragment.equals(frag)) { 123 lp= new LogicalPackage(frag); 124 lp.add(fragment); 125 newChildren.remove(frag); 126 newChildren.add(lp); 127 fMapToLogicalPackage.put(key, lp); 128 fMapToPackageFragments.remove(frag); 129 } else { 130 fMapToPackageFragments.put(key, fragment); 131 newChildren.add(fragment); 132 } 133 } 134 } 135 return newChildren.toArray(); 136 } 137 138 141 public void propertyChange(PropertyChangeEvent event) { 142 if (fCompoundState == isInCompoundState()) 143 return; 144 else 145 fCompoundState= isInCompoundState(); 146 147 if (!isInCompoundState()) { 148 fMapToLogicalPackage.clear(); 149 fMapToPackageFragments.clear(); 150 } 151 152 if(fViewer instanceof TreeViewer){ 153 TreeViewer viewer= (TreeViewer) fViewer; 154 Object [] expandedObjects= viewer.getExpandedElements(); 155 viewer.refresh(); 156 viewer.setExpandedElements(expandedObjects); 157 } else 158 fViewer.refresh(); 159 } 160 161 protected boolean isInCompoundState() { 162 return true; 165 } 166 167 public void dispose(){ 168 JavaPlugin.getDefault().getPreferenceStore().removePropertyChangeListener(this); 169 fMapToLogicalPackage= null; 170 fMapToPackageFragments= null; 171 } 172 173 176 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 177 if (newInput != null) { 178 JavaCore.addElementChangedListener(this); 179 } else { 180 JavaCore.removeElementChangedListener(this); 181 } 182 fInputIsProject= (newInput instanceof IJavaProject); 183 184 if(viewer instanceof StructuredViewer) 185 fViewer= (StructuredViewer)viewer; 186 } 187 188 abstract protected void processDelta(IJavaElementDelta delta) throws JavaModelException; 189 190 193 protected boolean isClassPathChange(IJavaElementDelta delta) { 194 195 if (delta.getElement().getElementType() != IJavaElement.PACKAGE_FRAGMENT_ROOT) 197 return false; 198 199 int flags= delta.getFlags(); 200 return (delta.getKind() == IJavaElementDelta.CHANGED && 201 ((flags & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0) || 202 ((flags & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0) || 203 ((flags & IJavaElementDelta.F_REORDER) != 0)); 204 } 205 206 209 public void elementChanged(ElementChangedEvent event) { 210 try { 211 processDelta(event.getDelta()); 212 } catch (JavaModelException e) { 213 JavaPlugin.log(e); 214 } 215 } 216 217 } 218 | Popular Tags |