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.ui.refactoring.reorg;
13 import org.eclipse.swt.widgets.Composite;
14 import org.eclipse.swt.widgets.Control;
15 import org.eclipse.swt.widgets.Shell;
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;
22 import org.eclipse.core.resources.IResource;
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;
29 import org.eclipse.ltk.core.refactoring.RefactoringStatus;
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;
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;
45 import org.eclipse.jdt.ui.JavaElementLabels;
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;
51 public class NewNameQueries implements INewNameQueries {
53 private static final String INVALID_NAME_NO_MESSAGE= "";//$NON-NLS-1$
54 private final Wizard fWizard;
55 private final Shell fShell;
57 public NewNameQueries() {
62 public NewNameQueries(Wizard wizard) {
67 public NewNameQueries(Shell shell) {
72 private Shell getShell() {
73 Assert.isTrue(fWizard == null || fShell == null);
75 return fWizard.getContainer().getShell();
79 return JavaPlugin.getActiveWorkbenchShell();
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());
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());
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());
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());
109 public INewNameQuery createNullQuery(){
110 return createStaticQuery(null);
114 public INewNameQuery createStaticQuery(final String newName){
115 return new INewNameQuery(){
116 public String getNewName() {
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) {
127 * @see org.eclipse.jface.dialogs.InputDialog#createDialogArea(org.eclipse.swt.widgets.Composite)
130 protected Control createDialogArea(Composite parent) {
131 Control area= super.createDialogArea(parent);
132 TextFieldNavigationHandler.install(getText());
136 if (dialog.open() == Window.CANCEL)
137 throw new OperationCanceledException();
138 return dialog.getValue();
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();
156 if (res.getName().equalsIgnoreCase(newText))
157 return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
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);
179 if (cu.getElementName().equalsIgnoreCase(newCuName))
180 return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;
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);
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();
207 IJavaElement parent= pack.getParent();
209 if (parent instanceof IPackageFragmentRoot){
210 if (! RenamePackageProcessor.isPackageNameOkInRoot(newText, (IPackageFragmentRoot)parent))
211 return ReorgMessages.ReorgQueries_packagewithThatNameexistsMassage;
213 } catch (CoreException e) {
214 return INVALID_NAME_NO_MESSAGE;
216 if (pack.getElementName().equalsIgnoreCase(newText))
217 return ReorgMessages.ReorgQueries_resourceExistsWithDifferentCaseMassage;