KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > core > refactoring > descriptors > MoveDescriptor


1 /*******************************************************************************
2  * Copyright (c) 2006, 2007 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.jdt.core.refactoring.descriptors;
12
13 import org.eclipse.core.runtime.Assert;
14
15 import org.eclipse.core.resources.IFile;
16 import org.eclipse.core.resources.IFolder;
17 import org.eclipse.core.resources.IResource;
18
19 import org.eclipse.ltk.core.refactoring.RefactoringContribution;
20 import org.eclipse.ltk.core.refactoring.RefactoringCore;
21 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
22
23 import org.eclipse.jdt.core.ICompilationUnit;
24 import org.eclipse.jdt.core.IJavaElement;
25 import org.eclipse.jdt.core.IMember;
26 import org.eclipse.jdt.core.IPackageFragment;
27 import org.eclipse.jdt.core.IPackageFragmentRoot;
28 import org.eclipse.jdt.core.refactoring.IJavaRefactorings;
29
30 import org.eclipse.jdt.internal.core.refactoring.descriptors.DescriptorMessages;
31
32 /**
33  * Refactoring descriptor for the move refactoring.
34  * <p>
35  * An instance of this refactoring descriptor may be obtained by calling
36  * {@link RefactoringContribution#createDescriptor()} on a refactoring
37  * contribution requested by invoking
38  * {@link RefactoringCore#getRefactoringContribution(String)} with the
39  * appropriate refactoring id.
40  * </p>
41  * <p>
42  * Note: this class is not intended to be instantiated by clients.
43  * </p>
44  *
45  * @since 3.3
46  */

