/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package clips.administrator.expenditure;
import cli_fmw.delegate.directory.DirectoryItemRO;
import cli_fmw.delegate.directory.complex.DirectoryLocator;
import cli_fmw.utils.SelectorEditableExceptional;
import clips.delegate.directory.filtered.DirectoryService;
import clips.delegate.directory.filtered.DirectoryServiceItem;
import clips.delegate.directory.complex.DirectoryServicesGroupItem;
import cli_fmw.utils.Selector;
import cli_fmw.main.ClipsException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.event.TreeModelListener;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.TreeModel;
import javax.swing.tree.TreePath;
/**
*
* @author Loki
*/
public class TreeModelServices implements TreeModel {
DirectoryItemRO items[];
DefaultMutableTreeNode root;
Map<DirectoryServicesGroupItem, Selector<DirectoryServiceItem>> services =
new HashMap<DirectoryServicesGroupItem, Selector<DirectoryServiceItem>>();
public TreeModelServices(DirectoryItemRO[] items, String rootTitle) throws ClipsException {
this.items = items;
root = new DefaultMutableTreeNode(rootTitle);
}
@Override
public Object getRoot() {
return this.root;
}
private Selector<DirectoryServiceItem> getServices(DirectoryServicesGroupItem item) throws ClipsException {
Selector<DirectoryServiceItem> ss = services.get(item);
if(ss == null) {
DirectoryService ds = (DirectoryService)
DirectoryLocator.getDirectory(DirectoryService.class, false);
ss = ds.getFilteredItems(item);
services.put(item, ss);
}
return ss;
}
@Override
public Object getChild(Object parent, int index) {
try {
if(parent instanceof DefaultMutableTreeNode) {
return items[index];
} else if(parent instanceof DirectoryServicesGroupItem) {
DirectoryServicesGroupItem item = (DirectoryServicesGroupItem) parent;
if(index < item.getItems().size()) {
return item.getItems().get(index);
}
index -= item.getItems().size();
Selector<DirectoryServiceItem> ss = getServices(item);
return ss.get(index);
}
} catch(ClipsException ex) {
ex.printStackTrace();
}
return new DefaultMutableTreeNode(new String("default leaf"));
}
@Override
public int getChildCount(Object parent) {
try {
if(parent instanceof DefaultMutableTreeNode) {
return items.length;
} else if(parent instanceof DirectoryServicesGroupItem) {
DirectoryServicesGroupItem item = (DirectoryServicesGroupItem) parent;
Selector<DirectoryServiceItem> ss = getServices((DirectoryServicesGroupItem) parent);
int count = item.getItems().size() + ss.size();
return count;
} else {
return 0;
}
} catch(ClipsException ex) {
ex.printStackTrace();
}
return 0;
}
@Override
public boolean isLeaf(Object node) {
if(node instanceof DirectoryServicesGroupItem ||
node == root){
return false;
}
return true;
}
@Override
public void valueForPathChanged(TreePath path, Object newValue) {
}
@Override
public int getIndexOfChild(Object parent, Object child) {
if (parent instanceof DirectoryServicesGroupItem && child instanceof DirectoryServicesGroupItem){
SelectorEditableExceptional<DirectoryServicesGroupItem> see = ((DirectoryServicesGroupItem) parent).getItems();
for (int i = 0; i < see.size(); i++){
if (((DirectoryServicesGroupItem) child).getID() == see.get(i).getID()){
return i;
}
}
}else if (parent instanceof DirectoryServicesGroupItem && child instanceof DirectoryServiceItem){
try {
Selector<DirectoryServiceItem> selector = getServices((DirectoryServicesGroupItem) parent);
for (int i = 0; i < selector.size(); i++) {
if (((DirectoryServiceItem) child).getID() == selector.get(i).getID()) {
return i;
}
}
} catch (ClipsException ex) {
ex.printStackTrace();
return 0;
}
}else{
for (int i = 0; i < items.length; i++){
if (items[i].getID() == ((DirectoryServicesGroupItem) child).getID()){
return i;
}
}
}
return 0;
}
@Override
public void addTreeModelListener(TreeModelListener l) {
}
@Override
public void removeTreeModelListener(TreeModelListener l) {
}
}