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: * Sebastian Davids <sdavids@gmx.de> - Fix for bug 90273 - [Dialogs]
011: * ListSelectionDialog dialog alignment
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016:
017: import org.eclipse.jface.dialogs.Dialog;
018: import org.eclipse.jface.dialogs.IDialogConstants;
019: import org.eclipse.jface.viewers.CheckboxTableViewer;
020: import org.eclipse.jface.viewers.ILabelProvider;
021: import org.eclipse.jface.viewers.IStructuredContentProvider;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.SelectionAdapter;
024: import org.eclipse.swt.events.SelectionEvent;
025: import org.eclipse.swt.events.SelectionListener;
026: import org.eclipse.swt.layout.GridData;
027: import org.eclipse.swt.layout.GridLayout;
028: import org.eclipse.swt.widgets.Button;
029: import org.eclipse.swt.widgets.Composite;
030: import org.eclipse.swt.widgets.Control;
031: import org.eclipse.swt.widgets.Shell;
032: import org.eclipse.ui.PlatformUI;
033: import org.eclipse.ui.internal.IWorkbenchHelpContextIds;
034: import org.eclipse.ui.internal.WorkbenchMessages;
035:
036: /**
037: * A standard dialog which solicits a list of selections from the user.
038: * This class is configured with an arbitrary data model represented by content
039: * and label provider objects. The <code>getResult</code> method returns the
040: * selected elements.
041: * <p>
042: * This class may be instantiated; it is not intended to be subclassed.
043: * </p>
044: * <p>
045: * Example:
046: * <pre>
047: * ListSelectionDialog dlg =
048: * new ListSelectionDialog(
049: * getShell(),
050: * input,
051: * new BaseWorkbenchContentProvider(),
052: * new WorkbenchLabelProvider(),
053: * "Select the resources to save:");
054: * dlg.setInitialSelections(dirtyEditors);
055: * dlg.setTitle("Save Resources");
056: * dlg.open();
057: * </pre>
058: * </p>
059: */
060: public class ListSelectionDialog extends SelectionDialog {
061: // the root element to populate the viewer with
062: private Object inputElement;
063:
064: // providers for populating this dialog
065: private ILabelProvider labelProvider;
066:
067: private IStructuredContentProvider contentProvider;
068:
069: // the visual selection widget group
070: CheckboxTableViewer listViewer;
071:
072: // sizing constants
073: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 250;
074:
075: private final static int SIZING_SELECTION_WIDGET_WIDTH = 300;
076:
077: /**
078: * Creates a list selection dialog.
079: *
080: * @param parentShell the parent shell
081: * @param input the root element to populate this dialog with
082: * @param contentProvider the content provider for navigating the model
083: * @param labelProvider the label provider for displaying model elements
084: * @param message the message to be displayed at the top of this dialog, or
085: * <code>null</code> to display a default message
086: */
087: public ListSelectionDialog(Shell parentShell, Object input,
088: IStructuredContentProvider contentProvider,
089: ILabelProvider labelProvider, String message) {
090: super (parentShell);
091: setTitle(WorkbenchMessages.ListSelection_title);
092: inputElement = input;
093: this .contentProvider = contentProvider;
094: this .labelProvider = labelProvider;
095: if (message != null) {
096: setMessage(message);
097: } else {
098: setMessage(WorkbenchMessages.ListSelection_message);
099: }
100: }
101:
102: /**
103: * Add the selection and deselection buttons to the dialog.
104: * @param composite org.eclipse.swt.widgets.Composite
105: */
106: private void addSelectionButtons(Composite composite) {
107: Composite buttonComposite = new Composite(composite, SWT.NONE);
108: GridLayout layout = new GridLayout();
109: layout.numColumns = 0;
110: layout.marginWidth = 0;
111: layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
112: buttonComposite.setLayout(layout);
113: buttonComposite.setLayoutData(new GridData(SWT.END, SWT.TOP,
114: 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,
128: false);
129:
130: listener = new SelectionAdapter() {
131: public void widgetSelected(SelectionEvent e) {
132: listViewer.setAllChecked(false);
133: }
134: };
135: deselectButton.addSelectionListener(listener);
136: }
137:
138: /**
139: * Visually checks the previously-specified elements in this dialog's list
140: * viewer.
141: */
142: private void checkInitialSelections() {
143: Iterator itemsToCheck = getInitialElementSelections()
144: .iterator();
145:
146: while (itemsToCheck.hasNext()) {
147: listViewer.setChecked(itemsToCheck.next(), true);
148: }
149: }
150:
151: /*
152: * (non-Javadoc)
153: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
154: */
155: protected void configureShell(Shell shell) {
156: super .configureShell(shell);
157: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
158: IWorkbenchHelpContextIds.LIST_SELECTION_DIALOG);
159: }
160:
161: /* (non-Javadoc)
162: * Method declared on Dialog.
163: */
164: protected Control createDialogArea(Composite parent) {
165: // page group
166: Composite composite = (Composite) super
167: .createDialogArea(parent);
168:
169: initializeDialogUnits(composite);
170:
171: createMessageArea(composite);
172:
173: listViewer = CheckboxTableViewer.newCheckList(composite,
174: SWT.BORDER);
175: GridData data = new GridData(GridData.FILL_BOTH);
176: data.heightHint = SIZING_SELECTION_WIDGET_HEIGHT;
177: data.widthHint = SIZING_SELECTION_WIDGET_WIDTH;
178: listViewer.getTable().setLayoutData(data);
179:
180: listViewer.setLabelProvider(labelProvider);
181: listViewer.setContentProvider(contentProvider);
182:
183: addSelectionButtons(composite);
184:
185: initializeViewer();
186:
187: // initialize page
188: if (!getInitialElementSelections().isEmpty()) {
189: checkInitialSelections();
190: }
191:
192: Dialog.applyDialogFont(composite);
193:
194: return composite;
195: }
196:
197: /**
198: * Returns the viewer used to show the list.
199: *
200: * @return the viewer, or <code>null</code> if not yet created
201: */
202: protected CheckboxTableViewer getViewer() {
203: return listViewer;
204: }
205:
206: /**
207: * Initializes this dialog's viewer after it has been laid out.
208: */
209: private void initializeViewer() {
210: listViewer.setInput(inputElement);
211: }
212:
213: /**
214: * The <code>ListSelectionDialog</code> implementation of this
215: * <code>Dialog</code> method builds a list of the selected elements for later
216: * retrieval by the client and closes this dialog.
217: */
218: protected void okPressed() {
219:
220: // Get the input children.
221: Object[] children = contentProvider.getElements(inputElement);
222:
223: // Build a list of selected children.
224: if (children != null) {
225: ArrayList list = new ArrayList();
226: for (int i = 0; i < children.length; ++i) {
227: Object element = children[i];
228: if (listViewer.getChecked(element)) {
229: list.add(element);
230: }
231: }
232: setResult(list);
233: }
234:
235: super.okPressed();
236: }
237: }
|