1 /*******************************************************************************
2 * Copyright (c) 2000, 2011 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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.jdt.internal.corext.refactoring.changes;
13 import org.eclipse.core.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.OperationCanceledException;
16 import org.eclipse.core.runtime.SubProgressMonitor;
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
21 import org.eclipse.ltk.core.refactoring.Change;
22 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
23 import org.eclipse.ltk.core.refactoring.resource.ResourceChange;
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IPackageFragment;
27 import org.eclipse.jdt.core.JavaCore;
29 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
30 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
32 import org.eclipse.jdt.ui.JavaElementLabels;
34 abstract class CompilationUnitReorgChange extends ResourceChange {
36 private String fCuHandle;
37 private String fOldPackageHandle;
38 private String fNewPackageHandle;
40 private INewNameQuery fNewNameQuery;
42 CompilationUnitReorgChange(ICompilationUnit cu, IPackageFragment dest, INewNameQuery newNameQuery) {
43 fCuHandle= cu.getHandleIdentifier();
44 fNewPackageHandle= dest.getHandleIdentifier();
45 fNewNameQuery= newNameQuery;
46 fOldPackageHandle= cu.getParent().getHandleIdentifier();
49 CompilationUnitReorgChange(ICompilationUnit cu, IPackageFragment dest) {
53 CompilationUnitReorgChange(String oldPackageHandle, String newPackageHandle, String cuHandle) {
54 fOldPackageHandle= oldPackageHandle;
55 fNewPackageHandle= newPackageHandle;
60 public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
61 pm.beginTask(getName(), 1);
63 ICompilationUnit unit= getCu();
64 ResourceMapping mapping= JavaElementResourceMapping.create(unit);
65 Change result= doPerformReorg(new SubProgressMonitor(pm, 1));
66 markAsExecuted(unit, mapping);
73 abstract Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException;
76 public Object getModifiedElement() {
81 * @see org.eclipse.jdt.internal.corext.refactoring.base.JDTChange#getModifiedResource()
84 protected IResource getModifiedResource() {
85 ICompilationUnit cu= getCu();
87 return cu.getResource();
92 ICompilationUnit getCu() {
93 return (ICompilationUnit)JavaCore.create(fCuHandle);
96 IPackageFragment getOldPackage() {
97 return (IPackageFragment)JavaCore.create(fOldPackageHandle);
100 IPackageFragment getDestinationPackage() {
101 return (IPackageFragment)JavaCore.create(fNewPackageHandle);
104 String getNewName() throws OperationCanceledException {
105 if (fNewNameQuery == null)
107 return fNewNameQuery.getNewName();
110 static String getPackageName(IPackageFragment pack) {
111 return JavaElementLabels.getElementLabel(pack, JavaElementLabels.ALL_DEFAULT);
114 private void markAsExecuted(ICompilationUnit unit, ResourceMapping mapping) {
115 ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class);
117 log.markAsProcessed(unit);
118 log.markAsProcessed(mapping);