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: *******************************************************************************/package org.eclipse.ui.dialogs;
011:
012: import java.util.ArrayList;
013: import java.util.Iterator;
014:
015: import org.eclipse.core.resources.IContainer;
016: import org.eclipse.core.resources.IResource;
017: import org.eclipse.core.runtime.CoreException;
018: import org.eclipse.core.runtime.IAdaptable;
019: import org.eclipse.jface.viewers.CheckStateChangedEvent;
020: import org.eclipse.jface.viewers.ICheckStateListener;
021: import org.eclipse.jface.viewers.ITreeContentProvider;
022: import org.eclipse.swt.SWT;
023: import org.eclipse.swt.events.ControlEvent;
024: import org.eclipse.swt.events.ControlListener;
025: import org.eclipse.swt.widgets.Composite;
026: import org.eclipse.swt.widgets.Control;
027: import org.eclipse.swt.widgets.Shell;
028: import org.eclipse.swt.widgets.TableColumn;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
031: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
032: import org.eclipse.ui.internal.ide.misc.CheckboxTreeAndListGroup;
033: import org.eclipse.ui.model.WorkbenchContentProvider;
034: import org.eclipse.ui.model.WorkbenchLabelProvider;
035:
036: /**
037: * A standard resource selection dialog which solicits a list of resources from
038: * the user. The <code>getResult</code> method returns the selected resources.
039: * <p>
040: * This class may be instantiated; it is not intended to be subclassed.
041: * </p>
042: * <p>
043: * Example:
044: * <pre>
045: * ResourceSelectionDialog dialog =
046: * new ResourceSelectionDialog(getShell(), rootResource, msg);
047: * dialog.setInitialSelections(selectedResources);
048: * dialog.open();
049: * return dialog.getResult();
050: * </pre>
051: * </p>
052: */
053: public class ResourceSelectionDialog extends SelectionDialog {
054: // the root element to populate the viewer with
055: private IAdaptable root;
056:
057: // the visual selection widget group
058: private CheckboxTreeAndListGroup selectionGroup;
059:
060: // constants
061: private final static int SIZING_SELECTION_WIDGET_WIDTH = 400;
062:
063: private final static int SIZING_SELECTION_WIDGET_HEIGHT = 300;
064:
065: /**
066: * Creates a resource selection dialog rooted at the given element.
067: *
068: * @param parentShell the parent shell
069: * @param rootElement the root element to populate this dialog with
070: * @param message the message to be displayed at the top of this dialog, or
071: * <code>null</code> to display a default message
072: */
073: public ResourceSelectionDialog(Shell parentShell,
074: IAdaptable rootElement, String message) {
075: super (parentShell);
076: setTitle(IDEWorkbenchMessages.ResourceSelectionDialog_title);
077: root = rootElement;
078: if (message != null) {
079: setMessage(message);
080: } else {
081: setMessage(IDEWorkbenchMessages.ResourceSelectionDialog_message);
082: }
083: }
084:
085: /**
086: * Visually checks the previously-specified elements in the container (left)
087: * portion of this dialog's resource selection viewer.
088: */
089: private void checkInitialSelections() {
090: Iterator itemsToCheck = getInitialElementSelections()
091: .iterator();
092:
093: while (itemsToCheck.hasNext()) {
094: IResource currentElement = (IResource) itemsToCheck.next();
095:
096: if (currentElement.getType() == IResource.FILE) {
097: selectionGroup.initialCheckListItem(currentElement);
098: } else {
099: selectionGroup.initialCheckTreeItem(currentElement);
100: }
101: }
102: }
103:
104: /**
105: * @param event the event
106: */
107: public void checkStateChanged(CheckStateChangedEvent event) {
108: getOkButton().setEnabled(
109: selectionGroup.getCheckedElementCount() > 0);
110: }
111:
112: /* (non-Javadoc)
113: * Method declared in Window.
114: */
115: protected void configureShell(Shell shell) {
116: super .configureShell(shell);
117: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
118: IIDEHelpContextIds.RESOURCE_SELECTION_DIALOG);
119: }
120:
121: public void create() {
122: super .create();
123: initializeDialog();
124: }
125:
126: /* (non-Javadoc)
127: * Method declared on Dialog.
128: */
129: protected Control createDialogArea(Composite parent) {
130: // page group
131: Composite composite = (Composite) super
132: .createDialogArea(parent);
133:
134: //create the input element, which has the root resource
135: //as its only child
136: ArrayList input = new ArrayList();
137: input.add(root);
138:
139: createMessageArea(composite);
140: selectionGroup = new CheckboxTreeAndListGroup(
141: composite,
142: input,
143: getResourceProvider(IResource.FOLDER
144: | IResource.PROJECT | IResource.ROOT),
145: WorkbenchLabelProvider
146: .getDecoratingWorkbenchLabelProvider(),
147: getResourceProvider(IResource.FILE),
148: WorkbenchLabelProvider
149: .getDecoratingWorkbenchLabelProvider(),
150: SWT.NONE,
151: // since this page has no other significantly-sized
152: // widgets we need to hardcode the combined widget's
153: // size, otherwise it will open too small
154: SIZING_SELECTION_WIDGET_WIDTH,
155: SIZING_SELECTION_WIDGET_HEIGHT);
156:
157: composite.addControlListener(new ControlListener() {
158: public void controlMoved(ControlEvent e) {
159: }
160:
161: public void controlResized(ControlEvent e) {
162: //Also try and reset the size of the columns as appropriate
163: TableColumn[] columns = selectionGroup.getListTable()
164: .getColumns();
165: for (int i = 0; i < columns.length; i++) {
166: columns[i].pack();
167: }
168: }
169: });
170:
171: return composite;
172: }
173:
174: /**
175: * Returns a content provider for <code>IResource</code>s that returns
176: * only children of the given resource type.
177: */
178: private ITreeContentProvider getResourceProvider(
179: final int resourceType) {
180: return new WorkbenchContentProvider() {
181: public Object[] getChildren(Object o) {
182: if (o instanceof IContainer) {
183: IResource[] members = null;
184: try {
185: members = ((IContainer) o).members();
186: } catch (CoreException e) {
187: //just return an empty set of children
188: return new Object[0];
189: }
190:
191: //filter out the desired resource types
192: ArrayList results = new ArrayList();
193: for (int i = 0; i < members.length; i++) {
194: //And the test bits with the resource types to see if they are what we want
195: if ((members[i].getType() & resourceType) > 0) {
196: results.add(members[i]);
197: }
198: }
199: return results.toArray();
200: }
201: //input element case
202: if (o instanceof ArrayList) {
203: return ((ArrayList) o).toArray();
204: }
205: return new Object[0];
206: }
207: };
208: }
209:
210: /**
211: * Initializes this dialog's controls.
212: */
213: private void initializeDialog() {
214: selectionGroup.addCheckStateListener(new ICheckStateListener() {
215: public void checkStateChanged(CheckStateChangedEvent event) {
216: getOkButton().setEnabled(
217: selectionGroup.getCheckedElementCount() > 0);
218: }
219: });
220:
221: if (getInitialElementSelections().isEmpty()) {
222: getOkButton().setEnabled(false);
223: } else {
224: checkInitialSelections();
225: }
226: }
227:
228: /**
229: * The <code>ResourceSelectionDialog</code> implementation of this
230: * <code>Dialog</code> method builds a list of the selected resources for later
231: * retrieval by the client and closes this dialog.
232: */
233: protected void okPressed() {
234: Iterator resultEnum = selectionGroup.getAllCheckedListItems();
235: ArrayList list = new ArrayList();
236: while (resultEnum.hasNext()) {
237: list.add(resultEnum.next());
238: }
239: setResult(list);
240: super.okPressed();
241: }
242: }
|