1 11 package org.eclipse.jdt.internal.ui.refactoring.reorg; 12 13 import org.eclipse.core.resources.IResource; 14 15 import org.eclipse.swt.SWT; 16 import org.eclipse.swt.events.SelectionAdapter; 17 import org.eclipse.swt.events.SelectionEvent; 18 import org.eclipse.swt.layout.GridData; 19 import org.eclipse.swt.layout.GridLayout; 20 import org.eclipse.swt.widgets.Button; 21 import org.eclipse.swt.widgets.Composite; 22 23 import org.eclipse.jface.dialogs.Dialog; 24 import org.eclipse.jface.dialogs.IDialogConstants; 25 26 import org.eclipse.ltk.core.refactoring.Refactoring; 27 import org.eclipse.ltk.core.refactoring.participants.DeleteRefactoring; 28 import org.eclipse.ltk.core.refactoring.participants.RefactoringProcessor; 29 import org.eclipse.ltk.ui.refactoring.RefactoringWizard; 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.JavaModelException; 36 37 import org.eclipse.jdt.internal.corext.refactoring.reorg.JavaDeleteProcessor; 38 import org.eclipse.jdt.internal.corext.refactoring.reorg.ReorgUtils; 39 import org.eclipse.jdt.internal.corext.refactoring.util.JavaElementUtil; 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.refactoring.MessageWizardPage; 45 import org.eclipse.jdt.internal.ui.refactoring.RefactoringMessages; 46 47 public class DeleteWizard extends RefactoringWizard { 48 49 public DeleteWizard(Refactoring refactoring) { 50 super(refactoring, DIALOG_BASED_USER_INTERFACE | YES_NO_BUTTON_STYLE | NO_PREVIEW_PAGE | NO_BACK_BUTTON_ON_STATUS_DIALOG); 51 setDefaultPageTitle(RefactoringMessages.DeleteWizard_1); 52 ((JavaDeleteProcessor)((DeleteRefactoring)getRefactoring()).getProcessor()).setQueries(new ReorgQueries(this)); 53 } 54 55 58 protected void addUserInputPages() { 59 addPage(new DeleteInputPage()); 60 } 61 62 65 public int getMessageLineWidthInChars() { 66 return 0; 67 } 68 69 72 public boolean needsProgressMonitor() { 73 DeleteRefactoring refactoring= (DeleteRefactoring)getRefactoring(); 74 RefactoringProcessor processor= refactoring.getProcessor(); 75 if (processor instanceof JavaDeleteProcessor) { 76 return ((JavaDeleteProcessor)processor).needsProgressMonitor(); 77 } 78 return super.needsProgressMonitor(); 79 } 80 81 private static class DeleteInputPage extends MessageWizardPage { 82 private static final String PAGE_NAME= "DeleteInputPage"; private static final String DIALOG_SETTINGS_DELETE_SUB_PACKAGES= "deleteSubPackages"; private Button fDeleteSubPackagesCheckBox; 85 86 public DeleteInputPage() { 87 super(PAGE_NAME, true, MessageWizardPage.STYLE_QUESTION); 88 } 89 90 protected String getMessageString() { 91 try { 92 if (1 == numberOfSelectedElements()) { 93 String pattern= createConfirmationStringForOneElement(); 94 String name= getNameOfSingleSelectedElement(); 95 return Messages.format(pattern, new String [] { name }); 96 } else { 97 String pattern= createConfirmationStringForManyElements(); 98 return Messages.format(pattern, new String [] { String.valueOf(numberOfSelectedElements())}); 99 } 100 } catch (JavaModelException e) { 101 if (JavaModelUtil.isExceptionToBeLogged(e)) 103 JavaPlugin.log(e); 104 setPageComplete(false); 105 if (e.isDoesNotExist()) 106 return RefactoringMessages.DeleteWizard_12; 107 return RefactoringMessages.DeleteWizard_2; 108 } 109 } 110 111 public void createControl(Composite parent) { 112 super.createControl(parent); 113 114 if (getDeleteProcessor().hasSubPackagesToDelete()) 115 addDeleteSubPackagesCheckBox(); 116 } 117 118 125 private void addDeleteSubPackagesCheckBox() { 126 127 Composite c= new Composite((Composite) getControl(), SWT.NONE); 128 GridLayout gd= new GridLayout(); 129 gd.horizontalSpacing= 10; 130 c.setLayout(gd); 131 132 GridData data= new GridData(GridData.FILL_HORIZONTAL); 133 data.horizontalSpan= 2; 134 c.setLayoutData(data); 135 136 final boolean selection= getRefactoringSettings().getBoolean(DIALOG_SETTINGS_DELETE_SUB_PACKAGES); 137 138 fDeleteSubPackagesCheckBox= new Button(c, SWT.CHECK); 139 fDeleteSubPackagesCheckBox.setText(RefactoringMessages.DeleteWizard_also_delete_sub_packages); 140 fDeleteSubPackagesCheckBox.setSelection(selection); 141 142 fDeleteSubPackagesCheckBox.addSelectionListener(new SelectionAdapter() { 143 144 public void widgetSelected(SelectionEvent event) { 145 getDeleteProcessor().setDeleteSubPackages(fDeleteSubPackagesCheckBox.getSelection()); 146 } 147 }); 148 149 getDeleteProcessor().setDeleteSubPackages(fDeleteSubPackagesCheckBox.getSelection()); 150 } 151 152 private String getNameOfSingleSelectedElement() throws JavaModelException { 153 if (getSingleSelectedResource() != null) 154 return ReorgUtils.getName(getSingleSelectedResource()); 155 else 156 return ReorgUtils.getName(getSingleSelectedJavaElement()); 157 } 158 159 private IJavaElement getSingleSelectedJavaElement() { 160 IJavaElement[] elements= getSelectedJavaElements(); 161 return elements.length == 1 ? elements[0] : null; 162 } 163 164 private IResource getSingleSelectedResource() { 165 IResource[] resources= getSelectedResources(); 166 return resources.length == 1 ? resources[0] : null; 167 } 168 169 private int numberOfSelectedElements() { 170 return getSelectedJavaElements().length + getSelectedResources().length; 171 } 172 173 176 protected boolean performFinish() { 177 return super.performFinish() || getDeleteProcessor().wasCanceled(); } 179 180 protected boolean saveSettings() { 181 if (getContainer() instanceof Dialog) 182 return ((Dialog) getContainer()).getReturnCode() == IDialogConstants.OK_ID; 183 return true; 184 } 185 186 public void dispose() { 187 if (fDeleteSubPackagesCheckBox != null && saveSettings()) 188 getRefactoringSettings().put(DIALOG_SETTINGS_DELETE_SUB_PACKAGES, fDeleteSubPackagesCheckBox.getSelection()); 189 super.dispose(); 190 } 191 192 private String createConfirmationStringForOneElement() throws JavaModelException { 193 IJavaElement[] elements= getSelectedJavaElements(); 194 if (elements.length == 1) { 195 IJavaElement element= elements[0]; 196 if (isDefaultPackageWithLinkedFiles(element)) 197 return RefactoringMessages.DeleteWizard_3; 198 199 if (!isLinkedResource(element)) 200 return RefactoringMessages.DeleteWizard_4; 201 202 if (isLinkedPackageOrPackageFragmentRoot(element)) 203 return RefactoringMessages.DeleteWizard_6; 205 206 return RefactoringMessages.DeleteWizard_5; 207 } else { 208 if (isLinked(getSelectedResources()[0])) return RefactoringMessages.DeleteWizard_7; 210 else 211 return RefactoringMessages.DeleteWizard_8; 212 } 213 } 214 215 private String createConfirmationStringForManyElements() throws JavaModelException { 216 IResource[] resources= getSelectedResources(); 217 IJavaElement[] javaElements= getSelectedJavaElements(); 218 if (!containsLinkedResources(resources, javaElements)) 219 return RefactoringMessages.DeleteWizard_9; 220 221 if (!containsLinkedPackagesOrPackageFragmentRoots(javaElements)) 222 return RefactoringMessages.DeleteWizard_10; 223 224 return RefactoringMessages.DeleteWizard_11; 226 } 227 228 private static boolean isLinkedPackageOrPackageFragmentRoot(IJavaElement element) { 229 if ((element instanceof IPackageFragment) || (element instanceof IPackageFragmentRoot)) 230 return isLinkedResource(element); 231 else 232 return false; 233 } 234 235 private static boolean containsLinkedPackagesOrPackageFragmentRoots(IJavaElement[] javaElements) { 236 for (int i= 0; i < javaElements.length; i++) { 237 IJavaElement element= javaElements[i]; 238 if (isLinkedPackageOrPackageFragmentRoot(element)) 239 return true; 240 } 241 return false; 242 } 243 244 private static boolean containsLinkedResources(IResource[] resources, IJavaElement[] javaElements) throws JavaModelException { 245 for (int i= 0; i < javaElements.length; i++) { 246 IJavaElement element= javaElements[i]; 247 if (isLinkedResource(element)) 248 return true; 249 if (isDefaultPackageWithLinkedFiles(element)) 250 return true; 251 } 252 for (int i= 0; i < resources.length; i++) { 253 IResource resource= resources[i]; 254 if (isLinked(resource)) 255 return true; 256 } 257 return false; 258 } 259 260 private static boolean isDefaultPackageWithLinkedFiles(Object firstElement) throws JavaModelException { 261 if (!JavaElementUtil.isDefaultPackage(firstElement)) 262 return false; 263 IPackageFragment defaultPackage= (IPackageFragment)firstElement; 264 ICompilationUnit[] cus= defaultPackage.getCompilationUnits(); 265 for (int i= 0; i < cus.length; i++) { 266 if (isLinkedResource(cus[i])) 267 return true; 268 } 269 return false; 270 } 271 272 private static boolean isLinkedResource(IJavaElement element) { 273 return isLinked(ReorgUtils.getResource(element)); 274 } 275 276 private static boolean isLinked(IResource resource) { 277 return resource != null && resource.isLinked(); 278 } 279 280 private IJavaElement[] getSelectedJavaElements() { 281 return getDeleteProcessor().getJavaElementsToDelete(); 282 } 283 284 private IResource[] getSelectedResources() { 285 return getDeleteProcessor().getResourcesToDelete(); 286 } 287 288 private JavaDeleteProcessor getDeleteProcessor() { 289 return (JavaDeleteProcessor) ((DeleteRefactoring) getRefactoring()).getProcessor(); 290 } 291 292 } 293 } 294 | Popular Tags |