001: /*******************************************************************************
002: * Copyright (c) 2000, 2006 IBM Corporation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * IBM Corporation - initial API and implementation
010: *******************************************************************************/package org.eclipse.jdt.internal.ui.wizards.dialogfields;
011:
012: import java.util.ArrayList;
013: import java.util.Collection;
014: import java.util.List;
015:
016: import org.eclipse.core.runtime.Assert;
017:
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.widgets.Composite;
020: import org.eclipse.swt.widgets.Control;
021: import org.eclipse.swt.widgets.Table;
022:
023: import org.eclipse.jface.viewers.CheckStateChangedEvent;
024: import org.eclipse.jface.viewers.CheckboxTableViewer;
025: import org.eclipse.jface.viewers.ICheckStateListener;
026: import org.eclipse.jface.viewers.ILabelProvider;
027: import org.eclipse.jface.viewers.ISelection;
028: import org.eclipse.jface.viewers.TableViewer;
029:
030: /**
031: * A list with check boxes and a button bar. Typical buttons are 'Check All' and 'Uncheck All'.
032: * List model is independent of widget creation.
033: * DialogFields controls are: Label, List and Composite containing buttons.
034: */
035: public class CheckedListDialogField extends ListDialogField {
036:
037: private int fCheckAllButtonIndex;
038: private int fUncheckAllButtonIndex;
039:
040: private List fCheckedElements;
041: private List fGrayedElements;
042:
043: public CheckedListDialogField(IListAdapter adapter,
044: String[] customButtonLabels, ILabelProvider lprovider) {
045: super (adapter, customButtonLabels, lprovider);
046: fCheckedElements = new ArrayList();
047: fGrayedElements = new ArrayList();
048:
049: fCheckAllButtonIndex = -1;
050: fUncheckAllButtonIndex = -1;
051: }
052:
053: /**
054: * Sets the index of the 'check' button in the button label array passed in the constructor.
055: * The behavior of the button marked as the check button will then be handled internally.
056: * (enable state, button invocation behavior)
057: */
058: public void setCheckAllButtonIndex(int checkButtonIndex) {
059: Assert.isTrue(checkButtonIndex < fButtonLabels.length);
060: fCheckAllButtonIndex = checkButtonIndex;
061: }
062:
063: /**
064: * Sets the index of the 'uncheck' button in the button label array passed in the constructor.
065: * The behavior of the button marked as the uncheck button will then be handled internally.
066: * (enable state, button invocation behavior)
067: */
068: public void setUncheckAllButtonIndex(int uncheckButtonIndex) {
069: Assert.isTrue(uncheckButtonIndex < fButtonLabels.length);
070: fUncheckAllButtonIndex = uncheckButtonIndex;
071: }
072:
073: /*
074: * @see ListDialogField#createTableViewer
075: */
076: protected TableViewer createTableViewer(Composite parent) {
077: Table table = new Table(parent, SWT.CHECK + getListStyle());
078: table.setFont(parent.getFont());
079: CheckboxTableViewer tableViewer = new CheckboxTableViewer(table);
080: tableViewer.addCheckStateListener(new ICheckStateListener() {
081: public void checkStateChanged(CheckStateChangedEvent e) {
082: doCheckStateChanged(e);
083: }
084: });
085: return tableViewer;
086: }
087:
088: /*
089: * @see ListDialogField#getListControl
090: */
091: public Control getListControl(Composite parent) {
092: Control control = super .getListControl(parent);
093: if (parent != null) {
094: ((CheckboxTableViewer) fTable)
095: .setCheckedElements(fCheckedElements.toArray());
096: ((CheckboxTableViewer) fTable)
097: .setGrayedElements(fGrayedElements.toArray());
098: }
099: return control;
100: }
101:
102: /*
103: * @see DialogField#dialogFieldChanged
104: * Hooks in to get element changes to update check model.
105: */
106: public void dialogFieldChanged() {
107: for (int i = fCheckedElements.size() - 1; i >= 0; i--) {
108: if (!fElements.contains(fCheckedElements.get(i))) {
109: fCheckedElements.remove(i);
110: }
111: }
112: super .dialogFieldChanged();
113: }
114:
115: private void checkStateChanged() {
116: //call super and do not update check model
117: super .dialogFieldChanged();
118: }
119:
120: /**
121: * Gets the checked elements.
122: */
123: public List getCheckedElements() {
124: if (isOkToUse(fTableControl)) {
125: // workaround for bug 53853
126: Object[] checked = ((CheckboxTableViewer) fTable)
127: .getCheckedElements();
128: ArrayList res = new ArrayList(checked.length);
129: for (int i = 0; i < checked.length; i++) {
130: res.add(checked[i]);
131: }
132: return res;
133: }
134:
135: return new ArrayList(fCheckedElements);
136: }
137:
138: /**
139: * Returns the number of checked elements.
140: */
141: public int getCheckedSize() {
142: return fCheckedElements.size();
143: }
144:
145: /**
146: * Returns true if the element is checked.
147: */
148: public boolean isChecked(Object obj) {
149: if (isOkToUse(fTableControl)) {
150: return ((CheckboxTableViewer) fTable).getChecked(obj);
151: }
152:
153: return fCheckedElements.contains(obj);
154: }
155:
156: public boolean isGrayed(Object obj) {
157: if (isOkToUse(fTableControl)) {
158: return ((CheckboxTableViewer) fTable).getGrayed(obj);
159: }
160:
161: return fGrayedElements.contains(obj);
162: }
163:
164: /**
165: * Sets the checked elements.
166: */
167: public void setCheckedElements(Collection list) {
168: fCheckedElements = new ArrayList(list);
169: if (isOkToUse(fTableControl)) {
170: ((CheckboxTableViewer) fTable).setCheckedElements(list
171: .toArray());
172: }
173: checkStateChanged();
174: }
175:
176: /**
177: * Sets the checked state of an element.
178: */
179: public void setChecked(Object object, boolean state) {
180: setCheckedWithoutUpdate(object, state);
181: checkStateChanged();
182: }
183:
184: /**
185: * Sets the checked state of an element. No dialog changed listener is informed.
186: */
187: public void setCheckedWithoutUpdate(Object object, boolean state) {
188: if (state) {
189: if (!fCheckedElements.contains(object)) {
190: fCheckedElements.add(object);
191: }
192: } else {
193: fCheckedElements.remove(object);
194: }
195: if (isOkToUse(fTableControl)) {
196: ((CheckboxTableViewer) fTable).setChecked(object, state);
197: }
198: }
199:
200: public void setGrayedWithoutUpdate(Object object, boolean state) {
201: if (state) {
202: if (!fGrayedElements.contains(object)) {
203: fGrayedElements.add(object);
204: }
205: } else {
206: fGrayedElements.remove(object);
207: }
208: if (isOkToUse(fTableControl)) {
209: ((CheckboxTableViewer) fTable).setGrayed(object, state);
210: }
211: }
212:
213: /**
214: * Sets the check state of all elements
215: */
216: public void checkAll(boolean state) {
217: if (state) {
218: fCheckedElements = getElements();
219: } else {
220: fCheckedElements.clear();
221: }
222: if (isOkToUse(fTableControl)) {
223: ((CheckboxTableViewer) fTable).setAllChecked(state);
224: }
225: checkStateChanged();
226: }
227:
228: private void doCheckStateChanged(CheckStateChangedEvent e) {
229: if (e.getChecked()) {
230: fCheckedElements.add(e.getElement());
231: } else {
232: fCheckedElements.remove(e.getElement());
233: }
234: checkStateChanged();
235: }
236:
237: /* (non-Javadoc)
238: * @see org.eclipse.jdt.internal.ui.wizards.dialogfields.ListDialogField#replaceElement(java.lang.Object, java.lang.Object)
239: */
240: public void replaceElement(Object oldElement, Object newElement)
241: throws IllegalArgumentException {
242: boolean wasChecked = isChecked(oldElement);
243: super .replaceElement(oldElement, newElement);
244: setChecked(newElement, wasChecked);
245: }
246:
247: // ------ enable / disable management
248:
249: /*
250: * @see ListDialogField#getManagedButtonState
251: */
252: protected boolean getManagedButtonState(ISelection sel, int index) {
253: if (index == fCheckAllButtonIndex) {
254: return !fElements.isEmpty();
255: } else if (index == fUncheckAllButtonIndex) {
256: return !fElements.isEmpty();
257: }
258: return super .getManagedButtonState(sel, index);
259: }
260:
261: /*
262: * @see ListDialogField#extraButtonPressed
263: */
264: protected boolean managedButtonPressed(int index) {
265: if (index == fCheckAllButtonIndex) {
266: checkAll(true);
267: } else if (index == fUncheckAllButtonIndex) {
268: checkAll(false);
269: } else {
270: return super .managedButtonPressed(index);
271: }
272: return true;
273: }
274:
275: public void refresh() {
276: super .refresh();
277: if (isOkToUse(fTableControl)) {
278: ((CheckboxTableViewer) fTable)
279: .setCheckedElements(fCheckedElements.toArray());
280: ((CheckboxTableViewer) fTable)
281: .setGrayedElements(fGrayedElements.toArray());
282: }
283: }
284:
285: }
|