001: /*******************************************************************************
002: * Copyright (c) 2000, 2007 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.pde.internal.ui.parts;
011:
012: import org.eclipse.jface.viewers.CheckboxTableViewer;
013: import org.eclipse.jface.viewers.StructuredViewer;
014: import org.eclipse.osgi.util.NLS;
015: import org.eclipse.pde.internal.ui.PDEUIMessages;
016: import org.eclipse.pde.internal.ui.util.SWTUtil;
017: import org.eclipse.pde.internal.ui.wizards.ListUtil;
018: import org.eclipse.swt.SWT;
019: import org.eclipse.swt.layout.GridData;
020: import org.eclipse.swt.widgets.Button;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Label;
023: import org.eclipse.ui.forms.widgets.FormToolkit;
024:
025: public class WizardCheckboxTablePart extends CheckboxTablePart {
026: private int selectAllIndex = -1;
027: private int deselectAllIndex = -1;
028: private String tableName;
029: private int counter;
030: private Label counterLabel;
031:
032: /**
033: * Constructor for WizardCheckboxTablePart.
034: * @param buttonLabels
035: */
036: public WizardCheckboxTablePart(String tableName,
037: String[] buttonLabels) {
038: super (buttonLabels);
039: this .tableName = tableName;
040: }
041:
042: public WizardCheckboxTablePart(String mainLabel) {
043: this (mainLabel, new String[] {
044: PDEUIMessages.WizardCheckboxTablePart_selectAll,
045: PDEUIMessages.WizardCheckboxTablePart_deselectAll });
046: setSelectAllIndex(0);
047: setDeselectAllIndex(1);
048: }
049:
050: public void setSelectAllIndex(int index) {
051: this .selectAllIndex = index;
052: }
053:
054: public void setDeselectAllIndex(int index) {
055: this .deselectAllIndex = index;
056: }
057:
058: protected void buttonSelected(Button button, int index) {
059: if (index == selectAllIndex) {
060: handleSelectAll(true);
061: }
062: if (index == deselectAllIndex) {
063: handleSelectAll(false);
064: }
065: }
066:
067: public Object[] getSelection() {
068: CheckboxTableViewer viewer = getTableViewer();
069: return viewer.getCheckedElements();
070: }
071:
072: public void setSelection(Object[] selected) {
073: CheckboxTableViewer viewer = getTableViewer();
074: viewer.setCheckedElements(selected);
075: updateCounter(viewer.getCheckedElements().length);
076: }
077:
078: public void createControl(Composite parent) {
079: createControl(parent, 2);
080: }
081:
082: public void createControl(Composite parent, int span) {
083: createControl(parent, SWT.NULL, span, null);
084: counterLabel = new Label(parent, SWT.NULL);
085: GridData gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING
086: | GridData.HORIZONTAL_ALIGN_FILL);
087: gd.horizontalSpan = span;
088: counterLabel.setLayoutData(gd);
089: updateCounter(0);
090: }
091:
092: protected Button createButton(Composite parent, String label,
093: int index, FormToolkit toolkit) {
094: Button button = super .createButton(parent, label, index,
095: toolkit);
096: SWTUtil.setButtonDimensionHint(button);
097: return button;
098: }
099:
100: protected StructuredViewer createStructuredViewer(Composite parent,
101: int style, FormToolkit toolkit) {
102: StructuredViewer viewer = super .createStructuredViewer(parent,
103: style, toolkit);
104: viewer.setComparator(ListUtil.NAME_COMPARATOR);
105: return viewer;
106: }
107:
108: protected void createMainLabel(Composite parent, int span,
109: FormToolkit toolkit) {
110: if (tableName == null)
111: return;
112: Label label = new Label(parent, SWT.NULL);
113: label.setText(tableName);
114: GridData gd = new GridData();
115: gd.horizontalSpan = span;
116: label.setLayoutData(gd);
117: }
118:
119: protected void updateCounter(int amount) {
120: counter = amount;
121: updateCounterLabel();
122: }
123:
124: protected void updateCounterLabel() {
125: String number = "" + getSelectionCount(); //$NON-NLS-1$
126: String totalNumber = "" + getTotalCount(); //$NON-NLS-1$
127: String message = NLS.bind(
128: PDEUIMessages.WizardCheckboxTablePart_counter,
129: (new String[] { number, totalNumber }));
130: counterLabel.setText(message);
131: }
132:
133: public int getSelectionCount() {
134: return counter;
135: }
136:
137: public void selectAll(boolean select) {
138: handleSelectAll(select);
139: }
140:
141: private int getTotalCount() {
142: CheckboxTableViewer viewer = getTableViewer();
143: return viewer.getTable().getItemCount();
144: }
145:
146: protected void handleSelectAll(boolean select) {
147: CheckboxTableViewer viewer = getTableViewer();
148: viewer.setAllChecked(select);
149: int selected;
150: if (!select) {
151: selected = 0;
152: } else {
153: selected = getTotalCount();
154: }
155: updateCounter(selected);
156: }
157:
158: protected void elementChecked(Object element, boolean checked) {
159: int count = getSelectionCount();
160: updateCounter(checked ? count + 1 : count - 1);
161: }
162:
163: public Label getCounterLabel() {
164: return counterLabel;
165: }
166: }
|