-
Notifications
You must be signed in to change notification settings - Fork 143
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expose cancel state of FileDialog and DirectoryDialog
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 `askSelectFile()`/`askSelectDirectory()` methods as an alternative to the existing `open()` method. This new method returns a FileDialogResult object encapsulating the selected file or folder and the state of the dialog, such as a user cancellation. The dialogs evaluate the operating system's response code for the system's dialog control to identify the dialogs result state.
- Loading branch information
1 parent
37de57e
commit 31937f7
Showing
5 changed files
with
122 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
bundles/org.eclipse.swt/Eclipse SWT/common/org/eclipse/swt/widgets/FileDialogResult.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package org.eclipse.swt.widgets; | ||
|
||
import java.util.*; | ||
|
||
/** | ||
* A representation of the result of file dialogs such as the {@link FileDialog} | ||
* or {@link DirectoryDialog}. | ||
* | ||
* @since 3.125 | ||
*/ | ||
public final class FileDialogResult { | ||
public static enum State { | ||
/** | ||
* The user has successfully selected a file or folder | ||
*/ | ||
SELECTED, | ||
/** | ||
* The user has canceled the dialog | ||
*/ | ||
CANCELED, | ||
/** | ||
* An unspecified error occurred while the dialog was open | ||
*/ | ||
UNKNOWN_ERROR | ||
} | ||
|
||
private String path; | ||
|
||
private State state; | ||
|
||
private FileDialogResult(String path, State state) { | ||
this.path = path; | ||
this.state = state; | ||
} | ||
|
||
/** | ||
* {@return an optional that contains the string describing the absolute path of | ||
* the selected file or folder or is empty if the dialog was canceled or if | ||
* there was an error} | ||
*/ | ||
public Optional<String> getPath() { | ||
return Optional.ofNullable(path); | ||
} | ||
|
||
/** | ||
* {@return the result state of the dialog, indicating a successful selection, | ||
* user cancellation and other errors} | ||
*/ | ||
public State getState() { | ||
return state; | ||
} | ||
|
||
static FileDialogResult createForSuccessfulSelection(String path) { | ||
return new FileDialogResult(path, State.SELECTED); | ||
} | ||
|
||
static FileDialogResult createForUserCancellation() { | ||
return new FileDialogResult(null, State.CANCELED); | ||
} | ||
|
||
static FileDialogResult createForUnkownError() { | ||
return new FileDialogResult(null, State.UNKNOWN_ERROR); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters