Category Tree : Tree « SWT JFace Eclipse « Java

Java
1. 2D Graphics GUI
2. 3D
3. Advanced Graphics
4. Ant
5. Apache Common
6. Chart
7. Class
8. Collections Data Structure
9. Data Type
10. Database SQL JDBC
11. Design Pattern
12. Development Class
13. EJB3
14. Email
15. Event
16. File Input Output
17. Game
18. Generics
19. GWT
20. Hibernate
21. I18N
22. J2EE
23. J2ME
24. JDK 6
25. JNDI LDAP
26. JPA
27. JSP
28. JSTL
29. Language Basics
30. Network Protocol
31. PDF RTF
32. Reflection
33. Regular Expressions
34. Scripting
35. Security
36. Servlets
37. Spring
38. Swing Components
39. Swing JFC
40. SWT JFace Eclipse
41. Threads
42. Tiny Application
43. Velocity
44. Web Services SOA
45. XML
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java » SWT JFace Eclipse » TreeScreenshots 
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(300200);
    shell.setSize(300200);
    
    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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.