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