KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > dialogs > SaveAsDialog


1 /*******************************************************************************
2  * Copyright (c) 2000, 2007 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  * Bob Foster <bob@objfac.com>
11  * - Fix for bug 23025 - SaveAsDialog should not assume what is being saved is an IFile
12  * Benjamin Muskalla <b.muskalla@gmx.net>
13  * - Fix for bug 82541 - [Dialogs] SaveAsDialog should better handle closed projects
14  *******************************************************************************/

15 package org.eclipse.ui.dialogs;
16
17 import org.eclipse.core.resources.IFile;
18 import org.eclipse.core.resources.IProject;
19 import org.eclipse.core.resources.IResource;
20 import org.eclipse.core.resources.IWorkspace;
21 import org.eclipse.core.resources.ResourcesPlugin;
22 import org.eclipse.core.runtime.IPath;
23 import org.eclipse.core.runtime.IStatus;
24 import org.eclipse.jface.dialogs.IDialogConstants;
25 import org.eclipse.jface.dialogs.IDialogSettings;
26 import org.eclipse.jface.dialogs.MessageDialog;
27 import org.eclipse.jface.dialogs.TitleAreaDialog;
28 import org.eclipse.osgi.util.NLS;
29 import org.eclipse.swt.SWT;
30 import org.eclipse.swt.graphics.Image;
31 import org.eclipse.swt.layout.GridData;
32 import org.eclipse.swt.layout.GridLayout;
33 import org.eclipse.swt.widgets.Button;
34 import org.eclipse.swt.widgets.Composite;
35 import org.eclipse.swt.widgets.Control;
36 import org.eclipse.swt.widgets.Event;
37 import org.eclipse.swt.widgets.Listener;
38 import org.eclipse.swt.widgets.Shell;
39 import org.eclipse.ui.PlatformUI;
40 import org.eclipse.ui.internal.ide.IDEInternalWorkbenchImages;
41 import org.eclipse.ui.internal.ide.IDEWorkbenchMessages;
42 import org.eclipse.ui.internal.ide.IDEWorkbenchPlugin;
43 import org.eclipse.ui.internal.ide.IIDEHelpContextIds;
44 import org.eclipse.ui.internal.ide.misc.ResourceAndContainerGroup;
45
46 /**
47  * A standard "Save As" dialog which solicits a path from the user. The
48  * <code>getResult</code> method returns the path. Note that the folder
49  * at the specified path might not exist and might need to be created.
50  * <p>
51  * This class may be instantiated; it is not intended to be subclassed.
52  * </p>
53  *
54  * @see org.eclipse.ui.dialogs.ContainerGenerator
55  */

56 public class SaveAsDialog extends TitleAreaDialog {
57     
58     private static final String JavaDoc DIALOG_SETTINGS_SECTION = "SaveAsDialogSettings"; //$NON-NLS-1$
59

60     private IFile originalFile = null;
61
62     private String JavaDoc originalName = null;
63
64     private IPath result;
65
66     // widgets
67
private ResourceAndContainerGroup resourceGroup;
68
69     private Button okButton;
70
71     /**
72      * Image for title area
73      */

74     private Image dlgTitleImage = null;
75
76     /**
77      * Creates a new Save As dialog for no specific file.
78      *
79      * @param parentShell the parent shell
80      */

81     public SaveAsDialog(Shell parentShell) {
82         super(parentShell);
83         setShellStyle(getShellStyle() | SWT.RESIZE);
84     }
85
86     /* (non-Javadoc)
87      * Method declared in Window.
88      */

89     protected void configureShell(Shell shell) {
90         super.configureShell(shell);
91         shell.setText(IDEWorkbenchMessages.SaveAsDialog_text);
92         PlatformUI.getWorkbench().getHelpSystem().setHelp(shell,
93                 IIDEHelpContextIds.SAVE_AS_DIALOG);
94     }
95
96     /* (non-Javadoc)
97      * Method declared in Window.
98      */

99     protected Control createContents(Composite parent) {
100
101         Control contents = super.createContents(parent);
102
103         initializeControls();
104         validatePage();
105         resourceGroup.setFocus();
106         setTitle(IDEWorkbenchMessages.SaveAsDialog_title);
107         dlgTitleImage = IDEInternalWorkbenchImages.getImageDescriptor(
108                 IDEInternalWorkbenchImages.IMG_DLGBAN_SAVEAS_DLG).createImage();
109         setTitleImage(dlgTitleImage);
110         setMessage(IDEWorkbenchMessages.SaveAsDialog_message);
111
112         return contents;
113     }
114
115     /**
116      * The <code>SaveAsDialog</code> implementation of this <code>Window</code>
117      * method disposes of the banner image when the dialog is closed.
118      */

119     public boolean close() {
120         if (dlgTitleImage != null) {
121             dlgTitleImage.dispose();
122         }
123         return super.close();
124     }
125
126     /* (non-Javadoc)
127      * Method declared on Dialog.
128      */

129     protected void createButtonsForButtonBar(Composite parent) {
130         okButton = createButton(parent, IDialogConstants.OK_ID,
131                 IDialogConstants.OK_LABEL, true);
132         createButton(parent, IDialogConstants.CANCEL_ID,
133                 IDialogConstants.CANCEL_LABEL, false);
134     }
135
136     /* (non-Javadoc)
137      * Method declared on Dialog.
138      */

139     protected Control createDialogArea(Composite parent) {
140         // top level composite
141
Composite parentComposite = (Composite) super.createDialogArea(parent);
142
143         // create a composite with standard margins and spacing
144
Composite composite = new Composite(parentComposite, SWT.NONE);
145         GridLayout layout = new GridLayout();
146         layout.marginHeight = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_MARGIN);
147         layout.marginWidth = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_MARGIN);
148         layout.verticalSpacing = convertVerticalDLUsToPixels(IDialogConstants.VERTICAL_SPACING);
149         layout.horizontalSpacing = convertHorizontalDLUsToPixels(IDialogConstants.HORIZONTAL_SPACING);
150         composite.setLayout(layout);
151         composite.setLayoutData(new GridData(GridData.FILL_BOTH));
152         composite.setFont(parentComposite.getFont());
153
154         Listener listener = new Listener() {
155             public void handleEvent(Event event) {
156                 setDialogComplete(validatePage());
157             }
158         };
159
160         resourceGroup = new ResourceAndContainerGroup(
161                 composite,
162                 listener,
163                 IDEWorkbenchMessages.SaveAsDialog_fileLabel, IDEWorkbenchMessages.SaveAsDialog_file);
164         resourceGroup.setAllowExistingResources(true);
165
166         return parentComposite;
167     }
168
169     /**
170      * Returns the full path entered by the user.
171      * <p>
172      * Note that the file and container might not exist and would need to be created.
173      * See the <code>IFile.create</code> method and the
174      * <code>ContainerGenerator</code> class.
175      * </p>
176      *
177      * @return the path, or <code>null</code> if Cancel was pressed
178      */

179     public IPath getResult() {
180         return result;
181     }
182
183     /**
184      * Initializes the controls of this dialog.
185      */

186     private void initializeControls() {
187         if (originalFile != null) {
188             resourceGroup.setContainerFullPath(originalFile.getParent()
189                     .getFullPath());
190             resourceGroup.setResource(originalFile.getName());
191         } else if (originalName != null) {
192             resourceGroup.setResource(originalName);
193         }
194         setDialogComplete(validatePage());
195     }
196
197     /* (non-Javadoc)
198      * Method declared on Dialog.
199      */

