KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ltk > core > refactoring > model > AbstractRefactoringHistoryResourceMapping


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.core.refactoring.model;
12
13 import java.util.HashSet JavaDoc;
14 import java.util.Set JavaDoc;
15
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.CoreException;
18 import org.eclipse.core.runtime.IProgressMonitor;
19
20 import org.eclipse.core.resources.IProject;
21 import org.eclipse.core.resources.IResource;
22 import org.eclipse.core.resources.IWorkspaceRoot;
23 import org.eclipse.core.resources.ResourcesPlugin;
24 import org.eclipse.core.resources.mapping.ModelProvider;
25 import org.eclipse.core.resources.mapping.ResourceMapping;
26 import org.eclipse.core.resources.mapping.ResourceMappingContext;
27 import org.eclipse.core.resources.mapping.ResourceTraversal;
28
29 import org.eclipse.ltk.core.refactoring.RefactoringDescriptorProxy;
30 import org.eclipse.ltk.core.refactoring.history.RefactoringHistory;
31
32 import org.eclipse.ltk.internal.core.refactoring.RefactoringCorePlugin;
33 import org.eclipse.ltk.internal.core.refactoring.history.RefactoringHistoryService;
34
35 /**
36  * Partial implementation of a resource mapping for a refactoring history
37  * object.
38  * <p>
39  * Note: this class is intended to be implemented by clients which need to
40  * enhance a model provider with a refactoring model.
41  * </p>
42  *
43  * @see ResourceMapping
44  * @see ModelProvider
45  *
46  * @since 3.2
47  */

48 public abstract class AbstractRefactoringHistoryResourceMapping extends ResourceMapping {
49
50     /** The refactoring history */
51     private final RefactoringHistory fRefactoringHistory;
52
53     /** The resource traversals */
54     private ResourceTraversal[] fResourceTraversals= null;
55
56     /**
57      * Creates a new abstract refactoring history resource mapping.
58      *
59      * @param history
60      * the refactoring history
61      */

62     protected AbstractRefactoringHistoryResourceMapping(final RefactoringHistory history) {
63         Assert.isNotNull(history);
64         fRefactoringHistory= history;
65     }
66
67     /**
68      * {@inheritDoc}
69      */

70     public boolean equals(final Object JavaDoc object) {
71         if (object instanceof AbstractRefactoringHistoryResourceMapping) {
72             final AbstractRefactoringHistoryResourceMapping mapping= (AbstractRefactoringHistoryResourceMapping) object;
73             return mapping.fRefactoringHistory.equals(fRefactoringHistory);
74         }
75         return false;
76     }
77
78     /**
79      * {@inheritDoc}
80      */

81     public final Object JavaDoc getModelObject() {
82         return fRefactoringHistory;
83     }
84
85     /**
86      * {@inheritDoc}
87      */

88     public final IProject[] getProjects() {
89         final Set JavaDoc set= new HashSet JavaDoc();
90         final IWorkspaceRoot root= ResourcesPlugin.getWorkspace().getRoot();
91         final RefactoringDescriptorProxy[] proxies= fRefactoringHistory.getDescriptors();
92         for (int index= 0; index < proxies.length; index++) {
93             final String JavaDoc name= proxies[index].getProject();
94             if (name != null && !"".equals(name)) //$NON-NLS-1$
95
set.add(root.getProject(name));
96         }
97         return (IProject[]) set.toArray(new IProject[set.size()]);
98     }
99
100     /**
101      * Returns the associated resource.
102      * <p>
103      * This method only returns a meaningful result if the history contains
104      * refactorings of a single project.
105      * </p>
106      *
107      * @return the associated resource, or <code>null</code> if the
108      * refactoring history contains workspace refactoring descriptors
109      * only, or if it contains refactoring descriptors from multiple
110      * projects.
111      */

112     public final IResource getResource() {
113         try {
114             final ResourceTraversal[] traversals= getTraversals(null, null);
115             if (traversals.length > 0) {
116                 final IResource[] resources= traversals[0].getResources();
117                 if (resources.length > 0)
118                     return resources[0];
119             }
120         } catch (CoreException exception) {
121             RefactoringCorePlugin.log(exception);
122         }
123         return null;
124     }
125
126     /**
127      * {@inheritDoc}
128      */

129     public final ResourceTraversal[] getTraversals(final ResourceMappingContext context, final IProgressMonitor monitor) throws CoreException {
130         if (fResourceTraversals == null) {
131             final IProject[] projects= getProjects();
132             final ResourceTraversal[] traversals= new ResourceTraversal[projects.length];
133             for (int index= 0; index < projects.length; index++)
134                 traversals[index]= new ResourceTraversal(new IResource[] { projects[index].getFolder(RefactoringHistoryService.NAME_HISTORY_FOLDER)}, IResource.DEPTH_INFINITE, IResource.NONE);
135             fResourceTraversals= traversals;
136         }
137         final ResourceTraversal[] traversals= new ResourceTraversal[fResourceTraversals.length];
138         System.arraycopy(fResourceTraversals, 0, traversals, 0, fResourceTraversals.length);
139         return traversals;
140     }
141
142     /**
143      * {@inheritDoc}
144      */

145     public int hashCode() {
146         return fRefactoringHistory.hashCode();
147     }
148 }
149
Popular Tags