]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-after/core refactoring/org/eclipse/jdt/internal/corext/refactoring/changes/CompilationUnitReorgChange.java
Some talks, mostly identical.
[ifi-stolz-refaktor.git] / case-study / jdt-after / core refactoring / org / eclipse / jdt / internal / corext / refactoring / changes / CompilationUnitReorgChange.java
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
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.runtime.CoreException;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.OperationCanceledException;
16 import org.eclipse.core.runtime.SubProgressMonitor;
17
18 import org.eclipse.core.resources.IResource;
19 import org.eclipse.core.resources.mapping.ResourceMapping;
20
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;
24
25 import org.eclipse.jdt.core.ICompilationUnit;
26 import org.eclipse.jdt.core.IPackageFragment;
27 import org.eclipse.jdt.core.JavaCore;
28
29 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
30 import org.eclipse.jdt.internal.corext.util.JavaElementResourceMapping;
31
32 import org.eclipse.jdt.ui.JavaElementLabels;
33
34 abstract class CompilationUnitReorgChange extends ResourceChange {
35
36         private String fCuHandle;
37         private String fOldPackageHandle;
38         private String fNewPackageHandle;
39
40         private INewNameQuery fNewNameQuery;
41
42         CompilationUnitReorgChange(ICompilationUnit cu, IPackageFragment dest, INewNameQuery newNameQuery) {
43                 fCuHandle= cu.getHandleIdentifier();
44                 fNewPackageHandle= dest.getHandleIdentifier();
45                 fNewNameQuery= newNameQuery;
46                 fOldPackageHandle= cu.getParent().getHandleIdentifier();
47         }
48
49         CompilationUnitReorgChange(ICompilationUnit cu, IPackageFragment dest) {
50                 this(cu, dest, null);
51         }
52
53         CompilationUnitReorgChange(String oldPackageHandle, String newPackageHandle, String cuHandle) {
54                 fOldPackageHandle= oldPackageHandle;
55                 fNewPackageHandle= newPackageHandle;
56                 fCuHandle= cuHandle;
57         }
58
59         @Override
60         public final Change perform(IProgressMonitor pm) throws CoreException, OperationCanceledException {
61                 pm.beginTask(getName(), 1);
62                 try {
63                         ICompilationUnit unit= getCu();
64                         ResourceMapping mapping= JavaElementResourceMapping.create(unit);
65                         Change result= doPerformReorg(new SubProgressMonitor(pm, 1));
66                         markAsExecuted(unit, mapping);
67                         return result;
68                 } finally {
69                         pm.done();
70                 }
71         }
72
73         abstract Change doPerformReorg(IProgressMonitor pm) throws CoreException, OperationCanceledException;
74
75         @Override
76         public Object getModifiedElement() {
77                 return getCu();
78         }
79
80         /* (non-Javadoc)
81          * @see org.eclipse.jdt.internal.corext.refactoring.base.JDTChange#getModifiedResource()
82          */
83         @Override
84         protected IResource getModifiedResource() {
85                 ICompilationUnit cu= getCu();
86                 if (cu != null) {
87                         return cu.getResource();
88                 }
89                 return null;
90         }
91
92         ICompilationUnit getCu() {
93                 return (ICompilationUnit)JavaCore.create(fCuHandle);
94         }
95
96         IPackageFragment getOldPackage() {
97                 return (IPackageFragment)JavaCore.create(fOldPackageHandle);
98         }
99
100         IPackageFragment getDestinationPackage() {
101                 return (IPackageFragment)JavaCore.create(fNewPackageHandle);
102         }
103
104         String getNewName() throws OperationCanceledException {
105                 if (fNewNameQuery == null)
106                         return null;
107                 return fNewNameQuery.getNewName();
108         }
109
110         static String getPackageName(IPackageFragment pack) {
111                 return JavaElementLabels.getElementLabel(pack, JavaElementLabels.ALL_DEFAULT);
112         }
113
114         private void markAsExecuted(ICompilationUnit unit, ResourceMapping mapping) {
115                 ReorgExecutionLog log= (ReorgExecutionLog)getAdapter(ReorgExecutionLog.class);
116                 if (log != null) {
117                         log.markAsProcessed(unit);
118                         log.markAsProcessed(mapping);
119                 }
120         }
121 }