KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > ide > dialogs > PathVariableSelectionDialog


1 /*******************************************************************************
2  * Copyright (c) 2005, 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  *******************************************************************************/

11
12 package org.eclipse.ui.ide.dialogs;
13
14 import org.eclipse.core.filesystem.EFS;
15 import org.eclipse.core.filesystem.IFileInfo;
16 import org.eclipse.core.filesystem.IFileStore;
17 import org.eclipse.core.filesystem.URIUtil;
18 import org.eclipse.core.runtime.CoreException;
19 import org.eclipse.core.runtime.IPath;
20 import org.eclipse.core.runtime.Path;
21 import org.eclipse.jface.dialogs.ErrorDialog;
22 import org.eclipse.jface.dialogs.IDialogConstants;
23 import org.eclipse.jface.window.Window;
24 import org.eclipse.osgi.util.NLS;
25 import org.eclipse.swt.SWT;
26 import org.eclipse.swt.widgets.Button;
27 import org.eclipse.swt.widgets.Composite;
28 import org.eclipse.swt.widgets.Control;
29 import org.eclipse.swt.widgets.Event;
30 import org.eclipse.swt.widgets.Listener;
31 import org.eclipse.swt.widgets.Shell;
32 import org.eclipse.ui.PlatformUI;
33 import org.eclipse.ui.dialogs.SelectionDialog;
34 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
35 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
36 import org.eclipse.ui.internal.ide.dialogs.FileFolderSelectionDialog;
37 import org.eclipse.ui.internal.ide.dialogs.IDEResourceInfoUtils;
38 import org.eclipse.ui.internal.ide.dialogs.PathVariablesGroup;
39
40 /**
41  * A selection dialog which shows the path variables defined in the
42  * workspace.
43  * The <code>getResult</code> method returns the name(s) of the
44  * selected path variable(s).
45  * <p>
46  * This class may be instantiated; it is not intended to be subclassed.
47  * </p>
48  * <p>
49  * Example:
50  * <pre>
51  * PathVariableSelectionDialog dialog =
52  * new PathVariableSelectionDialog(getShell(), IResource.FOLDER);
53  * dialog.open();
54  * String[] result = (String[]) dialog.getResult();
55  * </pre>
56  * </p>
57  *
58  * @since 3.1
59  */

60 public final class PathVariableSelectionDialog extends SelectionDialog {
61     private static final int EXTEND_ID = IDialogConstants.CLIENT_ID + 1;
62
63     private PathVariablesGroup pathVariablesGroup;
64
65     private int variableType;
66
67     /**
68      * Creates a path variable selection dialog.
69      *
70      * @param parentShell the parent shell
71      * @param variableType the type of variables that are displayed in
72      * this dialog. <code>IResource.FILE</code> and/or <code>IResource.FOLDER</code>
73      * logically ORed together.
74      */

75     public PathVariableSelectionDialog(Shell parentShell, int variableType) {
76         super(parentShell);
77         setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_title);
78         this.variableType = variableType;
79         pathVariablesGroup = new PathVariablesGroup(false, variableType,
80                 new Listener() {
81                     /* (non-Javadoc)
82                      * @see org.eclipse.swt.widgets.Listener#handleEvent(org.eclipse.swt.widgets.Event)
83                      */

84                     public void handleEvent(Event event) {
85                         updateExtendButtonState();
86                     }
87                 });
88         setShellStyle(getShellStyle() | SWT.RESIZE);
89     }
90
91
92     /* (non-Javadoc)
93      * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
94      */

