Changes:
[phpeclipse.git] / net.sourceforge.phpeclipse / src / net / sourceforge / phpdt / internal / ui / wizards / dialogfields / CheckedListDialogField.java
1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 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 net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
12
13 import java.util.ArrayList;
14 import java.util.List;
15
16 import org.eclipse.jface.util.Assert;
17 import org.eclipse.jface.viewers.CheckStateChangedEvent;
18 import org.eclipse.jface.viewers.CheckboxTableViewer;
19 import org.eclipse.jface.viewers.ICheckStateListener;
20 import org.eclipse.jface.viewers.ILabelProvider;
21 import org.eclipse.jface.viewers.ISelection;
22 import org.eclipse.jface.viewers.TableViewer;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.widgets.Composite;
25 import org.eclipse.swt.widgets.Control;
26 import org.eclipse.swt.widgets.Table;
27
28 /**
29  * A list with checkboxes and a button bar. Typical buttons are 'Check All' and 'Uncheck All'.
30  * List model is independend of widget creation.
31  * DialogFields controls are: Label, List and Composite containing buttons.
32  */
33 public class CheckedListDialogField extends ListDialogField {
34         
35         private int fCheckAllButtonIndex;
36         private int fUncheckAllButtonIndex;
37         
38         private List fCheckElements;
39
40         public CheckedListDialogField(IListAdapter adapter, String[] customButtonLabels, ILabelProvider lprovider) {
41                 super(adapter, customButtonLabels, lprovider);
42                 fCheckElements= new ArrayList();
43                 
44                 fCheckAllButtonIndex= -1;
45                 fUncheckAllButtonIndex= -1;
46         }
47
48         /**
49          * Sets the index of the 'check' button in the button label array passed in the constructor.
50          * The behaviour of the button marked as the check button will then be handled internally.
51          * (enable state, button invocation behaviour)
52          */     
53         public void setCheckAllButtonIndex(int checkButtonIndex) {
54                 Assert.isTrue(checkButtonIndex < fButtonLabels.length);
55                 fCheckAllButtonIndex= checkButtonIndex;
56         }
57
58         /**
59          * Sets the index of the 'uncheck' button in the button label array passed in the constructor.
60          * The behaviour of the button marked as the uncheck button will then be handled internally.
61          * (enable state, button invocation behaviour)
62          */     
63         public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
64                 Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
65                 fUncheckAllButtonIndex= uncheckButtonIndex;
66         }
67         
68
69         /*
70          * @see ListDialogField#createTableViewer
71          */
72         protected TableViewer createTableViewer(Composite parent) {
73                 Table table= new Table(parent, SWT.CHECK + getListStyle());
74                 CheckboxTableViewer tableViewer= new CheckboxTableViewer(table);
75                 tableViewer.addCheckStateListener(new ICheckStateListener() {
76                         public void checkStateChanged(CheckStateChangedEvent e) {
77                                 doCheckStateChanged(e);
78                         }
79                 });
80                 return tableViewer;
81         }               
82         
83         
84         /*
85          * @see ListDialogField#getListControl
86          */
87         public Control getListControl(Composite parent) {
88                 Control control= super.getListControl(parent);
89                 if (parent != null) {
90                         ((CheckboxTableViewer)fTable).setCheckedElements(fCheckElements.toArray());
91                 }
92                 return control;
93         }       
94         
95         /*
96          * @see DialogField#dialogFieldChanged
97          * Hooks in to get element changes to update check model.
98          */
99         public void dialogFieldChanged() {
100                 for (int i= fCheckElements.size() -1; i >= 0; i--) {
101                         if (!fElements.contains(fCheckElements.get(i))) {
102                                 fCheckElements.remove(i);
103                         }
104                 }
105                 super.dialogFieldChanged();
106         }       
107         
108         private void checkStateChanged() {
109                 //call super and do not update check model
110                 super.dialogFieldChanged();
111         }               
112
113         /**
114          * Gets the checked elements.
115          */
116         public List getCheckedElements() {
117                 return new ArrayList(fCheckElements);
118         }
119         
120         /**
121          * Returns true if the element is checked.
122          */
123         public boolean isChecked(Object obj) {
124                 return fCheckElements.contains(obj);
125         }       
126         
127         /**
128          * Sets the checked elements.
129          */     
130         public void setCheckedElements(List list) {
131                 fCheckElements= new ArrayList(list);
132                 if (fTable != null) {
133                         ((CheckboxTableViewer)fTable).setCheckedElements(list.toArray());
134                 }
135                 checkStateChanged();
136         }
137
138         /**
139          * Sets the checked state of an element.
140          */             
141         public void setChecked(Object object, boolean state) {
142                 setCheckedWithoutUpdate(object, state);
143                 checkStateChanged();
144         }
145         
146         /**
147          * Sets the checked state of an element. No dialog changed listener is informed.
148          */             
149         public void setCheckedWithoutUpdate(Object object, boolean state) {
150                 if (!fCheckElements.contains(object)) {
151                         fCheckElements.add(object);
152                 }
153                 if (fTable != null) {
154                         ((CheckboxTableViewer)fTable).setChecked(object, state);
155                 }
156         }
157
158         /**
159          * Sets the check state of all elements
160          */     
161         public void checkAll(boolean state) {
162                 if (state) {
163                         fCheckElements= getElements();
164                 } else {
165                         fCheckElements.clear();
166                 }
167                 if (fTable != null) {
168                         ((CheckboxTableViewer)fTable).setAllChecked(state);
169                 }
170                 checkStateChanged();
171         }
172         
173                         
174         private void doCheckStateChanged(CheckStateChangedEvent e) {
175                 if (e.getChecked()) {
176                         fCheckElements.add(e.getElement());
177                 } else {
178                         fCheckElements.remove(e.getElement());
179                 }               
180                 checkStateChanged();
181         }
182         
183         // ------ enable / disable management
184         
185         /*
186          * @see ListDialogField#getManagedButtonState
187          */
188         protected boolean getManagedButtonState(ISelection sel, int index) {
189                 if (index == fCheckAllButtonIndex) {
190                         return !fElements.isEmpty();
191                 } else if (index == fUncheckAllButtonIndex) {
192                         return !fElements.isEmpty();
193                 }
194                 return super.getManagedButtonState(sel, index);
195         }       
196
197         /*
198          * @see ListDialogField#extraButtonPressed
199          */     
200         protected boolean managedButtonPressed(int index) {
201                 if (index == fCheckAllButtonIndex) {
202                         checkAll(true);
203                 } else if (index == fUncheckAllButtonIndex) {
204                         checkAll(false);
205                 } else {
206                         return super.managedButtonPressed(index);
207                 }
208                 return true;
209         }
210         
211                                 
212         
213         
214
215 }