Set the initial directory in SWT FileDialog
Asked Answered
C

3

9

I'm working on an Eclipse RCP project and need to let the user select some file. For convenience, based on some conditions, the initial directory of the file choosing dialog should be set prior to opening it.

As I'm bound to Eclipse RCP / SWT, I am working with the org.eclipse.swt.widgets.FileDialog.
The documentation of this FileDialog points out to use the setFilterPath(String string)-method which should do exactly what I need (see documentation).

   FileDialog dialog = new FileDialog(shell, SWT.OPEN);
   dialog.setFilterExtensions(new String [] {"*.html"});
   dialog.setFilterPath("c:\\temp");
   String result = dialog.open();

Unfortunately it is not working, at least not "every time".

I have currently no installation to check on it, but I'm quite sure that the feature would work totally fine on a Windows 200/XP/Vista machine. I am working with a Windows 7 machine and I think I am suffering from the behaviour described here for lpstrInitialDir.

At least, this is exactly the behaviour I am facing: The path is good the first time I open the dialog, but the second time, the path is initially set to the last chosen path. This seems to be convenient in most cases, but it is not in mine.

Can this be right? If so, have I any chance on changing the behaviour according to my needs?

Thanks for any helping answer!

Crocodile answered 5/8, 2013 at 12:48 Comment(1)
I raised bug for that.. bugs.eclipse.org/bugs/show_bug.cgi?id=426849Eames
P
2

I ran into the same problem on Windows 10 and found a solution that seems to be working for me. A code snippet from the DirectoryDialog led to the right direction:

if (filterPath != null && filterPath.length() > 0) {
        String path = filterPath.replace('/', '\\');
        char[] buffer = new char[path.length() + 1];
        path.getChars(0, path.length(), buffer, 0);
        if (COM.SHCreateItemFromParsingName(buffer, 0, COM.IID_IShellItem, ppv) == OS.S_OK) {
            IShellItem psi = new IShellItem(ppv[0]);
            /*
             * SetDefaultDirectory does not work if the dialog has
             * persisted recently used folder. The fix is to clear the
             * persisted data.
             */
            fileDialog.ClearClientData();
            fileDialog.SetDefaultFolder(psi);
            psi.Release();
        }
}

The FileDialog misses this statement 'fileDialog.ClearClientData()'. My solution is to execute the following code before setting the path and open the dialog:

long [] ppv = new long [1];
if (COM.CoCreateInstance(COM.CLSID_FileOpenDialog, 0, COM.CLSCTX_INPROC_SERVER, COM.IID_IFileOpenDialog, ppv) == OS.S_OK) {
    IFileDialog fileDialog = new IFileDialog(ppv[0]);
    fileDialog.ClearClientData();
    fileDialog.Release();
}

Now you can set the filterpath without Windows messing things up.

Pronoun answered 1/10, 2021 at 11:35 Comment(0)
R
1

I found a simple Solution for the Problem you described (I had the exact same Problem).

Just rearrange the your code like this:

   FileDialog dialog = new FileDialog(shell, SWT.OPEN);
   dialog.setFilterPath("c:\\temp"); // This line is switched with the following line
   dialog.setFilterExtensions(new String [] {"*.html"});
   String result = dialog.open();

Somehow the Order of the methods called is relevant.

Regenerate answered 17/6, 2020 at 12:51 Comment(1)
Sorry, doesn't work for me. It's always opening to the same path it displayed the previous time. What's weird is that the "memory" survives across application re-launches. I find it a bit of a stretch to think that SWT's file dialog would store that information in the registry.Confutation
B
-1

Are you using the same FileDialog object when you re-open it?

I ran a few quick tests and found that, if you re-set the filterPath, the dialog opens in the correct location.

If I open the same object again, it starts in the previously selected location.

Bombardon answered 16/8, 2013 at 15:31 Comment(1)
I ran into the same problem, namely that the setFilterPath() does not always work. The nearest that I could deduce is that setFilterPath() is consistent, when supplying a folder, such as "C:\\", however if you specify a network host, such as "\\\\jmr-stamp01", then all bets are off. Sometimes you will see the network path and sometimes you will not. I suspect that the SWT FileDialog class has a bug in it.Natachanatal

© 2022 - 2024 — McMap. All rights reserved.