47 public final class MoveDescriptor extends JavaRefactoringDescriptor {
48
49     /** The destination attribute */
50     private static final String JavaDoc ATTRIBUTE_DESTINATION= "destination"; //$NON-NLS-1$
51

52     /** The files attribute */
53     private static final String JavaDoc ATTRIBUTE_FILES= "files"; //$NON-NLS-1$
54

55     /** The folders attribute */
56     private static final String JavaDoc ATTRIBUTE_FOLDERS= "folders"; //$NON-NLS-1$
57

58     /** The fragments attribute */
59     private static final String JavaDoc ATTRIBUTE_FRAGMENTS= "fragments"; //$NON-NLS-1$
60

61     /** The members attribute */
62     private static final String JavaDoc ATTRIBUTE_MEMBERS= "members"; //$NON-NLS-1$
63

64     /** The patterns attribute */
65     private static final String JavaDoc ATTRIBUTE_PATTERNS= "patterns"; //$NON-NLS-1$
66

67     /** The policy attribute */
68     private static final String JavaDoc ATTRIBUTE_POLICY= "policy"; //$NON-NLS-1$
69

70     /** The qualified attribute */
71     private static final String JavaDoc ATTRIBUTE_QUALIFIED= "qualified"; //$NON-NLS-1$
72

73     /** The roots attribute */
74     private static final String JavaDoc ATTRIBUTE_ROOTS= "roots"; //$NON-NLS-1$
75

76     /** The target attribute */
77     private static final String JavaDoc ATTRIBUTE_TARGET= "target"; //$NON-NLS-1$
78

79     /** The units attribute */
80     private static final String JavaDoc ATTRIBUTE_UNITS= "units"; //$NON-NLS-1$
81

82     /** The move members policy */
83     private static final String JavaDoc POLICY_MOVE_MEMBERS= "org.eclipse.jdt.ui.moveMembers"; //$NON-NLS-1$
84

85     /** The move packages policy */
86     private static final String JavaDoc POLICY_MOVE_PACKAGES= "org.eclipse.jdt.ui.movePackages"; //$NON-NLS-1$
87

88     /** The move resources policy */
89     private static final String JavaDoc POLICY_MOVE_RESOURCES= "org.eclipse.jdt.ui.moveResources"; //$NON-NLS-1$
90

91     /** The move package fragment roots policy */
92     private static final String JavaDoc POLICY_MOVE_ROOTS= "org.eclipse.jdt.ui.moveRoots"; //$NON-NLS-1$
93

94     /** The destination */
95     private Object JavaDoc fDestination;
96
97     /** The files */
98     private IFile[] fFiles;
99
100     /** The folders */
101     private IFolder[] fFolders;
102
103     /** The package fragments */
104     private IPackageFragment[] fFragments;
105
106     /** The members */
107     private IMember[] fMembers;
108
109     /** The move policy */
110     private String JavaDoc fMovePolicy= null;
111
112     /** The patterns attribute */
113     private String JavaDoc fPatterns= null;
114
115     /** The qualified attribute */
116     private boolean fQualified= false;
117
118     /** The references attribute */
119     private boolean fReferences= false;
120
121     /** The package fragment roots */
122     private IPackageFragmentRoot[] fRoots;
123
124     /** The compilation units */
125     private ICompilationUnit[] fUnits;
126
127     /**
128      * Creates a new refactoring descriptor.
129      */

130     public MoveDescriptor() {
131         super(IJavaRefactorings.MOVE);
132     }
133
134     /**
135      * {@inheritDoc}
136      */

137     protected void populateArgumentMap() {
138         super.populateArgumentMap();
139         fArguments.put(ATTRIBUTE_POLICY, fMovePolicy);
140         final String JavaDoc project= getProject();
141         if (fDestination instanceof IJavaElement)
142             fArguments.put(ATTRIBUTE_DESTINATION, JavaRefactoringDescriptor.elementToHandle(project, (IJavaElement) fDestination));
143         else if (fDestination instanceof IResource)
144             fArguments.put(ATTRIBUTE_TARGET, JavaRefactoringDescriptor.resourceToHandle(null, (IResource) fDestination));
145         if (POLICY_MOVE_RESOURCES.equals(fMovePolicy)) {
146             fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_REFERENCES, Boolean.toString(fReferences));
147             fArguments.put(ATTRIBUTE_QUALIFIED, Boolean.toString(fQualified));
148             if (fPatterns != null && !"".equals(fPatterns)) //$NON-NLS-1$
149
fArguments.put(ATTRIBUTE_PATTERNS, fPatterns);
150             fArguments.put(ATTRIBUTE_FILES, new Integer JavaDoc(fFiles.length).toString());
151             for (int offset= 0; offset < fFiles.length; offset++)
152                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptor.resourceToHandle(project, fFiles[offset]));
153             fArguments.put(ATTRIBUTE_FOLDERS, new Integer JavaDoc(fFolders.length).toString());
154             for (int offset= 0; offset < fFolders.length; offset++)
155                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + fFiles.length + 1), JavaRefactoringDescriptor.resourceToHandle(project, fFolders[offset]));
156             fArguments.put(ATTRIBUTE_UNITS, new Integer JavaDoc(fUnits.length).toString());
157             for (int offset= 0; offset < fUnits.length; offset++)
158                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + fFolders.length + fFiles.length + 1), JavaRefactoringDescriptor.elementToHandle(project, fUnits[offset]));
159         } else if (POLICY_MOVE_ROOTS.equals(fMovePolicy)) {
160             fArguments.put(ATTRIBUTE_ROOTS, new Integer JavaDoc(fRoots.length).toString());
161             for (int offset= 0; offset < fRoots.length; offset++)
162                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptor.elementToHandle(project, fRoots[offset]));
163         } else if (POLICY_MOVE_PACKAGES.equals(fMovePolicy)) {
164             fArguments.put(ATTRIBUTE_FRAGMENTS, new Integer JavaDoc(fFragments.length).toString());
165             for (int offset= 0; offset < fFragments.length; offset++)
166                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptor.elementToHandle(project, fFragments[offset]));
167         } else if (POLICY_MOVE_MEMBERS.equals(fMovePolicy)) {
168             fArguments.put(ATTRIBUTE_MEMBERS, new Integer JavaDoc(fMembers.length).toString());
169             for (int offset= 0; offset < fMembers.length; offset++)
170                 fArguments.put(JavaRefactoringDescriptor.ATTRIBUTE_ELEMENT + (offset + 1), JavaRefactoringDescriptor.elementToHandle(project, fMembers[offset]));
171         }
172     }
173
174     /**
175      * Sets the destination of the move.
176      * <p>
177      * Note: Clients may call only one of the <code>setDestination</code>
178      * methods.
179      * </p>
180      *
181      * @param element
182      * the java element
183      */

184     public void setDestination(IJavaElement element) {
185         Assert.isNotNull(element);
186         fDestination= element;
187     }
188
189     /**
190      * Sets the destination of the move.
191      * <p>
192      * Note: Clients may call only one of the <code>setDestination</code>
193      * methods.
194      * </p>
195      *
196      * @param resource
197      * the resource
198      */

199     public void setDestination(IResource resource) {
200         Assert.isNotNull(resource);
201         fDestination= resource;
202     }
203
204     /**
205      * Sets the file name patterns to use during qualified name updating.
206      * <p>
207      * The syntax of the file name patterns is a sequence of individual name
208      * patterns, separated by comma. Additionally, wildcard characters '*' (any
209      * string) and '?' (any character) may be used.
210      * </p>
211      * <p>
212      * Note: If file name patterns are set, qualified name updating must be
213      * enabled by calling {@link #setUpdateQualifiedNames(boolean)}.
214      * </p>
215      * <p>
216      * Note: Qualified name updating is currently applicable to files, folders
217      * and compilation units. The default is to not update qualified names.
218      * </p>
219      *
220      * @param patterns
221      * the non-empty file name patterns string
222      */

