/*
* xulfaces : bring XUL power to Java
*
* Copyright (C) 2005 Olivier SCHMITT
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package org.xulfaces.rubis;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;
import org.xulfaces.component.tree.TreeComponent;
/**
*
* @author kito31
* @version $Id: TreeController.java,v 1.3 2006/05/20 17:51:53 kito31 Exp $
*/
public class TreeController extends Controller {
private TreeComponent treeView;
private DefaultTreeModel treeModel = new DefaultTreeModel(new DefaultMutableTreeNode(null));
public DefaultTreeModel getTreeModel() {
return treeModel;
}
public void setTreeModel(DefaultTreeModel treeModel) {
this.treeModel = treeModel;
if(this.treeView != null){
this.treeView.setTreeModel(treeModel);
}
}
public TreeComponent getTreeView() {
return treeView;
}
public void setTreeView(TreeComponent treeView) {
this.treeView = treeView;
}
public void clearModel() {
if(treeModel != null){
Object root = getTreeModel().getRoot();
if(root != null){
((DefaultMutableTreeNode)root).removeAllChildren();
}
}
}
public void removeSelectedItems() {
TreePath selectionPath[] = getTreeView().getSelectionPath();
if(selectionPath != null){
for(int i=0; i < selectionPath.length; i++){
TreePath treePath = selectionPath[i];
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
if(treeNode.getParent() != null){
this.treeModel.removeNodeFromParent(treeNode);
}
}
}
}
public Object getSelectedObject(){
TreePath selectionPath[] = getTreeView().getSelectionPath();
if(selectionPath != null){
if(selectionPath.length > 0){
TreePath treePath = selectionPath[0];
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
return treeNode.getUserObject();
}
}
return null;
}
public Collection getSelectedObjects(){
Collection selection = new ArrayList();
TreePath selectionPath[] = getTreeView().getSelectionPath();
if(selectionPath != null){
for(int i=0; i < selectionPath.length; i++){
TreePath treePath = selectionPath[i];
DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
selection.add(treeNode.getUserObject());
}
}
return selection;
}
protected DefaultTreeModel buildTreeModelFromCollection(Collection collection){
DefaultMutableTreeNode root = new DefaultMutableTreeNode(null);
DefaultTreeModel treeModel = new DefaultTreeModel(root);
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
Object object = iterator.next();
DefaultMutableTreeNode defaultMutableTreeNode = new DefaultMutableTreeNode(object);
root.add(defaultMutableTreeNode);
}
return treeModel;
}
protected void addTreeNodesFromCollection(DefaultMutableTreeNode treeNode,Collection collection){
Iterator iterator = collection.iterator();
while(iterator.hasNext()){
Object object = iterator.next();
DefaultMutableTreeNode defaultMutableTreeNode = new DefaultMutableTreeNode(object);
treeNode.add(defaultMutableTreeNode);
}
}
protected void sortTreeModel(Comparator comparator){
DefaultMutableTreeNode rootNode = (DefaultMutableTreeNode) getTreeModel().getRoot();
Enumeration<DefaultMutableTreeNode> enumeration = rootNode.children();
List<DefaultMutableTreeNode> list = Collections.list(enumeration);
Collections.sort(list,comparator);
rootNode.removeAllChildren();
for(DefaultMutableTreeNode node:list){
rootNode.add(node);
}
}
}