KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > internal > ui > refactoring > history > BrowseRefactoringHistoryContentProvider


1 /*******************************************************************************
2  * Copyright (c) 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ltk.internal.ui.refactoring.history;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Collection JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.HashSet JavaDoc;
18 import java.util.Iterator JavaDoc;
19 import java.util.List JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
35  * Tree content provider for the browse refactoring history control.
36  *
37  * @since 3.2
38  */

39 public final class BrowseRefactoringHistoryContentProvider extends RefactoringHistoryContentProvider {
40
41     /** The no elements constant */
42     private static final Object JavaDoc[] NO_ELEMENTS= {};
43
44     /** The workspace project constant */
45     private static final String JavaDoc WORKSPACE_PROJECT= ".workspace"; //$NON-NLS-1$
46

47     /** The refactoring history control configuration to use */
48     private final RefactoringHistoryControlConfiguration fControlConfiguration;
49
50     /** The project content providers */
51     private Map JavaDoc fProjectContentProviders= null;
52
53     /** The project refactoring histories map */
54     private Map JavaDoc fProjectRefactoringHistories= null;
55
56     /** The refactoring history, or <code>null</code> */
57     private RefactoringHistory fRefactoringHistory= null;
58
59     /** Should the refactoring history be sorted by projects? */
60     private boolean fSortProjects= true;
61
62     /**
63      * Creates a new browse refactoring history content provider.
64      *
65      * @param configuration
66      * the refactoring history control configuration
67      */

68     public BrowseRefactoringHistoryContentProvider(final RefactoringHistoryControlConfiguration configuration) {
69         super(configuration);
70         Assert.isNotNull(configuration);
71         fControlConfiguration= configuration;
72     }
73
74     /**
75      * {@inheritDoc}
76      */

77     public Object JavaDoc[] getChildren(final Object JavaDoc 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     /**
93      * {@inheritDoc}
94      */

95     public Object JavaDoc[] getElements(final Object JavaDoc element) {
96         if (fSortProjects && element instanceof RefactoringHistory)
97             return getRootElements();
98         return super.getElements(element);
99     }
100
101     /**
102      * {@inheritDoc}
103      */

104     public Object JavaDoc getParent(final Object JavaDoc 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     /**
115      * Returns the refactoring histories.
116      *
117      * @return the map of projects to refactoring histories
118      */

119     private Map JavaDoc getRefactoringHistories() {
120         if (fProjectRefactoringHistories == null) {
121             fProjectRefactoringHistories= new HashMap JavaDoc();
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 JavaDoc current= proxy.getProject();
127                     if (current == null || current.length() == 0)
128                         current= WORKSPACE_PROJECT;
129                     Collection JavaDoc collection= (Collection JavaDoc) fProjectRefactoringHistories.get(current);
130                     if (collection == null) {
131                         collection= new HashSet JavaDoc();
132                         fProjectRefactoringHistories.put(current, collection);
133                     }
134                     collection.add(proxy);
135                 }
136                 for (final Iterator JavaDoc iterator= new ArrayList JavaDoc(fProjectRefactoringHistories.keySet()).iterator(); iterator.hasNext();) {
137                     final String JavaDoc current= (String JavaDoc) iterator.next();
138                     final Collection JavaDoc collection= (Collection JavaDoc) 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     /**
148      * Returns the refactoring descriptor proxies for the specified project.
149      *
150      * @param project
151      * the project
152      * @return the refactoring history, or <code>null</code> if no history is
153      * available for the project
154      */

155     private RefactoringHistory getRefactoringHistory(final String JavaDoc project) {
156         getRefactoringHistories();
157         return (RefactoringHistory) fProjectRefactoringHistories.get(project);
158     }
159
160     /**
161      * Returns the refactoring history content provider for the specified node.
162      *
163      * @param node
164      * the refactoring history node
165      * @return the refactoring history content provider, or <code>null</code>
166      */

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     /**
186      * Returns the refactoring history content provider for the specified
187      * project.
188      *
189      * @param project
190      * the project
191      * @return the refactoring history content provider
192      */

193     private RefactoringHistoryContentProvider getRefactoringHistoryContentProvider(final String JavaDoc project) {
194         if (fProjectContentProviders == null)
195             fProjectContentProviders= new HashMap JavaDoc();
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     /**
205      * Returns the refactoring history entries for the specified project.
206      *
207      * @param project
208      * the project
209      * @return the refactoring history entries
210      */

211     private Object JavaDoc[] getRefactoringHistoryEntries(final RefactoringHistoryProject project) {
212         final String JavaDoc 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 JavaDoc[] 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     /**
242      * {@inheritDoc}
243      */

244     public Object JavaDoc[] getRootElements() {
245         if (fSortProjects) {
246             final List JavaDoc list= new ArrayList JavaDoc(32);
247             for (final Iterator JavaDoc iterator= getRefactoringHistories().keySet().iterator(); iterator.hasNext();) {
248                 final String JavaDoc project= (String JavaDoc) 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 JavaDoc[] { new RefactoringHistoryCollection()};
274     }
275
276     /**
277      * Returns the root node of the specified node.
278      *
279      * @param node
280      * the refactoring history node
281      * @return the root node, or the node itself
282      */

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     /**
294      * {@inheritDoc}
295      */

296     public void inputChanged(final Viewer viewer, final Object JavaDoc predecessor, final Object JavaDoc 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     /**
309      * Should the refactoring history be sorted by projects?
310      *
311      * @return <code>true</code> if it should be sorted by projects,
312      * <code>false</code> otherwise
313      */

314     public boolean isSortProjects() {
315         return fSortProjects;
316     }
317
318     /**
319      * Determines whether the refactoring history should be sorted by projects.
320      *
321      * @param sort
322      * <code>true</code> to sort by projects, <code>false</code>
323      * otherwise
324      */

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