KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > junit > util > CheckedTableSelectionDialog


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  *******************************************************************************/

11 package org.eclipse.jdt.internal.junit.util;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.IStatus;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.custom.BusyIndicator;
21 import org.eclipse.swt.events.SelectionAdapter;
22 import org.eclipse.swt.events.SelectionEvent;
23 import org.eclipse.swt.events.SelectionListener;
24 import org.eclipse.swt.layout.GridData;
25 import org.eclipse.swt.layout.GridLayout;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Label;
30 import org.eclipse.swt.widgets.Shell;
31 import org.eclipse.swt.widgets.Table;
32
33 import org.eclipse.jface.dialogs.IDialogConstants;
34 import org.eclipse.jface.viewers.CheckStateChangedEvent;
35 import org.eclipse.jface.viewers.CheckboxTableViewer;
36 import org.eclipse.jface.viewers.ICheckStateListener;
37 import org.eclipse.jface.viewers.ILabelProvider;
38 import org.eclipse.jface.viewers.IStructuredContentProvider;
39 import org.eclipse.jface.viewers.ViewerFilter;
40
41 import org.eclipse.ui.dialogs.ISelectionStatusValidator;
42 import org.eclipse.ui.dialogs.SelectionStatusDialog;
43
44 import org.eclipse.jdt.internal.junit.wizards.WizardMessages;
45
46 /**
47  * A dialog with checked table viewer.
48  */