223     public void setFileNamePatterns(final String JavaDoc patterns) {
224         Assert.isNotNull(patterns);
225         Assert.isLegal(!"".equals(patterns), "Pattern must not be empty"); //$NON-NLS-1$ //$NON-NLS-2$
226
fPatterns= patterns;
227     }
228
229     /**
230      * Sets the members to move.
231      * <p>
232      * Note: Clients must only call one of the <code>setMoveXXX</code>
233      * methods.
234      * </p>
235      *
236      * @param members
237      * the members to move
238      */

239     public void setMoveMembers(final IMember[] members) {
240         Assert.isNotNull(members);
241         Assert.isTrue(fMovePolicy == null, "Clients must only call one of the 'setMoveXXX' methods."); //$NON-NLS-1$
242
fMembers= members;
243         fMovePolicy= POLICY_MOVE_MEMBERS;
244     }
245
246     /**
247      * Sets the package fragment roots to move.
248      * <p>
249      * Note: Clients must only call one of the <code>setMoveXXX</code>
250      * methods.
251      * </p>
252      *
253      * @param roots
254      * the package fragment roots to move
255      */

256     public void setMovePackageFragmentRoots(final IPackageFragmentRoot[] roots) {
257         Assert.isNotNull(roots);
258         Assert.isTrue(fMovePolicy == null, "Clients must only call one of the 'setMoveXXX' methods."); //$NON-NLS-1$
259
fRoots= roots;
260         fMovePolicy= POLICY_MOVE_ROOTS;
261     }
262
263     /**
264      * Sets the package fragments to move.
265      * <p>
266      * Note: Clients must only call one of the <code>setMoveXXX</code>
267      * methods.
268      * </p>
269      *
270      * @param fragments
271      * the package fragments to move
272      */

273     public void setMovePackages(final IPackageFragment[] fragments) {
274         Assert.isNotNull(fragments);
275         Assert.isTrue(fMovePolicy == null, "Clients must only call one of the 'setMoveXXX' methods."); //$NON-NLS-1$
276
fFragments= fragments;
277         fMovePolicy= POLICY_MOVE_PACKAGES;
278     }
279
280     /**
281      * Sets the resources and compilation units to move.
282      * <p>
283      * Note: Clients must only call one of the <code>setMoveXXX</code>
284      * methods.
285      * </p>
286      *
287      * @param files
288      * the files to move
289      * @param folders
290      * the folders to move
291      * @param units
292      * the compilation units to move
293      */

294     public void setMoveResources(final IFile[] files, final IFolder[] folders, final ICompilationUnit[] units) {
295         Assert.isNotNull(files);
296         Assert.isNotNull(folders);
297         Assert.isNotNull(units);
298         Assert.isTrue(fMovePolicy == null, "Clients must only call one of the 'setMoveXXX' methods."); //$NON-NLS-1$
299
fFiles= files;
300         fFolders= folders;
301         fUnits= units;
302         fMovePolicy= POLICY_MOVE_RESOURCES;
303     }
304
305     /**
306      * Determines whether qualified names of the Java element should be renamed.
307      * <p>
308      * Qualified name updating adapts fully qualified names of the Java element
309      * to be renamed in non-Java text files. Clients may specify file name
310      * patterns by calling {@link #setFileNamePatterns(String)} to constrain the
311      * set of text files to be processed.
312      * </p>
313      * <p>
314      * Note: Qualified name updating is currently applicable to files, folders
315      * and compilation units. The default is to use no file name patterns
316      * (meaning that all files are processed).
317      * </p>
318      *
319      * @param update
320      * <code>true</code> to update qualified names,
321      * <code>false</code> otherwise
322      */

323     public void setUpdateQualifiedNames(final boolean update) {
324         fQualified= update;
325     }
326
327     /**
328      * Determines whether references to the Java element should be renamed.
329      *
330      * @param update
331      * <code>true</code> to update references, <code>false</code>
332      * otherwise
333      */

334     public void setUpdateReferences(final boolean update) {
335         fReferences= update;
336     }
337
338     /**
339      * {@inheritDoc}
340      */

341     public RefactoringStatus validateDescriptor() {
342         RefactoringStatus status= super.validateDescriptor();
343         if (!status.hasFatalError()) {
344             if (fMovePolicy == null)
345                 status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.MoveDescriptor_no_elements_set));
346             if (fDestination == null)
347                 status.merge(RefactoringStatus.createFatalErrorStatus(DescriptorMessages.MoveDescriptor_no_destination_set));
348         }
349         return status;
350     }
351 }
352
Popular Tags