JDK-6260650 : FileDialog.getDirectory() does not return null when file dialog is cancelled
  • Type: Bug
  • Component: client-libs
  • Sub-Component: java.awt
  • Affected Version: 5.0
  • Priority: P3
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_9
  • CPU: sparc
  • Submitted: 2005-04-25
  • Updated: 2011-01-19
  • Resolved: 2005-08-06
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 6
6 b47Fixed
Related Reports
Relates :  
Description
I am opening an AWT FileDialog on clicking a button on the frame. When the dialog opens, I am navigating through some of the directories listed and finally I am cancelling the dialog. When I call FileDialog.getDirectory() after that, it returns me the default directory. I expect this method to return 'null' when the dialog is cancelled.

This is how it behaves on Win32 and Motif and XToolkit must be compatible with Motif. This is reproducible on Mustang as well as Tiger-fcs. Not reproducible on Win32. 

I have attached a sample test. Execute the sample test. Click on 'OPEN' or 'SAVE' button. A file dialog will be opened. Cancel the file dialog. If you see a valid directory being printed on the console, the bug is reproduced. I reproduced it on SolarisSparc9-CDE.
###@###.### 2005-04-25 12:44:15 GMT

Comments
SUGGESTED FIX *** /net/aquila/export/dc158259/Mustang//webrev/src/solaris/classes/sun/awt/X11/XFileDialogPeer.java- 2005-07-13 18:47:02.000000000 +0400 --- /net/aquila/export/dc158259/Mustang//webrev/src/solaris/classes/sun/awt/X11/XFileDialogPeer.java 2005-07-13 18:47:02.000000000 +0400 *************** *** 45,51 **** --- 45,57 ---- // Perhaps, 'target.file' just more correct (see target.setFile()) String savedFile; + // Holds value of the directory which was chosen before + // We use it in order to restore previously selected directory + // at the time of the next showing of the file dialog String savedDir; + // Holds value of the system property 'user.dir' + // in order to init current directory + String userDir; Dialog fileDialog; *************** *** 119,132 **** savedFile = target.getFile(); savedDir = target.getDirectory(); ! if (savedDir == null) { ! savedDir = (String)AccessController.doPrivileged( ! new PrivilegedAction() { ! public Object run() { ! return System.getProperty("user.dir"); ! } ! }); ! } installStrings(); gbl = new GridBagLayout(); --- 125,138 ---- savedFile = target.getFile(); savedDir = target.getDirectory(); ! // Shouldn't save 'user.dir' to 'savedDir' ! // since getDirectory() will be incorrect after handleCancel ! userDir = (String)AccessController.doPrivileged( ! new PrivilegedAction() { ! public Object run() { ! return System.getProperty("user.dir"); ! } ! }); installStrings(); gbl = new GridBagLayout(); *************** *** 165,171 **** addComponent(label, gbl, gbc, 0, 0, 1, GridBagConstraints.WEST, (Container)fileDialog, 1, 0, GridBagConstraints.NONE, labelInset); ! pathField = new TextField(savedDir); pathChoice = new Choice() { public Dimension getPreferredSize() { return new Dimension(PATH_CHOICE_WIDTH, pathField.getPreferredSize().height); --- 171,182 ---- addComponent(label, gbl, gbc, 0, 0, 1, GridBagConstraints.WEST, (Container)fileDialog, 1, 0, GridBagConstraints.NONE, labelInset); ! ! // Fixed 6260650: FileDialog.getDirectory() does not return null when file dialog is cancelled ! // After showing we should display 'user.dir' as current directory ! // if user didn't set directory programatically ! pathField = new TextField(savedDir != null ? savedDir : userDir); ! pathChoice = new Choice() { public Dimension getPreferredSize() { return new Dimension(PATH_CHOICE_WIDTH, pathField.getPreferredSize().height); *************** *** 610,615 **** --- 621,631 ---- /** * set the directory + * FIXME: we should update 'savedDir' after programmatically 'setDirectory' + * Otherwise, SavedDir will be not null before second showing + * So the current directory of the file dialog will be incorrect after second showing + * since 'setDirectory' will be ignored + * We cann't update savedDir here now since it used very often */ public void setDirectory(String dir) { *************** *** 655,664 **** handleFilter(""); } ! pathChoice.removeAll(); ! String dirList[] = getDirList(dir); ! for (i=0;i<dirList.length;i++) pathChoice.addItem(dirList[i]); ! pathPanel.validate(); } /** --- 671,681 ---- handleFilter(""); } ! // Some code was removed ! // Now we do updating of the pathChoice at the time of the choice opening ! // Fixed problem: ! // The exception java.awt.IllegalComponentStateException will be thrown ! // if the user invoke setDirectory after the closing of the file dialog } /** *************** *** 673,681 **** if (fileDialog == null) { init((FileDialog)target); } ! if (savedDir != null) { ! setDirectory(savedDir); } if (savedFile != null) { // Actually in Motif implementation lost file value which was saved after prevously showing // Seems we shouldn't restore Motif behaviour in this case --- 690,700 ---- if (fileDialog == null) { init((FileDialog)target); } ! ! if (savedDir != null || userDir != null) { ! setDirectory(savedDir != null ? savedDir : userDir); } + if (savedFile != null) { // Actually in Motif implementation lost file value which was saved after prevously showing // Seems we shouldn't restore Motif behaviour in this case *** (#1 of 1): [ UNSAVED ] ###@###.###
26-07-2005

EVALUATION In order to handle cancel event XToolkit implementation uses XFileDialogPeer.handleCancel() method. As you can see from this code there is the setting of target 'file' variable to null using setFile method but 'directory' is set to the savedDir variable. This variable initially store the system property 'user.dir' so this value will be returned to the user. It would be a good idea to create additional variable for system property and use it if savedDir is null. ###@###.### 2005-07-13 14:39:50 GMT
13-07-2005