-
Notifications
You must be signed in to change notification settings - Fork 143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Expose cancel state of FileDialog and DirectoryDialog #1026
Expose cancel state of FileDialog and DirectoryDialog #1026
Conversation
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/DirectoryDialog.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just some suggestions/thoughts ...
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/FileDialogResult.java
Outdated
Show resolved
Hide resolved
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/FileDialogResult.java
Outdated
Show resolved
Hide resolved
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/FileDialogResult.java
Outdated
Show resolved
Hide resolved
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/FileDialogResult.java
Outdated
Show resolved
Hide resolved
bundles/org.eclipse.swt/Eclipse SWT/win32/org/eclipse/swt/widgets/DirectoryDialog.java
Outdated
Show resolved
Hide resolved
bff9b11
to
31937f7
Compare
Thinking more, maybe the result class is not required and one could have a singel method
that in case of error throws an exception with a meaningful message (e.g. path is to long, IO exception seems suitable if SWT Exception seems not well suited) and an empty optional on cancel, then all cases should be covered. |
4306e90
to
9641b01
Compare
Thanks for all the input, @laeubi! I tend to completely agree with your point of view. In particular, I took a look at the kinds of "errors" that may happen and found that the currently used OS APIs only seem to provide "OK" and "Cancel", and maybe something else if there really is a error (that may then reasonable be thrown as an SWTException). I took the according information from here: So I changed the implementation to only return an Optional result and throw an exception in other cases in 9641b01. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks fine for me just a bit unsure about the naming but maybe others like to give advice.
@@ -182,7 +212,12 @@ public String open() { | |||
fileDialog.Release(); | |||
} | |||
|
|||
return directoryPath; | |||
if (hr == COM.S_OK) { | |||
return Optional.of(directoryPath); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return Optional.of(directoryPath); | |
return Optional.ofNullable(directoryPath); |
could it be null? or maybe the case of empty should also result in a Optional.empty()....
* | ||
* @since 3.125 | ||
*/ | ||
public Optional<String> askSelectDirectory() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
public Optional<String> askSelectDirectory() { | |
public Optional<String> openDirectory() { |
What do you think about something like this? askSelect sounds a bit strange and that way probabbly if it also starts with "open" users that are used to the old method might discover the new one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think something that starts with open so that it's sorted to be adjacent in documentation or in proposals....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. Maybe just openDialog()
? Since the call does not open a file/directory, but a dialog (to choose one).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would be fine with openDialog() as well
8bd21ca
to
eeb8cd9
Compare
I have manually checked the behavior on Windows 11, Ubuntu 22.04 (Gnome 42) and MacOS Sonoma with this simply snippet (also with System.out.println(new FileDialog(new Shell(new Display())).openDialog()); They all printed an empty Optional when cancelling the dialog and the selected path in case of a successful selection. |
50e5058
to
5096e6f
Compare
f87f799
to
7fa0257
Compare
The FileDialog and DirectoryDialog implementations do currently not provide any way to identify that the dialog was canceled. The dialogs return a null string in case no file or folder was selected, but is not possible to identify whether this was intended (via a cancel action) or if there was some unexpected error while the dialog was open (such as too long file paths on Windows). This change introduces the `openDialog()` method as an alternative to the existing `open()` method. This new method returns an optional String, which is only present if a file or directory was successfully selected. An empty Optional indicates a cancellation by the user and an actual error is realized as an SWTException. The dialogs evaluate the operating system's response code for the system's dialog control to identify the dialogs result state.
7fa0257
to
e6bb8b0
Compare
The
FileDialog
andDirectoryDialog
implementations do currently not provide any way to identify that the dialog was canceled. The dialogs return a null string in case no file or folder was selected, but is not possible to identify whether this was intended (via a cancel action) or if there was some unexpected error while the dialog was open (such as too long file paths on Windows).This change introduces the
openDialog()
method as an alternative to the existingopen()
method. This new method returns an optional String, which is only present if a file or directory was successfully selected. An empty Optional indicates a cancellation by the user and an actual error is realized as an SWTException. The dialogs evaluate the operating system's response code for the system's dialog control to identify the dialogs result state.This PR is a result of the proposal by @laeubi in #1005 (comment). Supercedes and thus closes #1005.