KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > ui > JUnitPreferencePage


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

12 package org.eclipse.jdt.internal.junit.ui;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Arrays JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 import org.eclipse.swt.SWT;
21 import org.eclipse.swt.custom.TableEditor;
22 import org.eclipse.swt.events.FocusAdapter;
23 import org.eclipse.swt.events.FocusEvent;
24 import org.eclipse.swt.events.KeyAdapter;
25 import org.eclipse.swt.events.KeyEvent;
26 import org.eclipse.swt.graphics.Image;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Event;
33 import org.eclipse.swt.widgets.Label;
34 import org.eclipse.swt.widgets.Listener;
35 import org.eclipse.swt.widgets.Shell;
36 import org.eclipse.swt.widgets.Table;
37 import org.eclipse.swt.widgets.TableColumn;
38 import org.eclipse.swt.widgets.TableItem;
39 import org.eclipse.swt.widgets.Text;
40
41 import org.eclipse.jface.dialogs.Dialog;
42 import org.eclipse.jface.dialogs.IDialogConstants;
43 import org.eclipse.jface.preference.IPreferenceStore;
44 import org.eclipse.jface.preference.PreferencePage;
45 import org.eclipse.jface.viewers.CheckStateChangedEvent;
46 import org.eclipse.jface.viewers.CheckboxTableViewer;
47 import org.eclipse.jface.viewers.ColumnWeightData;
48 import org.eclipse.jface.viewers.ContentViewer;
49 import org.eclipse.jface.viewers.ICheckStateListener;
50 import org.eclipse.jface.viewers.ILabelProvider;
51 import org.eclipse.jface.viewers.ISelection;
52 import org.eclipse.jface.viewers.ISelectionChangedListener;
53 import org.eclipse.jface.viewers.IStructuredContentProvider;
54 import org.eclipse.jface.viewers.IStructuredSelection;
55 import org.eclipse.jface.viewers.ITableLabelProvider;
56 import org.eclipse.jface.viewers.LabelProvider;
57 import org.eclipse.jface.viewers.SelectionChangedEvent;
58 import org.eclipse.jface.viewers.Viewer;
59 import org.eclipse.jface.viewers.ViewerComparator;
60 import org.eclipse.jface.window.Window;
61
62 import org.eclipse.ui.IWorkbench;
63 import org.eclipse.ui.IWorkbenchPreferencePage;
64 import org.eclipse.ui.PlatformUI;
65 import org.eclipse.ui.dialogs.SelectionDialog;
66 import org.eclipse.ui.progress.IProgressService;
67
68 import org.eclipse.jdt.core.IJavaElement;
69 import org.eclipse.jdt.core.IType;
70 import org.eclipse.jdt.core.JavaModelException;
71 import org.eclipse.jdt.core.search.IJavaSearchScope;
72 import org.eclipse.jdt.core.search.SearchEngine;
73
74 import org.eclipse.jdt.ui.IJavaElementSearchConstants;
75 import org.eclipse.jdt.ui.ISharedImages;
76 import org.eclipse.jdt.ui.JavaUI;
77
78 import org.eclipse.jdt.internal.ui.util.SWTUtil;
79 import org.eclipse.jdt.internal.ui.util.TableLayoutComposite;
80
81 import org.eclipse.jdt.internal.junit.launcher.AssertionVMArg;
82 import org.eclipse.jdt.internal.junit.util.ExceptionHandler;
83 import org.eclipse.jdt.internal.junit.util.LayoutUtil;
84
85 /**
86  * Preference page for JUnit settings. Supports to define the failure
87  * stack filter patterns.
88  */

