Programmatically open FileTreeComponent


#1

I want to open a FileTreeComponent and selected a certain file or directory. I thought

fileTreeComp->setSelectedFile(File("C:\\Midi\\Beatles\bearable"))

would do the job, but it won't do more than just open the C:\Midi node.

Is setSelectedFile() not supposed to also open relevant tree branches?

Or does it only set a selected file if that particular tree node is already open?

 


#2

I don't know the answer, but if that string is taken straight from your code, have you spotted the typo?:

..."Beatles\bearable" instead of ..."Beatles\\bearable"" "?


#3

No, it's not, it's a typo in the forum, but thanks anyway for the suggestion.

In fact I checked setSelectedFile() in the debugger, and it will recursively find the right node even when the path is very long. And set it to selected. But it won't open the tree. Maybe it's not supposed to do that, but then what's the purpose of this function?


#4

It does open the tree if it can do, but bear in mind that opening these folders isn't trivial - it involves recursively scanning potentially huge folders for child files, so there are timeouts and background threads involved, etc. There's another whole thread about this somewhere. If it doesn't work, try debugging into the method and see what's going on.


#5

You mean this?

http://www.juce.com/forum/topic/interesting-observations

Well, after a few monts the outcome of that thread seemed to be... inconclusive. I'm obvisouly in deep sh... uncharted code territory. I don't feel I have the mental strength to take up that thread rigth now. Instead I've decided to enter the Hack of the month competition with the following entry:

class SelectDeeplyNestedFile : public Timer
    {
    public:
        SelectDeeplyNestedFile(FileTreeComponent *fileTreeComp_, String fileName) :
            fileTreeComp(fileTreeComp_)
        {
            StringArray check = StringArray::fromTokens(fileName, File::separatorString, String::empty);
            countDown = check.size();

            file = File(fileName),

            startTimer(100);
        }
        
    private:
        int countDown;
        File file;
        FileTreeComponent *fileTreeComp;
                
        void timerCallback() override
        {
            fileTreeComp->setSelectedFile(file);
            TreeViewItem *tvi = fileTreeComp->getSelectedItem(0);
            if (tvi)
                tvi->setOpen(true);

            if (--countDown <= 0)
            {
                if (tvi)
                    fileTreeComp->scrollToKeepItemVisible (tvi);

                delete this;
            }
        }
    };

    String lastSelectedFile = getStringSetting("lastSelectedFile");
    if (lastSelectedFile.isNotEmpty())
        new SelectDeeplyNestedFile(fileTreeComp, lastSelectedFile);

FileTreeComponent - how to expand tree to selected item
#6

A very respectable hack!


#7

Great, thanks!

 

I just changed it a bit by using a safe pointer :

 


class SelectDeeplyNestedFile : public Timer
{
public:
    //==============================================================================
    SelectDeeplyNestedFile (FileTreeComponent* fileTreeComp_, String fileName)
    : fileTreeComp (fileTreeComp_)
    {
        StringArray check = StringArray::fromTokens (fileName, File::separatorString, String::empty);
        countDown = check.size();
        
        file = File (fileName),
        
        startTimer (100);
    }
    
private:
    //==============================================================================
    int countDown;
    File file;
    Component::SafePointer<FileTreeComponent> fileTreeComp;
    
    void timerCallback() override
    {
        if (fileTreeComp == nullptr)
        {
            delete this;
        }
        else
        {
            fileTreeComp->setSelectedFile (file);
            TreeViewItem *tvi = fileTreeComp->getSelectedItem (0);
            
            if (tvi)
                tvi->setOpen (true);
            
            if (--countDown <= 0)
            {
                if (tvi)
                    fileTreeComp->scrollToKeepItemVisible (tvi);
                
                delete this;
            }
        }
    }
};