KickJava   Java API By Example, From Geeks To Geeks.

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


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

46 public abstract class AbstractRefactoringDescriptorResourceMapping extends ResourceMapping {
47
48     /** The refactoring descriptor */
49     private final RefactoringDescriptorProxy fDescriptor;
50
51     /** The resource traversals */
52     private ResourceTraversal[] fResourceTraversals= null;
53
54     /**
55      * Creates a new abstract refactoring descriptor resource mapping.
56      *
57      * @param descriptor
58      * the refactoring descriptor
59      */

60     protected AbstractRefactoringDescriptorResourceMapping(final RefactoringDescriptorProxy descriptor) {
61         Assert.isNotNull(descriptor);
62         fDescriptor= descriptor;
63     }
64
65     /**
66      * {@inheritDoc}
67      */

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

79     public final Object JavaDoc getModelObject() {
80         return fDescriptor;
81     }
82
83     /**
84      * {@inheritDoc}
85      */

86     public final IProject[] getProjects() {
87         final String JavaDoc project= fDescriptor.getProject();
88         if (project != null && !"".equals(project)) //$NON-NLS-1$
89
return new IProject[] { ResourcesPlugin.getWorkspace().getRoot().getProject(project)};
90         return new IProject[] {};
91     }
92
93     /**
94      * Returns the associated resource.
95      *
96      * @return the associated resource, or <code>null</code> if the descriptor
97      * contains no timestamp or project information
98      */

99     public final IResource getResource() {
100         try {
101             final ResourceTraversal[] traversals= getTraversals(null, null);
102             if (traversals.length > 0) {
103                 final IResource[] resources= traversals[0].getResources();
104                 if (resources.length > 0)
105                     return resources[0];
106             }
107         } catch (CoreException exception) {
108             RefactoringCorePlugin.log(exception);
109         }
110         return null;
111     }
112
113     /**
114      * {@inheritDoc}
115      */

116     public final ResourceTraversal[] getTraversals(final ResourceMappingContext context, final IProgressMonitor monitor) throws CoreException {
117         if (fResourceTraversals == null) {
118             fResourceTraversals= new ResourceTraversal[] {};
119             final long stamp= fDescriptor.getTimeStamp();
120             if (stamp >= 0) {
121                 final IPath path= RefactoringHistoryManager.stampToPath(stamp);
122                 if (path != null) {
123                     final IProject[] projects= getProjects();
124                     if (projects != null && projects.length == 1 && projects[0] != null) {
125                         final IFolder folder= projects[0].getFolder(RefactoringHistoryService.NAME_HISTORY_FOLDER).getFolder(path);
126                         fResourceTraversals= new ResourceTraversal[] { new ResourceTraversal(new IResource[] { folder.getFile(RefactoringHistoryService.NAME_HISTORY_FILE)}, IResource.DEPTH_ZERO, IResource.NONE), new ResourceTraversal(new IResource[] { folder.getFile(RefactoringHistoryService.NAME_INDEX_FILE)}, IResource.DEPTH_ZERO, IResource.NONE)};
127                     }
128                 }
129             }
130         }
131         final ResourceTraversal[] traversals= new ResourceTraversal[fResourceTraversals.length];
132         System.arraycopy(fResourceTraversals, 0, traversals, 0, fResourceTraversals.length);
133         return traversals;
134     }
135
136     /**
137      * {@inheritDoc}
138      */

139     public int hashCode() {
140         return fDescriptor.hashCode();
141     }
142 }
143
Popular Tags