89 public class JUnitPreferencePage extends PreferencePage implements IWorkbenchPreferencePage {
90
91     private static final String JavaDoc DEFAULT_NEW_FILTER_TEXT= ""; //$NON-NLS-1$
92
private static final Image IMG_CUNIT= JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_CLASS);
93     private static final Image IMG_PKG= JavaUI.getSharedImages().getImage(ISharedImages.IMG_OBJS_PACKAGE);
94     
95     // enable assertions widget
96
private Button fEnableAssertionsCheckBox;
97
98     // Step filter widgets
99
private Label fFilterViewerLabel;
100     private CheckboxTableViewer fFilterViewer;
101     private Table fFilterTable;
102
103     private Button fAddPackageButton;
104     private Button fAddTypeButton;
105     private Button fRemoveFilterButton;
106     private Button fAddFilterButton;
107
108     private Button fEnableAllButton;
109     private Button fDisableAllButton;
110
111     private Text fEditorText;
112     private String JavaDoc fInvalidEditorText= null;
113     private TableEditor fTableEditor;
114     private TableItem fNewTableItem;
115     private Filter fNewStackFilter;
116
117     private StackFilterContentProvider fStackFilterContentProvider;
118
119     /**
120      * Model object that represents a single entry in the filter table.
121      */

122     private static class Filter {
123
124         private String JavaDoc fName;
125         private boolean fChecked;
126
127         public Filter(String JavaDoc name, boolean checked) {
128             setName(name);
129             setChecked(checked);
130         }
131
132         public String JavaDoc getName() {
133             return fName;
134         }
135         
136         public void setName(String JavaDoc name) {
137             fName= name;
138         }
139                 
140         public boolean isChecked() {
141             return fChecked;
142         }
143
144         public void setChecked(boolean checked) {
145             fChecked= checked;
146         }
147
148         public boolean equals(Object JavaDoc o) {
149             if (!(o instanceof Filter))
150                 return false;
151
152             Filter other= (Filter) o;
153             return (getName().equals(other.getName()));
154         }
155
156         public int hashCode() {
157             return fName.hashCode();
158         }
159     }
160
161     /**
162      * Sorter for the filter table; sorts alphabetically ascending.
163      */

164     private static class FilterViewerSorter extends ViewerComparator {
165         public int compare(Viewer viewer, Object JavaDoc e1, Object JavaDoc e2) {
166             ILabelProvider lprov= (ILabelProvider) ((ContentViewer) viewer).getLabelProvider();
167             String JavaDoc name1= lprov.getText(e1);
168             String JavaDoc name2= lprov.getText(e2);
169             if (name1 == null)
170                 name1= ""; //$NON-NLS-1$
171

172             if (name2 == null)
173                 name2= ""; //$NON-NLS-1$
174

175             if (name1.length() > 0 && name2.length() > 0) {
176                 char char1= name1.charAt(name1.length() - 1);
177                 char char2= name2.charAt(name2.length() - 1);
178                 if (char1 == '*' && char1 != char2)
179                     return -1;
180
181                 if (char2 == '*' && char2 != char1)
182                     return 1;
183             }
184             return getComparator().compare(name1, name2);
185         }
186     }
187
188     /**
189      * Label provider for Filter model objects
190      */

191     private static class FilterLabelProvider extends LabelProvider implements ITableLabelProvider {
192
193         public String JavaDoc getColumnText(Object JavaDoc object, int column) {
194             return (column == 0) ? ((Filter) object).getName() : ""; //$NON-NLS-1$
195
}
196
197         public String JavaDoc getText(Object JavaDoc element) {
198             return ((Filter) element).getName();
199         }
200
201         public Image getColumnImage(Object JavaDoc object, int column) {
202             String JavaDoc name= ((Filter) object).getName();
203             if (name.endsWith(".*") || name.equals(JUnitMessages.JUnitMainTab_label_defaultpackage)) { //$NON-NLS-1$
204
//package
205
return IMG_PKG;
206             } else if ("".equals(name)) { //$NON-NLS-1$
207
//needed for the in-place editor
208
return null;
209             } else if ((Character.isUpperCase(name.charAt(0))) && (name.indexOf('.') < 0)) {
210                 //class in default package
211
return IMG_CUNIT;
212             } else {
213                 //fully-qualified class or other filter
214
final int lastDotIndex= name.lastIndexOf('.');
215                 if ((-1 != lastDotIndex) && ((name.length() - 1) != lastDotIndex) && Character.isUpperCase(name.charAt(lastDotIndex + 1)))
216                     return IMG_CUNIT;
217             }
218             //other filter
219
return null;
220         }
221     }
222
223     /**
224      * Content provider for the filter table. Content consists of instances of
225      * Filter.
226      */

