KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > changes > PackageReorgChange


1 /*******************************************************************************
2  * Copyright (c) 2000, 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.jdt.internal.corext.refactoring.changes;
12
13 import org.eclipse.core.resources.mapping.ResourceMapping;
14
15 import org.eclipse.core.runtime.CoreException;
16 import org.eclipse.core.runtime.IProgressMonitor;
17 import org.eclipse.core.runtime.OperationCanceledException;
18
19
20 import org.eclipse.ltk.core.refactoring.Change;
21 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
22 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
23
24 import org.eclipse.jdt.core.IPackageFragment;
25 import org.eclipse.jdt.core.IPackageFragmentRoot;
26 import org.eclipse.jdt.core.JavaCore;
27 import org.eclipse.jdt.core.JavaModelException;
28
29 import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
30 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
31 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
32
33 abstract class PackageReorgChange extends JDTChange {
34
35     private String JavaDoc fPackageHandle;
36     private String JavaDoc fDestinationHandle;
37     private INewNameQuery fNameQuery;
38
39     PackageReorgChange(IPackageFragment pack, IPackageFragmentRoot dest, INewNameQuery nameQuery) {
40         fPackageHandle= pack.getHandleIdentifier();
41         fDestinationHandle= dest.getHandleIdentifier();
42         fNameQuery= nameQuery;
43     }
44
45     abstract Change doPerformReorg(IProgressMonitor pm) throws JavaModelException, OperationCanceledException;
46
47     public RefactoringStatus isValid(IProgressMonitor pm) throws CoreException {
48         // it is enough to check the package only since package reorg changes
49
// are not undoable. Don't check for read only here since
50
// we already ask for user confirmation and moving a read
51
// only package doesn't go thorugh validate edit (no
52
// file content is modified).
53
return isValid(pm, NONE);
54     }
55
56     public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
57         pm.beginTask(getName(), 1);
58         try {
59             IPackageFragment pack= getPackage();
60             ResourceMapping mapping= JavaElementResourceMapping.create(pack);
61             final Change result= doPerformReorg(pm);
62             markAsExecuted(pack, mapping);
63             return result;
64         } finally {
65             pm.done();
66         }
67     }
68
69     public Object JavaDoc getModifiedElement() {
70         return getPackage();
71     }
72
73     IPackageFragmentRoot getDestination() {
74         return (IPackageFragmentRoot)JavaCore.create(fDestinationHandle);
75     }
76
77     IPackageFragment getPackage() {
78         return (IPackageFragment)JavaCore.create(fPackageHandle);
79     }
80
81     String JavaDoc getNewName() throws OperationCanceledException {
82         if (fNameQuery == null)
83             return null;
84         return fNameQuery.getNewName();
85     }
86
87     private void markAsExecuted(IPackageFragment pack, ResourceMapping mapping) {
88         ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class);
89         if (log != null) {
90             log.markAsProcessed(pack);
91             log.markAsProcessed(mapping);
92         }
93     }
94 }
95
Popular Tags