200     protected void okPressed() {
201         // Get new path.
202
IPath path = resourceGroup.getContainerFullPath().append(
203                 resourceGroup.getResource());
204
205         //If the user does not supply a file extension and if the save
206
//as dialog was provided a default file name append the extension
207
//of the default filename to the new name
208
if (path.getFileExtension() == null) {
209             if (originalFile != null && originalFile.getFileExtension() != null) {
210                 path = path.addFileExtension(originalFile.getFileExtension());
211             } else if (originalName != null) {
212                 int pos = originalName.lastIndexOf('.');
213                 if (++pos > 0 && pos < originalName.length()) {
214                     path = path.addFileExtension(originalName.substring(pos));
215                 }
216             }
217         }
218
219         // If the path already exists then confirm overwrite.
220
IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
221         if (file.exists()) {
222             String JavaDoc[] buttons = new String JavaDoc[] { IDialogConstants.YES_LABEL,
223                     IDialogConstants.NO_LABEL, IDialogConstants.CANCEL_LABEL };
224             String JavaDoc question = NLS.bind(IDEWorkbenchMessages.SaveAsDialog_overwriteQuestion, path.toOSString());
225             MessageDialog d = new MessageDialog(getShell(),
226                     IDEWorkbenchMessages.Question,
227                     null, question, MessageDialog.QUESTION, buttons, 0);
228             int overwrite = d.open();
229             switch (overwrite) {
230             case 0: // Yes
231
break;
232             case 1: // No
233
return;
234             case 2: // Cancel
235
default:
236                 cancelPressed();
237                 return;
238             }
239         }
240
241         // Store path and close.
242
result = path;
243         close();
244     }
245
246     /**
247      * Sets the completion state of this dialog and adjusts the enable state of
248      * the Ok button accordingly.
249      *
250      * @param value <code>true</code> if this dialog is compelete, and
251      * <code>false</code> otherwise
252      */

253     protected void setDialogComplete(boolean value) {
254         okButton.setEnabled(value);
255     }
256
257     /**
258      * Sets the original file to use.
259      *
260      * @param originalFile the original file
261      */

262     public void setOriginalFile(IFile originalFile) {
263         this.originalFile = originalFile;
264     }
265
266     /**
267      * Set the original file name to use.
268      * Used instead of <code>setOriginalFile</code>
269      * when the original resource is not an IFile.
270      * Must be called before <code>create</code>.
271      * @param originalName default file name
272      */

273     public void setOriginalName(String JavaDoc originalName) {
274         this.originalName = originalName;
275     }
276
277     /**
278      * Returns whether this page's visual components all contain valid values.
279      *
280      * @return <code>true</code> if valid, and <code>false</code> otherwise
281      */

282     private boolean validatePage() {
283         if (!resourceGroup.areAllValuesValid()) {
284             if (!resourceGroup.getResource().equals("")) { //$NON-NLS-1$
285
setErrorMessage(resourceGroup.getProblemMessage());
286             } else {
287                 setErrorMessage(null);
288             }
289             return false;
290         }
291         
292         String JavaDoc resourceName = resourceGroup.getResource();
293         IWorkspace workspace = ResourcesPlugin.getWorkspace();
294         
295         // Do not allow a closed project to be selected
296
IPath fullPath = resourceGroup.getContainerFullPath();
297         if (fullPath != null) {
298             String JavaDoc projectName = fullPath.segment(0);
299             IStatus isValidProjectName = workspace.validateName(projectName, IResource.PROJECT);
300             if(isValidProjectName.isOK()) {
301                 IProject project = workspace.getRoot().getProject(projectName);
302                 if(!project.isOpen()) {
303                     setErrorMessage(IDEWorkbenchMessages.SaveAsDialog_closedProjectMessage);
304                     return false;
305                 }
306             }
307         }
308         
309         IStatus result = workspace.validateName(resourceName, IResource.FILE);
310         if (!result.isOK()){
311             setErrorMessage(result.getMessage());
312             return false;
313         }
314         
315         setErrorMessage(null);
316         return true;
317     }
318     
319     /* (non-Javadoc)
320      * @see org.eclipse.jface.window.Dialog#getDialogBoundsSettings()
321      *
322      * @since 3.2
323      */

324     protected IDialogSettings getDialogBoundsSettings() {
325         IDialogSettings settings = IDEWorkbenchPlugin.getDefault().getDialogSettings();
326         IDialogSettings section = settings.getSection(DIALOG_SETTINGS_SECTION);
327         if (section == null) {
328             section = settings.addNewSection(DIALOG_SETTINGS_SECTION);
329         }
330         return section;
331     }
332 }
333
Popular Tags