227     private class StackFilterContentProvider implements IStructuredContentProvider {
228
229         private List JavaDoc fFilters;
230
231         public StackFilterContentProvider() {
232             List JavaDoc active= createActiveStackFiltersList();
233             List JavaDoc inactive= createInactiveStackFiltersList();
234             populateFilters(active, inactive);
235         }
236
237         public void setDefaults() {
238             fFilterViewer.remove(fFilters.toArray());
239             List JavaDoc active= JUnitPreferencesConstants.createDefaultStackFiltersList();
240             List JavaDoc inactive= new ArrayList JavaDoc();
241             populateFilters(active, inactive);
242         }
243
244         protected void populateFilters(List JavaDoc activeList, List JavaDoc inactiveList) {
245             fFilters= new ArrayList JavaDoc(activeList.size() + inactiveList.size());
246             populateList(activeList, true);
247             if (inactiveList.size() != 0)
248                 populateList(inactiveList, false);
249         }
250
251         protected void populateList(List JavaDoc list, boolean checked) {
252             Iterator JavaDoc iterator= list.iterator();
253
254             while (iterator.hasNext()) {
255                 String JavaDoc name= (String JavaDoc) iterator.next();
256                 addFilter(name, checked);
257             }
258         }
259
260         public Filter addFilter(String JavaDoc name, boolean checked) {
261             Filter filter= new Filter(name, checked);
262             if (!fFilters.contains(filter)) {
263                 fFilters.add(filter);
264                 fFilterViewer.add(filter);
265                 fFilterViewer.setChecked(filter, checked);
266             }
267             updateActions();
268             return filter;
269         }
270
271         public void saveFilters() {
272             List JavaDoc active= new ArrayList JavaDoc(fFilters.size());
273             List JavaDoc inactive= new ArrayList JavaDoc(fFilters.size());
274             Iterator JavaDoc iterator= fFilters.iterator();
275             while (iterator.hasNext()) {
276                 Filter filter= (Filter) iterator.next();
277                 String JavaDoc name= filter.getName();
278                 if (filter.isChecked())
279                     active.add(name);
280                 else
281                     inactive.add(name);
282             }
283             String JavaDoc pref= JUnitPreferencesConstants.serializeList((String JavaDoc[]) active.toArray(new String JavaDoc[active.size()]));
284             getPreferenceStore().setValue(JUnitPreferencesConstants.PREF_ACTIVE_FILTERS_LIST, pref);
285             pref= JUnitPreferencesConstants.serializeList((String JavaDoc[]) inactive.toArray(new String JavaDoc[inactive.size()]));
286             getPreferenceStore().setValue(JUnitPreferencesConstants.PREF_INACTIVE_FILTERS_LIST, pref);
287         }
288
289         public void removeFilters(Object JavaDoc[] filters) {
290             for (int i= (filters.length - 1); i >= 0; --i) {
291                 Filter filter= (Filter) filters[i];
292                 fFilters.remove(filter);
293             }
294             fFilterViewer.remove(filters);
295             updateActions();
296         }
297
298         public void toggleFilter(Filter filter) {
299             boolean newState= !filter.isChecked();
300             filter.setChecked(newState);
301             fFilterViewer.setChecked(filter, newState);
302         }
303
304         public Object JavaDoc[] getElements(Object JavaDoc inputElement) {
305             return fFilters.toArray();
306         }
307         
308         public void inputChanged(Viewer viewer, Object JavaDoc oldInput, Object JavaDoc newInput) {}
309         public void dispose() {}
310
311     }
312         
313     public JUnitPreferencePage() {
314         super();
315         setDescription(JUnitMessages.JUnitPreferencePage_description);
316         setPreferenceStore(JUnitPlugin.getDefault().getPreferenceStore());
317     }
318
319     protected Control createContents(Composite parent) {
320         PlatformUI.getWorkbench().getHelpSystem().setHelp(parent, IJUnitHelpContextIds.JUNIT_PREFERENCE_PAGE);
321
322         Composite composite= new Composite(parent, SWT.NULL);
323         GridLayout layout= new GridLayout();
324         layout.numColumns= 1;
325         layout.marginHeight= 0;
326         layout.marginWidth= 0;
327         composite.setLayout(layout);
328         GridData data= new GridData();
329         data.verticalAlignment= GridData.FILL;
330         data.horizontalAlignment= GridData.FILL;
331         composite.setLayoutData(data);
332
333         createEnableAssertionsCheckbox(composite);
334         createStackFilterPreferences(composite);
335         Dialog.applyDialogFont(composite);
336         return composite;
337     }
338
339     private void createEnableAssertionsCheckbox(Composite container) {
340         fEnableAssertionsCheckBox= new Button(container, SWT.CHECK | SWT.WRAP);
341         fEnableAssertionsCheckBox.setText(JUnitMessages.JUnitPreferencePage_enableassertionscheckbox_label);
342         fEnableAssertionsCheckBox.setToolTipText(JUnitMessages.JUnitPreferencePage_enableassertionscheckbox_tooltip);
343         GridData gd= getButtonGridData(fEnableAssertionsCheckBox);
344         fEnableAssertionsCheckBox.setLayoutData(gd);
345         SWTUtil.setButtonDimensionHint(fEnableAssertionsCheckBox);
346         setAssertionCheckBoxSelection(AssertionVMArg.getEnableAssertionsPreference());
347     }
348     
349     /**
350      * Programatic access to enable assertions checkbox
351      * @return boolean indicating check box selected or not
352      */

