]> git.uio.no Git - ifi-stolz-refaktor.git/blob - case-study/jdt-before/ui refactoring/org/eclipse/jdt/internal/ui/refactoring/reorg/NewNameQueries.java
ef497c3d1509960b9bed0160157f72b3033ad143
[ifi-stolz-refaktor.git] / case-study / jdt-before / ui refactoring / org / eclipse / jdt / internal / ui / refactoring / reorg / NewNameQueries.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.ui.refactoring.reorg;
12
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Shell;
16
17 import org.eclipse.core.runtime.Assert;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IStatus;
20 import org.eclipse.core.runtime.OperationCanceledException;
21
22 import org.eclipse.core.resources.IResource;
23
24 import org.eclipse.jface.dialogs.IInputValidator;
25 import org.eclipse.jface.dialogs.InputDialog;
26 import org.eclipse.jface.window.Window;
27 import org.eclipse.jface.wizard.Wizard;
28
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
30
31 import org.eclipse.jdt.core.ICompilationUnit;
32 import org.eclipse.jdt.core.IJavaElement;
33 import org.eclipse.jdt.core.IPackageFragment;
34 import org.eclipse.jdt.core.IPackageFragmentRoot;
35 import org.eclipse.jdt.core.JavaCore;
36
37 import org.eclipse.jdt.internal.corext.refactoring.Checks;
38 import org.eclipse.jdt.internal.corext.refactoring.rename.RenamePackageProcessor;
39 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQueries;
40 import org.eclipse.jdt.internal.corext.refactoring.reorg.INewNameQuery;
41 import org.eclipse.jdt.internal.corext.util.JavaConventionsUtil;
42 import org.eclipse.jdt.internal.corext.util.JavaModelUtil;
43 import org.eclipse.jdt.internal.corext.util.Messages;
44
45 import org.eclipse.jdt.ui.JavaElementLabels;
46
47 import org.eclipse.jdt.internal.ui.JavaPlugin;
48 import org.eclipse.jdt.internal.ui.dialogs.TextFieldNavigationHandler;
49 import org.eclipse.jdt.internal.ui.viewsupport.BasicElementLabels;
50
51 public class NewNameQueries implements INewNameQueries {
52
53         private static final String INVALID_NAME_NO_MESSAGE= "";//$NON-NLS-1$
54         private final Wizard fWizard;
55         private final Shell fShell;
56
57         public NewNameQueries() {
58                 fShell= null;
59                 fWizard= null;
60         }
61
62         public NewNameQueries(Wizard wizard) {
63                 fWizard= wizard;
64                 fShell= null;
65         }
66
67         public NewNameQueries(Shell shell) {
68                 fShell = shell;
69                 fWizard= null;
70         }
71
72         private Shell getShell() {
73                 Assert.isTrue(fWizard == null || fShell == null);
74                 if (fWizard != null)
75                         return fWizard.getContainer().getShell();
76
77                 if (fShell != null)
78                         return fShell;
79                 return JavaPlugin.getActiveWorkbenchShell();
80         }
81
82         public INewNameQuery createNewCompilationUnitNameQuery(ICompilationUnit cu, String initialSuggestedName) {
83                 String[] keys= { BasicElementLabels.getJavaElementName(JavaCore.removeJavaLikeExtension(cu.getElementName()))};
84                 String message= Messages.format(ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
85                 return createStaticQuery(createCompilationUnitNameValidator(cu), message, initialSuggestedName, getShell());
86         }
87
88
89         public INewNameQuery createNewResourceNameQuery(IResource res, String initialSuggestedName) {
90                 String[] keys= { BasicElementLabels.getResourceName(res)};
91                 String message= Messages.format(ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
92                 return createStaticQuery(createResourceNameValidator(res), message, initialSuggestedName, getShell());
93         }
94
95
96         public INewNameQuery createNewPackageNameQuery(IPackageFragment pack, String initialSuggestedName) {
97                 String[] keys= {JavaElementLabels.getElementLabel(pack, JavaElementLabels.ALL_DEFAULT)};
98                 String message= Messages.format(ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
99                 return createStaticQuery(createPackageNameValidator(pack), message, initialSuggestedName, getShell());
100         }
101
102         public INewNameQuery createNewPackageFragmentRootNameQuery(IPackageFragmentRoot root, String initialSuggestedName) {
103                 String[] keys= {JavaElementLabels.getElementLabel(root, JavaElementLabels.ALL_DEFAULT)};
104                 String message= Messages.format(ReorgMessages.ReorgQueries_enterNewNameQuestion, keys);
105                 return createStaticQuery(createPackageFragmentRootNameValidator(root), message, initialSuggestedName, getShell());
106         }
107
108
109         public INewNameQuery createNullQuery(){
110                 return createStaticQuery(null);
111         }
112
113
114         public INewNameQuery createStaticQuery(final String newName){
115                 return new INewNameQuery(){
116                         public String getNewName() {
117                                 return newName;
118                         }
119                 };
120         }
121
122         private static INewNameQuery createStaticQuery(final IInputValidator validator, final String message, final String initial, final Shell shell){
123                 return new INewNameQuery(){
124                         public String getNewName() throws OperationCanceledException {
125                                 InputDialog dialog= new InputDialog(shell, ReorgMessages.ReorgQueries_nameConflictMessage, message, initial, validator) {
126                                         /* (non-Javadoc)
127                                          * @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
128                                          */
129                                         @Override
130                                         protected Control createDialogArea(Composite parent) {
131                                                 Control area= super.createDialogArea(parent);
132                                                 TextFieldNavigationHandler.install(getText());
133                                                 return area;
134                                         }
135                                 };
136                                 if (dialog.open() == Window.CANCEL)
137                                         throw new OperationCanceledException();
138                                 return dialog.getValue();
139                         }
140                 };
141         }
142
143         private static IInputValidator createResourceNameValidator(final IResource res){
144                 IInputValidator validator= new IInputValidator(){
145                         public String isValid(String newText) {
146                                 if (newText == null || "".equals(newText) || res.getParent() == null) //$NON-NLS-1$
147                                         return INVALID_NAME_NO_MESSAGE;
148                                 if (res.getParent().findMember(newText) != null)
149                                         return ReorgMessages.ReorgQueries_resourceWithThisNameAlreadyExists;
150                                 if (! res.getParent().getFullPath().isValidSegment(newText))
151                                         return ReorgMessages.ReorgQueries_invalidNameMessage;
152                                 IStatus status= res.getParent().getWorkspace().validateName(newText, res.getType());
153                                 if (status.getSeverity() == IStatus.ERROR)
154                                         return status.getMessage();
155
156                                 if (res.getName().equalsIgnoreCase(newText))
157                                         return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
158
159                                 return null;
160                         }
161                 };
162                 return validator;
163         }
164
165         private static IInputValidator createCompilationUnitNameValidator(final ICompilationUnit cu) {
166                 IInputValidator validator= new IInputValidator(){
167                         public String isValid(String newText) {
168                                 if (newText == null || "".equals(newText)) //$NON-NLS-1$
169                                         return INVALID_NAME_NO_MESSAGE;
170                                 String newCuName= JavaModelUtil.getRenamedCUName(cu, newText);
171                                 IStatus status= JavaConventionsUtil.validateCompilationUnitName(newCuName, cu);
172                                 if (status.getSeverity() == IStatus.ERROR)
173                                         return status.getMessage();
174                                 RefactoringStatus refStatus;
175                                 refStatus= Checks.checkCompilationUnitNewName(cu, newText);
176                                 if (refStatus.hasFatalError())
177                                         return refStatus.getMessageMatchingSeverity(RefactoringStatus.FATAL);
178
179                                 if (cu.getElementName().equalsIgnoreCase(newCuName))
180                                         return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
181
182                                 return null;
183                         }
184                 };
185                 return validator;
186         }
187
188
189         private static IInputValidator createPackageFragmentRootNameValidator(final IPackageFragmentRoot root) {
190                 return new IInputValidator() {
191                         IInputValidator resourceNameValidator= createResourceNameValidator(root.getResource());
192                         public String isValid(String newText) {
193                                 return resourceNameValidator.isValid(newText);
194                         }
195                 };
196         }
197
198         private static IInputValidator createPackageNameValidator(final IPackageFragment pack) {
199                 IInputValidator validator= new IInputValidator(){
200                         public String isValid(String newText) {
201                                 if (newText == null || "".equals(newText)) //$NON-NLS-1$
202                                         return INVALID_NAME_NO_MESSAGE;
203                                 IStatus status= JavaConventionsUtil.validatePackageName(newText, pack);
204                                 if (status.getSeverity() == IStatus.ERROR)
205                                         return status.getMessage();
206
207                                 IJavaElement parent= pack.getParent();
208                                 try {
209                                         if (parent instanceof IPackageFragmentRoot){
210                                                 if (! RenamePackageProcessor.isPackageNameOkInRoot(newText, (IPackageFragmentRoot)parent))
211                                                         return ReorgMessages.ReorgQueries_packagewithThatNameexistsMassage;
212                                         }
213                                 } catch (CoreException e) {
214                                         return INVALID_NAME_NO_MESSAGE;
215                                 }
216                                 if (pack.getElementName().equalsIgnoreCase(newText))
217                                         return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
218
219                                 return null;
220                         }
221                 };
222                 return validator;
223         }
224 }