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 19346 - Dialog
011: * font should be activated and used by other components.
012: *******************************************************************************/package org.eclipse.ui.dialogs;
013:
014: import java.util.ArrayList;
015: import java.util.List;
016:
017: import org.eclipse.core.resources.IContainer;
018: import org.eclipse.core.runtime.IPath;
019: import org.eclipse.swt.SWT;
020: import org.eclipse.swt.layout.GridData;
021: import org.eclipse.swt.widgets.Composite;
022: import org.eclipse.swt.widgets.Control;
023: import org.eclipse.swt.widgets.Event;
024: import org.eclipse.swt.widgets.Label;
025: import org.eclipse.swt.widgets.Listener;
026: import org.eclipse.swt.widgets.Shell;
027: import org.eclipse.ui.PlatformUI;
028: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
029: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
030: import org.eclipse.ui.internal.ide.misc.ContainerSelectionGroup;
031:
032: /**
033: * A standard selection dialog which solicits a container resource from the user.
034: * The <code>getResult</code> method returns the selected container resource.
035: * <p>
036: * This class may be instantiated; it is not intended to be subclassed.
037: * </p>
038: * <p>
039: * Example:
040: * <pre>
041: * ContainerSelectionDialog dialog =
042: * new ContainerSelectionDialog(getShell(), initialSelection, allowNewContainerName(), msg);
043: * dialog.open();
044: * Object[] result = dialog.getResult();
045: * </pre>
046: * </p>
047: */
048: public class ContainerSelectionDialog extends SelectionDialog {
049: /**
050: *
051: */
052: private static final String EMPTY_STRING = ""; //$NON-NLS-1$
053:
054: // the widget group;
055: ContainerSelectionGroup group;
056:
057: // the root resource to populate the viewer with
058: private IContainer initialSelection;
059:
060: // allow the user to type in a new container name
061: private boolean allowNewContainerName = true;
062:
063: // the validation message
064: Label statusMessage;
065:
066: //for validating the selection
067: ISelectionValidator validator;
068:
069: // show closed projects by default
070: private boolean showClosedProjects = true;
071:
072: /**
073: * Creates a resource container selection dialog rooted at the given resource.
074: * All selections are considered valid.
075: *
076: * @param parentShell the parent shell
077: * @param initialRoot the initial selection in the tree
078: * @param allowNewContainerName <code>true</code> to enable the user to type in
079: * a new container name, and <code>false</code> to restrict the user to just
080: * selecting from existing ones
081: * @param message the message to be displayed at the top of this dialog, or
082: * <code>null</code> to display a default message
083: */
084: public ContainerSelectionDialog(Shell parentShell,
085: IContainer initialRoot, boolean allowNewContainerName,
086: String message) {
087: super (parentShell);
088: setTitle(IDEWorkbenchMessages.ContainerSelectionDialog_title);
089: this .initialSelection = initialRoot;
090: this .allowNewContainerName = allowNewContainerName;
091: if (message != null) {
092: setMessage(message);
093: } else {
094: setMessage(IDEWorkbenchMessages.ContainerSelectionDialog_message);
095: }
096: }
097:
098: /* (non-Javadoc)
099: * Method declared in Window.
100: */
101: protected void configureShell(Shell shell) {
102: super .configureShell(shell);
103: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
104: IIDEHelpContextIds.CONTAINER_SELECTION_DIALOG);
105: }
106:
107: /* (non-Javadoc)
108: * Method declared on Dialog.
109: */
110: protected Control createDialogArea(Composite parent) {
111: // create composite
112: Composite area = (Composite) super .createDialogArea(parent);
113:
114: Listener listener = new Listener() {
115: public void handleEvent(Event event) {
116: if (statusMessage != null && validator != null) {
117: String errorMsg = validator.isValid(group
118: .getContainerFullPath());
119: if (errorMsg == null
120: || errorMsg.equals(EMPTY_STRING)) {
121: statusMessage.setText(EMPTY_STRING);
122: getOkButton().setEnabled(true);
123: } else {
124: statusMessage.setText(errorMsg);
125: getOkButton().setEnabled(false);
126: }
127: }
128: }
129: };
130:
131: // container selection group
132: group = new ContainerSelectionGroup(area, listener,
133: allowNewContainerName, getMessage(), showClosedProjects);
134: if (initialSelection != null) {
135: group.setSelectedContainer(initialSelection);
136: }
137:
138: statusMessage = new Label(area, SWT.WRAP);
139: statusMessage.setLayoutData(new GridData(
140: GridData.FILL_HORIZONTAL));
141: statusMessage.setText(" \n "); //$NON-NLS-1$
142: statusMessage.setFont(parent.getFont());
143:
144: return dialogArea;
145: }
146:
147: /**
148: * The <code>ContainerSelectionDialog</code> implementation of this
149: * <code>Dialog</code> method builds a list of the selected resource containers
150: * for later retrieval by the client and closes this dialog.
151: */
152: protected void okPressed() {
153:
154: List chosenContainerPathList = new ArrayList();
155: IPath returnValue = group.getContainerFullPath();
156: if (returnValue != null) {
157: chosenContainerPathList.add(returnValue);
158: }
159: setResult(chosenContainerPathList);
160: super .okPressed();
161: }
162:
163: /**
164: * Sets the validator to use.
165: *
166: * @param validator A selection validator
167: */
168: public void setValidator(ISelectionValidator validator) {
169: this .validator = validator;
170: }
171:
172: /**
173: * Set whether or not closed projects should be shown
174: * in the selection dialog.
175: *
176: * @param show Whether or not to show closed projects.
177: */
178: public void showClosedProjects(boolean show) {
179: this.showClosedProjects = show;
180: }
181: }
|