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

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

/*
*   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 java.util.Comparator;

import javax.swing.tree.DefaultMutableTreeNode;

import org.xulfaces.rubis.TreeController;
import org.xulfaces.rubis.admin.dao.ItemDAO;
import org.xulfaces.rubis.model.Item;

public  class ItemsTreeController extends TreeController {
 
  private ItemDAO itemDAO;
  private CategoriesTreeController categoriesTreeController;
  private Item selectedItem;
  private boolean ascendingOrderForName;
 
  public CategoriesTreeController getCategoriesTreeController() {
    return categoriesTreeController;
  }

  public void setCategoriesTreeController(
      CategoriesTreeController categoriesTreeController) {
    this.categoriesTreeController = categoriesTreeController;
  }

  public Item getSelectedItem() {
    return selectedItem;
  }

  public void setSelectedItem(Item selectedItem) {
    this.selectedItem = selectedItem;
  }

  public ItemDAO getItemDAO() {
    return itemDAO;
  }

  public void setItemDAO(ItemDAO itemDAO) {
    this.itemDAO = itemDAO;
  }

  public void setItems(Collection items) {
    clearModel();
    addTreeNodesFromCollection((DefaultMutableTreeNode) getTreeModel().getRoot(),items);   
 
 
  public void showPreview(){
    this.selectedItem = (Item) this.getSelectedObject();
  }
 
  public void sortByName(){
    sortTreeModel(new NameComparator());
    this.ascendingOrderForName = !this.ascendingOrderForName;
  }
 
  public void removeItem(){
    Collection<Item> items = this.getSelectedObjects();
    for(Item item:items){           
      this.itemDAO.deleteItem(item.getId());             
    }
    if(!items.isEmpty()){
      refresh()
      this.ascendingOrderForName = !this.ascendingOrderForName;
      sortByName();
    }
       
  }
 
  public void refresh(){
    getCategoriesTreeController().loadTreeRows();   
  }
 
  class NameComparator implements Comparator {

    public int compare(Object o1, Object o2) {
      DefaultMutableTreeNode n1 = (DefaultMutableTreeNode) o1;
      DefaultMutableTreeNode n2 = (DefaultMutableTreeNode) o2;
      Item i1 = (Item) n1.getUserObject();
      Item i2 = (Item ) n2.getUserObject();
     
      if(ascendingOrderForName){
        return i1.getName().compareTo(i2.getName())
      }
      else {
        return i2.getName().compareTo(i1.getName());
      }
     
    }     
  }
}
TOP

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

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.