KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*******************************************************************************
2  * Copyright (c) 2000, 2003 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Common Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/cpl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11
12 package org.eclipse.ui.internal.ide.dialogs;
13
14 import java.io.File JavaDoc;
15
16 import org.eclipse.core.runtime.IPath;
17 import org.eclipse.core.runtime.Path;
18 import org.eclipse.jface.dialogs.IDialogConstants;
19 import org.eclipse.swt.SWT;
20 import org.eclipse.swt.widgets.*;
21 import org.eclipse.ui.dialogs.SelectionDialog;
22 import org.eclipse.ui.help.WorkbenchHelp;
23 import org.eclipse.ui.internal.ide.IHelpContextIds;
24 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
25
26 /**
27  * A selection dialog which shows the path variables defined in the
28  * workspace.
29  * The <code>getResult</code> method returns the name(s) of the
30  * selected path variable(s).
31  * <p>
32  * This class may be instantiated; it is not intended to be subclassed.
33  * </p>
34  * <p>
35  * Example:
36  * <pre>
37  * PathVariableSelectionDialog dialog =
38  * new PathVariableSelectionDialog(getShell(), IResource.FOLDER);
39  * dialog.open();
40  * String[] result = (String[]) dialog.getResult();
41  * </pre>
42  * </p>
43  *
44  * @since 2.1
45  */

46 public class PathVariableSelectionDialog extends SelectionDialog {
47     private static final int EXTEND_ID = IDialogConstants.CLIENT_ID + 1;
48     private PathVariablesGroup pathVariablesGroup;
49     private int variableType;
50
51 /**
52  * Creates a path variable selection dialog.
53  *
54  * @param parentShell the parent shell
55  * @param variableType the type of variables that are displayed in
56  * this dialog. <code>IResource.FILE</code> and/or <code>IResource.FOLDER</code>
57  * logically ORed together.
58  */

59 public PathVariableSelectionDialog(Shell parentShell, int variableType) {
60     super(parentShell);
61     setTitle(IDEWorkbenchMessages.getString("PathVariableSelectionDialog.title")); //$NON-NLS-1$
62
this.variableType = variableType;
63     pathVariablesGroup = new PathVariablesGroup(
64         false,
65         variableType,
66         new Listener() {
67             public void handleEvent(Event event) {
68                 updateExtendButtonState();
69             }
70         }
71     );
72     setShellStyle(getShellStyle() | SWT.RESIZE);
73 }
74 /**
75  * Handles an "Extend" button press.
76  *
77  * @see org.eclipse.jface.dialogs.Dialog#buttonPressed(int)
78  */

79 protected void buttonPressed(int buttonId) {
80     if (buttonId == EXTEND_ID) {
81         FileFolderSelectionDialog dialog = new FileFolderSelectionDialog(getShell(), false, variableType);
82         PathVariablesGroup.PathVariableElement selection = pathVariablesGroup.getSelection()[0];
83         dialog.setTitle(IDEWorkbenchMessages.getString("PathVariableSelectionDialog.ExtensionDialog.title")); //$NON-NLS-1$
84
dialog.setMessage(IDEWorkbenchMessages.format("PathVariableSelectionDialog.ExtensionDialog.description", new Object JavaDoc[] {selection.name})); //$NON-NLS-1$
85
dialog.setInput(selection.path.toFile());
86         if (dialog.open() == FileFolderSelectionDialog.OK && pathVariablesGroup.performOk()) {
87             setExtensionResult(selection, (File JavaDoc) dialog.getResult()[0]);
88             super.okPressed();
89         }
90     }
91     else
92         super.buttonPressed(buttonId);
93 }
94 /* (non-Javadoc)
95  * Method declared in Window.
96  */

97 protected void configureShell(Shell shell) {
98     super.configureShell(shell);
99     WorkbenchHelp.setHelp(shell, IHelpContextIds.PATH_VARIABLE_SELECTION_DIALOG);
100 }
101 /**
102  * Adds an Extend button in addition to OK, Cancel.
103  *
104  * @see org.eclipse.jface.dialogs.Dialog#createButtonsForButtonBar(Composite)
105  */

106 protected void createButtonsForButtonBar(Composite parent) {
107     createButton(parent, IDialogConstants.OK_ID, IDialogConstants.OK_LABEL, true);
108     createButton(parent, EXTEND_ID, IDEWorkbenchMessages.getString("PathVariableSelectionDialog.extendButton"), false);//$NON-NLS-1$
109
createButton(parent, IDialogConstants.CANCEL_ID, IDialogConstants.CANCEL_LABEL, false);
110     updateExtendButtonState();
111 }
112 /* (non-Javadoc)
113  * Method declared on Dialog.
114  */

115 protected Control createDialogArea(Composite parent) {
116     // create composite
117
Composite dialogArea = (Composite)super.createDialogArea(parent);
118
119     pathVariablesGroup.createContents(dialogArea);
120     return dialogArea;
121 }
122 /**
123  * Disposes the path variables group.
124  * @see org.eclipse.jface.window.Window#close()
125  */

126 public boolean close() {
127     pathVariablesGroup.dispose();
128     return super.close();
129 }
130 /**
131  * Sets the dialog result to the selected path variable name(s).
132  */

133 protected void okPressed() {
134     if (pathVariablesGroup.performOk()) {
135         PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup.getSelection();
136         String JavaDoc[] variableNames = new String JavaDoc[selection.length];
137         
138         for (int i = 0; i < selection.length; i++)
139             variableNames[i] = selection[i].name;
140         setSelectionResult(variableNames);
141     }
142     else {
143         setSelectionResult(null);
144     }
145     super.okPressed();
146 }
147 /**
148  * Sets the dialog result to the concatenated variable name and extension.
149  *
150  * @param variable variable selected in the variables list and extended
151  * by <code>extensionFile</code>
152  * @param extensionFile file selected to extend the variable.
153  */

154 private void setExtensionResult(PathVariablesGroup.PathVariableElement variable, File JavaDoc extensionFile) {
155     IPath extensionPath = new Path(extensionFile.getPath());
156     int matchCount = extensionPath.matchingFirstSegments(variable.path);
157     IPath resultPath = new Path(variable.name);
158         
159     extensionPath = extensionPath.removeFirstSegments(matchCount);
160     resultPath = resultPath.append(extensionPath);
161     setSelectionResult(new String JavaDoc[] {resultPath.toOSString()});
162 }
163 /**
164  * Updates the enabled state of the Extend button based on the
165  * current variable selection.
166  */

167 protected void updateExtendButtonState() {
168     PathVariablesGroup.PathVariableElement[] selection = pathVariablesGroup.getSelection();
169     Button extendButton = getButton(EXTEND_ID);
170     
171     if (extendButton == null)
172         return;
173     if (selection.length == 1) {
174         File JavaDoc file = selection[0].path.toFile();
175         if (file.exists() == false || file.isFile())
176             extendButton.setEnabled(false);
177         else
178             extendButton.setEnabled(true);
179     }
180     else
181         extendButton.setEnabled(false);
182 }
183
184 }
185
Popular Tags