diff options
author | Sravan Kumar Lakkimsetti | 2014-01-30 04:51:36 +0000 |
---|---|---|
committer | Gerrit Code Review @ Eclipse.org | 2014-02-21 11:48:43 +0000 |
commit | b647d62c5bdcee9c1d9104cd6245d80b8fca31e7 (patch) | |
tree | e8d18019f3efdee0bff5672cd3095731e83b8a29 | |
parent | 24fdfd9c26f6e2472ad6e0a091d30c9851d291d5 (diff) | |
download | eclipse.platform.swt-b647d62c5bdcee9c1d9104cd6245d80b8fca31e7.tar.gz eclipse.platform.swt-b647d62c5bdcee9c1d9104cd6245d80b8fca31e7.tar.xz eclipse.platform.swt-b647d62c5bdcee9c1d9104cd6245d80b8fca31e7.zip |
Bug 332178 - FileDialog overwrites file without prompt.
Change-Id: I2265c59fb67619c6c5f4d69649a01ab1327879d6
Signed-off-by: Sravan Kumar Lakkimsetti <sravankumarl@in.ibm.com>
-rw-r--r-- | bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java | 65 |
1 files changed, 49 insertions, 16 deletions
diff --git a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java index a4f4c399b9..38de0c0634 100644 --- a/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java +++ b/bundles/org.eclipse.swt/Eclipse SWT/gtk/org/eclipse/swt/widgets/FileDialog.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2012 IBM Corporation and others. + * Copyright (c) 2000, 2014 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -199,20 +199,6 @@ String computeResultChooserDialog () { int separatorIndex = fullPath.lastIndexOf (SEPARATOR); fileName = fullPath.substring (separatorIndex + 1); filterPath = fullPath.substring (0, separatorIndex); - int fileExtensionIndex = fileName.indexOf(FILE_EXTENSION_SEPARATOR); - if ((style & SWT.SAVE) != 0 && fileExtensionIndex == -1 && filterIndex != -1) { - if (filterExtensions.length > filterIndex) { - String selection = filterExtensions [filterIndex]; - int length = selection.length (); - int index = selection.indexOf (EXTENSION_SEPARATOR); - if (index == -1) index = length; - String extension = selection.substring (0, index).trim (); - if (!extension.equals ("*") && !extension.equals ("*.*")) { - if (extension.startsWith ("*.")) extension = extension.substring (1); - fullPath = fullPath + extension; - } - } - } } return fullPath; } @@ -379,6 +365,9 @@ void presetChooserDialog () { if (filterPath == null) filterPath = ""; if (fileName == null) fileName = ""; if ((style & SWT.SAVE) != 0) { + if (fileName.equals ("")) { + fileName = "Untitled"; + } if (filterPath.length () > 0) { if (uriMode) { byte [] buffer = Converter.wcsToMbcs (null, filterPath, true); @@ -390,7 +379,32 @@ void presetChooserDialog () { } } if (fileName.length () > 0) { - byte [] buffer = Converter.wcsToMbcs (null, fileName, true); + StringBuffer filenameWithExt = new StringBuffer(); + filenameWithExt.append (fileName); + if ((fileName.lastIndexOf (FILE_EXTENSION_SEPARATOR) == -1) && + (filterExtensions.length != 0)) { // Filename doesn't contain the extension and user has provided filter extensions + String selectedFilter = null; + if (this.filterIndex == -1) { + selectedFilter = filterExtensions[0]; + } else { + selectedFilter = filterExtensions[filterIndex]; + } + String extFilter = null; + int index = selectedFilter.indexOf (EXTENSION_SEPARATOR); + if (index == -1) { + extFilter = selectedFilter.trim (); + } else { + extFilter = selectedFilter.substring (0, index).trim (); + } + + int separatorIndex = extFilter.lastIndexOf (FILE_EXTENSION_SEPARATOR); + String extension = extFilter.substring (separatorIndex); + + if (!isGlobPattern (extension)) { //if the extension is of type glob pattern we should not add the extension + filenameWithExt.append (extension); + } + } + byte [] buffer = Converter.wcsToMbcs (null, filenameWithExt.toString (), true); OS.gtk_file_chooser_set_current_name (handle, buffer); } } else { @@ -462,6 +476,25 @@ void presetChooserDialog () { fileNames = new String [0]; } /** + * Check whether the file extension is a glob pattern. + * For example, *.* is a glob pattern which corresponds to all files + * *.jp* corresponds to all the files with extension starting with jp like jpg,jpeg etc + * *.jp? corresponds to 3 letter extension starting with jp with any 3rd letter + * *.[pq]ng this corresponds to *.png and *.qng + * + * @param extension file extension as a string + * + * @returns true if the extension contains any of the glob pattern wildcards + */ +private boolean isGlobPattern (String extension) { + if (extension.contains ("*") || + extension.contains ("?") || + (extension.contains ("[") && extension.contains ("]"))) { + return true; + } + return false; +} +/** * Set the initial filename which the dialog will * select by default when opened to the argument, * which may be null. The name will be prefixed with |