353     public boolean getAssertionCheckBoxSelection() {
354         return fEnableAssertionsCheckBox.getSelection();
355     }
356
357     public void setAssertionCheckBoxSelection(boolean selected) {
358         fEnableAssertionsCheckBox.setSelection(selected);
359     }
360
361     /*
362      * Create a group to contain the step filter related widgets
363      */

364     private void createStackFilterPreferences(Composite composite) {
365         fFilterViewerLabel= new Label(composite, SWT.SINGLE | SWT.LEFT);
366         fFilterViewerLabel.setText(JUnitMessages.JUnitPreferencePage_filter_label);
367         
368         Composite container= new Composite(composite, SWT.NONE);
369         GridLayout layout= new GridLayout();
370         layout.numColumns= 2;
371         layout.marginHeight= 0;
372         layout.marginWidth= 0;
373         container.setLayout(layout);
374         GridData gd= new GridData(GridData.FILL_BOTH);
375         container.setLayoutData(gd);
376
377         createFilterTable(container);
378         createStepFilterButtons(container);
379     }
380
381     private void createFilterTable(Composite container) {
382         TableLayoutComposite layouter= new TableLayoutComposite(container, SWT.NONE);
383         layouter.addColumnData(new ColumnWeightData(100));
384         layouter.setLayoutData(new GridData(GridData.FILL_BOTH));
385         
386         fFilterTable= new Table(layouter, SWT.CHECK | SWT.BORDER | SWT.MULTI | SWT.FULL_SELECTION);
387
388         new TableColumn(fFilterTable, SWT.NONE);
389         fFilterViewer= new CheckboxTableViewer(fFilterTable);
390         fTableEditor= new TableEditor(fFilterTable);
391         fFilterViewer.setLabelProvider(new FilterLabelProvider());
392         fFilterViewer.setComparator(new FilterViewerSorter());
393         fStackFilterContentProvider= new StackFilterContentProvider();
394         fFilterViewer.setContentProvider(fStackFilterContentProvider);
395         // input just needs to be non-null
396
fFilterViewer.setInput(this);
397         fFilterViewer.addCheckStateListener(new ICheckStateListener() {
398             public void checkStateChanged(CheckStateChangedEvent event) {
399                 Filter filter= (Filter) event.getElement();
400                 fStackFilterContentProvider.toggleFilter(filter);
401             }
402         });
403         fFilterViewer.addSelectionChangedListener(new ISelectionChangedListener() {
404             public void selectionChanged(SelectionChangedEvent event) {
405                 ISelection selection= event.getSelection();
406                 fRemoveFilterButton.setEnabled(!selection.isEmpty());
407             }
408         });
409     }
410
411     private void createStepFilterButtons(Composite container) {
412         Composite buttonContainer= new Composite(container, SWT.NONE);
413         GridData gd= new GridData(GridData.FILL_VERTICAL);
414         buttonContainer.setLayoutData(gd);
415         GridLayout buttonLayout= new GridLayout();
416         buttonLayout.numColumns= 1;
417         buttonLayout.marginHeight= 0;
418         buttonLayout.marginWidth= 0;
419         buttonContainer.setLayout(buttonLayout);
420
421         fAddFilterButton= new Button(buttonContainer, SWT.PUSH);
422         fAddFilterButton.setText(JUnitMessages.JUnitPreferencePage_addfilterbutton_label);
423         fAddFilterButton.setToolTipText(JUnitMessages.JUnitPreferencePage_addfilterbutton_tooltip);
424         gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
425         fAddFilterButton.setLayoutData(gd);
426         LayoutUtil.setButtonDimensionHint(fAddFilterButton);
427         fAddFilterButton.addListener(SWT.Selection, new Listener() {
428             public void handleEvent(Event e) {
429                 editFilter();
430             }
431         });
432
433         fAddTypeButton= new Button(buttonContainer, SWT.PUSH);
434         fAddTypeButton.setText(JUnitMessages.JUnitPreferencePage_addtypebutton_label);
435         fAddTypeButton.setToolTipText(JUnitMessages.JUnitPreferencePage_addtypebutton_tooltip);
436         gd= getButtonGridData(fAddTypeButton);
437         fAddTypeButton.setLayoutData(gd);
438         LayoutUtil.setButtonDimensionHint(fAddTypeButton);
439         fAddTypeButton.addListener(SWT.Selection, new Listener() {
440             public void handleEvent(Event e) {
441                 addType();
442             }
443         });
444
445         fAddPackageButton= new Button(buttonContainer, SWT.PUSH);
446         fAddPackageButton.setText(JUnitMessages.JUnitPreferencePage_addpackagebutton_label);
447         fAddPackageButton.setToolTipText(JUnitMessages.JUnitPreferencePage_addpackagebutton_tooltip);
448         gd= getButtonGridData(fAddPackageButton);
449         fAddPackageButton.setLayoutData(gd);
450         SWTUtil.setButtonDimensionHint(fAddPackageButton);
451         fAddPackageButton.addListener(SWT.Selection, new Listener() {
452             public void handleEvent(Event e) {
453                 addPackage();
454             }
455         });
456
457         fRemoveFilterButton= new Button(buttonContainer, SWT.PUSH);
458         fRemoveFilterButton.setText(JUnitMessages.JUnitPreferencePage_removefilterbutton_label);
459         fRemoveFilterButton.setToolTipText(JUnitMessages.JUnitPreferencePage_removefilterbutton_tooltip);
460         gd= getButtonGridData(fRemoveFilterButton);
461         fRemoveFilterButton.setLayoutData(gd);
462         SWTUtil.setButtonDimensionHint(fRemoveFilterButton);
463         fRemoveFilterButton.addListener(SWT.Selection, new Listener() {
464             public void handleEvent(Event e) {
465                 removeFilters();
466             }
467         });
468         fRemoveFilterButton.setEnabled(false);
469
470         fEnableAllButton= new Button(buttonContainer, SWT.PUSH);
471         fEnableAllButton.setText(JUnitMessages.JUnitPreferencePage_enableallbutton_label);
472         fEnableAllButton.setToolTipText(JUnitMessages.JUnitPreferencePage_enableallbutton_tooltip);
473         gd= getButtonGridData(fEnableAllButton);
474         fEnableAllButton.setLayoutData(gd);
475         SWTUtil.setButtonDimensionHint(fEnableAllButton);
476         fEnableAllButton.addListener(SWT.Selection, new Listener() {
477             public void handleEvent(Event e) {
478                 checkAllFilters(true);
479             }
480         });
481
482         fDisableAllButton= new Button(buttonContainer, SWT.PUSH);
483         fDisableAllButton.setText(JUnitMessages.JUnitPreferencePage_disableallbutton_label);
484         fDisableAllButton.setToolTipText(JUnitMessages.JUnitPreferencePage_disableallbutton_tooltip);
485         gd= getButtonGridData(fDisableAllButton);
486         fDisableAllButton.setLayoutData(gd);
487         SWTUtil.setButtonDimensionHint(fDisableAllButton);
488         fDisableAllButton.addListener(SWT.Selection, new Listener() {
489             public void handleEvent(Event e) {
490                 checkAllFilters(false);
491             }
492         });
493
494     }
495
496     private GridData getButtonGridData(Button button) {
497         GridData gd= new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_BEGINNING);
498         int widthHint= convertHorizontalDLUsToPixels(IDialogConstants.BUTTON_WIDTH);
499         gd.widthHint= Math.max(widthHint, button.computeSize(SWT.DEFAULT, SWT.DEFAULT, true).x);
500         return gd;
501     }
502
503     public void init(IWorkbench workbench) {}
504     
505     /**
506      * Create a new filter in the table (with the default 'new filter' value),
507      * then open up an in-place editor on it.
508      */

