KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > wizards > NewTestCaseCreationWizardPage2


1 /*******************************************************************************
2  * Copyright (c) 2000, 2004 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.wizards;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.Vector JavaDoc;
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 /**
47  * Wizard page to select the methods from a class under test.
48  */

49 public class NewTestCaseCreationWizardPage2 extends WizardPage implements IAboutToRunOperation {
50
51     private final static String JavaDoc PAGE_NAME= "NewTestCaseCreationWizardPage2"; //$NON-NLS-1$
52
private final static String JavaDoc STORE_USE_TASKMARKER= PAGE_NAME + ".USE_TASKMARKER"; //$NON-NLS-1$
53
private final static String JavaDoc STORE_CREATE_FINAL_METHOD_STUBS= PAGE_NAME + ".CREATE_FINAL_METHOD_STUBS"; //$NON-NLS-1$
54
public final static String JavaDoc PREFIX= "test"; //$NON-NLS-1$
55

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 JavaDoc[] fCheckedObjects;
66     private boolean fCreateFinalStubs;
67     private boolean fCreateTasks;
68     
69     /**
70      * Constructor for NewTestCaseCreationWizardPage2.
71      */

72     protected NewTestCaseCreationWizardPage2(NewTestCaseCreationWizardPage firstPage) {
73         super(PAGE_NAME);
74         fFirstPage= firstPage;
75         setTitle(WizardMessages.getString("NewTestClassWizPage2.title")); //$NON-NLS-1$
76
setDescription(WizardMessages.getString("NewTestClassWizPage2.description")); //$NON-NLS-1$
77
}
78
79     /**
80      * @see org.eclipse.jface.dialogs.IDialogPage#createControl(Composite)
81      */

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 JavaDoc buttonNames[] = {
115             WizardMessages.getString("NewTestClassWizPage2.create_final_method_stubs.text"), //$NON-NLS-1$
116
WizardMessages.getString("NewTestClassWizPage2.create_tasks.text") //$NON-NLS-1$
117
};
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]); //$NON-NLS-1$
122
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")); //$NON-NLS-1$
137
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 JavaDoc parentElement, Object JavaDoc element) {
155                 if (element instanceof IMethod) {
156                     IMethod method = (IMethod) element;
157                     return !method.getElementName().equals("<clinit>"); //$NON-NLS-1$
158
}
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")); //$NON-NLS-1$
173
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 JavaDoc[]) fMethodsTree.getInput());
178                 updateSelectedMethodsLabel();
179             }
180         });
181
182         fDeselectAllButton= new Button(buttonContainer, SWT.PUSH);
183         fDeselectAllButton.setText(WizardMessages.getString("NewTestClassWizPage2.deselectAll")); //$NON-NLS-1$
184
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 JavaDoc[0]);
189                 updateSelectedMethodsLabel();
190             }
191         });
192
193         /* No of selected methods label */
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     /**
218      * @see org.eclipse.jface.dialogs.DialogPage#setVisible(boolean)
219      */

220     public void setVisible(boolean visible) {
221         super.setVisible(visible);
222         if (visible) {
223             fClassToTest= fFirstPage.getClassToTest();
224             IType currType= fClassToTest;
225             ArrayList JavaDoc 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 JavaDoc(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 JavaDoc();
244             fMethodsTree.setInput(types.toArray());
245             fMethodsTree.setSelection(new StructuredSelection(currType), true);
246             updateSelectedMethodsLabel();
247             setFocus();
248         }
249     }
250
251     /**
252      * Returns all checked methods in the Methods tree.
253      */

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 JavaDoc[] fTypes;
273         private IMethod[] fMethods;
274         private final Object JavaDoc[] fEmpty= new Object JavaDoc[0];
275
276         public MethodsTreeContentProvider(Object JavaDoc[] types) {
277             fTypes= types;
278             Vector JavaDoc methods= new Vector JavaDoc();
279             for (int i = types.length-1; i > -1; i--) {
280                 Object JavaDoc 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         /*
311          * @see ITreeContentProvider#getChildren(Object)
312          */

313         public Object JavaDoc[] getChildren(Object JavaDoc parentElement) {
314             if (parentElement instanceof IType) {
315                 IType parentType= (IType)parentElement;
316                 ArrayList JavaDoc result= new ArrayList JavaDoc(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         /*
328          * @see ITreeContentProvider#getParent(Object)
329          */

330         public Object JavaDoc getParent(Object JavaDoc element) {
331             if (element instanceof IMethod)
332                 return ((IMethod)element).getDeclaringType();
333             return null;
334         }
335
336         /*
337          * @see ITreeContentProvider#hasChildren(Object)
338          */

339         public boolean hasChildren(Object JavaDoc element) {
340             return getChildren(element).length > 0;
341         }
342
343         /*
344          * @see IStructuredContentProvider#getElements(Object)
345          */

346         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
347             return fTypes;
348         }
349
350         /*
351          * @see IContentProvider#dispose()
352          */

353         public void dispose() {
354         }
355
356         /*
357          * @see IContentProvider#inputChanged(Viewer, Object, Object)
358          */

359         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {
360         }
361         
362         public IMethod[] getAllMethods() {
363             return fMethods;
364         }
365     }
366
367     /**
368      * Returns true if the checkbox for creating tasks is checked.
369      */

370     public boolean getCreateTasksButtonSelection() {
371         return fCreateTasks;
372     }
373
374     /**
375      * Returns true if the checkbox for final method stubs is checked.
376      */

377     public boolean getCreateFinalMethodStubsButtonSelection() {
378         return fCreateFinalStubs;
379     }
380         
381     private void updateSelectedMethodsLabel() {
382         Object JavaDoc[] 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 JavaDoc label= ""; //$NON-NLS-1$
389
if (checkedMethodCount == 1)
390             label= WizardMessages.getFormattedString("NewTestClassWizPage2.selected_methods.label_one", new Integer JavaDoc(checkedMethodCount)); //$NON-NLS-1$
391
else
392             label= WizardMessages.getFormattedString("NewTestClassWizPage2.selected_methods.label_many", new Integer JavaDoc(checkedMethodCount)); //$NON-NLS-1$
393
fSelectedMethodsLabel.setText(label);
394     }
395     
396     /**
397      * Returns all the methods in the Methods tree.
398      */

399     public IMethod[] getAllMethods() {
400         return ((MethodsTreeContentProvider)fMethodsTree.getContentProvider()).getAllMethods();
401     }
402
403     /**
404      * Sets the focus on the type name.
405      */

406     protected void setFocus() {
407         fMethodsTree.getControl().setFocus();
408     }
409         
410     /**
411      * Use the dialog store to restore widget values to the values that they held
412      * last time this wizard was used to completion
413      */

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     /**
423      * Since Finish was pressed, write widget values to the dialog store so that they
424      * will persist into the next invocation of this wizard page
425      */

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