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