509     private void editFilter() {
510         // if a previous edit is still in progress, finish it
511
if (fEditorText != null)
512             validateChangeAndCleanup();
513
514         fNewStackFilter= fStackFilterContentProvider.addFilter(DEFAULT_NEW_FILTER_TEXT, true);
515         fNewTableItem= fFilterTable.getItem(0);
516
517         // create & configure Text widget for editor
518
// Fix for bug 1766. Border behavior on for text fields varies per platform.
519
// On Motif, you always get a border, on other platforms,
520
// you don't. Specifying a border on Motif results in the characters
521
// getting pushed down so that only there very tops are visible. Thus,
522
// we have to specify different style constants for the different platforms.
523
int textStyles= SWT.SINGLE | SWT.LEFT;
524         if (!SWT.getPlatform().equals("motif")) //$NON-NLS-1$
525
textStyles |= SWT.BORDER;
526
527         fEditorText= new Text(fFilterTable, textStyles);
528         GridData gd= new GridData(GridData.FILL_BOTH);
529         fEditorText.setLayoutData(gd);
530
531         // set the editor
532
fTableEditor.horizontalAlignment= SWT.LEFT;
533         fTableEditor.grabHorizontal= true;
534         fTableEditor.setEditor(fEditorText, fNewTableItem, 0);
535
536         // get the editor ready to use
537
fEditorText.setText(fNewStackFilter.getName());
538         fEditorText.selectAll();
539         setEditorListeners(fEditorText);
540         fEditorText.setFocus();
541     }
542
543     private void setEditorListeners(Text text) {
544         // CR means commit the changes, ESC means abort and don't commit
545
text.addKeyListener(new KeyAdapter() {
546             public void keyReleased(KeyEvent event) {
547                 if (event.character == SWT.CR) {
548                     if (fInvalidEditorText != null) {
549                         fEditorText.setText(fInvalidEditorText);
550                         fInvalidEditorText= null;
551                     } else
552                         validateChangeAndCleanup();
553                 } else if (event.character == SWT.ESC) {
554                     removeNewFilter();
555                     cleanupEditor();
556                 }
557             }
558         });
559         // Consider loss of focus on the editor to mean the same as CR
560
text.addFocusListener(new FocusAdapter() {
561             public void focusLost(FocusEvent event) {
562                 if (fInvalidEditorText != null) {
563                     fEditorText.setText(fInvalidEditorText);
564                     fInvalidEditorText= null;
565                 } else
566                     validateChangeAndCleanup();
567             }
568         });
569         // Consume traversal events from the text widget so that CR doesn't
570
// traverse away to dialog's default button. Without this, hitting
571
// CR in the text field closes the entire dialog.
572
text.addListener(SWT.Traverse, new Listener() {
573             public void handleEvent(Event event) {
574                 event.doit= false;
575             }
576         });
577     }
578
579     private void validateChangeAndCleanup() {
580         String JavaDoc trimmedValue= fEditorText.getText().trim();
581         // if the new value is blank, remove the filter
582
if (trimmedValue.length() < 1)
583             removeNewFilter();
584
585         // if it's invalid, beep and leave sitting in the editor
586
else if (!validateEditorInput(trimmedValue)) {
587             fInvalidEditorText= trimmedValue;
588             fEditorText.setText(JUnitMessages.JUnitPreferencePage_invalidstepfilterreturnescape);
589             getShell().getDisplay().beep();
590             return;
591             // otherwise, commit the new value if not a duplicate
592
} else {
593             Object JavaDoc[] filters= fStackFilterContentProvider.getElements(null);
594             for (int i= 0; i < filters.length; i++) {
595                 Filter filter= (Filter) filters[i];
596                 if (filter.getName().equals(trimmedValue)) {
597                     removeNewFilter();
598                     cleanupEditor();
599                     return;
600                 }
601             }
602             fNewTableItem.setText(trimmedValue);
603             fNewStackFilter.setName(trimmedValue);
604             fFilterViewer.refresh();
605         }
606         cleanupEditor();
607     }
608
609     /*
610      * Cleanup all widgets & resources used by the in-place editing
611      */

