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;
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;
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.
35 public class CheckedListDialogField extends ListDialogField {
37 private int fCheckAllButtonIndex;
39 private int fUncheckAllButtonIndex;
41 private List fCheckElements;
43 public CheckedListDialogField(IListAdapter adapter,
44 String[] customButtonLabels, ILabelProvider lprovider) {
45 super(adapter, customButtonLabels, lprovider);
46 fCheckElements = new ArrayList();
48 fCheckAllButtonIndex = -1;
49 fUncheckAllButtonIndex = -1;
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
58 // public void setCheckAllButtonIndex(int checkButtonIndex) {
59 // Assert.isTrue(checkButtonIndex < fButtonLabels.length);
60 // fCheckAllButtonIndex = checkButtonIndex;
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
69 public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
70 Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
71 fUncheckAllButtonIndex = uncheckButtonIndex;
75 * @see ListDialogField#createTableViewer
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);
89 * @see ListDialogField#getListControl
91 public Control getListControl(Composite parent) {
92 Control control = super.getListControl(parent);
94 ((CheckboxTableViewer) fTable).setCheckedElements(fCheckElements
101 * @see DialogField#dialogFieldChanged Hooks in to get element changes to
102 * update check model.
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);
110 super.dialogFieldChanged();
113 private void checkStateChanged() {
114 // call super and do not update check model
115 super.dialogFieldChanged();
119 * Gets the checked elements.
121 public List getCheckedElements() {
122 return new ArrayList(fCheckElements);
126 * Returns true if the element is checked.
128 // public boolean isChecked(Object obj) {
129 // return fCheckElements.contains(obj);
133 * Sets the checked elements.
135 public void setCheckedElements(List list) {
136 fCheckElements = new ArrayList(list);
137 if (fTable != null) {
138 ((CheckboxTableViewer) fTable).setCheckedElements(list.toArray());
144 * Sets the checked state of an element.
146 public void setChecked(Object object, boolean state) {
147 setCheckedWithoutUpdate(object, state);
152 * Sets the checked state of an element. No dialog changed listener is
155 public void setCheckedWithoutUpdate(Object object, boolean state) {
156 if (!fCheckElements.contains(object)) {
157 fCheckElements.add(object);
159 if (fTable != null) {
160 ((CheckboxTableViewer) fTable).setChecked(object, state);
165 * Sets the check state of all elements
167 public void checkAll(boolean state) {
169 fCheckElements = getElements();
171 fCheckElements.clear();
173 if (fTable != null) {
174 ((CheckboxTableViewer) fTable).setAllChecked(state);
179 private void doCheckStateChanged(CheckStateChangedEvent e) {
180 if (e.getChecked()) {
181 fCheckElements.add(e.getElement());
183 fCheckElements.remove(e.getElement());
188 // ------ enable / disable management
191 * @see ListDialogField#getManagedButtonState
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();
199 return super.getManagedButtonState(sel, index);
203 * @see ListDialogField#extraButtonPressed
205 protected boolean managedButtonPressed(int index) {
206 if (index == fCheckAllButtonIndex) {
208 } else if (index == fUncheckAllButtonIndex) {
211 return super.managedButtonPressed(index);