49 public class CheckedTableSelectionDialog extends SelectionStatusDialog {
50     
51     private CheckboxTableViewer fViewer;
52     
53     private ILabelProvider fLabelProvider;
54     private IStructuredContentProvider fContentProvider;
55     
56     private ISelectionStatusValidator fValidator= null;
57     private String JavaDoc fEmptyListMessage= WizardMessages.CheckedTableSelectionDialog_emptyListMessage;
58     
59     private IStatus fCurrStatus= new JUnitStatus();
60     private List JavaDoc fFilters;
61     private Object JavaDoc fInput;
62     private boolean fIsEmpty;
63     
64     private int fWidth= 40;
65     private int fHeight= 18;
66     
67     /**
68      * Constructs an instance of <code>ElementTreeSelectionDialog</code>.
69      * @param labelProvider the label provider to render the entries
70      * @param contentProvider the content provider to evaluate the tree structure
71      */

72     public CheckedTableSelectionDialog(Shell parent, ILabelProvider labelProvider,
73         IStructuredContentProvider contentProvider) {
74         super(parent);
75         
76         fLabelProvider= labelProvider;
77         fContentProvider= contentProvider;
78
79         setResult(new ArrayList JavaDoc(0));
80         setStatusLineAboveButtons(true);
81     }
82
83     /**
84      * Sets the initial selection.
85      * Convenience method.
86      * @param selection the initial selection.
87      */

88     public void setInitialSelection(Object JavaDoc selection) {
89         setInitialSelections(new Object JavaDoc[] {selection});
90     }
91
92     /**
93      * Sets the message to be displayed if the list is empty.
94      * @param message the message to be displayed.
95      */

96     public void setEmptyListMessage(String JavaDoc message) {
97         fEmptyListMessage= message;
98     }
99         
100     /**
101      * Adds a filter to the tree viewer.
102      * @param filter a filter.
103      */

104     public void addFilter(ViewerFilter filter) {
105         if (fFilters == null)
106             fFilters= new ArrayList JavaDoc(4);
107             
108         fFilters.add(filter);
109     }
110     
111     /**
112      * Sets an optional validator to check if the selection is valid.
113      * The validator is invoked whenever the selection changes.
114      * @param validator the validator to validate the selection.
115      */

116     public void setValidator(ISelectionStatusValidator validator) {
117         fValidator= validator;
118     }
119     
120     /**
121      * Sets the tree input.
122      * @param input the tree input.
123      */

124     public void setInput(Object JavaDoc input) {
125         fInput= input;
126     }
127
128     /**
129      * Sets the size of the tree in unit of characters.
130      * @param width the width of the tree.
131      * @param height the height of the tree.
132      */

133     public void setSize(int width, int height) {
134         fWidth= width;
135         fHeight= height;
136     }
137
138     protected void updateOKStatus() {
139         if (!fIsEmpty) {
140             if (fValidator != null) {
141                 fCurrStatus= fValidator.validate(fViewer.getCheckedElements());
142                 updateStatus(fCurrStatus);
143             } else if (!fCurrStatus.isOK()) {
144                 fCurrStatus= new JUnitStatus();
145             }
146         } else {
147             fCurrStatus= new JUnitStatus(IStatus.ERROR, fEmptyListMessage);
148         }
149         updateStatus(fCurrStatus);
150     }
151     
152     /*
153      * @see Window#open()
154      */

155     public int open() {
156         fIsEmpty= evaluateIfTableEmpty(fInput);
157         BusyIndicator.showWhile(null, new Runnable JavaDoc() {
158             public void run() {
159                 access$superOpen();
160             }
161         });
162         return getReturnCode();
163     }
164
165     private void access$superOpen() {
166         super.open();
167     }
168             
169     /**
170      * Handles cancel button pressed event.
171      */

172     protected void cancelPressed() {
173         setResult(null);
174         super.cancelPressed();
175     }
176
177     /*
178      * @see SelectionStatusDialog#computeResult()
179      */

180     protected void computeResult() {
181         setResult(Arrays.asList(fViewer.getCheckedElements()));
182     }
183      
184     /*
185      * @see Window#create()
186      */

187     public void create() {
188         super.create();
189
190         List JavaDoc initialSelections= getInitialElementSelections();
191         if (initialSelections.size() > 0) {
192             fViewer.setCheckedElements(initialSelections.toArray());
193         }
194             
195         updateOKStatus();
196     }
197     
198     /*
199      * @see Dialog#createDialogArea(Composite)
200      */

201     protected Control createDialogArea(Composite parent) {
202         Composite composite= (Composite) super.createDialogArea(parent);
203         
204         Label messageLabel= createMessageArea(composite);
205         Control treeWidget= createTableViewer(composite);
206         Control buttonComposite= createSelectionButtons(composite);
207
208         GridData data= new GridData(GridData.FILL_BOTH);
209         data.widthHint= convertWidthInCharsToPixels(fWidth);
210         data.heightHint= convertHeightInCharsToPixels(fHeight);
211         treeWidget.setLayoutData(data);
212         
213         if (fIsEmpty) {
214             messageLabel.setEnabled(false);
215             treeWidget.setEnabled(false);
216             buttonComposite.setEnabled(false);
217         }
218         applyDialogFont(composite);
219         return composite;
220     }
221     
222     private Table createTableViewer(Composite parent) {
223         fViewer= CheckboxTableViewer.newCheckList(parent, SWT.BORDER);
224             
225         fViewer.setContentProvider(fContentProvider);
226         fViewer.setLabelProvider(fLabelProvider);
227         fViewer.addCheckStateListener(new ICheckStateListener() {
228             public void checkStateChanged(CheckStateChangedEvent event) {
229                 updateOKStatus();
230             }
231         });
232         
233         if (fFilters != null) {
234             for (int i= 0; i != fFilters.size(); i++)
235                 fViewer.addFilter((ViewerFilter) fFilters.get(i));
236         }
237                 
238         fViewer.setInput(fInput);
239         return fViewer.getTable();
240     }
241         
242     /**
243      * Add the selection and deselection buttons to the dialog.
244      * @param composite org.eclipse.swt.widgets.Composite
245      */

246     private Composite createSelectionButtons(Composite composite) {
247         Composite buttonComposite= new Composite(composite, SWT.RIGHT);
248         GridLayout layout= new GridLayout();
249         layout.numColumns= 2;
250         buttonComposite.setLayout(layout);
251         GridData data= new GridData(GridData.HORIZONTAL_ALIGN_END | GridData.GRAB_HORIZONTAL);
252         data.grabExcessHorizontalSpace= true;
253         composite.setData(data);
254
255         Button selectButton= createButton(buttonComposite, IDialogConstants.SELECT_ALL_ID, WizardMessages.CheckedTableSelectionDialog_selectAll, false);
256
257         SelectionListener listener= new SelectionAdapter() {
258             public void widgetSelected(SelectionEvent e) {
259                 fViewer.setCheckedElements(fContentProvider.getElements(fInput));
260                 updateOKStatus();
261             }
262         };
263         selectButton.addSelectionListener(listener);
264
265         Button deselectButton= createButton(buttonComposite, IDialogConstants.DESELECT_ALL_ID, WizardMessages.CheckedTableSelectionDialog_deselectAll, false);
266
267         listener= new SelectionAdapter() {
268             public void widgetSelected(SelectionEvent e) {
269                 fViewer.setCheckedElements(new Object JavaDoc[0]);
270                 updateOKStatus();
271             }
272         };
273         deselectButton.addSelectionListener(listener);
274         return buttonComposite;
275     }
276     
277     private boolean evaluateIfTableEmpty(Object JavaDoc input) {
278         Object JavaDoc[] elements= fContentProvider.getElements(input);
279         if (elements.length > 0) {
280             if (fFilters != null) {
281                 for (int i= 0; i < fFilters.size(); i++) {
282                     ViewerFilter curr= (ViewerFilter)fFilters.get(i);
283                     elements= curr.filter(fViewer, input, elements);
284                 }
285             }
286         }
287         return elements.length == 0;
288     }
289 }
290
Popular Tags