/**
* This file is part of HIDB2.
*
* HIDB2 is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HIDB2 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 Lesser Public License for more details.
*
* You should have received a copy of the GNU Lesser Public License
* along with HIDB2. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import hidb2.gui.node.DefaultNode;
import hidb2.gui.node.FolderListNode;
import hidb2.gui.node.FolderNode;
import hidb2.kern.Folder;
import hidb2.kern.FolderDescription;
import java.util.logging.Logger;
import org.eclipse.jface.viewers.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.Viewer;
/**
* Provides the content of the data tree
*
*/
public class SearchViewContentProvider implements IStructuredContentProvider, ITreeContentProvider
{
final static Logger log = Logger.getLogger("hidb2.gui");
private SearchView _arbre;
private DefaultNode invisibleRoot;
public SearchViewContentProvider(SearchView arbre)
{
_arbre = arbre;
if (_arbre != null)
{
DefaultNode.init(_arbre.getTree().getTree().getDisplay());
}
}
public DefaultNode getRoot()
{
return invisibleRoot;
}
public void inputChanged(Viewer v, Object oldInput, Object newInput)
{
}
public void dispose()
{
}
public Object[] getElements(Object parent)
{
if ((parent == null) || ((_arbre != null) && (parent.equals(_arbre.getViewSite()))))
{
if (invisibleRoot == null)
{
initialize();
}
return getChildren(invisibleRoot);
}
return getChildren(parent);
}
public Object getParent(Object child)
{
if (child instanceof DefaultNode)
{
return ((DefaultNode) child).getParent();
}
return null;
}
public Object[] getChildren(Object parent)
{
if (parent instanceof DefaultNode)
{
return ((DefaultNode) parent).getChildren();
}
return new Object[0];
}
public boolean hasChildren(Object parent)
{
if (parent instanceof DefaultNode)
return ((DefaultNode) parent).hasChildren();
return false;
}
private void initialize()
{
invisibleRoot = new DefaultNode("root", "");
}
/**
* Create or Update the list of FolderDescription nodes.
* @return
*/
public FolderListNode update(FolderDescription fdin)
{
FolderListNode fdnExist = null;
for (DefaultNode node : invisibleRoot.getChildren())
{
FolderListNode fdn = (FolderListNode) node;
if (fdn.getDescr() == fdin)
{
// The folder description was already recorded
fdnExist = fdn;
}
}
if (fdnExist == null)
{
// Create a Folder list node, but do not read DB data
fdnExist = new FolderListNode(fdin, false);
invisibleRoot.addChild(fdnExist);
}
return fdnExist;
}
/**
* Create or Update the list of FolderDescription nodes.
* @return
*/
public FolderListNode update(Folder f)
{
FolderListNode fdtn = update((FolderDescription) f.getDescription());
FolderNode prevFn = null;
for (DefaultNode node : fdtn.getChildren())
{
FolderNode fn = (FolderNode) node;
if (fn.getFolder().getID() == f.getID())
{
// The folder was already recorded
prevFn = fn;
}
}
if (prevFn == null)
{
// Create a Folder list node, but do not read DB data
prevFn = new FolderNode(f);
fdtn.addChild(prevFn);
}
return fdtn;
}
}