01: /*******************************************************************************
02: * Copyright (c) 2000, 2006 IBM Corporation and others.
03: * All rights reserved. This program and the accompanying materials
04: * are made available under the terms of the Eclipse Public License v1.0
05: * which accompanies this distribution, and is available at
06: * http://www.eclipse.org/legal/epl-v10.html
07: *
08: * Contributors:
09: * IBM Corporation - initial API and implementation
10: *******************************************************************************/package org.eclipse.jdt.internal.corext.refactoring.changes;
11:
12: import org.eclipse.core.resources.mapping.ResourceMapping;
13:
14: import org.eclipse.core.runtime.CoreException;
15: import org.eclipse.core.runtime.IProgressMonitor;
16: import org.eclipse.core.runtime.OperationCanceledException;
17:
18: import org.eclipse.ltk.core.refactoring.Change;
19: import org.eclipse.ltk.core.refactoring.RefactoringStatus;
20: import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
21:
22: import org.eclipse.jdt.core.IPackageFragment;
23: import org.eclipse.jdt.core.IPackageFragmentRoot;
24: import org.eclipse.jdt.core.JavaCore;
25: import org.eclipse.jdt.core.JavaModelException;
26:
27: import org.eclipse.jdt.internal.corext.refactoring.base.JDTChange;
28: import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
29: import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
30:
31: abstract class PackageReorgChange extends JDTChange {
32:
33: private String fPackageHandle;
34: private String fDestinationHandle;
35: private INewNameQuery fNameQuery;
36:
37: PackageReorgChange(IPackageFragment pack,
38: IPackageFragmentRoot dest, INewNameQuery nameQuery) {
39: fPackageHandle = pack.getHandleIdentifier();
40: fDestinationHandle = dest.getHandleIdentifier();
41: fNameQuery = nameQuery;
42: }
43:
44: abstract Change doPerformReorg(IProgressMonitor pm)
45: throws JavaModelException, OperationCanceledException;
46:
47: public RefactoringStatus isValid(IProgressMonitor pm)
48: throws CoreException {
49: // it is enough to check the package only since package reorg changes
50: // are not undoable. Don't check for read only here since
51: // we already ask for user confirmation and moving a read
52: // only package doesn't go thorugh validate edit (no
53: // file content is modified).
54: return isValid(pm, NONE);
55: }
56:
57: public final Change perform(IProgressMonitor pm)
58: throws CoreException, OperationCanceledException {
59: pm.beginTask(getName(), 1);
60: try {
61: IPackageFragment pack = getPackage();
62: ResourceMapping mapping = JavaElementResourceMapping
63: .create(pack);
64: final Change result = doPerformReorg(pm);
65: markAsExecuted(pack, mapping);
66: return result;
67: } finally {
68: pm.done();
69: }
70: }
71:
72: public Object getModifiedElement() {
73: return getPackage();
74: }
75:
76: IPackageFragmentRoot getDestination() {
77: return (IPackageFragmentRoot) JavaCore
78: .create(fDestinationHandle);
79: }
80:
81: IPackageFragment getPackage() {
82: return (IPackageFragment) JavaCore.create(fPackageHandle);
83: }
84:
85: String getNewName() throws OperationCanceledException {
86: if (fNameQuery == null)
87: return null;
88: return fNameQuery.getNewName();
89: }
90:
91: private void markAsExecuted(IPackageFragment pack,
92: ResourceMapping mapping) {
93: ReorgExecutionLog log = (ReorgExecutionLog) getAdapter(ReorgExecutionLog.class);
94: if (log != null) {
95: log.markAsProcessed(pack);
96: log.markAsProcessed(mapping);
97: }
98: }
99: }
|