1 11 package org.eclipse.jdt.internal.ui.refactoring; 12 13 import java.util.ArrayList ; 14 import java.util.Iterator ; 15 import java.util.List ; 16 17 import org.eclipse.core.runtime.Assert; 18 import org.eclipse.core.runtime.IStatus; 19 import org.eclipse.core.runtime.NullProgressMonitor; 20 21 import org.eclipse.swt.SWT; 22 import org.eclipse.swt.events.SelectionAdapter; 23 import org.eclipse.swt.events.SelectionEvent; 24 import org.eclipse.swt.graphics.Image; 25 import org.eclipse.swt.layout.GridData; 26 import org.eclipse.swt.layout.GridLayout; 27 import org.eclipse.swt.widgets.Button; 28 import org.eclipse.swt.widgets.Composite; 29 import org.eclipse.swt.widgets.Table; 30 31 import org.eclipse.jface.viewers.ISelection; 32 import org.eclipse.jface.viewers.ISelectionChangedListener; 33 import org.eclipse.jface.viewers.IStructuredContentProvider; 34 import org.eclipse.jface.viewers.IStructuredSelection; 35 import org.eclipse.jface.viewers.ITableLabelProvider; 36 import org.eclipse.jface.viewers.LabelProvider; 37 import org.eclipse.jface.viewers.SelectionChangedEvent; 38 import org.eclipse.jface.viewers.StructuredSelection; 39 import org.eclipse.jface.viewers.TableViewer; 40 import org.eclipse.jface.viewers.Viewer; 41 import org.eclipse.jface.window.Window; 42 43 import org.eclipse.ui.PlatformUI; 44 import org.eclipse.ui.dialogs.ISelectionStatusValidator; 45 46 import org.eclipse.jdt.core.IJavaElement; 47 import org.eclipse.jdt.core.IJavaProject; 48 import org.eclipse.jdt.core.IType; 49 import org.eclipse.jdt.core.ITypeHierarchy; 50 import org.eclipse.jdt.core.JavaModelException; 51 import org.eclipse.jdt.core.search.IJavaSearchConstants; 52 import org.eclipse.jdt.core.search.IJavaSearchScope; 53 import org.eclipse.jdt.core.search.SearchEngine; 54 55 import org.eclipse.jdt.internal.corext.refactoring.ExceptionInfo; 56 57 import org.eclipse.jdt.internal.ui.JavaPlugin; 58 import org.eclipse.jdt.internal.ui.JavaPluginImages; 59 import org.eclipse.jdt.internal.ui.JavaUIStatus; 60 import org.eclipse.jdt.internal.ui.dialogs.FilteredTypesSelectionDialog; 61 import org.eclipse.jdt.internal.ui.dialogs.StatusInfo; 62 import org.eclipse.jdt.internal.ui.util.SWTUtil; 63 64 67 public class ChangeExceptionsControl extends Composite { 68 70 private static class ExceptionInfoContentProvider implements IStructuredContentProvider { 71 public Object [] getElements(Object inputElement) { 72 return removeMarkedAsDeleted((List ) inputElement); 73 } 74 private ExceptionInfo[] removeMarkedAsDeleted(List exceptionInfos){ 75 List result= new ArrayList (exceptionInfos.size()); 76 for (Iterator iter= exceptionInfos.iterator(); iter.hasNext();) { 77 ExceptionInfo info= (ExceptionInfo) iter.next(); 78 if (! info.isDeleted()) 79 result.add(info); 80 } 81 return (ExceptionInfo[]) result.toArray(new ExceptionInfo[result.size()]); 82 } 83 public void dispose() { 84 } 86 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 87 } 89 } 90 91 private static class ExceptionInfoLabelProvider extends LabelProvider implements ITableLabelProvider { 92 private Image fInterfaceImage; 93 94 public ExceptionInfoLabelProvider() { 95 super(); 96 fInterfaceImage= JavaPluginImages.get(JavaPluginImages.IMG_OBJS_CLASS); 97 } 98 99 public Image getColumnImage(Object element, int columnIndex) { 100 return fInterfaceImage; 101 } 102 public String getColumnText(Object element, int columnIndex) { 103 ExceptionInfo info= (ExceptionInfo) element; 104 return info.getType().getFullyQualifiedName(); 105 } 106 } 107 108 private final IExceptionListChangeListener fListener; 109 private final IJavaProject fProject; 110 111 private TableViewer fTableViewer; 112 private Button fRemoveButton; 113 private List fExceptionInfos; 114 115 public ChangeExceptionsControl(Composite parent, int style, IExceptionListChangeListener listener, IJavaProject project) { 116 super(parent, style); 117 Assert.isNotNull(listener); 118 fListener= listener; 119 Assert.isNotNull(project); 120 fProject= project; 121 GridLayout layout= new GridLayout(); 122 layout.numColumns= 2; 123 layout.marginWidth= 0; 124 layout.marginHeight= 0; 125 setLayout(layout); 126 127 createExceptionList(this); 128 createButtonComposite(this); 129 } 130 131 public void setInput(List exceptionInfos) { 132 Assert.isNotNull(exceptionInfos); 133 fExceptionInfos= exceptionInfos; 134 fTableViewer.setInput(fExceptionInfos); 135 if (fExceptionInfos.size() > 0) 136 fTableViewer.setSelection(new StructuredSelection(fExceptionInfos.get(0))); 137 } 138 139 private void createExceptionList(Composite parent) { 140 final Table table= new Table(parent, SWT.MULTI | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL); 141 table.setLayoutData(new GridData(GridData.FILL_BOTH)); 142 143 fTableViewer= new TableViewer(table); 144 fTableViewer.setUseHashlookup(true); 145 fTableViewer.setContentProvider(new ExceptionInfoContentProvider()); 146 fTableViewer.setLabelProvider(new ExceptionInfoLabelProvider()); 147 fTableViewer.addSelectionChangedListener(new ISelectionChangedListener() { 148 public void selectionChanged(SelectionChangedEvent event) { 149 updateButtonsEnabledState(); 150 } 151 }); 152 } 153 154 private ExceptionInfo[] getSelectedItems() { 155 ISelection selection= fTableViewer.getSelection(); 156 if (selection == null) 157 return new ExceptionInfo[0]; 158 159 if (!(selection instanceof IStructuredSelection)) 160 return new ExceptionInfo[0]; 161 162 List selected= ((IStructuredSelection) selection).toList(); 163 return (ExceptionInfo[]) selected.toArray(new ExceptionInfo[selected.size()]); 164 } 165 166 168 private void createButtonComposite(Composite parent) { 169 Composite buttonComposite= new Composite(parent, SWT.NONE); 170 buttonComposite.setLayoutData(new GridData(GridData.FILL_VERTICAL)); 171 GridLayout gl= new GridLayout(); 172 gl.marginHeight= 0; 173 gl.marginWidth= 0; 174 buttonComposite.setLayout(gl); 175 176 createAddButton(buttonComposite); 177 fRemoveButton= createRemoveButton(buttonComposite); 178 updateButtonsEnabledState(); 179 } 180 181 private void updateButtonsEnabledState() { 182 if (fRemoveButton != null) 183 fRemoveButton.setEnabled(getTableSelectionCount() != 0); 184 } 185 186 private int getTableSelectionCount() { 187 return getTable().getSelectionCount(); 188 } 189 190 private int getTableItemCount() { 191 return getTable().getItemCount(); 192 } 193 194 private Table getTable() { 195 return fTableViewer.getTable(); 196 } 197 198 private Button createAddButton(Composite buttonComposite) { 199 Button button= new Button(buttonComposite, SWT.PUSH); 200 button.setText(RefactoringMessages.ChangeExceptionsControl_buttons_add); 201 button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 202 SWTUtil.setButtonDimensionHint(button); 203 button.setEnabled(true); 204 button.addSelectionListener(new SelectionAdapter() { 205 public void widgetSelected(SelectionEvent e) { 206 doAddException(); 207 } 208 }); 209 return button; 210 } 211 212 private void doAddException() { 213 IType newException= chooseException(); 214 if (newException == null) 215 return; 216 217 ExceptionInfo info= findExceptionInfo(newException); 218 if (info != null) { 219 if (info.isDeleted()) { 220 info.markAsOld(); 221 fTableViewer.refresh(); 222 } 223 fListener.exceptionListChanged(); 224 fTableViewer.getControl().setFocus(); 225 fTableViewer.setSelection(new StructuredSelection(info), true); 226 return; 227 } 228 229 info= ExceptionInfo.createInfoForAddedException(newException); 230 fExceptionInfos.add(info); 231 fListener.exceptionListChanged(); 232 fTableViewer.refresh(); 233 fTableViewer.getControl().setFocus(); 234 int row= getTableItemCount() - 1; 235 getTable().setSelection(row); 236 updateButtonsEnabledState(); 237 238 } 239 240 private IType chooseException() { 241 IJavaElement[] elements= new IJavaElement[] { fProject.getJavaProject() }; 242 final IJavaSearchScope scope= SearchEngine.createJavaSearchScope(elements); 243 244 FilteredTypesSelectionDialog dialog= new FilteredTypesSelectionDialog(getShell(), false, 245 PlatformUI.getWorkbench().getProgressService(), scope, IJavaSearchConstants.CLASS); 246 dialog.setTitle(RefactoringMessages.ChangeExceptionsControl_choose_title); 247 dialog.setMessage(RefactoringMessages.ChangeExceptionsControl_choose_message); 248 dialog.setInitialPattern("*Exception*"); dialog.setValidator(new ISelectionStatusValidator() { 250 public IStatus validate(Object [] selection) { 251 if (selection.length == 0) 252 return new StatusInfo(IStatus.ERROR, ""); try { 254 return checkException((IType)selection[0]); 255 } catch (JavaModelException e) { 256 JavaPlugin.log(e); 257 return StatusInfo.OK_STATUS; 258 } 259 } 260 }); 261 262 if (dialog.open() == Window.OK) { 263 return (IType) dialog.getFirstResult(); 264 } 265 return null; 266 } 267 268 private IStatus checkException(final IType type) throws JavaModelException { 269 ITypeHierarchy hierarchy= type.newSupertypeHierarchy(new NullProgressMonitor()); 270 IType curr= type; 271 while (curr != null) { 272 String name= curr.getFullyQualifiedName(); 273 if ("java.lang.Throwable".equals(name)) return StatusInfo.OK_STATUS; 275 curr= hierarchy.getSuperclass(curr); 276 } 277 return JavaUIStatus.createError(IStatus.ERROR, 278 RefactoringMessages.ChangeExceptionsControl_not_exception, null); 279 } 280 281 private ExceptionInfo findExceptionInfo(IType exception) { 282 for (Iterator iter= fExceptionInfos.iterator(); iter.hasNext(); ) { 283 ExceptionInfo info= (ExceptionInfo) iter.next(); 284 if (info.getType().equals(exception)) 285 return info; 286 } 287 return null; 288 } 289 290 private Button createRemoveButton(Composite buttonComposite) { 291 final Button button= new Button(buttonComposite, SWT.PUSH); 292 button.setText(RefactoringMessages.ChangeExceptionsControl_buttons_remove); 293 button.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); 294 SWTUtil.setButtonDimensionHint(button); 295 button.addSelectionListener(new SelectionAdapter() { 296 public void widgetSelected(SelectionEvent e) { 297 int index= getTable().getSelectionIndices()[0]; 298 ExceptionInfo[] selected= getSelectedItems(); 299 for (int i= 0; i < selected.length; i++) { 300 if (selected[i].isAdded()) 301 fExceptionInfos.remove(selected[i]); 302 else 303 selected[i].markAsDeleted(); 304 } 305 restoreSelection(index); 306 } 307 private void restoreSelection(int index) { 308 fTableViewer.refresh(); 309 fTableViewer.getControl().setFocus(); 310 int itemCount= getTableItemCount(); 311 if (itemCount != 0) { 312 if (index >= itemCount) 313 index= itemCount - 1; 314 getTable().setSelection(index); 315 } 316 fListener.exceptionListChanged(); 317 updateButtonsEnabledState(); 318 } 319 }); 320 return button; 321 } 322 323 } 324 | Popular Tags |