KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > jdt > internal > ui > wizards > dialogfields > CheckedListDialogField


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.ui.wizards.dialogfields;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Collection JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.core.runtime.Assert;
18
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.Composite;
21 import org.eclipse.swt.widgets.Control;
22 import org.eclipse.swt.widgets.Table;
23
24 import org.eclipse.jface.viewers.CheckStateChangedEvent;
25 import org.eclipse.jface.viewers.CheckboxTableViewer;
26 import org.eclipse.jface.viewers.ICheckStateListener;
27 import org.eclipse.jface.viewers.ILabelProvider;
28 import org.eclipse.jface.viewers.ISelection;
29 import org.eclipse.jface.viewers.TableViewer;
30
31 /**
32  * A list with check boxes and a button bar. Typical buttons are 'Check All' and 'Uncheck All'.
33  * List model is independent of widget creation.
34  * DialogFields controls are: Label, List and Composite containing buttons.
35  */

36 public class CheckedListDialogField extends ListDialogField {
37     
38     private int fCheckAllButtonIndex;
39     private int fUncheckAllButtonIndex;
40     
41     private List JavaDoc fCheckedElements;
42     private List JavaDoc fGrayedElements;
43
44     public CheckedListDialogField(IListAdapter adapter, String JavaDoc[] customButtonLabels, ILabelProvider lprovider) {
45         super(adapter, customButtonLabels, lprovider);
46         fCheckedElements= new ArrayList JavaDoc();
47         fGrayedElements= new ArrayList JavaDoc();
48         
49         fCheckAllButtonIndex= -1;
50         fUncheckAllButtonIndex= -1;
51     }
52
53     /**
54      * Sets the index of the 'check' button in the button label array passed in the constructor.
55      * The behavior of the button marked as the check button will then be handled internally.
56      * (enable state, button invocation behavior)
57      */

58     public void setCheckAllButtonIndex(int checkButtonIndex) {
59         Assert.isTrue(checkButtonIndex < fButtonLabels.length);
60         fCheckAllButtonIndex= checkButtonIndex;
61     }
62
63     /**
64      * Sets the index of the 'uncheck' button in the button label array passed in the constructor.
65      * The behavior of the button marked as the uncheck button will then be handled internally.
66      * (enable state, button invocation behavior)
67      */

68     public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
69         Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
70         fUncheckAllButtonIndex= uncheckButtonIndex;
71     }
72     
73
74     /*
75      * @see ListDialogField#createTableViewer
76      */

77     protected TableViewer createTableViewer(Composite parent) {
78         Table table= new Table(parent, SWT.CHECK + getListStyle());
79         table.setFont(parent.getFont());
80         CheckboxTableViewer tableViewer= new CheckboxTableViewer(table);
81         tableViewer.addCheckStateListener(new ICheckStateListener() {
82             public void checkStateChanged(CheckStateChangedEvent e) {
83                 doCheckStateChanged(e);
84             }
85         });
86         return tableViewer;
87     }
88     
89     
90     /*
91      * @see ListDialogField#getListControl
92      */

93     public Control getListControl(Composite parent) {
94         Control control= super.getListControl(parent);
95         if (parent != null) {
96             ((CheckboxTableViewer)fTable).setCheckedElements(fCheckedElements.toArray());
97             ((CheckboxTableViewer)fTable).setGrayedElements(fGrayedElements.toArray());
98         }
99         return control;
100     }
101     
102     /*
103      * @see DialogField#dialogFieldChanged
104      * Hooks in to get element changes to update check model.
105      */

106     public void dialogFieldChanged() {
107         for (int i= fCheckedElements.size() -1; i >= 0; i--) {
108             if (!fElements.contains(fCheckedElements.get(i))) {
109                 fCheckedElements.remove(i);
110             }
111         }
112         super.dialogFieldChanged();
113     }
114     
115     private void checkStateChanged() {
116         //call super and do not update check model
117
super.dialogFieldChanged();
118     }
119
120     /**
121      * Gets the checked elements.
122      */

123     public List JavaDoc getCheckedElements() {
124         if (isOkToUse(fTableControl)) {
125             // workaround for bug 53853
126
Object JavaDoc[] checked= ((CheckboxTableViewer) fTable).getCheckedElements();
127             ArrayList JavaDoc res= new ArrayList JavaDoc(checked.length);
128             for (int i= 0; i < checked.length; i++) {
129                 res.add(checked[i]);
130             }
131             return res;
132         }
133         
134         
135         return new ArrayList JavaDoc(fCheckedElements);
136     }
137     
138     /**
139      * Returns the number of checked elements.
140      */

