001: /*******************************************************************************
002: * Copyright (c) 2005, 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.ide.dialogs;
011:
012: import org.eclipse.core.filesystem.EFS;
013: import org.eclipse.core.filesystem.IFileInfo;
014: import org.eclipse.core.filesystem.IFileStore;
015: import org.eclipse.core.filesystem.URIUtil;
016: import org.eclipse.core.runtime.CoreException;
017: import org.eclipse.core.runtime.IPath;
018: import org.eclipse.core.runtime.Path;
019: import org.eclipse.jface.dialogs.ErrorDialog;
020: import org.eclipse.jface.dialogs.IDialogConstants;
021: import org.eclipse.jface.window.Window;
022: import org.eclipse.osgi.util.NLS;
023: import org.eclipse.swt.widgets.Button;
024: import org.eclipse.swt.widgets.Composite;
025: import org.eclipse.swt.widgets.Control;
026: import org.eclipse.swt.widgets.Event;
027: import org.eclipse.swt.widgets.Listener;
028: import org.eclipse.swt.widgets.Shell;
029: import org.eclipse.ui.PlatformUI;
030: import org.eclipse.ui.dialogs.SelectionDialog;
031: import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
032: import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
033: import org.eclipse.ui.internal.ide.dialogs.FileFolderSelectionDialog;
034: import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
035: import org.eclipse.ui.internal.ide.dialogs.PathVariablesGroup;
036:
037: /**
038: * A selection dialog which shows the path variables defined in the
039: * workspace.
040: * The <code>getResult</code> method returns the name(s) of the
041: * selected path variable(s).
042: * <p>
043: * This class may be instantiated; it is not intended to be subclassed.
044: * </p>
045: * <p>
046: * Example:
047: * <pre>
048: * PathVariableSelectionDialog dialog =
049: * new PathVariableSelectionDialog(getShell(), IResource.FOLDER);
050: * dialog.open();
051: * String[] result = (String[]) dialog.getResult();
052: * </pre>
053: * </p>
054: *
055: * @since 3.1
056: */
057: public final class PathVariableSelectionDialog extends SelectionDialog {
058: private static final int EXTEND_ID = IDialogConstants.CLIENT_ID + 1;
059:
060: private PathVariablesGroup pathVariablesGroup;
061:
062: private int variableType;
063:
064: /**
065: * Creates a path variable selection dialog.
066: *
067: * @param parentShell the parent shell
068: * @param variableType the type of variables that are displayed in
069: * this dialog. <code>IResource.FILE</code> and/or <code>IResource.FOLDER</code>
070: * logically ORed together.
071: */
072: public PathVariableSelectionDialog(Shell parentShell,
073: int variableType) {
074: super (parentShell);
075: setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_title);
076: this .variableType = variableType;
077: pathVariablesGroup = new PathVariablesGroup(false,
078: variableType, new Listener() {
079: /* (non-Javadoc)
080: * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
081: */
082: public void handleEvent(Event event) {
083: updateExtendButtonState();
084: }
085: });
086: }
087:
088: /* (non-Javadoc)
089: * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
090: */
091: protected void buttonPressed(int buttonId) {
092: if (buttonId == EXTEND_ID) {
093: FileFolderSelectionDialog dialog = new FileFolderSelectionDialog(
094: getShell(), false, variableType);
095: PathVariablesGroup.PathVariableElement selection = pathVariablesGroup
096: .getSelection()[0];
097: dialog
098: .setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_title);
099: dialog
100: .setMessage(NLS
101: .bind(
102: IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_description,
103: selection.name));
104: //XXX This only works for variables that refer to local file system locations
105: try {
106: dialog.setInput(EFS.getStore(URIUtil
107: .toURI(selection.path)));
108: } catch (CoreException e) {
109: ErrorDialog.openError(getShell(), null, null, e
110: .getStatus());
111: }
112: if (dialog.open() == Window.OK
113: && pathVariablesGroup.performOk()) {
114: setExtensionResult(selection, (IFileStore) dialog
115: .getResult()[0]);
116: super .okPressed();
117: }
118: } else {
119: super .buttonPressed(buttonId);
120: }
121: }
122:
123: /* (non-Javadoc)
124: * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
125: */
126: protected void configureShell(Shell shell) {
127: super .configureShell(shell);
128: PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
129: IIDEHelpContextIds.PATH_VARIABLE_SELECTION_DIALOG);
130: }
131:
132: /* (non-Javadoc)
133: * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
134: */
135: protected void createButtonsForButtonBar(Composite parent) {
136: createButton(parent, IDialogConstants.OK_ID,
137: IDialogConstants.OK_LABEL, true);
138: createButton(
139: parent,
140: EXTEND_ID,
141: IDEWorkbenchMessages.PathVariableSelectionDialog_extendButton,
142: false);
143: createButton(parent, IDialogConstants.CANCEL_ID,
144: IDialogConstants.CANCEL_LABEL, false);
145: updateExtendButtonState();
146: }
147:
148: /* (non-Javadoc)
149: * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
150: */
151: protected Control createDialogArea(Composite parent) {
152: // create composite
153: Composite dialogArea = (Composite) super
154: .createDialogArea(parent);
155:
156: pathVariablesGroup.createContents(dialogArea);
157: return dialogArea;
158: }
159:
160: /* (non-Javadoc)
161: * @see org.eclipse.jface.window.Window#close()
162: */
163: public boolean close() {
164: pathVariablesGroup.dispose();
165: return super .close();
166: }
167:
168: /* (non-Javadoc)
169: * @see org.eclipse.jface.dialogs.Dialog#okPressed()
170: */
171: protected void okPressed() {
172: //Sets the dialog result to the selected path variable name(s).
173: if (pathVariablesGroup.performOk()) {
174: PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup
175: .getSelection();
176: String[] variableNames = new String[selection.length];
177:
178: for (int i = 0; i < selection.length; i++) {
179: variableNames[i] = selection[i].name;
180: }
181: setSelectionResult(variableNames);
182: } else {
183: setSelectionResult(null);
184: }
185: super .okPressed();
186: }
187:
188: /**
189: * Sets the dialog result to the concatenated variable name and extension.
190: *
191: * @param variable variable selected in the variables list and extended
192: * by <code>extensionFile</code>
193: * @param extensionFile file selected to extend the variable.
194: */
195: private void setExtensionResult(
196: PathVariablesGroup.PathVariableElement variable,
197: IFileStore extensionFile) {
198: IPath extensionPath = new Path(extensionFile.toString());
199: int matchCount = extensionPath
200: .matchingFirstSegments(variable.path);
201: IPath resultPath = new Path(variable.name);
202:
203: extensionPath = extensionPath.removeFirstSegments(matchCount);
204: resultPath = resultPath.append(extensionPath);
205: setSelectionResult(new String[] { resultPath.toOSString() });
206: }
207:
208: /**
209: * Updates the enabled state of the Extend button based on the
210: * current variable selection.
211: */
212: private void updateExtendButtonState() {
213: PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup
214: .getSelection();
215: Button extendButton = getButton(EXTEND_ID);
216:
217: if (extendButton == null) {
218: return;
219: }
220: if (selection.length == 1) {
221: IFileInfo info = IDEResourceInfoUtils
222: .getFileInfo(selection[0].path);
223: if (info.exists() && info.isDirectory()) {
224: extendButton.setEnabled(true);
225: } else {
226: extendButton.setEnabled(false);
227: }
228:
229: } else {
230: extendButton.setEnabled(false);
231: }
232: }
233:
234: }
|