
com.alee.extended.tree.FileTreeDataProvider Maven / Gradle / Ivy
/*
* This file is part of WebLookAndFeel library.
*
* WebLookAndFeel library is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* WebLookAndFeel library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with WebLookAndFeel library. If not, see .
*/
package com.alee.extended.tree;
import com.alee.api.annotations.NotNull;
import com.alee.api.annotations.Nullable;
import com.alee.managers.task.TaskManager;
import com.alee.utils.CollectionUtils;
import com.alee.utils.FileUtils;
import com.alee.utils.compare.Filter;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
/**
* Asynchronous data provider for WebFileTree.
*
* @author Mikle Garin
*/
public class FileTreeDataProvider extends AbstractAsyncTreeDataProvider
{
/**
* Tree root files.
*/
protected List rootFiles;
/**
* Constructs new {@link FileTreeDataProvider} with the specified root {@link File}s.
*
* @param rootFiles root {@link File}s
*/
public FileTreeDataProvider ( @NotNull final File... rootFiles )
{
this ( CollectionUtils.asList ( rootFiles ) );
}
/**
* Constructs new {@link FileTreeDataProvider} with the specified root {@link File}s.
*
* @param rootFiles {@link List} of root {@link File}s
*/
public FileTreeDataProvider ( @NotNull final List rootFiles )
{
this.rootFiles = rootFiles;
}
@NotNull
@Override
public String getThreadGroupId ()
{
return TaskManager.FILE_SYSTEM;
}
@NotNull
@Override
public FileTreeNode getRoot ()
{
return rootFiles.size () == 1 ? new FileTreeNode ( rootFiles.get ( 0 ) ) : new FileTreeNode ( null );
}
@Override
public void loadChildren ( @NotNull final FileTreeNode parent, @NotNull final NodesLoadCallback listener )
{
try
{
listener.completed ( parent.getFile () == null ? getRootChildren () : getFileChildren ( parent ) );
}
catch ( final Exception cause )
{
listener.failed ( cause );
}
}
/**
* Returns root child nodes.
*
* @return root child nodes
*/
@NotNull
protected List getRootChildren ()
{
final List children = new ArrayList ( rootFiles.size () );
for ( final File rootFile : rootFiles )
{
children.add ( new FileTreeNode ( rootFile ) );
}
return children;
}
/**
* Returns child nodes for specified node.
*
* @param node parent node
* @return child nodes
*/
@NotNull
public List getFileChildren ( @NotNull final FileTreeNode node )
{
final List children;
final File file = node.getFile ();
final File[] childrenArray = file != null ? file.listFiles () : null;
if ( childrenArray == null || childrenArray.length == 0 )
{
children = new ArrayList ( 0 );
}
else
{
children = new ArrayList ( childrenArray.length );
for ( final File f : childrenArray )
{
children.add ( new FileTreeNode ( f ) );
}
}
return children;
}
@Nullable
@Override
public Filter getChildrenFilter ( @NotNull final FileTreeNode parent, @NotNull final List children )
{
// We must not filter out given roots
return parent.getFile () != null ? super.getChildrenFilter ( parent, children ) : null;
}
@Override
public boolean isLeaf ( @NotNull final FileTreeNode node )
{
return node.getFile () != null && !FileUtils.isDirectory ( node.getFile () );
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy