1 11 package org.eclipse.jdt.internal.junit.wizards; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.Vector ; 16 17 import org.eclipse.jdt.core.Flags; 18 import org.eclipse.jdt.core.IMethod; 19 import org.eclipse.jdt.core.IType; 20 import org.eclipse.jdt.core.ITypeHierarchy; 21 import org.eclipse.jdt.core.JavaModelException; 22 import org.eclipse.jdt.internal.junit.ui.IJUnitHelpContextIds; 23 import org.eclipse.jdt.internal.junit.ui.JUnitPlugin; 24 import org.eclipse.jdt.internal.ui.viewsupport.AppearanceAwareLabelProvider; 25 26 import org.eclipse.jface.dialogs.Dialog; 27 import org.eclipse.jface.dialogs.IDialogSettings; 28 import org.eclipse.jface.viewers.CheckStateChangedEvent; 29 import org.eclipse.jface.viewers.ICheckStateListener; 30 import org.eclipse.jface.viewers.ITreeContentProvider; 31 import org.eclipse.jface.viewers.StructuredSelection; 32 import org.eclipse.jface.viewers.Viewer; 33 import org.eclipse.jface.viewers.ViewerFilter; 34 import org.eclipse.jface.wizard.WizardPage; 35 import org.eclipse.swt.SWT; 36 import org.eclipse.swt.events.SelectionAdapter; 37 import org.eclipse.swt.events.SelectionEvent; 38 import org.eclipse.swt.layout.GridData; 39 import org.eclipse.swt.layout.GridLayout; 40 import org.eclipse.swt.widgets.Button; 41 import org.eclipse.swt.widgets.Composite; 42 import org.eclipse.swt.widgets.Label; 43 import org.eclipse.ui.help.WorkbenchHelp; 44 import org.eclipse.ui.internal.dialogs.ContainerCheckedTreeViewer; 45 46 49 public class NewTestCaseCreationWizardPage2 extends WizardPage implements IAboutToRunOperation { 50 51 private final static String PAGE_NAME= "NewTestCaseCreationWizardPage2"; 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"; public final static String PREFIX= "test"; 56 private NewTestCaseCreationWizardPage fFirstPage; 57 private IType fClassToTest; 58 59 private Button fCreateFinalMethodStubsButton; 60 private Button fCreateTasksButton; 61 private ContainerCheckedTreeViewer fMethodsTree; 62 private Button fSelectAllButton; 63 private Button fDeselectAllButton; 64 private Label fSelectedMethodsLabel; 65 private Object [] fCheckedObjects; 66 private boolean fCreateFinalStubs; 67 private boolean fCreateTasks; 68 69 72 protected NewTestCaseCreationWizardPage2(NewTestCaseCreationWizardPage firstPage) { 73 super(PAGE_NAME); 74 fFirstPage= firstPage; 75 setTitle(WizardMessages.getString("NewTestClassWizPage2.title")); setDescription(WizardMessages.getString("NewTestClassWizPage2.description")); } 78 79 82 public void createControl(Composite parent) { 83 Composite container= new Composite(parent, SWT.NONE); 84 GridLayout layout= new GridLayout(); 85 layout.numColumns= 2; 86 container.setLayout(layout); 87 88 createMethodsTreeControls(container); 89 createSpacer(container); 90 createButtonChoices(container); 91 setControl(container); 92 restoreWidgetValues(); 93 Dialog.applyDialogFont(container); 94 WorkbenchHelp.setHelp(container, IJUnitHelpContextIds.NEW_TESTCASE_WIZARD_PAGE2); 95 } 96 97 protected void createButtonChoices(Composite container) { 98 GridLayout layout; 99 GridData gd; 100 Composite prefixContainer= new Composite(container, SWT.NONE); 101 gd= new GridData(); 102 gd.horizontalAlignment = GridData.FILL; 103 gd.horizontalSpan = 1; 104 prefixContainer.setLayoutData(gd); 105 106 layout = new GridLayout(); 107 layout.numColumns = 1; 108 layout.marginWidth = 0; 109 layout.marginHeight = 0; 110 prefixContainer.setLayout(layout); 111 112 Button buttons[] = {null, null}; 113 114 String buttonNames[] = { 115 WizardMessages.getString("NewTestClassWizPage2.create_final_method_stubs.text"), WizardMessages.getString("NewTestClassWizPage2.create_tasks.text") }; 118 119 for (int i=0; i < buttons.length; i++) { 120 buttons[i]= new Button(prefixContainer, SWT.CHECK | SWT.LEFT); 121 buttons[i].setText(buttonNames[i]); buttons[i].setEnabled(true); 123 buttons[i].setSelection(true); 124 gd= new GridData(); 125 gd.horizontalAlignment= GridData.FILL; 126 gd.horizontalSpan= 1; 127 buttons[i].setLayoutData(gd); 128 } 129 fCreateFinalMethodStubsButton= buttons[0]; 130 fCreateTasksButton= buttons[1]; 131 } 132 133 protected void createMethodsTreeControls(Composite container) { 134 Label label= new Label(container, SWT.LEFT | SWT.WRAP); 135 label.setFont(container.getFont()); 136 label.setText(WizardMessages.getString("NewTestClassWizPage2.methods_tree.label")); GridData gd = new GridData(); 138 gd.horizontalSpan = 2; 139 label.setLayoutData(gd); 140 141 fMethodsTree= new ContainerCheckedTreeViewer(container, SWT.BORDER); 142 gd= new GridData(GridData.FILL_BOTH | GridData.GRAB_HORIZONTAL | GridData.GRAB_VERTICAL); 143 gd.heightHint= 180; 144 fMethodsTree.getTree().setLayoutData(gd); 145 146 fMethodsTree.setLabelProvider(new AppearanceAwareLabelProvider()); 147 fMethodsTree.setAutoExpandLevel(2); 148 fMethodsTree.addCheckStateListener(new ICheckStateListener() { 149 public void checkStateChanged(CheckStateChangedEvent event) { 150 updateSelectedMethodsLabel(); 151 } 152 }); 153 fMethodsTree.addFilter(new ViewerFilter() { 154 public boolean select(Viewer viewer, Object parentElement, Object element) { 155 if (element instanceof IMethod) { 156 IMethod method = (IMethod) element; 157 return !method.getElementName().equals("<clinit>"); } 159 return true; 160 } 161 }); 162 163 Composite buttonContainer= new Composite(container, SWT.NONE); 164 gd= new GridData(GridData.FILL_VERTICAL); 165 buttonContainer.setLayoutData(gd); 166 GridLayout buttonLayout= new GridLayout(); 167 buttonLayout.marginWidth= 0; 168 buttonLayout.marginHeight= 0; 169 buttonContainer.setLayout(buttonLayout); 170 171 fSelectAllButton= new Button(buttonContainer, SWT.PUSH); 172 fSelectAllButton.setText(WizardMessages.getString("NewTestClassWizPage2.selectAll")); gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 174 fSelectAllButton.setLayoutData(gd); 175 fSelectAllButton.addSelectionListener(new SelectionAdapter() { 176 public void widgetSelected(SelectionEvent e) { 177 fMethodsTree.setCheckedElements((Object []) fMethodsTree.getInput()); 178 updateSelectedMethodsLabel(); 179 } 180 }); 181 182 fDeselectAllButton= new Button(buttonContainer, SWT.PUSH); 183 fDeselectAllButton.setText(WizardMessages.getString("NewTestClassWizPage2.deselectAll")); gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING); 185 fDeselectAllButton.setLayoutData(gd); 186 fDeselectAllButton.addSelectionListener(new SelectionAdapter() { 187 public void widgetSelected(SelectionEvent e) { 188 fMethodsTree.setCheckedElements(new Object [0]); 189 updateSelectedMethodsLabel(); 190 } 191 }); 192 193 194 fSelectedMethodsLabel= new Label(container, SWT.LEFT); 195 fSelectedMethodsLabel.setFont(container.getFont()); 196 updateSelectedMethodsLabel(); 197 gd= new GridData(GridData.FILL_HORIZONTAL); 198 gd.horizontalSpan= 1; 199 fSelectedMethodsLabel.setLayoutData(gd); 200 201 Label emptyLabel= new Label(container, SWT.LEFT); 202 gd= new GridData(); 203 gd.horizontalSpan= 1; 204 emptyLabel.setLayoutData(gd); 205 } 206 207 protected void createSpacer(Composite container) { 208 Label spacer= new Label(container, SWT.NONE); 209 GridData data= new GridData(); 210 data.horizontalSpan= 2; 211 data.horizontalAlignment= GridData.FILL; 212 data.verticalAlignment= GridData.BEGINNING; 213 data.heightHint= 4; 214 spacer.setLayoutData(data); 215 } 216 217 220 public void setVisible(boolean visible) { 221 super.setVisible(visible); 222 if (visible) { 223 fClassToTest= fFirstPage.getClassToTest(); 224 IType currType= fClassToTest; 225 ArrayList types= null; 226 try { 227 ITypeHierarchy hierarchy= currType.newSupertypeHierarchy(null); 228 IType[] superTypes; 229 if (currType.isClass()) 230 superTypes= hierarchy.getAllSuperclasses(currType); 231 else if (currType.isInterface()) 232 superTypes= hierarchy.getAllSuperInterfaces(currType); 233 else 234 superTypes= new IType[0]; 235 types= new ArrayList (superTypes.length+1); 236 types.add(currType); 237 types.addAll(Arrays.asList(superTypes)); 238 } catch(JavaModelException e) { 239 JUnitPlugin.log(e); 240 } 241 fMethodsTree.setContentProvider(new MethodsTreeContentProvider(types.toArray())); 242 if (types == null) 243 types= new ArrayList (); 244 fMethodsTree.setInput(types.toArray()); 245 fMethodsTree.setSelection(new StructuredSelection(currType), true); 246 updateSelectedMethodsLabel(); 247 setFocus(); 248 } 249 } 250 251 254 public IMethod[] getCheckedMethods() { 255 int methodCount= 0; 256 for (int i = 0; i < fCheckedObjects.length; i++) { 257 if (fCheckedObjects[i] instanceof IMethod) 258 methodCount++; 259 } 260 IMethod[] checkedMethods= new IMethod[methodCount]; 261 int j= 0; 262 for (int i = 0; i < fCheckedObjects.length; i++) { 263 if (fCheckedObjects[i] instanceof IMethod) { 264 checkedMethods[j]= (IMethod)fCheckedObjects[i]; 265 j++; 266 } 267 } 268 return checkedMethods; 269 } 270 271 private static class MethodsTreeContentProvider implements ITreeContentProvider { 272 private Object [] fTypes; 273 private IMethod[] fMethods; 274 private final Object [] fEmpty= new Object [0]; 275 276 public MethodsTreeContentProvider(Object [] types) { 277 fTypes= types; 278 Vector methods= new Vector (); 279 for (int i = types.length-1; i > -1; i--) { 280 Object object = types[i]; 281 if (object instanceof IType) { 282 IType type = (IType) object; 283 try { 284 IMethod[] currMethods= type.getMethods(); 285 for_currMethods: 286 for (int j = 0; j < currMethods.length; j++) { 287 IMethod currMethod = currMethods[j]; 288 int flags= currMethod.getFlags(); 289 if (!Flags.isPrivate(flags)) { 290 for (int k = 0; k < methods.size(); k++) { 291 IMethod m= ((IMethod)methods.get(k)); 292 if (m.getElementName().equals(currMethod.getElementName()) 293 && m.getSignature().equals(currMethod.getSignature())) { 294 methods.set(k,currMethod); 295 continue for_currMethods; 296 } 297 } 298 methods.add(currMethod); 299 } 300 } 301 } catch (JavaModelException e) { 302 JUnitPlugin.log(e); 303 } 304 } 305 } 306 fMethods= new IMethod[methods.size()]; 307 methods.copyInto(fMethods); 308 } 309 310 313 public Object [] getChildren(Object parentElement) { 314 if (parentElement instanceof IType) { 315 IType parentType= (IType)parentElement; 316 ArrayList result= new ArrayList (fMethods.length); 317 for (int i= 0; i < fMethods.length; i++) { 318 if (fMethods[i].getDeclaringType().equals(parentType)) { 319 result.add(fMethods[i]); 320 } 321 } 322 return result.toArray(); 323 } 324 return fEmpty; 325 } 326 327 330 public Object getParent(Object element) { 331 if (element instanceof IMethod) 332 return ((IMethod)element).getDeclaringType(); 333 return null; 334 } 335 336 339 public boolean hasChildren(Object element) { 340 return getChildren(element).length > 0; 341 } 342 343 346 public Object [] getElements(Object inputElement) { 347 return fTypes; 348 } 349 350 353 public void dispose() { 354 } 355 356 359 public void inputChanged(Viewer viewer, Object oldInput, Object newInput) { 360 } 361 362 public IMethod[] getAllMethods() { 363 return fMethods; 364 } 365 } 366 367 370 public boolean getCreateTasksButtonSelection() { 371 return fCreateTasks; 372 } 373 374 377 public boolean getCreateFinalMethodStubsButtonSelection() { 378 return fCreateFinalStubs; 379 } 380 381 private void updateSelectedMethodsLabel() { 382 Object [] checked= fMethodsTree.getCheckedElements(); 383 int checkedMethodCount= 0; 384 for (int i= 0; i < checked.length; i++) { 385 if (checked[i] instanceof IMethod) 386 checkedMethodCount++; 387 } 388 String label= ""; if (checkedMethodCount == 1) 390 label= WizardMessages.getFormattedString("NewTestClassWizPage2.selected_methods.label_one", new Integer (checkedMethodCount)); else 392 label= WizardMessages.getFormattedString("NewTestClassWizPage2.selected_methods.label_many", new Integer (checkedMethodCount)); fSelectedMethodsLabel.setText(label); 394 } 395 396 399 public IMethod[] getAllMethods() { 400 return ((MethodsTreeContentProvider)fMethodsTree.getContentProvider()).getAllMethods(); 401 } 402 403 406 protected void setFocus() { 407 fMethodsTree.getControl().setFocus(); 408 } 409 410 414 private void restoreWidgetValues() { 415 IDialogSettings settings= getDialogSettings(); 416 if (settings != null) { 417 fCreateTasksButton.setSelection(settings.getBoolean(STORE_USE_TASKMARKER)); 418 fCreateFinalMethodStubsButton.setSelection(settings.getBoolean(STORE_CREATE_FINAL_METHOD_STUBS)); 419 } 420 } 421 422 426 void saveWidgetValues() { 427 IDialogSettings settings= getDialogSettings(); 428 if (settings != null) { 429 settings.put(STORE_USE_TASKMARKER, fCreateTasksButton.getSelection()); 430 settings.put(STORE_CREATE_FINAL_METHOD_STUBS, fCreateFinalMethodStubsButton.getSelection()); 431 } 432 } 433 434 public void aboutToRunOperation() { 435 fCheckedObjects= fMethodsTree.getCheckedElements(); 436 fCreateFinalStubs= fCreateFinalMethodStubsButton.getSelection(); 437 fCreateTasks= fCreateTasksButton.getSelection(); 438 } 439 } 440 | Popular Tags |