Category Tree : Tree « SWT JFace Eclipse « Java






Category Tree


/******************************************************************************
 * Copyright (c) 1998, 2004 Jackwind Li Guojie
 * All right reserved. 
 * 
 * Created on Dec 28, 2003 7:56:40 PM by JACK
 * $Id$
 * 
 * visit: http://www.asprise.com/swt
 *****************************************************************************/

import java.util.Vector;

import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;

public class CategoryTree {
  Display display = new Display();
  Shell shell = new Shell(display);
  
  final Tree tree = new Tree(shell, SWT.BORDER);
  
  Vector categories = new Vector();
  
  public CategoryTree() {
    Category category = new Category("Java libraries", null);
    categories.add(category);
    
    category = new Category("UI Toolkits", category);
    new Category("AWT", category);
    new Category("Swing", category);
    new Category("SWT/JFace", category);
    
    category = new Category("Java IDEs", null);
    categories.add(category);
    
    new Category("Eclipse", category);
    new Category("JBuilder", category);
  }
  
  /**
   * Builds up the tree with traditional approach. 
   *
   */
  public void traditional() {
    for(int i=0; categories != null && i < categories.size(); i++) {
      Category category = (Category)categories.elementAt(i);
      addCategory(null, category);
    }
  }
  
  /**
   * Adds a category to the tree (recursively).
   * @param parentItem
   * @param category
   */
  private void addCategory(TreeItem parentItem, Category category) {
    TreeItem item = null;
    if(parentItem == null) 
      item = new TreeItem(tree, SWT.NONE);
    else
      item = new TreeItem(parentItem, SWT.NONE);
    
    item.setText(category.getName());
    
    Vector subs = category.getSubCategories();
    for(int i=0; subs != null && i < subs.size(); i++)
      addCategory(item, (Category)subs.elementAt(i));
  }
  
  /**
   * Builds up the tree with MVC approach. 
   *
   */
  public void MVC() {
    
    TreeViewer treeViewer = new TreeViewer(tree);
    
    treeViewer.setContentProvider(new ITreeContentProvider() {
      public Object[] getChildren(Object parentElement) {
        Vector subcats = ((Category)parentElement).getSubCategories();
        return subcats == null ? new Object[0] : subcats.toArray();
      }
      
      public Object getParent(Object element) {
        return ((Category)element).getParent();
      }
      
      public boolean hasChildren(Object element) {
        return ((Category)element).getSubCategories() != null;
      }
      
      public Object[] getElements(Object inputElement) {
        if(inputElement != null && inputElement instanceof Vector) {
          return ((Vector)inputElement).toArray();
        }
        return new Object[0];
      }
      
      public void dispose() {
        // 
      }
      
      public void inputChanged(Viewer viewer,
                   Object oldInput,
                   Object newInput) {
        // 
      }
    });
    
    treeViewer.setLabelProvider(new LabelProvider() {
      public String getText(Object element) {
        return ((Category)element).getName();
      }
    });
    
    treeViewer.setInput(categories);
    
  }
  
  public void show() {
    tree.setSize(300, 200);
    shell.setSize(300, 200);
    
    shell.open();
    
    // Set up the event loop.
    while (!shell.isDisposed()) {
      if (!display.readAndDispatch()) {
        // If no more entries in event queue
        display.sleep();
      }
    }

    display.dispose();
  }
  
  public static void main(String[] args) {
    CategoryTree tree = new CategoryTree();
    //tree.traditional();
    tree.MVC();
    tree.show();
  }

  /**
   * Represents a category of items. 
   * The max level of categories is 2 only.
   *
   */
  class Category {
    private String name;
    private Vector subCategories;
    private Category parent;
    
    public Category(String name, Category parent) {
      this.name = name;
      this.parent = parent;
      if(parent != null)
        parent.addSubCategory(this);
    }
    
    public Vector getSubCategories() {
      return subCategories;
    }
    
    private void addSubCategory(Category subcategory) {
      if(subCategories == null)
        subCategories = new Vector();
      if(! subCategories.contains(subcategory))
        subCategories.add(subcategory);
    }
    
    public String getName() {
      return name;
    }
    
    public Category getParent() {
      return parent;
    }
  }  
  
}


           
       








Related examples in the same category

1.SWT Tree With Multi columnsSWT Tree With Multi columns
2.Detect Mouse Down In SWT Tree ItemDetect Mouse Down In SWT Tree Item
3.Limit selection to items that match a pattern in SWT TreeLimit selection to items that match a pattern in SWT Tree
4.SWT Tree Simple DemoSWT Tree Simple Demo
5.Simple TreeSimple Tree
6.Bookmark OrganizerBookmark Organizer
7.Displays a single-selection tree, a multi-selection tree, and a checkbox treeDisplays a single-selection tree, a multi-selection tree, and a checkbox tree
8.Demonstrates TableTreeDemonstrates TableTree
9.Demonstrates TreeEditorDemonstrates TreeEditor
10.Tree ExampleTree Example
11.Tree Example 2
12.Demonstrates CheckboxTreeViewer
13.Demonstrates TreeViewerDemonstrates TreeViewer
14.SWT Tree SWT Tree
15.SWT Tree Composite
16.Print selected items in a SWT treePrint selected items in a SWT tree
17.Insert a SWT tree item (at an index)Insert a SWT tree item (at an index)
18.Detect a selection or check event in a tree (SWT.CHECK)Detect a selection or check event in a tree (SWT.CHECK)
19.Create a SWT tree (lazy)Create a SWT tree (lazy)
20.Create a treeCreate a tree