KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2005, 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.IFile;
14 import org.eclipse.core.resources.IResource;
15
16 import org.eclipse.ltk.core.refactoring.MultiStateTextFileChange;
17
18 import org.eclipse.jdt.core.ICompilationUnit;
19
20 import org.eclipse.jdt.internal.corext.refactoring.RefactoringCoreMessages;
21 import org.eclipse.jdt.internal.corext.util.Messages;
22
23 /**
24  * Multi state compilation unit change for composite refactorings.
25  *
26  * @since 3.2
27  */

28 public final class MultiStateCompilationUnitChange extends MultiStateTextFileChange {
29
30     /** The compilation unit */
31     private final ICompilationUnit fUnit;
32
33     /**
34      * Creates a new multi state compilation unit change.
35      *
36      * @param name
37      * the name of the change
38      * @param unit
39      * the compilation unit
40      */

41     public MultiStateCompilationUnitChange(final String JavaDoc name, final ICompilationUnit unit) {
42         super(name, (IFile) unit.getResource());
43
44         fUnit= unit;
45
46         setTextType("java"); //$NON-NLS-1$
47
}
48
49     /*
50      * @see org.eclipse.ltk.core.refactoring.Change#getAdapter(java.lang.Class)
51      */

52     public final Object JavaDoc getAdapter(final Class JavaDoc adapter) {
53
54         if (ICompilationUnit.class.equals(adapter))
55             return fUnit;
56
57         return super.getAdapter(adapter);
58     }
59
60     /**
61      * Returns the compilation unit.
62      *
63      * @return the compilation unit
64      */

65     public final ICompilationUnit getCompilationUnit() {
66         return fUnit;
67     }
68
69     /**
70      * {@inheritDoc}
71      */

72     public String JavaDoc getName() {
73         return Messages.format(RefactoringCoreMessages.MultiStateCompilationUnitChange_name_pattern, new String JavaDoc[] { fUnit.getElementName(), getPath(fUnit.getResource()) });
74     }
75
76     /**
77      * Returns the path of the resource to display.
78      *
79      * @param resource
80      * the resource
81      * @return the path
82      */

83     private String JavaDoc getPath(IResource resource) {
84         final StringBuffer JavaDoc buffer= new StringBuffer JavaDoc(resource.getProject().getName());
85         final String JavaDoc path= resource.getParent().getProjectRelativePath().toString();
86         if (path.length() > 0) {
87             buffer.append('/');
88             buffer.append(path);
89         }
90         return buffer.toString();
91     }
92 }
93
Popular Tags