141     public int getCheckedSize() {
142         return fCheckedElements.size();
143     }
144     
145     /**
146      * Returns true if the element is checked.
147      */

148     public boolean isChecked(Object JavaDoc obj) {
149         if (isOkToUse(fTableControl)) {
150             return ((CheckboxTableViewer) fTable).getChecked(obj);
151         }
152     
153         return fCheckedElements.contains(obj);
154     }
155     
156     public boolean isGrayed(Object JavaDoc obj) {
157         if (isOkToUse(fTableControl)) {
158             return ((CheckboxTableViewer) fTable).getGrayed(obj);
159         }
160     
161         return fGrayedElements.contains(obj);
162     }
163     
164     /**
165      * Sets the checked elements.
166      */

167     public void setCheckedElements(Collection JavaDoc list) {
168         fCheckedElements= new ArrayList JavaDoc(list);
169         if (isOkToUse(fTableControl)) {
170             ((CheckboxTableViewer)fTable).setCheckedElements(list.toArray());
171         }
172         checkStateChanged();
173     }
174
175     /**
176      * Sets the checked state of an element.
177      */

178     public void setChecked(Object JavaDoc object, boolean state) {
179         setCheckedWithoutUpdate(object, state);
180         checkStateChanged();
181     }
182     
183     /**
184      * Sets the checked state of an element. No dialog changed listener is informed.
185      */

186     public void setCheckedWithoutUpdate(Object JavaDoc object, boolean state) {
187         if (state) {
188             if (!fCheckedElements.contains(object)) {
189                 fCheckedElements.add(object);
190             }
191         } else {
192             fCheckedElements.remove(object);
193         }
194         if (isOkToUse(fTableControl)) {
195             ((CheckboxTableViewer)fTable).setChecked(object, state);
196         }
197     }
198     
199     public void setGrayedWithoutUpdate(Object JavaDoc object, boolean state) {
200         if (state) {
201             if (!fGrayedElements.contains(object)) {
202                 fGrayedElements.add(object);
203             }
204         } else {
205             fGrayedElements.remove(object);
206         }
207         if (isOkToUse(fTableControl)) {
208             ((CheckboxTableViewer)fTable).setGrayed(object, state);
209         }
210     }
211
212     /**
213      * Sets the check state of all elements
214      */

215     public void checkAll(boolean state) {
216         if (state) {
217             fCheckedElements= getElements();
218         } else {
219             fCheckedElements.clear();
220         }
221         if (isOkToUse(fTableControl)) {
222             ((CheckboxTableViewer)fTable).setAllChecked(state);
223         }
224         checkStateChanged();
225     }
226     
227             
228     private void doCheckStateChanged(CheckStateChangedEvent e) {
229         if (e.getChecked()) {
230             fCheckedElements.add(e.getElement());
231         } else {
232             fCheckedElements.remove(e.getElement());
233         }
234         checkStateChanged();
235     }
236     
237     /* (non-Javadoc)
238      * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField#replaceElement(java.lang.Object, java.lang.Object)
239      */

240     public void replaceElement(Object JavaDoc oldElement, Object JavaDoc newElement) throws IllegalArgumentException JavaDoc {
241         boolean wasChecked= isChecked(oldElement);
242         super.replaceElement(oldElement, newElement);
243         setChecked(newElement, wasChecked);
244     }
245     
246     // ------ enable / disable management
247

248     /*
249      * @see ListDialogField#getManagedButtonState
250      */

251     protected boolean getManagedButtonState(ISelection sel, int index) {
252         if (index == fCheckAllButtonIndex) {
253             return !fElements.isEmpty();
254         } else if (index == fUncheckAllButtonIndex) {
255             return !fElements.isEmpty();
256         }
257         return super.getManagedButtonState(sel, index);
258     }
259
260     /*
261      * @see ListDialogField#extraButtonPressed
262      */

263     protected boolean managedButtonPressed(int index) {
264         if (index == fCheckAllButtonIndex) {
265             checkAll(true);
266         } else if (index == fUncheckAllButtonIndex) {
267             checkAll(false);
268         } else {
269             return super.managedButtonPressed(index);
270         }
271         return true;
272     }
273     
274     public void refresh() {
275         super.refresh();
276         if (isOkToUse(fTableControl)) {
277             ((CheckboxTableViewer)fTable).setCheckedElements(fCheckedElements.toArray());
278             ((CheckboxTableViewer)fTable).setGrayedElements(fGrayedElements.toArray());
279         }
280     }
281     
282                 
283     
284     
285
286 }
287
Popular Tags