KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > team > internal > ccvs > ui > ListSelectionArea


1 /*******************************************************************************
2  * Copyright (c) 2000, 2006 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.team.internal.ccvs.ui;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Iterator JavaDoc;
15 import java.util.List JavaDoc;
16
17 import org.eclipse.jface.dialogs.Dialog;
18 import org.eclipse.jface.viewers.CheckStateChangedEvent;
19 import org.eclipse.jface.viewers.CheckboxTableViewer;
20 import org.eclipse.jface.viewers.ICheckStateListener;
21 import org.eclipse.jface.viewers.ILabelProvider;
22 import org.eclipse.jface.viewers.IStructuredContentProvider;
23 import org.eclipse.swt.SWT;
24 import org.eclipse.swt.events.SelectionAdapter;
25 import org.eclipse.swt.events.SelectionEvent;
26 import org.eclipse.swt.events.SelectionListener;
27 import org.eclipse.swt.layout.GridData;
28 import org.eclipse.swt.layout.GridLayout;
29 import org.eclipse.swt.widgets.Button;
30 import org.eclipse.swt.widgets.Composite;
31 import org.eclipse.team.internal.ui.dialogs.DialogArea;
32
33 /**
34  * Reusable area that provides a list to select from and a select all and
35  * deselect all button.
36  */

37 public class ListSelectionArea extends DialogArea {
38     private Object JavaDoc inputElement;
39     private IStructuredContentProvider contentProvider;
40     private ILabelProvider labelProvider;
41     private String JavaDoc message;
42     private List JavaDoc initialSelections;
43
44     // the visual selection widget group
45
private CheckboxTableViewer listViewer;
46     
47     private Object JavaDoc[] previousCheckedElements;
48
49     public static final String JavaDoc LIST_SELECTION = "ListSelection"; //$NON-NLS-1$
50

51     /**
52      * Constructor for ListSelectionArea.
53      * @param parentDialog
54      * @param settings
55      */

56     public ListSelectionArea(
57             Object JavaDoc input,
58             IStructuredContentProvider contentProvider,
59             ILabelProvider labelProvider,
60             String JavaDoc message) {
61         this.inputElement = input;
62         this.contentProvider = contentProvider;
63         this.labelProvider = labelProvider;
64         this.message = message;
65         this.initialSelections = new ArrayList JavaDoc();
66     }
67
68     /**
69      * @see org.eclipse.team.internal.ccvs.ui.DialogArea#createArea(org.eclipse.swt.widgets.Composite)
70      */

71     public void createArea(Composite parent) {
72
73         Dialog.applyDialogFont(parent);
74
75         final Composite composite = createComposite(parent, 1, true);
76         
77         initializeDialogUnits(composite);
78
79         if (message != null)
80             createWrappingLabel(composite, message, 1);
81
82         listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
83         GridData data = new GridData(SWT.FILL, SWT.FILL, true, true);
84         data.heightHint = 0; // It will expand to the size of the wizard page!
85
data.widthHint = 0;
86         listViewer.getTable().setLayoutData(data);
87
88         listViewer.setLabelProvider(labelProvider);
89         listViewer.setContentProvider(contentProvider);
90         
91         listViewer.addCheckStateListener(new ICheckStateListener() {
92             public void checkStateChanged(CheckStateChangedEvent event) {
93                 Object JavaDoc[] checkedElements = getViewer().getCheckedElements();
94                 firePropertyChangeChange(LIST_SELECTION, previousCheckedElements, checkedElements);
95                 previousCheckedElements = checkedElements;
96             }
97         });
98
99         addSelectionButtons(composite);
100
101         initializeViewer();
102
103         // initialize page
104
if (!getInitialElementSelections().isEmpty())
105             checkInitialSelections();
106     }
107     
108     /**
109      * Initializes this dialog's viewer after it has been laid out.
110      */

111     private void initializeViewer() {
112         listViewer.setInput(inputElement);
113     }
114     
115     /**
116      * Visually checks the previously-specified elements in this dialog's list
117      * viewer.
118      */

119     private void checkInitialSelections() {
120         Iterator JavaDoc itemsToCheck = getInitialElementSelections().iterator();
121
122         while (itemsToCheck.hasNext())
123             listViewer.setChecked(itemsToCheck.next(),true);
124     }
125     
126     /**
127      * Add the selection and deselection buttons to the dialog.
128      * @param composite org.eclipse.swt.widgets.Composite
129      */

130     private void addSelectionButtons(Composite composite) {
131         Composite buttonComposite = new Composite(composite, SWT.RIGHT);
132         buttonComposite.setLayout(new GridLayout(2, false));
133         buttonComposite.setData(new GridData(SWT.END, SWT.BEGINNING, true, false));
134
135         Button selectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_selectAll, GridData.HORIZONTAL_ALIGN_FILL);
136
137         SelectionListener listener = new SelectionAdapter() {
138             public void widgetSelected(SelectionEvent e) {
139                 listViewer.setAllChecked(true);
140             }
141         };
142         selectButton.addSelectionListener(listener);
143
144
145         Button deselectButton = createButton(buttonComposite, CVSUIMessages.ListSelectionArea_deselectAll, GridData.HORIZONTAL_ALIGN_FILL);
146
147         listener = new SelectionAdapter() {
148             public void widgetSelected(SelectionEvent e) {
149                 listViewer.setAllChecked(false);
150
151             }
152         };
153         deselectButton.addSelectionListener(listener);
154     }
155     
156     /**
157      * Returns the list of initial element selections.
158      * @return List
159      */

160     protected List JavaDoc getInitialElementSelections(){
161         return initialSelections;
162     }
163     
164     /**
165      * Returns the listViewer.
166      * @return CheckboxTableViewer
167      */

168     public CheckboxTableViewer getViewer() {
169         return listViewer;
170     }
171
172 }
173
Popular Tags