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