KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > corext > refactoring > reorg > LoggedNewNameQueries


1 /*******************************************************************************
2  * Copyright (c) 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.reorg;
12
13 import org.eclipse.core.resources.IResource;
14
15 import org.eclipse.ltk.core.refactoring.participants.ReorgExecutionLog;
16
17 import org.eclipse.jdt.core.ICompilationUnit;
18 import org.eclipse.jdt.core.IPackageFragment;
19 import org.eclipse.jdt.core.IPackageFragmentRoot;
20
21 /**
22  * Logged implementation of new name queries.
23  *
24  * @since 3.3
25  */

26 public final class LoggedNewNameQueries implements INewNameQueries {
27
28     /** Default implementation of a new name query */
29     private final class NewNameQuery implements INewNameQuery {
30
31         /** The name */
32         private final String JavaDoc fName;
33
34         /** The object */
35         private final Object JavaDoc fObject;
36
37         /**
38          * Creates a new new name query.
39          *
40          * @param object
41          * the object
42          * @param name
43          * the initial suggested name
44          */

45         public NewNameQuery(final Object JavaDoc object, String JavaDoc name) {
46             fObject= object;
47             fName= name;
48         }
49
50         /**
51          * Returns the new name of the compilation unit, without any extension.
52          *
53          * @return the new name, or <code>null</code>
54          */

55         private String JavaDoc getCompilationUnitName() {
56             String JavaDoc name= fLog.getNewName(fObject);
57             if (name != null) {
58                 int index= name.lastIndexOf('.');
59                 if (index > 0)
60                     name= name.substring(0, index);
61             }
62             return name;
63         }
64
65         /**
66          * {@inheritDoc}
67          */

68         public String JavaDoc getNewName() {
69             String JavaDoc name= null;
70             if (fObject instanceof ICompilationUnit)
71                 name= getCompilationUnitName();
72             else
73                 name= fLog.getNewName(fObject);
74             if (name == null)
75                 name= fName;
76             return fName;
77         }
78     }
79
80     /** Null implementation of new name query */
81     private static final class NullNewNameQuery implements INewNameQuery {
82
83         /**
84          * {@inheritDoc}
85          */

86         public String JavaDoc getNewName() {
87             return "null"; //$NON-NLS-1$
88
}
89     }
90
91     /** The reorg execution log */
92     private final ReorgExecutionLog fLog;
93
94     /**
95      * Creates a new logged new name queries.
96      *
97      * @param log
98      * the reorg execution log
99      */

100     public LoggedNewNameQueries(final ReorgExecutionLog log) {
101         fLog= log;
102     }
103
104     /**
105      * {@inheritDoc}
106      */

107     public INewNameQuery createNewCompilationUnitNameQuery(final ICompilationUnit unit, final String JavaDoc initialSuggestedName) {
108         return new NewNameQuery(unit, initialSuggestedName);
109     }
110
111     /**
112      * {@inheritDoc}
113      */

114     public INewNameQuery createNewPackageFragmentRootNameQuery(final IPackageFragmentRoot root, final String JavaDoc initialSuggestedName) {
115         return new NewNameQuery(root, initialSuggestedName);
116     }
117
118     /**
119      * {@inheritDoc}
120      */

121     public INewNameQuery createNewPackageNameQuery(final IPackageFragment fragment, final String JavaDoc initialSuggestedName) {
122         return new NewNameQuery(fragment, initialSuggestedName);
123     }
124
125     /**
126      * {@inheritDoc}
127      */

128     public INewNameQuery createNewResourceNameQuery(final IResource resource, final String JavaDoc initialSuggestedName) {
129         return new NewNameQuery(resource, initialSuggestedName);
130     }
131
132     /**
133      * {@inheritDoc}
134      */

135     public INewNameQuery createNullQuery() {
136         return new NullNewNameQuery();
137     }
138
139     /**
140      * {@inheritDoc}
141      */

142     public INewNameQuery createStaticQuery(final String JavaDoc name) {
143         return new INewNameQuery() {
144
145             public String JavaDoc getNewName() {
146                 return name;
147             }
148         };
149     }
150 }
Popular Tags