1 11 package org.eclipse.ltk.internal.ui.refactoring.history; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Collection ; 16 import java.util.HashMap ; 17 import java.util.HashSet ; 18 import java.util.Iterator ; 19 import java.util.List ; 20 import java.util.Map ; 21 22 import org.eclipse.core.runtime.Assert; 23 24 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy; 25 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory; 26 27 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryImplementation; 28 29 import org.eclipse.jface.viewers.Viewer; 30 31 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryContentProvider; 32 import org.eclipse.ltk.ui.refactoring.history.RefactoringHistoryControlConfiguration; 33 34 39 public final class BrowseRefactoringHistoryContentProvider extends RefactoringHistoryContentProvider { 40 41 42 private static final Object [] NO_ELEMENTS= {}; 43 44 45 private static final String WORKSPACE_PROJECT= ".workspace"; 47 48 private final RefactoringHistoryControlConfiguration fControlConfiguration; 49 50 51 private Map fProjectContentProviders= null; 52 53 54 private Map fProjectRefactoringHistories= null; 55 56 57 private RefactoringHistory fRefactoringHistory= null; 58 59 60 private boolean fSortProjects= true; 61 62 68 public BrowseRefactoringHistoryContentProvider(final RefactoringHistoryControlConfiguration configuration) { 69 super(configuration); 70 Assert.isNotNull(configuration); 71 fControlConfiguration= configuration; 72 } 73 74 77 public Object [] getChildren(final Object element) { 78 if (fSortProjects && element instanceof RefactoringHistoryNode) { 79 final RefactoringHistoryNode node= (RefactoringHistoryNode) element; 80 if (node instanceof RefactoringHistoryProject) 81 return getRefactoringHistoryEntries((RefactoringHistoryProject) node); 82 else { 83 final RefactoringHistoryContentProvider provider= getRefactoringHistoryContentProvider(node); 84 if (provider != null) 85 return provider.getChildren(element); 86 } 87 return NO_ELEMENTS; 88 } 89 return super.getChildren(element); 90 } 91 92 95 public Object [] getElements(final Object element) { 96 if (fSortProjects && element instanceof RefactoringHistory) 97 return getRootElements(); 98 return super.getElements(element); 99 } 100 101 104 public Object getParent(final Object element) { 105 if (fSortProjects && element instanceof RefactoringHistoryNode) { 106 final RefactoringHistoryContentProvider provider= getRefactoringHistoryContentProvider((RefactoringHistoryNode) element); 107 if (provider != null) 108 return provider.getParent(element); 109 return null; 110 } 111 return super.getParent(element); 112 } 113 114 119 private Map getRefactoringHistories() { 120 if (fProjectRefactoringHistories == null) { 121 fProjectRefactoringHistories= new HashMap (); 122 if (fRefactoringHistory != null && !fRefactoringHistory.isEmpty()) { 123 final RefactoringDescriptorProxy[] proxies= fRefactoringHistory.getDescriptors(); 124 for (int index= 0; index < proxies.length; index++) { 125 final RefactoringDescriptorProxy proxy= proxies[index]; 126 String current= proxy.getProject(); 127 if (current == null || current.length() == 0) 128 current= WORKSPACE_PROJECT; 129 Collection collection= (Collection ) fProjectRefactoringHistories.get(current); 130 if (collection == null) { 131 collection= new HashSet (); 132 fProjectRefactoringHistories.put(current, collection); 133 } 134 collection.add(proxy); 135 } 136 for (final Iterator iterator= new ArrayList (fProjectRefactoringHistories.keySet()).iterator(); iterator.hasNext();) { 137 final String current= (String ) iterator.next(); 138 final Collection collection= (Collection ) fProjectRefactoringHistories.get(current); 139 if (collection != null) 140 fProjectRefactoringHistories.put(current, new RefactoringHistoryImplementation((RefactoringDescriptorProxy[]) collection.toArray(new RefactoringDescriptorProxy[collection.size()]))); 141 } 142 } 143 } 144 return fProjectRefactoringHistories; 145 } 146 147 155 private RefactoringHistory getRefactoringHistory(final String project) { 156 getRefactoringHistories(); 157 return (RefactoringHistory) fProjectRefactoringHistories.get(project); 158 } 159 160 167 private RefactoringHistoryContentProvider getRefactoringHistoryContentProvider(final RefactoringHistoryNode node) { 168 Assert.isNotNull(node); 169 final RefactoringHistoryNode root= getRootNode(node); 170 if (root instanceof RefactoringHistoryProject) { 171 final RefactoringHistoryProject extended= (RefactoringHistoryProject) root; 172 final RefactoringHistory history= getRefactoringHistory(extended.getProject()); 173 if (history != null) { 174 final RefactoringHistoryContentProvider provider= getRefactoringHistoryContentProvider(extended.getProject()); 175 if (provider != null) { 176 provider.inputChanged(null, null, history); 177 return provider; 178 } 179 } 180 } else if (!(node instanceof RefactoringHistoryEntry)) 181 return getRefactoringHistoryContentProvider(WORKSPACE_PROJECT); 182 return null; 183 } 184 185 193 private RefactoringHistoryContentProvider getRefactoringHistoryContentProvider(final String project) { 194 if (fProjectContentProviders == null) 195 fProjectContentProviders= new HashMap (); 196 RefactoringHistoryContentProvider provider= (RefactoringHistoryContentProvider) fProjectContentProviders.get(project); 197 if (provider == null) { 198 provider= fControlConfiguration.getContentProvider(); 199 fProjectContentProviders.put(project, provider); 200 } 201 return provider; 202 } 203 204 211 private Object [] getRefactoringHistoryEntries(final RefactoringHistoryProject project) { 212 final String name= project.getProject(); 213 final RefactoringHistory history= getRefactoringHistory(name); 214 if (history != null) { 215 if (fControlConfiguration.isTimeDisplayed()) { 216 final RefactoringHistoryContentProvider provider= getRefactoringHistoryContentProvider(project); 217 if (provider != null) { 218 provider.inputChanged(null, null, history); 219 final Object [] elements= provider.getRootElements(); 220 if (!WORKSPACE_PROJECT.equals(name)) { 221 for (int index= 0; index < elements.length; index++) { 222 if (elements[index] instanceof RefactoringHistoryDate) { 223 final RefactoringHistoryDate date= (RefactoringHistoryDate) elements[index]; 224 elements[index]= new RefactoringHistoryDate(project, date.getTimeStamp(), date.getKind()); 225 } 226 } 227 } 228 return elements; 229 } 230 } else { 231 final RefactoringDescriptorProxy[] proxies= history.getDescriptors(); 232 final RefactoringHistoryEntry[] entries= new RefactoringHistoryEntry[proxies.length]; 233 for (int index= 0; index < proxies.length; index++) 234 entries[index]= new RefactoringHistoryEntry(project, proxies[index]); 235 return entries; 236 } 237 } 238 return NO_ELEMENTS; 239 } 240 241 244 public Object [] getRootElements() { 245 if (fSortProjects) { 246 final List list= new ArrayList (32); 247 for (final Iterator iterator= getRefactoringHistories().keySet().iterator(); iterator.hasNext();) { 248 final String project= (String ) iterator.next(); 249 if (project.equals(WORKSPACE_PROJECT)) { 250 final RefactoringHistory history= getRefactoringHistory(project); 251 if (project != null) { 252 if (fControlConfiguration.isTimeDisplayed()) { 253 final RefactoringHistoryContentProvider provider= getRefactoringHistoryContentProvider(project); 254 if (provider != null) { 255 provider.inputChanged(null, null, history); 256 list.addAll(Arrays.asList(provider.getRootElements())); 257 } 258 } else { 259 final RefactoringDescriptorProxy[] proxies= history.getDescriptors(); 260 final RefactoringHistoryEntry[] entries= new RefactoringHistoryEntry[proxies.length]; 261 for (int index= 0; index < proxies.length; index++) 262 entries[index]= new RefactoringHistoryEntry(null, proxies[index]); 263 list.addAll(Arrays.asList(entries)); 264 } 265 } 266 } else 267 list.add(new RefactoringHistoryProject(project)); 268 } 269 return list.toArray(); 270 } else if (fControlConfiguration.isTimeDisplayed()) 271 return super.getRootElements(); 272 else 273 return new Object [] { new RefactoringHistoryCollection()}; 274 } 275 276 283 private RefactoringHistoryNode getRootNode(final RefactoringHistoryNode node) { 284 RefactoringHistoryNode current= node; 285 RefactoringHistoryNode parent= current.getParent(); 286 while (parent != null) { 287 current= parent; 288 parent= current.getParent(); 289 } 290 return current; 291 } 292 293 296 public void inputChanged(final Viewer viewer, final Object predecessor, final Object successor) { 297 super.inputChanged(viewer, predecessor, successor); 298 if (predecessor == successor) 299 return; 300 if (successor instanceof RefactoringHistory) 301 fRefactoringHistory= (RefactoringHistory) successor; 302 else 303 fRefactoringHistory= null; 304 fProjectRefactoringHistories= null; 305 fProjectContentProviders= null; 306 } 307 308 314 public boolean isSortProjects() { 315 return fSortProjects; 316 } 317 318 325 public void setSortProjects(final boolean sort) { 326 if (sort != fSortProjects) { 327 fProjectRefactoringHistories= null; 328 fProjectContentProviders= null; 329 } 330 fSortProjects= sort; 331 } 332 } | Popular Tags |