Source for org.jfree.report.modules.gui.swing.preview.CategoryTreeItem

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: CategoryTreeItem.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: 
  32: package org.jfree.report.modules.gui.swing.preview;
  33: 
  34: import java.util.ArrayList;
  35: 
  36: /**
  37:  * Creation-Date: 01.12.2006, 20:01:32
  38:  *
  39:  * @author Thomas Morgner
  40:  */
  41: public class CategoryTreeItem implements Comparable
  42: {
  43:   private CategoryTreeItem parent;
  44:   private ActionCategory category;
  45:   private ArrayList childs;
  46:   private String name;
  47: 
  48:   public CategoryTreeItem(final ActionCategory category)
  49:   {
  50:     this.category = category;
  51:     this.name = category.getName();
  52:   }
  53: 
  54:   public String getName()
  55:   {
  56:     return name;
  57:   }
  58: 
  59:   public CategoryTreeItem getParent()
  60:   {
  61:     return parent;
  62:   }
  63: 
  64:   public void setParent(final CategoryTreeItem parent)
  65:   {
  66:     this.parent = parent;
  67:   }
  68: 
  69:   public ActionCategory getCategory()
  70:   {
  71:     return category;
  72:   }
  73: 
  74:   public void add(final CategoryTreeItem item)
  75:   {
  76:     if (childs == null)
  77:     {
  78:       childs = new ArrayList();
  79:     }
  80:     childs.add(item);
  81:   }
  82: 
  83:   public CategoryTreeItem[] getChilds()
  84:   {
  85:     if (childs == null)
  86:     {
  87:       return new CategoryTreeItem[0];
  88:     }
  89:     return (CategoryTreeItem[]) childs.toArray(new CategoryTreeItem[childs.size()]);
  90:   }
  91: 
  92:   /**
  93:    * Compares this object with the specified object for order.  Returns a
  94:    * negative integer, zero, or a positive integer as this object is less than,
  95:    * equal to, or greater than the specified object.<p>
  96:    * <p/>
  97:    *
  98:    * @param o the Object to be compared.
  99:    * @return a negative integer, zero, or a positive integer as this object is
 100:    *         less than, equal to, or greater than the specified object.
 101:    * @throws ClassCastException if the specified object's type prevents it from
 102:    *                            being compared to this Object.
 103:    */
 104:   public int compareTo(final Object o)
 105:   {
 106:     final CategoryTreeItem other = (CategoryTreeItem) o;
 107:     final int position = category.getPosition();
 108:     final int otherPosition = other.getCategory().getPosition();
 109:     if (position < otherPosition)
 110:     {
 111:       return -1;
 112:     }
 113:     if (position > otherPosition)
 114:     {
 115:       return 1;
 116:     }
 117:     return name.compareTo(other.name);
 118:   }
 119: 
 120: }