612     private void cleanupEditor() {
613         if (fEditorText == null)
614             return;
615
616         fNewStackFilter= null;
617         fNewTableItem= null;
618         fTableEditor.setEditor(null, null, 0);
619         fEditorText.dispose();
620         fEditorText= null;
621     }
622
623     private void removeNewFilter() {
624         fStackFilterContentProvider.removeFilters(new Object JavaDoc[] { fNewStackFilter });
625     }
626
627     /*
628      * A valid step filter is simply one that is a valid Java identifier.
629      * and, as defined in the JDI spec, the regular expressions used for
630      * step filtering must be limited to exact matches or patterns that
631      * begin with '*' or end with '*'. Beyond this, a string cannot be validated
632      * as corresponding to an existing type or package (and this is probably not
633      * even desirable).
634      */

635     private boolean validateEditorInput(String JavaDoc trimmedValue) {
636         char firstChar= trimmedValue.charAt(0);
637         if ((!(Character.isJavaIdentifierStart(firstChar)) || (firstChar == '*')))
638             return false;
639
640         int length= trimmedValue.length();
641         for (int i= 1; i < length; i++) {
642             char c= trimmedValue.charAt(i);
643             if (!Character.isJavaIdentifierPart(c)) {
644                 if (c == '.' && i != (length - 1))
645                     continue;
646                 if (c == '*' && i == (length - 1))
647                     continue;
648
649                 return false;
650             }
651         }
652         return true;
653     }
654
655     private void addType() {
656         Shell shell= getShell();
657         SelectionDialog dialog= null;
658         try {
659             dialog=
660                 JavaUI.createTypeDialog(
661                     shell,
662                     PlatformUI.getWorkbench().getProgressService(),
663                     SearchEngine.createWorkspaceScope(),
664                     IJavaElementSearchConstants.CONSIDER_CLASSES,
665                     false);
666         } catch (JavaModelException jme) {
667             String JavaDoc title= JUnitMessages.JUnitPreferencePage_addtypedialog_title;
668             String JavaDoc message= JUnitMessages.JUnitPreferencePage_addtypedialog_error_message;
669             ExceptionHandler.handle(jme, shell, title, message);
670             return;
671         }
672
673         dialog.setTitle(JUnitMessages.JUnitPreferencePage_addtypedialog_title);
674         dialog.setMessage(JUnitMessages.JUnitPreferencePage_addtypedialog_message);
675         if (dialog.open() == IDialogConstants.CANCEL_ID)
676             return;
677
678         Object JavaDoc[] types= dialog.getResult();
679         if (types != null && types.length > 0) {
680             IType type= (IType) types[0];
681             fStackFilterContentProvider.addFilter(type.getFullyQualifiedName('.'), true);
682         }
683     }
684
685     private void addPackage() {
686         Shell shell= getShell();
687         IProgressService context= PlatformUI.getWorkbench().getProgressService();
688         IJavaSearchScope createWorkspaceScope= SearchEngine.createWorkspaceScope();
689         SelectionDialog dialog= JavaUI.createPackageDialog(shell, context, createWorkspaceScope, true, true, ""); //$NON-NLS-1$
690
dialog.setTitle(JUnitMessages.JUnitPreferencePage_addpackagedialog_title);
691         dialog.setMessage(JUnitMessages.JUnitPreferencePage_addpackagedialog_message);
692         if (dialog.open() != Window.OK)
693             return;
694
695         Object JavaDoc[] packages= dialog.getResult();
696         if (packages == null)
697             return;
698
699         for (int i= 0; i < packages.length; i++) {
700             IJavaElement pkg= (IJavaElement) packages[i];
701
702             String JavaDoc filter= pkg.getElementName();
703             if (filter.length() < 1)
704                 filter= JUnitMessages.JUnitMainTab_label_defaultpackage;
705             else
706                 filter += ".*"; //$NON-NLS-1$
707

708             fStackFilterContentProvider.addFilter(filter, true);
709         }
710     }
711     private void removeFilters() {
712         IStructuredSelection selection= (IStructuredSelection) fFilterViewer.getSelection();
713         fStackFilterContentProvider.removeFilters(selection.toArray());
714     }
715
716     private void checkAllFilters(boolean check) {
717         Object JavaDoc[] filters= fStackFilterContentProvider.getElements(null);
718         for (int i= (filters.length - 1); i >= 0; --i)
719              ((Filter) filters[i]).setChecked(check);
720
721         fFilterViewer.setAllChecked(check);
722     }
723     
724     public boolean performOk() {
725         AssertionVMArg.setEnableAssertionsPreference(getAssertionCheckBoxSelection());
726         fStackFilterContentProvider.saveFilters();
727         return true;
728     }
729
730     protected void performDefaults() {
731         setDefaultValues();
732         super.performDefaults();
733     }
734
735     private void setDefaultValues() {
736         fEnableAssertionsCheckBox.setSelection(false);
737         fStackFilterContentProvider.setDefaults();
738     }
739
740     /**
741      * Returns a list of active stack filters.
742      *
743      * @return list
744      */

