1 11 package org.eclipse.jdt.junit.wizards; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Vector ; 16 17 import org.eclipse.swt.SWT; 18 import org.eclipse.swt.events.SelectionAdapter; 19 import org.eclipse.swt.events.SelectionEvent; 20 import org.eclipse.swt.events.SelectionListener; 21 import org.eclipse.swt.layout.GridData; 22 import org.eclipse.swt.layout.GridLayout; 23 import org.eclipse.swt.widgets.Button; 24 import org.eclipse.swt.widgets.Composite; 25 import org.eclipse.swt.widgets.Label; 26 import org.eclipse.swt.widgets.Widget; 27 28 import org.eclipse.jface.dialogs.Dialog; 29 import org.eclipse.jface.dialogs.IDialogSettings; 30 import org.eclipse.jface.viewers.CheckStateChangedEvent; 31 import org.eclipse.jface.viewers.ICheckStateListener; 32 import org.eclipse.jface.viewers.ITreeContentProvider; 33 import org.eclipse.jface.viewers.StructuredSelection; 34 import org.eclipse.jface.viewers.Viewer; 35 import org.eclipse.jface.viewers.ViewerFilter; 36 import org.eclipse.jface.wizard.WizardPage; 37 38 import org.eclipse.ui.PlatformUI; 39 import org.eclipse.ui.dialogs.ContainerCheckedTreeViewer; 40 41 import org.eclipse.jdt.core.Flags; 42 import org.eclipse.jdt.core.IMethod; 43 import org.eclipse.jdt.core.IType; 44 import org.eclipse.jdt.core.ITypeHierarchy; 45 import org.eclipse.jdt.core.JavaModelException; 46 47 import org.eclipse.jdt.ui.JavaElementLabelProvider; 48 49 import org.eclipse.jdt.internal.junit.Messages; 50 import org.eclipse.jdt.internal.junit.ui.IJUnitHelpContextIds; 51 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 52 import org.eclipse.jdt.internal.junit.util.LayoutUtil; 53 import org.eclipse.jdt.internal.junit.wizards.WizardMessages; 54 55 64 public class NewTestCaseWizardPageTwo extends WizardPage { 65 66 private final static String PAGE_NAME= "NewTestCaseCreationWizardPage2"; 68 private final static String STORE_USE_TASKMARKER= PAGE_NAME + ".USE_TASKMARKER"; private final static String STORE_CREATE_FINAL_METHOD_STUBS= PAGE_NAME + ".CREATE_FINAL_METHOD_STUBS"; 71 private IType fClassToTest; 72 73 private Button fCreateFinalMethodStubsButton; 74 private Button fCreateTasksButton; 75 private ContainerCheckedTreeViewer fMethodsTree; 76 private Button fSelectAllButton; 77 private Button fDeselectAllButton; 78 private Label fSelectedMethodsLabel; 79 private Object [] fCheckedObjects; 80 private boolean fCreateFinalStubs; 81 private boolean fCreateTasks; 82 83 86 public NewTestCaseWizardPageTwo() { 87 super(PAGE_NAME); 88 setTitle(WizardMessages.NewTestCaseWizardPageTwo_title); 89 setDescription(WizardMessages.NewTestCaseWizardPageTwo_description); 90 } 91 92 95 public void createControl(Composite parent) { 96 Composite container= new Composite(parent, SWT.NONE); 97 GridLayout layout= new GridLayout(); 98 layout.numColumns= 2; 99 container.setLayout(layout); 100 101 createMethodsTreeControls(container); 102 createSpacer(container); 103 createButtonChoices(container); 104 setControl(container); 105 restoreWidgetValues(); 106 Dialog.applyDialogFont(container); 107 PlatformUI.getWorkbench().getHelpSystem().setHelp(container, IJUnitHelpContextIds.NEW_TESTCASE_WIZARD_PAGE2); 108 } 109 110 private void createButtonChoices(Composite container) { 111 GridLayout layout; 112 GridData gd; 113 Composite prefixContainer= new Composite(container, SWT.NONE); 114 gd= new GridData(); 115 gd.horizontalAlignment = GridData.FILL; 116 gd.horizontalSpan = 1; 117 prefixContainer.setLayoutData(gd); 118 119 layout = new GridLayout(); 120 layout.numColumns = 1; 121 layout.marginWidth = 0; 122 layout.marginHeight = 0; 123 prefixContainer.setLayout(layout); 124 125 SelectionListener listener= new SelectionAdapter() { 126 public void widgetSelected(SelectionEvent e) { 127 doCheckBoxSelected(e.widget); 128 } 129 }; 130 fCreateFinalMethodStubsButton= createCheckBox(prefixContainer, WizardMessages.NewTestCaseWizardPageTwo_create_final_method_stubs_text, listener); 131 fCreateTasksButton= createCheckBox(prefixContainer, WizardMessages.NewTestCaseWizardPageTwo_create_tasks_text, listener); 132 } 133 134 private Button createCheckBox(Composite parent, String name, SelectionListener listener) { 135 Button button= new Button(parent, SWT.CHECK | SWT.LEFT); 136 button.setText(name); 137 button.setEnabled(true); 138 button.setSelection(true); 139 button.addSelectionListener(listener); 140 GridData gd= new GridData(GridData.FILL, GridData.CENTER, false, false); 141 button.setLayoutData(gd); 142 return button; 143 } 144 145 146 private void doCheckBoxSelected(Widget widget) { 147 if (widget == fCreateFinalMethodStubsButton) { 148 fCreateFinalStubs= fCreateFinalMethodStubsButton.getSelection(); 149 } else if (widget == fCreateTasksButton) { 150 fCreateTasks= fCreateTasksButton.getSelection(); 151 } 152 saveWidgetValues(); 153 } 154 155 private void createMethodsTreeControls(Composite container) { 156 Label label= new Label(container, SWT.LEFT | SWT.WRAP); 157 label.setFont(container.getFont()); 158 label.setText(WizardMessages.NewTestCaseWizardPageTwo_methods_tree_label); 159 GridData gd = new GridData(); 160 gd.horizontalSpan = 2; 161 label.setLayoutData(gd); 162 163 fMethodsTree= new ContainerCheckedTreeViewer(container, SWT.BORDER); 164 gd= new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 165 gd.heightHint= 180; 166 fMethodsTree.getTree().setLayoutData(gd); 167 168 fMethodsTree.setLabelProvider(new JavaElementLabelProvider()); 169 fMethodsTree.setAutoExpandLevel(2); 170 fMethodsTree.addCheckStateListener(new ICheckStateListener() { 171 public void checkStateChanged(CheckStateChangedEvent event) { 172 doCheckedStateChanged(); 173 } 174 }); 175 fMethodsTree.addFilter(new ViewerFilter() { 176 public boolean select(Viewer viewer, Object parentElement, Object element) { 177 if (element instanceof IMethod) { 178 IMethod method = (IMethod) element; 179 return !method.getElementName().equals("<clinit>"); } 181 return true; 182 } 183 }); 184 185 186 Composite buttonContainer= new Composite(container, SWT.NONE); 187 gd= new GridData(GridData.FILL_VERTICAL); 188 buttonContainer.setLayoutData(gd); 189 GridLayout buttonLayout= new GridLayout(); 190 buttonLayout.marginWidth= 0; 191 buttonLayout.marginHeight= 0; 192 buttonContainer.setLayout(buttonLayout); 193 194 fSelectAllButton= new Button(buttonContainer, SWT.PUSH); 195 fSelectAllButton.setText(WizardMessages.NewTestCaseWizardPageTwo_selectAll); 196 gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 197 fSelectAllButton.setLayoutData(gd); 198 fSelectAllButton.addSelectionListener(new SelectionAdapter() { 199 public void widgetSelected(SelectionEvent e) { 200 fMethodsTree.setCheckedElements((Object []) fMethodsTree.getInput()); 201 doCheckedStateChanged(); 202 } 203 }); 204 LayoutUtil.setButtonDimensionHint(fSelectAllButton); 205 206 fDeselectAllButton= new Button(buttonContainer, SWT.PUSH); 207 fDeselectAllButton.setText(WizardMessages.NewTestCaseWizardPageTwo_deselectAll); 208 gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 209 fDeselectAllButton.setLayoutData(gd); 210 fDeselectAllButton.addSelectionListener(new SelectionAdapter() { 211 public void widgetSelected(SelectionEvent e) { 212 fMethodsTree.setCheckedElements(new Object [0]); 213 doCheckedStateChanged(); 214 } 215 }); 216 LayoutUtil.setButtonDimensionHint(fDeselectAllButton); 217 218 219 fSelectedMethodsLabel= new Label(container, SWT.LEFT); 220 fSelectedMethodsLabel.setFont(container.getFont()); 221 doCheckedStateChanged(); 222 gd= new GridData(GridData.FILL_HORIZONTAL); 223 gd.horizontalSpan= 1; 224 fSelectedMethodsLabel.setLayoutData(gd); 225 226 Label emptyLabel= new Label(container, SWT.LEFT); 227 gd= new GridData(); 228 gd.horizontalSpan= 1; 229 emptyLabel.setLayoutData(gd); 230 } 231 232 private void createSpacer(Composite container) { 233 Label spacer= new Label(container, SWT.NONE); 234 GridData data= new GridData(); 235 data.horizontalSpan= 2; 236 data.horizontalAlignment= GridData.FILL; 237 data.verticalAlignment= GridData.BEGINNING; 238 data.heightHint= 4; 239 spacer.setLayoutData(data); 240 } 241 242 247 public void setClassUnderTest(IType classUnderTest) { 248 fClassToTest= classUnderTest; 249 } 250 251 252 255 public void setVisible(boolean visible) { 256 super.setVisible(visible); 257 if (visible) { 258 if (fClassToTest == null) { 259 return; 260 } 261 262 ArrayList types= null; 263 try { 264 ITypeHierarchy hierarchy= fClassToTest.newSupertypeHierarchy(null); 265 IType[] superTypes; 266 if (fClassToTest.isClass()) 267 superTypes= hierarchy.getAllSuperclasses(fClassToTest); 268 else if (fClassToTest.isInterface()) 269 superTypes= hierarchy.getAllSuperInterfaces(fClassToTest); 270 else 271 superTypes= new IType[0]; 272 types= new ArrayList (superTypes.length+1); 273 types.add(fClassToTest); 274 types.addAll(Arrays.asList(superTypes)); 275 } catch(JavaModelException e) { 276 JUnitPlugin.log(e); 277 } 278 if (types == null) 279 types= new ArrayList (); 280 fMethodsTree.setContentProvider(new MethodsTreeContentProvider(types.toArray())); 281 fMethodsTree.setInput(types.toArray()); 282 fMethodsTree.setSelection(new StructuredSelection(fClassToTest), true); 283 doCheckedStateChanged(); 284 285 fMethodsTree.getControl().setFocus(); 286 } 287 } 288 289 294 public IMethod[] getCheckedMethods() { 295 int methodCount= 0; 296 for (int i = 0; i < fCheckedObjects.length; i++) { 297 if (fCheckedObjects[i] instanceof IMethod) 298 methodCount++; 299 } 300 IMethod[] checkedMethods= new IMethod[methodCount]; 301 int j= 0; 302 for (int i = 0; i < fCheckedObjects.length; i++) { 303 if (fCheckedObjects[i] instanceof IMethod) { 304 checkedMethods[j]= (IMethod)fCheckedObjects[i]; 305 j++; 306 } 307 } 308 return checkedMethods; 309 } 310 311 private static class MethodsTreeContentProvider implements ITreeContentProvider { 312 private Object [] fTypes; 313 private IMethod[] fMethods; 314 private final Object [] fEmpty= new Object [0]; 315 316 public MethodsTreeContentProvider(Object [] types) { 317 fTypes= types; 318 Vector methods= new Vector (); 319 for (int i = types.length-1; i > -1; i--) { 320 Object object = types[i]; 321 if (object instanceof IType) { 322 IType type = (IType) object; 323 try { 324 IMethod[] currMethods= type.getMethods(); 325 for_currMethods: 326 for (int j = 0; j < currMethods.length; j++) { 327 IMethod currMethod = currMethods[j]; 328 int flags= currMethod.getFlags(); 329 if (!Flags.isPrivate(flags) && !Flags.isSynthetic(flags)) { 330 for (int k = 0; k < methods.size(); k++) { 331 IMethod m= ((IMethod)methods.get(k)); 332 if (m.getElementName().equals(currMethod.getElementName()) 333 && m.getSignature().equals(currMethod.getSignature())) { 334 methods.set(k,currMethod); 335 continue for_currMethods; 336 } 337 } 338 methods.add(currMethod); 339 } 340 } 341 } catch (JavaModelException e) { 342 JUnitPlugin.log(e); 343 } 344 } 345 } 346 fMethods= new IMethod[methods.size()]; 347 methods.copyInto(fMethods); 348 } 349 350 353 public Object [] getChildren(Object parentElement) { 354 if (parentElement instanceof IType) { 355 IType parentType= (IType)parentElement; 356 ArrayList result= new ArrayList (fMethods.length); 357 for (int i= 0; i < fMethods.length; i++) { 358 if (fMethods[i].getDeclaringType().equals(parentType)) { 359 result.add(fMethods[i]); 360 } 361 } 362 return result.toArray(); 363 } 364 return fEmpty; 365 } 366 367 370 public Object getParent(Object element) { 371 if (element instanceof IMethod) 372 return ((IMethod)element).getDeclaringType(); 373 return null; 374 } 375 376 379 public boolean hasChildren(Object element) { 380 return getChildren(element).length > 0; 381 } 382 383 386 public Object [] getElements(Object inputElement) { 387 return fTypes; 388 } 389 390 393 public void dispose() { 394 } 395 396 399 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 400 } 401 402 public IMethod[] getAllMethods() { 403 return fMethods; 404 } 405 } 406 407 412 public boolean isCreateTasks() { 413 return fCreateTasks; 414 } 415 416 420 public boolean getCreateFinalMethodStubsButtonSelection() { 421 return fCreateFinalStubs; 422 } 423 424 private void doCheckedStateChanged() { 425 Object [] checked= fMethodsTree.getCheckedElements(); 426 fCheckedObjects= checked; 427 428 int checkedMethodCount= 0; 429 for (int i= 0; i < checked.length; i++) { 430 if (checked[i] instanceof IMethod) 431 checkedMethodCount++; 432 } 433 String label= ""; if (checkedMethodCount == 1) 435 label= Messages.format(WizardMessages.NewTestCaseWizardPageTwo_selected_methods_label_one, new Integer (checkedMethodCount)); 436 else 437 label= Messages.format(WizardMessages.NewTestCaseWizardPageTwo_selected_methods_label_many, new Integer (checkedMethodCount)); 438 fSelectedMethodsLabel.setText(label); 439 } 440 441 446 public IMethod[] getAllMethods() { 447 return ((MethodsTreeContentProvider)fMethodsTree.getContentProvider()).getAllMethods(); 448 } 449 450 454 private void restoreWidgetValues() { 455 IDialogSettings settings= getDialogSettings(); 456 if (settings != null) { 457 fCreateTasks= settings.getBoolean(STORE_USE_TASKMARKER); 458 fCreateTasksButton.setSelection(fCreateTasks); 459 fCreateFinalStubs= settings.getBoolean(STORE_CREATE_FINAL_METHOD_STUBS); 460 fCreateFinalMethodStubsButton.setSelection(fCreateFinalStubs); 461 } 462 } 463 464 private void saveWidgetValues() { 465 IDialogSettings settings= getDialogSettings(); 466 if (settings != null) { 467 settings.put(STORE_USE_TASKMARKER, fCreateTasks); 468 settings.put(STORE_CREATE_FINAL_METHOD_STUBS, fCreateFinalStubs); 469 } 470 } 471 472 } 473 | Popular Tags |