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
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11 package net.sourceforge.phpdt.internal.ui.wizards.dialogfields;
13 import java.util.ArrayList;
14 import java.util.List;
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;
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.
33 public class CheckedListDialogField extends ListDialogField {
35 private int fCheckAllButtonIndex;
37 private int fUncheckAllButtonIndex;
39 private List fCheckElements;
41 public CheckedListDialogField(IListAdapter adapter,
42 String[] customButtonLabels, ILabelProvider lprovider) {
43 super(adapter, customButtonLabels, lprovider);
44 fCheckElements = new ArrayList();
46 fCheckAllButtonIndex = -1;
47 fUncheckAllButtonIndex = -1;
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
56 public void setCheckAllButtonIndex(int checkButtonIndex) {
57 Assert.isTrue(checkButtonIndex < fButtonLabels.length);
58 fCheckAllButtonIndex = checkButtonIndex;
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
67 public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
68 Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
69 fUncheckAllButtonIndex = uncheckButtonIndex;
73 * @see ListDialogField#createTableViewer
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);
87 * @see ListDialogField#getListControl
89 public Control getListControl(Composite parent) {
90 Control control = super.getListControl(parent);
92 ((CheckboxTableViewer) fTable).setCheckedElements(fCheckElements
99 * @see DialogField#dialogFieldChanged Hooks in to get element changes to
100 * update check model.
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);
108 super.dialogFieldChanged();
111 private void checkStateChanged() {
112 // call super and do not update check model
113 super.dialogFieldChanged();
117 * Gets the checked elements.
119 public List getCheckedElements() {
120 return new ArrayList(fCheckElements);
124 * Returns true if the element is checked.
126 public boolean isChecked(Object obj) {
127 return fCheckElements.contains(obj);
131 * Sets the checked elements.
133 public void setCheckedElements(List list) {
134 fCheckElements = new ArrayList(list);
135 if (fTable != null) {
136 ((CheckboxTableViewer) fTable).setCheckedElements(list.toArray());
142 * Sets the checked state of an element.
144 public void setChecked(Object object, boolean state) {
145 setCheckedWithoutUpdate(object, state);
150 * Sets the checked state of an element. No dialog changed listener is
153 public void setCheckedWithoutUpdate(Object object, boolean state) {
154 if (!fCheckElements.contains(object)) {
155 fCheckElements.add(object);
157 if (fTable != null) {
158 ((CheckboxTableViewer) fTable).setChecked(object, state);
163 * Sets the check state of all elements
165 public void checkAll(boolean state) {
167 fCheckElements = getElements();
169 fCheckElements.clear();
171 if (fTable != null) {
172 ((CheckboxTableViewer) fTable).setAllChecked(state);
177 private void doCheckStateChanged(CheckStateChangedEvent e) {
178 if (e.getChecked()) {
179 fCheckElements.add(e.getElement());
181 fCheckElements.remove(e.getElement());
186 // ------ enable / disable management
189 * @see ListDialogField#getManagedButtonState
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();
197 return super.getManagedButtonState(sel, index);
201 * @see ListDialogField#extraButtonPressed
203 protected boolean managedButtonPressed(int index) {
204 if (index == fCheckAllButtonIndex) {
206 } else if (index == fUncheckAllButtonIndex) {
209 return super.managedButtonPressed(index);