Package org.xulfaces.rubis.admin.controller.category

Source Code of org.xulfaces.rubis.admin.controller.category.CategoriesTreeController

/*
*   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.admin.controller.category;

import java.util.Collection;

import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.xulfaces.LabelProvider;
import org.xulfaces.rubis.TreeController;
import org.xulfaces.rubis.admin.dao.CategoryDAO;
import org.xulfaces.rubis.model.Category;
import org.xulfaces.rubis.model.Item;
import org.xulfaces.rubis.model.User;

public  class CategoriesTreeController extends TreeController implements LabelProvider {
 
  private CategoryDAO categoryDAO;
  private boolean loaded = false;
 
 
  public CategoryDAO getCategoryDAO() {
    return categoryDAO;
  }

  public void setCategoryDAO(CategoryDAO categoryDAO) {
    this.categoryDAO = categoryDAO;
  }

  @Override
  public DefaultTreeModel getTreeModel() {
    if(!this.loaded){
      setTreeModel(buildTreeModelFromCollection(this.categoryDAO.getCategories()));
      this.loaded = true;
    }
    return super.getTreeModel();
   
  }
 
  public void refresh(){
    setTreeModel(buildTreeModelFromCollection(this.categoryDAO.getCategories()));
  }
 
  public void loadTreeRows(){   
    TreePath selectionPath[] = getTreeView().getSelectionPath();
    if(selectionPath != null){
      for(int i=0; i < selectionPath.length; i++){
        TreePath treePath = selectionPath[i];     
        DefaultMutableTreeNode treeNode = (DefaultMutableTreeNode) treePath.getLastPathComponent();
        treeNode.removeAllChildren();
        if(treeNode.getParent() != null){
          Object userObject = treeNode.getUserObject();
          if(userObject instanceof Category){
            Category category = (Category)userObject;
            Collection items = categoryDAO.loadItemsForCategory(category.getId());
            category.setNbItems(items.size());
            ItemsTreeController itemsTreeCtrl = (ItemsTreeController) getController("itemsTreeCtrl");
            itemsTreeCtrl.setItems(items);                       
          }
        }   
      }     
    }
  }

  public String getText(Object arg0, int arg1) {
    StringBuffer label = new StringBuffer();
    if(arg0 instanceof Category){
      Category category = (Category) arg0;     
      label.append(category.getName());
      label.append(" (");
      label.append(category.getNbItems());
      label.append(")")
    }
    else if(arg0 instanceof Item){
      Item item = (Item) arg0;
      label.append(item.getName());
    }
    return label.toString();
  }
 
  public void removeCategory(){
    Category category = (Category) getSelectedObject();
    if(category != null){     
      this.categoryDAO.deleteCategory(category.getId());
      refresh();
    }
  }
 
  public void editCategory(){
    Category category = (Category) getSelectedObject();
    if(category != null){     
      this.categoryDAO.deleteCategory(category.getId());
      refresh();
    }
  }
     
}
TOP

Related Classes of org.xulfaces.rubis.admin.controller.category.CategoriesTreeController

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.