KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > ListSelectionDialog


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  * Sebastian Davids <sdavids@gmx.de> - Fix for bug 90273 - [Dialogs]
11  * ListSelectionDialog dialog alignment
12  *******************************************************************************/

13 package org.eclipse.ui.dialogs;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17
18 import org.eclipse.jface.dialogs.Dialog;
19 import org.eclipse.jface.dialogs.IDialogConstants;
20 import org.eclipse.jface.viewers.CheckboxTableViewer;
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.swt.widgets.Control;
32 import org.eclipse.swt.widgets.Shell;
33 import org.eclipse.ui.PlatformUI;
34 import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
35 import org.eclipse.ui.internal.WorkbenchMessages;
36
37 /**
38  * A standard dialog which solicits a list of selections from the user.
39  * This class is configured with an arbitrary data model represented by content
40  * and label provider objects. The <code>getResult</code> method returns the
41  * selected elements.
42  * <p>
43  * This class may be instantiated; it is not intended to be subclassed.
44  * </p>
45  * <p>
46  * Example:
47  * <pre>
48  * ListSelectionDialog dlg =
49  * new ListSelectionDialog(
50  * getShell(),
51  * input,
52  * new BaseWorkbenchContentProvider(),
53  * new WorkbenchLabelProvider(),
54  * "Select the resources to save:");
55  * dlg.setInitialSelections(dirtyEditors);
56  * dlg.setTitle("Save Resources");
57  * dlg.open();
58  * </pre>
59  * </p>
60  */

61 public class ListSelectionDialog extends SelectionDialog {
62     // the root element to populate the viewer with
63
private Object JavaDoc inputElement;
64
65     // providers for populating this dialog
66
private ILabelProvider labelProvider;
67
68     private IStructuredContentProvider contentProvider;
69
70     // the visual selection widget group
71
CheckboxTableViewer listViewer;
72
73     // sizing constants
74
private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
75
76     private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
77
78     /**
79      * Creates a list selection dialog.
80      *
81      * @param parentShell the parent shell
82      * @param input the root element to populate this dialog with
83      * @param contentProvider the content provider for navigating the model
84      * @param labelProvider the label provider for displaying model elements
85      * @param message the message to be displayed at the top of this dialog, or
86      * <code>null</code> to display a default message
87      */

88     public ListSelectionDialog(Shell parentShell, Object JavaDoc input,
89             IStructuredContentProvider contentProvider,
90             ILabelProvider labelProvider, String JavaDoc message) {
91         super(parentShell);
92         setTitle(WorkbenchMessages.ListSelection_title);
93         inputElement = input;
94         this.contentProvider = contentProvider;
95         this.labelProvider = labelProvider;
96         if (message != null) {
97             setMessage(message);
98         } else {
99             setMessage(WorkbenchMessages.ListSelection_message);
100         }
101     }
102
103     /**
104      * Add the selection and deselection buttons to the dialog.
105      * @param composite org.eclipse.swt.widgets.Composite
106      */

107     private void addSelectionButtons(Composite composite) {
108         Composite buttonComposite = new Composite(composite, SWT.NONE);
109         GridLayout layout = new GridLayout();
110         layout.numColumns = 0;
111         layout.marginWidth = 0;
112         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
113         buttonComposite.setLayout(layout);
114         buttonComposite.setLayoutData(new GridData(SWT.END, SWT.TOP, true, false));
115
116         Button selectButton = createButton(buttonComposite,
117                 IDialogConstants.SELECT_ALL_ID, SELECT_ALL_TITLE, false);
118
119         SelectionListener listener = new SelectionAdapter() {
120             public void widgetSelected(SelectionEvent e) {
121                 listViewer.setAllChecked(true);
122             }
123         };
124         selectButton.addSelectionListener(listener);
125
126         Button deselectButton = createButton(buttonComposite,
127                 IDialogConstants.DESELECT_ALL_ID, DESELECT_ALL_TITLE, false);
128
129         listener = new SelectionAdapter() {
130             public void widgetSelected(SelectionEvent e) {
131                 listViewer.setAllChecked(false);
132             }
133         };
134         deselectButton.addSelectionListener(listener);
135     }
136
137     /**
138      * Visually checks the previously-specified elements in this dialog's list
139      * viewer.
140      */

141     private void checkInitialSelections() {
142         Iterator JavaDoc itemsToCheck = getInitialElementSelections().iterator();
143
144         while (itemsToCheck.hasNext()) {
145             listViewer.setChecked(itemsToCheck.next(), true);
146         }
147     }
148
149     /*
150      * (non-Javadoc)
151      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
152      */

153     protected void configureShell(Shell shell) {
154         super.configureShell(shell);
155         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
156                 IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
157     }
158
159     /* (non-Javadoc)
160      * Method declared on Dialog.
161      */

162     protected Control createDialogArea(Composite parent) {
163         // page group
164
Composite composite = (Composite) super.createDialogArea(parent);
165         
166         initializeDialogUnits(composite);
167         
168         createMessageArea(composite);
169
170         listViewer = CheckboxTableViewer.newCheckList(composite, SWT.BORDER);
171         GridData data = new GridData(GridData.FILL_BOTH);
172         data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
173         data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
174         listViewer.getTable().setLayoutData(data);
175
176         listViewer.setLabelProvider(labelProvider);
177         listViewer.setContentProvider(contentProvider);
178
179         addSelectionButtons(composite);
180
181         initializeViewer();
182
183         // initialize page
184
if (!getInitialElementSelections().isEmpty()) {
185             checkInitialSelections();
186         }
187
188         Dialog.applyDialogFont(composite);
189         
190         return composite;
191     }
192
193     /**
194      * Returns the viewer used to show the list.
195      *
196      * @return the viewer, or <code>null</code> if not yet created
197      */

198     protected CheckboxTableViewer getViewer() {
199         return listViewer;
200     }
201
202     /**
203      * Initializes this dialog's viewer after it has been laid out.
204      */

205     private void initializeViewer() {
206         listViewer.setInput(inputElement);
207     }
208
209     /**
210      * The <code>ListSelectionDialog</code> implementation of this
211      * <code>Dialog</code> method builds a list of the selected elements for later
212      * retrieval by the client and closes this dialog.
213      */

214     protected void okPressed() {
215
216         // Get the input children.
217
Object JavaDoc[] children = contentProvider.getElements(inputElement);
218
219         // Build a list of selected children.
220
if (children != null) {
221             ArrayList JavaDoc list = new ArrayList JavaDoc();
222             for (int i = 0; i < children.length; ++i) {
223                 Object JavaDoc element = children[i];
224                 if (listViewer.getChecked(element)) {
225                     list.add(element);
226                 }
227             }
228             setResult(list);
229         }
230
231         super.okPressed();
232     }
233 }
234
Popular Tags