95     protected void buttonPressed(int buttonId) {
96         if (buttonId == EXTEND_ID) {
97             FileFolderSelectionDialog dialog = new FileFolderSelectionDialog(
98                     getShell(), false, variableType);
99             PathVariablesGroup.PathVariableElement selection = pathVariablesGroup
100                     .getSelection()[0];
101             dialog.setTitle(IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_title);
102             dialog.setMessage(NLS.bind(IDEWorkbenchMessages.PathVariableSelectionDialog_ExtensionDialog_description, selection.name));
103             //XXX This only works for variables that refer to local file system locations
104
try {
105                 dialog.setInput(EFS.getStore(URIUtil.toURI(selection.path)));
106             } catch (CoreException e) {
107                 ErrorDialog.openError(getShell(), null, null, e.getStatus());
108             }
109             if (dialog.open() == Window.OK
110                     && pathVariablesGroup.performOk()) {
111                 setExtensionResult(selection, (IFileStore) dialog.getResult()[0]);
112                 super.okPressed();
113             }
114         } else {
115             super.buttonPressed(buttonId);
116         }
117     }
118
119     /* (non-Javadoc)
120      * @see org.eclipse.jface.window.Window#configureShell(org.eclipse.swt.widgets.Shell)
121      */

122     protected void configureShell(Shell shell) {
123         super.configureShell(shell);
124         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
125                 IIDEHelpContextIds.PATH_VARIABLE_SELECTION_DIALOG);
126     }
127
128     /* (non-Javadoc)
129      * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(org.eclipse.swt.widgets.Composite)
130      */

131     protected void createButtonsForButtonBar(Composite parent) {
132         createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL,
133                 true);
134         createButton(parent, EXTEND_ID, IDEWorkbenchMessages.PathVariableSelectionDialog_extendButton, false);
135         createButton(parent, IDialogConstants.CANCEL_ID,
136                 IDialogConstants.CANCEL_LABEL, false);
137         updateExtendButtonState();
138     }
139
140     /* (non-Javadoc)
141      * @see org.eclipse.jface.dialogs.Dialog#createDialogArea(org.eclipse.swt.widgets.Composite)
142      */

143     protected Control createDialogArea(Composite parent) {
144         // create composite
145
Composite dialogArea = (Composite) super.createDialogArea(parent);
146
147         pathVariablesGroup.createContents(dialogArea);
148         return dialogArea;
149     }
150
151     
152     /* (non-Javadoc)
153      * @see org.eclipse.jface.window.Window#close()
154      */

155     public boolean close() {
156         pathVariablesGroup.dispose();
157         return super.close();
158     }
159
160   
161     /* (non-Javadoc)
162      * @see org.eclipse.jface.dialogs.Dialog#okPressed()
163      */

164     protected void okPressed() {
165         //Sets the dialog result to the selected path variable name(s).
166
if (pathVariablesGroup.performOk()) {
167             PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup
168                     .getSelection();
169             String JavaDoc[] variableNames = new String JavaDoc[selection.length];
170
171             for (int i = 0; i < selection.length; i++) {
172                 variableNames[i] = selection[i].name;
173             }
174             setSelectionResult(variableNames);
175         } else {
176             setSelectionResult(null);
177         }
178         super.okPressed();
179     }
180
181     /**
182      * Sets the dialog result to the concatenated variable name and extension.
183      *
184      * @param variable variable selected in the variables list and extended
185      * by <code>extensionFile</code>
186      * @param extensionFile file selected to extend the variable.
187      */

188     private void setExtensionResult(
189             PathVariablesGroup.PathVariableElement variable, IFileStore extensionFile) {
190         IPath extensionPath = new Path(extensionFile.toString());
191         int matchCount = extensionPath.matchingFirstSegments(variable.path);
192         IPath resultPath = new Path(variable.name);
193
194         extensionPath = extensionPath.removeFirstSegments(matchCount);
195         resultPath = resultPath.append(extensionPath);
196         setSelectionResult(new String JavaDoc[] { resultPath.toOSString() });
197     }
198
199     /**
200      * Updates the enabled state of the Extend button based on the
201      * current variable selection.
202      */

203     private void updateExtendButtonState() {
204         PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup
205                 .getSelection();
206         Button extendButton = getButton(EXTEND_ID);
207
208         if (extendButton == null) {
209             return;
210         }
211         if (selection.length == 1) {
212             IFileInfo info = IDEResourceInfoUtils.getFileInfo(selection[0].path);
213             if (info.exists() && info.isDirectory()) {
214                 extendButton.setEnabled(true);
215             } else {
216                 extendButton.setEnabled(false);
217             }
218                 
219         } else {
220             extendButton.setEnabled(false);
221         }
222     }
223
224 }
225
Popular Tags