KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > pde > internal > ui > parts > WizardCheckboxTablePart


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.pde.internal.ui.parts;
12 import org.eclipse.jface.viewers.CheckboxTableViewer;
13 import org.eclipse.jface.viewers.StructuredViewer;
14 import org.eclipse.osgi.util.NLS;
15 import org.eclipse.pde.internal.ui.PDEUIMessages;
16 import org.eclipse.pde.internal.ui.util.SWTUtil;
17 import org.eclipse.pde.internal.ui.wizards.ListUtil;
18 import org.eclipse.swt.SWT;
19 import org.eclipse.swt.layout.GridData;
20 import org.eclipse.swt.widgets.Button;
21 import org.eclipse.swt.widgets.Composite;
22 import org.eclipse.swt.widgets.Label;
23 import org.eclipse.ui.forms.widgets.FormToolkit;
24
25
26 public class WizardCheckboxTablePart extends CheckboxTablePart {
27     private int selectAllIndex = -1;
28     private int deselectAllIndex = -1;
29     private String JavaDoc tableName;
30     private int counter;
31     private Label counterLabel;
32
33     /**
34      * Constructor for WizardCheckboxTablePart.
35      * @param buttonLabels
36      */

37     public WizardCheckboxTablePart(String JavaDoc tableName, String JavaDoc[] buttonLabels) {
38         super(buttonLabels);
39         this.tableName = tableName;
40     }
41
42     public WizardCheckboxTablePart(String JavaDoc mainLabel) {
43         this(
44             mainLabel,
45             new String JavaDoc[] {
46                 PDEUIMessages.WizardCheckboxTablePart_selectAll,
47                 PDEUIMessages.WizardCheckboxTablePart_deselectAll});
48         setSelectAllIndex(0);
49         setDeselectAllIndex(1);
50     }
51
52     public void setSelectAllIndex(int index) {
53         this.selectAllIndex = index;
54     }
55     public void setDeselectAllIndex(int index) {
56         this.deselectAllIndex = index;
57     }
58
59     protected void buttonSelected(Button button, int index) {
60         if (index == selectAllIndex) {
61             handleSelectAll(true);
62         }
63         if (index == deselectAllIndex) {
64             handleSelectAll(false);
65         }
66     }
67
68     public Object JavaDoc[] getSelection() {
69         CheckboxTableViewer viewer = getTableViewer();
70         return viewer.getCheckedElements();
71     }
72
73     public void setSelection(Object JavaDoc[] selected) {
74         CheckboxTableViewer viewer = getTableViewer();
75         viewer.setCheckedElements(selected);
76         updateCounter(viewer.getCheckedElements().length);
77     }
78
79     public void createControl(Composite parent) {
80         createControl(parent, 2);
81     }
82     
83     public void createControl(Composite parent, int span) {
84         createControl(parent, SWT.NULL, span, null);
85         counterLabel = new Label(parent, SWT.NULL);
86         GridData gd =
87             new GridData(
88                 GridData.VERTICAL_ALIGN_BEGINNING | GridData.HORIZONTAL_ALIGN_FILL);
89         gd.horizontalSpan = span;
90         counterLabel.setLayoutData(gd);
91         updateCounter(0);
92     }
93
94     protected Button createButton(
95         Composite parent,
96         String JavaDoc label,
97         int index,
98         FormToolkit toolkit) {
99         Button button = super.createButton(parent, label, index, toolkit);
100         SWTUtil.setButtonDimensionHint(button);
101         return button;
102     }
103
104     protected StructuredViewer createStructuredViewer(
105         Composite parent,
106         int style,
107         FormToolkit toolkit) {
108         StructuredViewer viewer = super.createStructuredViewer(parent, style, toolkit);
109         viewer.setComparator(ListUtil.NAME_COMPARATOR);
110         return viewer;
111     }
112
113     protected void createMainLabel(
114         Composite parent,
115         int span,
116         FormToolkit toolkit) {
117         if (tableName == null)
118             return;
119         Label label = new Label(parent, SWT.NULL);
120         label.setText(tableName);
121         GridData gd = new GridData();
122         gd.horizontalSpan = span;
123         label.setLayoutData(gd);
124     }
125
126     protected void updateCounter(int amount) {
127         counter = amount;
128         updateCounterLabel();
129     }
130
131     protected void updateCounterLabel() {
132         String JavaDoc number = "" + getSelectionCount(); //$NON-NLS-1$
133
String JavaDoc totalNumber = "" + getTotalCount(); //$NON-NLS-1$
134
String JavaDoc message =
135             NLS.bind(PDEUIMessages.WizardCheckboxTablePart_counter, (new String JavaDoc[] { number, totalNumber }));
136         counterLabel.setText(message);
137     }
138
139     public int getSelectionCount() {
140         return counter;
141     }
142
143     public void selectAll(boolean select) {
144         handleSelectAll(select);
145     }
146
147     private int getTotalCount() {
148         CheckboxTableViewer viewer = getTableViewer();
149         return viewer.getTable().getItemCount();
150     }
151
152     protected void handleSelectAll(boolean select) {
153         CheckboxTableViewer viewer = getTableViewer();
154         viewer.setAllChecked(select);
155         int selected;
156         if (!select) {
157             selected = 0;
158         } else {
159             selected = getTotalCount();
160         }
161         updateCounter(selected);
162     }
163     protected void elementChecked(Object JavaDoc element, boolean checked) {
164         int count = getSelectionCount();
165         updateCounter(checked ? count + 1 : count - 1);
166     }
167     
168     public Label getCounterLabel() {
169         return counterLabel;
170     }
171 }
172
Popular Tags