KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > reorg > CopyRefactoring


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

11 package org.eclipse.jdt.internal.corext.refactoring.reorg;
12
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.SubProgressMonitor;
16
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.jdt.core.IJavaElement;
20 import org.eclipse.jdt.core.JavaModelException;
21
22 import org.eclipse.jdt.internal.corext.Assert;
23 import org.eclipse.jdt.internal.corext.codemanipulation.CodeGenerationSettings;
24 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
25 import org.eclipse.jdt.internal.corext.refactoring.changes.DynamicValidationStateChange;
26 import org.eclipse.jdt.internal.corext.refactoring.reorg.IReorgPolicy.ICopyPolicy;
27 import org.eclipse.jdt.internal.corext.util.Resources;
28 import org.eclipse.ltk.core.refactoring.Change;
29 import org.eclipse.ltk.core.refactoring.CompositeChange;
30 import org.eclipse.ltk.core.refactoring.Refactoring;
31 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
32 import org.eclipse.ltk.core.refactoring.participants.CheckConditionsContext;
33 import org.eclipse.ltk.core.refactoring.participants.IConditionChecker;
34 import org.eclipse.ltk.core.refactoring.participants.ValidateEditChecker;
35
36 public final class CopyRefactoring extends Refactoring implements IReorgDestinationValidator {
37     //TODO: offer ICopyPolicy getCopyPolicy(); IReorgPolicy getReorgPolicy();
38
// and remove delegate methods (also for JavaMoveProcessor)?
39

40     private INewNameQueries fNewNameQueries;
41     private IReorgQueries fReorgQueries;
42     private ICopyPolicy fCopyPolicy;
43     
44     public static boolean isAvailable(IResource[] resources, IJavaElement[] javaElements, CodeGenerationSettings settings) throws JavaModelException{
45         return isAvailable(ReorgPolicyFactory.createCopyPolicy(resources, javaElements, settings));
46     }
47     
48     public static CopyRefactoring create(IResource[] resources, IJavaElement[] javaElements, CodeGenerationSettings settings) throws JavaModelException{
49         ICopyPolicy copyPolicy= ReorgPolicyFactory.createCopyPolicy(resources, javaElements, settings);
50         if (! isAvailable(copyPolicy))
51             return null;
52         return new CopyRefactoring(copyPolicy);
53     }
54
55     private static boolean isAvailable(ICopyPolicy copyPolicy) throws JavaModelException{
56         return copyPolicy.canEnable();
57     }
58         
59     private CopyRefactoring(ICopyPolicy copyPolicy) {
60         fCopyPolicy= copyPolicy;
61     }
62     
63     public void setNewNameQueries(INewNameQueries newNameQueries){
64         Assert.isNotNull(newNameQueries);
65         fNewNameQueries= newNameQueries;
66     }
67
68     public void setReorgQueries(IReorgQueries queries){
69         Assert.isNotNull(queries);
70         fReorgQueries= queries;
71     }
72
73     public RefactoringStatus checkInitialConditions(IProgressMonitor pm) throws CoreException {
74         RefactoringStatus result= new RefactoringStatus();
75         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(fCopyPolicy.getResources()))));
76         IResource[] javaResources= ReorgUtils.getResources(fCopyPolicy.getJavaElements());
77         result.merge(RefactoringStatus.create(Resources.checkInSync(ReorgUtils.getNotNulls(javaResources))));
78         return result;
79     }
80
81     public Object JavaDoc getCommonParentForInputElements(){
82         return new ParentChecker(fCopyPolicy.getResources(), fCopyPolicy.getJavaElements()).getCommonParent();
83     }
84     
85     public IJavaElement[] getJavaElements() {
86         return fCopyPolicy.getJavaElements();
87     }
88
89     public IResource[] getResources() {
90         return fCopyPolicy.getResources();
91     }
92
93     public RefactoringStatus setDestination(IJavaElement destination) throws JavaModelException{
94         return fCopyPolicy.setDestination(destination);
95     }
96
97     public RefactoringStatus setDestination(IResource destination) throws JavaModelException{
98         return fCopyPolicy.setDestination(destination);
99     }
100     
101     public boolean canChildrenBeDestinations(IJavaElement javaElement) {
102         return fCopyPolicy.canChildrenBeDestinations(javaElement);
103     }
104     public boolean canChildrenBeDestinations(IResource resource) {
105         return fCopyPolicy.canChildrenBeDestinations(resource);
106     }
107     public boolean canElementBeDestination(IJavaElement javaElement) {
108         return fCopyPolicy.canElementBeDestination(javaElement);
109     }
110     public boolean canElementBeDestination(IResource resource) {
111         return fCopyPolicy.canElementBeDestination(resource);
112     }
113     
114     public RefactoringStatus checkFinalConditions(IProgressMonitor pm) throws CoreException {
115         Assert.isNotNull(fNewNameQueries, "Missing new name queries"); //$NON-NLS-1$
116
Assert.isNotNull(fReorgQueries, "Missing reorg queries"); //$NON-NLS-1$
117
pm.beginTask("", 2); //$NON-NLS-1$
118
CheckConditionsContext context= createCheckConditionsContext();
119         RefactoringStatus result= fCopyPolicy.checkFinalConditions(new SubProgressMonitor(pm, 1), context, fReorgQueries);
120         result.merge(context.check(new SubProgressMonitor(pm, 1)));
121         return result;
122     }
123
124     /* (non-Javadoc)
125      * @see org.eclipse.jdt.internal.corext.refactoring.base.IRefactoring#createChange(org.eclipse.core.runtime.IProgressMonitor)
126      */

127     public Change createChange(IProgressMonitor pm) throws CoreException {
128         Assert.isNotNull(fNewNameQueries);
129         Assert.isTrue(fCopyPolicy.getJavaElementDestination() == null || fCopyPolicy.getResourceDestination() == null);
130         Assert.isTrue(fCopyPolicy.getJavaElementDestination() != null || fCopyPolicy.getResourceDestination() != null);
131         try {
132             final DynamicValidationStateChange result= new DynamicValidationStateChange(getName()) {
133                 public Change perform(IProgressMonitor pm2) throws CoreException {
134                     super.perform(pm2);
135                     return null;
136                 }
137             };
138             Change change= fCopyPolicy.createChange(pm, fNewNameQueries);
139             if (change instanceof CompositeChange){
140                 CompositeChange subComposite= (CompositeChange)change;
141                 result.merge(subComposite);
142             } else{
143                 result.add(change);
144             }
145             return result;
146         } finally {
147             pm.done();
148         }
149     }
150
151     public String JavaDoc getName() {
152         return RefactoringCoreMessages.getString("CopyRefactoring.0"); //$NON-NLS-1$
153
}
154     
155     private CheckConditionsContext createCheckConditionsContext() throws CoreException {
156         CheckConditionsContext result= new CheckConditionsContext();
157         IConditionChecker checker= new ValidateEditChecker(null);
158         result.add(checker);
159         return result;
160         
161     }
162 }
163
Popular Tags