1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import java.util.HashMap ; 14 import java.util.HashSet ; 15 import java.util.Iterator ; 16 import java.util.Map ; 17 import java.util.Set ; 18 19 import org.eclipse.swt.SWT; 20 import org.eclipse.swt.events.SelectionAdapter; 21 import org.eclipse.swt.events.SelectionEvent; 22 import org.eclipse.swt.layout.GridData; 23 import org.eclipse.swt.layout.GridLayout; 24 import org.eclipse.swt.widgets.Button; 25 import org.eclipse.swt.widgets.Composite; 26 import org.eclipse.swt.widgets.Label; 27 import org.eclipse.swt.widgets.Tree; 28 import org.eclipse.swt.widgets.TreeItem; 29 30 import org.eclipse.jface.dialogs.Dialog; 31 import org.eclipse.jface.dialogs.IDialogSettings; 32 import org.eclipse.jface.dialogs.IMessageProvider; 33 import org.eclipse.jface.viewers.ISelectionChangedListener; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.ITreeContentProvider; 36 import org.eclipse.jface.viewers.SelectionChangedEvent; 37 import org.eclipse.jface.viewers.TreeViewer; 38 import org.eclipse.jface.viewers.Viewer; 39 import org.eclipse.jface.viewers.ViewerComparator; 40 import org.eclipse.jface.wizard.IWizardPage; 41 42 import org.eclipse.ui.PlatformUI; 43 44 import org.eclipse.ltk.ui.refactoring.RefactoringWizard; 45 import org.eclipse.ltk.ui.refactoring.UserInputWizardPage; 46 47 import org.eclipse.jdt.core.IType; 48 import org.eclipse.jdt.core.ITypeHierarchy; 49 import org.eclipse.jdt.core.JavaModelException; 50 51 import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeProcessor; 52 import org.eclipse.jdt.internal.corext.refactoring.structure.UseSuperTypeRefactoring; 53 import org.eclipse.jdt.internal.corext.util.Messages; 54 import org.eclipse.jdt.internal.corext.util.SuperTypeHierarchyCache; 55 56 import org.eclipse.jdt.ui.JavaElementLabelProvider; 57 import org.eclipse.jdt.ui.JavaElementLabels; 58 59 import org.eclipse.jdt.internal.ui.IJavaHelpContextIds; 60 import org.eclipse.jdt.internal.ui.JavaPlugin; 61 62 public class UseSupertypeWizard extends RefactoringWizard{ 63 64 static final String DIALOG_SETTING_SECTION= "UseSupertypeWizard"; 66 public UseSupertypeWizard(UseSuperTypeRefactoring ref) { 67 super(ref, DIALOG_BASED_USER_INTERFACE); 68 setDefaultPageTitle(RefactoringMessages.UseSupertypeWizard_Use_Super_Type_Where_Possible); 69 } 70 71 protected void addUserInputPages(){ 72 addPage(new UseSupertypeInputPage()); 73 } 74 75 private static class UseSupertypeInputPage extends UserInputWizardPage{ 76 77 private class UseSupertypeContentProvider implements ITreeContentProvider { 78 79 private ITypeHierarchy fHierarchy; 80 81 public Object [] getChildren(Object element) { 82 if (element instanceof ITypeHierarchy) 83 return getElements(element); 84 return getDirectSuperTypes((IType)element).toArray(); 85 } 86 87 public Set getDirectSuperTypes(IType type){ 88 Set result= new HashSet (); 89 final IType superclass= fHierarchy.getSuperclass(type); 90 if (superclass != null) { 91 result.add(superclass); 92 } 93 IType[] superInterface= fHierarchy.getSuperInterfaces(type); 94 for (int i=0; i < superInterface.length; i++){ 95 result.add(superInterface[i]); 96 } 97 try { 98 if (type.isInterface()) { 99 IType found= type.getJavaProject().findType("java.lang.Object"); result.add(found); 101 } 102 } catch (JavaModelException exception) { 103 JavaPlugin.log(exception); 104 } 105 return result; 106 } 107 108 public Object [] getElements(Object element) { 109 if (element instanceof ITypeHierarchy) 110 return getChildren(((ITypeHierarchy) element).getType()); 111 return new Object [0]; 112 } 113 114 public boolean hasChildren(Object element) { 115 return getChildren(element).length > 0; 116 } 117 118 public Object getParent(Object element) { 119 return null; 120 } 121 122 public void dispose() { 123 } 125 126 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 127 if (newInput instanceof ITypeHierarchy) 128 fHierarchy= (ITypeHierarchy) newInput; 129 else 130 fHierarchy= null; 131 } 132 } 133 134 private static final String REWRITE_INSTANCEOF= "rewriteInstanceOf"; public static final String PAGE_NAME= "UseSupertypeInputPage"; private TreeViewer fTreeViewer; 137 private final Map fFileCount; private final static String MESSAGE= RefactoringMessages.UseSupertypeInputPage_Select_supertype; 139 private JavaElementLabelProvider fLabelProvider; 140 private IDialogSettings fSettings; 141 142 public UseSupertypeInputPage() { 143 super(PAGE_NAME); 144 fFileCount= new HashMap (2); 145 setMessage(MESSAGE); 146 } 147 148 private void loadSettings() { 149 fSettings= getDialogSettings().getSection(UseSupertypeWizard.DIALOG_SETTING_SECTION); 150 if (fSettings == null) { 151 fSettings= getDialogSettings().addNewSection(UseSupertypeWizard.DIALOG_SETTING_SECTION); 152 fSettings.put(REWRITE_INSTANCEOF, false); 153 } 154 getUseSupertypeProcessor().setInstanceOf(fSettings.getBoolean(REWRITE_INSTANCEOF)); 155 } 156 157 private UseSuperTypeProcessor getUseSupertypeProcessor() { 158 return getUseSupertypeRefactoring().getUseSuperTypeProcessor(); 159 } 160 161 private UseSuperTypeRefactoring getUseSupertypeRefactoring() { 162 return ((UseSuperTypeRefactoring)getRefactoring()); 163 } 164 165 public void createControl(Composite parent) { 166 initializeDialogUnits(parent); 167 loadSettings(); 168 Composite composite= new Composite(parent, SWT.NONE); 169 setControl(composite); 170 composite.setLayout(new GridLayout()); 171 172 Label label= new Label(composite, SWT.NONE); 173 label.setText(Messages.format( 174 RefactoringMessages.UseSupertypeInputPage_Select_supertype_to_use, 175 JavaElementLabels.getElementLabel(getUseSupertypeProcessor().getSubType(), JavaElementLabels.T_FULLY_QUALIFIED))); 176 label.setLayoutData(new GridData()); 177 178 addTreeViewer(composite); 179 180 final Button checkbox= new Button(composite, SWT.CHECK); 181 checkbox.setText(RefactoringMessages.UseSupertypeInputPage_Use_in_instanceof); 182 checkbox.setLayoutData(new GridData()); 183 checkbox.setSelection(getUseSupertypeProcessor().isInstanceOf()); 184 checkbox.addSelectionListener(new SelectionAdapter(){ 185 public void widgetSelected(SelectionEvent e) { 186 getUseSupertypeProcessor().setInstanceOf(checkbox.getSelection()); 187 fSettings.put(REWRITE_INSTANCEOF, checkbox.getSelection()); 188 setMessage(MESSAGE); 189 setPageComplete(true); 190 fFileCount.clear(); 191 fTreeViewer.refresh(); 192 } 193 }); 194 195 Dialog.applyDialogFont(composite); 196 PlatformUI.getWorkbench().getHelpSystem().setHelp(getControl(), IJavaHelpContextIds.USE_SUPERTYPE_WIZARD_PAGE); 197 } 198 199 private void addTreeViewer(Composite composite) { 200 fTreeViewer= new TreeViewer(composite, SWT.SINGLE | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL); 201 final Tree tree= fTreeViewer.getTree(); 202 final GridData data= new GridData(GridData.FILL_BOTH); 203 data.heightHint= convertHeightInCharsToPixels(12); 204 tree.setLayoutData(data); 205 fLabelProvider= new UseSupertypeLabelProvider(fFileCount); 206 fTreeViewer.setLabelProvider(fLabelProvider); 207 fTreeViewer.setContentProvider(new UseSupertypeContentProvider()); 208 fTreeViewer.setComparator(new ViewerComparator() { 209 210 public boolean isSorterProperty(Object element, String property) { 211 return true; 212 } 213 214 public int compare(Viewer viewer, Object first, Object second) { 215 final IType type1= (IType)first; 216 final IType type2= (IType)second; 217 try { 218 final int kind1= type1.isInterface() ? 1 : 0; 219 final int kind2= type2.isInterface() ? 1 : 0; 220 if (kind1 - kind2 != 0) 221 return kind1 - kind2; 222 } catch (JavaModelException exception) { 223 JavaPlugin.log(exception); 224 } 225 return getComparator().compare(type1.getElementName(), type2.getElementName()); 226 } 227 }); 228 fTreeViewer.addSelectionChangedListener(new ISelectionChangedListener(){ 229 public void selectionChanged(SelectionChangedEvent event) { 230 IStructuredSelection ss= (IStructuredSelection)event.getSelection(); 231 if (new Integer (0).equals(fFileCount.get(ss.getFirstElement()))){ 232 setMessage(RefactoringMessages.UseSupertypeInputPage_No_updates, IMessageProvider.INFORMATION); 233 setPageComplete(false); 234 } else { 235 setMessage(MESSAGE); 236 setPageComplete(true); 237 } 238 fTreeViewer.refresh(); 239 } 240 }); 241 try { 242 fTreeViewer.setInput(SuperTypeHierarchyCache.getTypeHierarchy(getUseSupertypeProcessor().getSubType())); 243 } catch (JavaModelException exception) { 244 JavaPlugin.log(exception); 245 } 246 fTreeViewer.expandAll(); 247 final TreeItem[] items= tree.getItems(); 248 if (items.length > 0) 249 tree.setSelection(new TreeItem[] {items[0]}); 250 } 251 252 public IWizardPage getNextPage() { 253 initializeRefactoring(); 254 IWizardPage nextPage= super.getNextPage(); 255 updateUpdateLabels(); 256 return nextPage; 257 } 258 259 private void updateUpdateLabels() { 260 IType selectedType= getSelectedSupertype(); 261 final int count= getUseSupertypeProcessor().getChanges(); 262 fFileCount.put(selectedType, new Integer (count)); 263 if (count == 0) { 264 setMessage(RefactoringMessages.UseSupertypeInputPage_No_updates, IMessageProvider.INFORMATION); 265 setPageComplete(false); 266 } 267 fTreeViewer.refresh(); 268 if (noSupertypeCanBeUsed()){ 269 setMessage(RefactoringMessages.UseSupertypeWizard_10, IMessageProvider.INFORMATION); 270 setPageComplete(false); 271 } 272 } 273 274 private boolean noSupertypeCanBeUsed() { 275 return fTreeViewer.getTree().getItemCount() == countFilesWithValue(0); 276 } 277 278 private int countFilesWithValue(int i) { 279 int count= 0; 280 for (Iterator iter= fFileCount.keySet().iterator(); iter.hasNext();) { 281 if (((Integer )fFileCount.get(iter.next())).intValue() == i) 282 count++; 283 } 284 return count; 285 } 286 287 private IType getSelectedSupertype() { 288 IStructuredSelection ss= (IStructuredSelection)fTreeViewer.getSelection(); 289 return (IType)ss.getFirstElement(); 290 } 291 292 public boolean performFinish(){ 293 initializeRefactoring(); 294 boolean superFinish= super.performFinish(); 295 if (! superFinish) 296 return false; 297 final int count= getUseSupertypeProcessor().getChanges(); 298 if (count == 0) { 299 updateUpdateLabels(); 300 return false; 301 } 302 return superFinish; 303 } 304 305 private void initializeRefactoring() { 306 IStructuredSelection ss= (IStructuredSelection)fTreeViewer.getSelection(); 307 getUseSupertypeProcessor().setSuperType((IType)ss.getFirstElement()); 308 } 309 310 public void dispose() { 311 fTreeViewer= null; 312 fFileCount.clear(); 313 fLabelProvider= null; 314 super.dispose(); 315 } 316 317 private static class UseSupertypeLabelProvider extends JavaElementLabelProvider{ 318 private final Map fFileCount; 319 private UseSupertypeLabelProvider(Map fileCount){ 320 fFileCount= fileCount; 321 } 322 public String getText(Object element) { 323 String superText= super.getText(element); 324 if (! fFileCount.containsKey(element)) 325 return superText; 326 int count= ((Integer )fFileCount.get(element)).intValue(); 327 if (count == 0){ 328 String [] keys= {superText}; 329 return Messages.format(RefactoringMessages.UseSupertypeInputPage_no_possible_updates, keys); 330 } else if (count == 1){ 331 String [] keys= {superText}; 332 return Messages.format(RefactoringMessages.UseSupertypeInputPage_updates_possible_in_file, keys); 333 } else { 334 String [] keys= {superText, String.valueOf(count)}; 335 return Messages.format(RefactoringMessages.UseSupertypeInputPage_updates_possible_in_files, keys); 336 } 337 } 338 } 339 340 public void setVisible(boolean visible) { 341 super.setVisible(visible); 342 if (visible && fTreeViewer != null) 343 fTreeViewer.getTree().setFocus(); 344 } 345 } 346 } 347 | Popular Tags |