745     protected List JavaDoc createActiveStackFiltersList() {
746         return Arrays.asList(getFilterPatterns());
747     }
748
749     /**
750      * Returns a list of active stack filters.
751      *
752      * @return list
753      */

754     protected List JavaDoc createInactiveStackFiltersList() {
755         String JavaDoc[] strings=
756             JUnitPreferencePage.parseList(getPreferenceStore().getString(JUnitPreferencesConstants.PREF_INACTIVE_FILTERS_LIST));
757         return Arrays.asList(strings);
758     }
759
760     protected void updateActions() {
761         if (fEnableAllButton == null)
762             return;
763
764         boolean enabled= fFilterViewer.getTable().getItemCount() > 0;
765         fEnableAllButton.setEnabled(enabled);
766         fDisableAllButton.setEnabled(enabled);
767     }
768
769     public static String JavaDoc[] getFilterPatterns() {
770         IPreferenceStore store= JUnitPlugin.getDefault().getPreferenceStore();
771         return JUnitPreferencePage.parseList(store.getString(JUnitPreferencesConstants.PREF_ACTIVE_FILTERS_LIST));
772     }
773
774     public static boolean getFilterStack() {
775         IPreferenceStore store= JUnitPlugin.getDefault().getPreferenceStore();
776         return store.getBoolean(JUnitPreferencesConstants.DO_FILTER_STACK);
777     }
778
779     public static void setFilterStack(boolean filter) {
780         IPreferenceStore store= JUnitPlugin.getDefault().getPreferenceStore();
781         store.setValue(JUnitPreferencesConstants.DO_FILTER_STACK, filter);
782     }
783
784     /*
785      * Parses the comma separated string into an array of strings
786      */

787     private static String JavaDoc[] parseList(String JavaDoc listString) {
788         List JavaDoc list= new ArrayList JavaDoc(10);
789         StringTokenizer JavaDoc tokenizer= new StringTokenizer JavaDoc(listString, ","); //$NON-NLS-1$
790
while (tokenizer.hasMoreTokens())
791             list.add(tokenizer.nextToken());
792         return (String JavaDoc[]) list.toArray(new String JavaDoc[list.size()]);
793     }
794 }
795
Popular Tags