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 'Uncheck All'.
30 * List model is independend of widget creation.
31 * DialogFields controls are: Label, List and Composite containing buttons.
33 public class CheckedListDialogField extends ListDialogField {
35 private int fCheckAllButtonIndex;
36 private int fUncheckAllButtonIndex;
38 private List fCheckElements;
40 public CheckedListDialogField(IListAdapter adapter, String[] customButtonLabels, ILabelProvider lprovider) {
41 super(adapter, customButtonLabels, lprovider);
42 fCheckElements= new ArrayList();
44 fCheckAllButtonIndex= -1;
45 fUncheckAllButtonIndex= -1;
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)
53 public void setCheckAllButtonIndex(int checkButtonIndex) {
54 Assert.isTrue(checkButtonIndex < fButtonLabels.length);
55 fCheckAllButtonIndex= checkButtonIndex;
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)
63 public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
64 Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
65 fUncheckAllButtonIndex= uncheckButtonIndex;
70 * @see ListDialogField#createTableViewer
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);
85 * @see ListDialogField#getListControl
87 public Control getListControl(Composite parent) {
88 Control control= super.getListControl(parent);
90 ((CheckboxTableViewer)fTable).setCheckedElements(fCheckElements.toArray());
96 * @see DialogField#dialogFieldChanged
97 * Hooks in to get element changes to update check model.
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);
105 super.dialogFieldChanged();
108 private void checkStateChanged() {
109 //call super and do not update check model
110 super.dialogFieldChanged();
114 * Gets the checked elements.
116 public List getCheckedElements() {
117 return new ArrayList(fCheckElements);
121 * Returns true if the element is checked.
123 public boolean isChecked(Object obj) {
124 return fCheckElements.contains(obj);
128 * Sets the checked elements.
130 public void setCheckedElements(List list) {
131 fCheckElements= new ArrayList(list);
132 if (fTable != null) {
133 ((CheckboxTableViewer)fTable).setCheckedElements(list.toArray());
139 * Sets the checked state of an element.
141 public void setChecked(Object object, boolean state) {
142 setCheckedWithoutUpdate(object, state);
147 * Sets the checked state of an element. No dialog changed listener is informed.
149 public void setCheckedWithoutUpdate(Object object, boolean state) {
150 if (!fCheckElements.contains(object)) {
151 fCheckElements.add(object);
153 if (fTable != null) {
154 ((CheckboxTableViewer)fTable).setChecked(object, state);
159 * Sets the check state of all elements
161 public void checkAll(boolean state) {
163 fCheckElements= getElements();
165 fCheckElements.clear();
167 if (fTable != null) {
168 ((CheckboxTableViewer)fTable).setAllChecked(state);
174 private void doCheckStateChanged(CheckStateChangedEvent e) {
175 if (e.getChecked()) {
176 fCheckElements.add(e.getElement());
178 fCheckElements.remove(e.getElement());
183 // ------ enable / disable management
186 * @see ListDialogField#getManagedButtonState
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();
194 return super.getManagedButtonState(sel, index);
198 * @see ListDialogField#extraButtonPressed
200 protected boolean managedButtonPressed(int index) {
201 if (index == fCheckAllButtonIndex) {
203 } else if (index == fUncheckAllButtonIndex) {
206 return super.managedButtonPressed(index);