/**
* 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.
*
* PoJamas 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 PoJamas. If not, see <http://www.gnu.org/licenses/>.
*/
package hidb2.gui;
import hidb2.gui.node.DataPathNode;
import hidb2.gui.node.DataPathTitleNode;
import hidb2.gui.node.DefaultNode;
import hidb2.gui.node.FolderDescrNode;
import hidb2.gui.node.FolderDescrTitleNode;
import hidb2.gui.node.ListDescrTitleNode;
import hidb2.gui.node.ListDescrNode;
import hidb2.gui.ressources.RscMan;
import hidb2.kern.AttrType;
import hidb2.kern.DataPath;
import hidb2.kern.FolderDescription;
import hidb2.kern.ListDescription;
import java.util.List;
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 DB structure tree.
*
*/
public class DBStructViewContentProvider implements IStructuredContentProvider, ITreeContentProvider
{
final static Logger log = Logger.getLogger("hidb2.gui");
private DBStructView _arbre;
private DefaultNode invisibleRoot;
private FolderDescrTitleNode FolderDescr;
private ListDescrTitleNode ListDescr;
private DataPathTitleNode DataPaths;
private DefaultNode Types;
public DBStructViewContentProvider(DBStructView 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)
{
// log.info("oldInput:" + oldInput+ " ==> " + 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", "");
// Fill tree with FolderDescriptions
updateFolderDescr(null);
invisibleRoot.addChild(FolderDescr);
// Fill tree with ListDescriptions
updateListDescr(null);
invisibleRoot.addChild(ListDescr);
updateDataPath();
invisibleRoot.addChild(DataPaths);
// List types
Types = new DefaultNode("Types", RscMan.IN_EQUIPEMENTS);
invisibleRoot.addChild(Types);
for (AttrType ty : AttrType.values())
{
// Use correct node type
Types.addChild(new DefaultNode(ty.name, RscMan.IN_EQUIPEMENT));
}
}
/**
* Create or Update the list of DataPath nodes.
* @return
*/
public DataPathTitleNode updateDataPath()
{
if (DataPaths == null)
{
DataPaths = new DataPathTitleNode("Data Paths", RscMan.IN_TARGET);
}
DataPaths.removeAllChildren();
// Fill tree with DataPath
for (DataPath dp : Application.getDataStore().getDataPathList())
{
DataPaths.addChild(new DataPathNode(dp));
}
return getDataPaths();
}
public DataPathTitleNode getDataPaths()
{
return DataPaths;
}
/**
* Create or Update the list of FolderDescription nodes.
* @return
*/
public FolderDescrTitleNode updateFolderDescr(FolderDescription fdin)
{
if (FolderDescr == null)
{
FolderDescr = new FolderDescrTitleNode("Folder Descriptions", RscMan.IN_SCENARIO);
}
List<FolderDescription> lstFldDescr = Application.getDataStore().getFolderDescriptionList();
if (fdin == null)
{
FolderDescr.removeAllChildren();
for (FolderDescription fd : lstFldDescr)
{
FolderDescr.addChild(new FolderDescrNode(fd));
}
}
else
{
for (DefaultNode node : FolderDescr.getChildren())
{
FolderDescrNode fdn = (FolderDescrNode) node;
fdn.setName(fdn.getDescr().getName());
if (fdn.getDescr() == fdin)
{
fdn.removeAllChildren();
fdn.createChildren();
}
}
}
return getFolderDescr();
}
public FolderDescrTitleNode getFolderDescr()
{
return FolderDescr;
}
/**
* Create or Update the list of FolderDescription nodes.
* @return
*/
public ListDescrTitleNode updateListDescr(ListDescription ldin)
{
if (ListDescr == null)
{
ListDescr = new ListDescrTitleNode("List Descriptions", RscMan.IN_TARGET);
}
// List Description node filling
List<ListDescription> lstRefLstDescr = Application.getDataStore().getListDescriptionList();
if (ldin == null)
{
ListDescr.removeAllChildren();
for (ListDescription fd : lstRefLstDescr)
{
ListDescr.addChild(new ListDescrNode(fd));
}
}
else
{
for (DefaultNode node : ListDescr.getChildren())
{
ListDescrNode fdn = (ListDescrNode) node;
fdn.setName(fdn.getDescr().getName());
if (fdn.getDescr() == ldin)
{
fdn.removeAllChildren();
fdn.createChildren();
}
}
}
return getListDescr();
}
public ListDescrTitleNode getListDescr()
{
return ListDescr;
}
}