Demonstrates TableTreeViewer 2 : TableTree « 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 » TableTreeScreenshots 
Demonstrates TableTreeViewer 2

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TableTreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;

/**
 * This class demonstrates TableTreeViewer.
 */
public class PlayerTableTree extends ApplicationWindow {
  // The TableTreeViewer
  private TableTreeViewer ttv;

  /**
   * PlayerTableTree constructor
   */
  public PlayerTableTree() {
    super(null);
  }

  /**
   * Runs the application
   */
  public void run() {
    // Don't return from open() until window closes
    setBlockOnOpen(true);

    // Open the main window
    open();

    // Dispose the display
    Display.getCurrent().dispose();
  }

  /**
   * Configures the shell
   
   @param shell
   *            the shell
   */
  protected void configureShell(Shell shell) {
    super.configureShell(shell);
    shell.setText("Team Tree");
  }

  /**
   * Creates the main window's contents
   
   @param parent
   *            the main window
   @return Control
   */
  protected Control createContents(Composite parent) {
    // Create the table viewer to display the players
    ttv = new TableTreeViewer(parent);
    ttv.getTableTree().setLayoutData(new GridData(GridData.FILL_BOTH));

    // Set the content and label providers
    ttv.setContentProvider(new PlayerTreeContentProvider());
    ttv.setLabelProvider(new PlayerTreeLabelProvider());
    ttv.setInput(new PlayerTableModel());

    // Set up the table
    Table table = ttv.getTableTree().getTable();
    new TableColumn(table, SWT.LEFT).setText("First Name");
    new TableColumn(table, SWT.LEFT).setText("Last Name");
    new TableColumn(table, SWT.RIGHT).setText("Points");
    new TableColumn(table, SWT.RIGHT).setText("Rebounds");
    new TableColumn(table, SWT.RIGHT).setText("Assists");

    // Expand everything
    ttv.expandAll();

    // Pack the columns
    for (int i = 0, n = table.getColumnCount(); i < n; i++) {
      table.getColumn(i).pack();
    }

    // Turn on the header and the lines
    table.setHeaderVisible(true);
    table.setLinesVisible(true);

    // Pack the window
    parent.pack();

    // Scroll to top
    ttv.reveal(ttv.getElementAt(0));

    return ttv.getTableTree();
  }

  /**
   * The application entry point
   
   @param args
   *            the command line arguments
   */
  public static void main(String[] args) {
    new PlayerTableTree().run();
  }
}

/**
 * This class provides the content for the TableTreeViewer in PlayerTableTree
 */

class PlayerTreeContentProvider implements ITreeContentProvider {
  private static final Object[] EMPTY = new Object[] {};

  /**
   * Gets the children for a team or player
   
   @param arg0
   *            the team or player
   @return Object[]
   */
  public Object[] getChildren(Object arg0) {
    if (arg0 instanceof Team)
      return ((Teamarg0).getPlayers().toArray();
    // Players have no children . . . except Shawn Kemp
    return EMPTY;
  }

  /**
   * Gets the parent team for a player
   
   @param arg0
   *            the player
   @return Object
   */
  public Object getParent(Object arg0) {
    return ((Playerarg0).getTeam();
  }

  /**
   * Gets whether this team or player has children
   
   @param arg0
   *            the team or player
   @return boolean
   */
  public boolean hasChildren(Object arg0) {
    return getChildren(arg0).length > 0;
  }

  /**
   * Gets the elements for the table
   
   @param arg0
   *            the model
   @return Object[]
   */
  public Object[] getElements(Object arg0) {
    // Returns all the teams in the model
    return ((PlayerTableModelarg0).teams;
  }

  /**
   * Disposes any resources
   */
  public void dispose() {
    // We don't create any resources, so we don't dispose any
  }

  /**
   * Called when the input changes
   
   @param arg0
   *            the parent viewer
   @param arg1
   *            the old input
   @param arg2
   *            the new input
   */
  public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
    // Nothing to do
  }
}

/**
 * This class provides the labels for the PlayerTableTree application
 */

class PlayerTreeLabelProvider extends PlayerLabelProvider {
  /**
   * Gets the image for the specified column
   
   @param arg0
   *            the player or team
   @param arg1
   *            the column
   @return Image
   */
  public Image getColumnImage(Object arg0, int arg1) {
    // Teams have no image
    if (arg0 instanceof Player)
      return super.getColumnImage(arg0, arg1);
    return null;
  }

  /**
   * Gets the text for the specified column
   
   @param arg0
   *            the player or team
   @param arg1
   *            the column
   @return String
   */
  public String getColumnText(Object arg0, int arg1) {
    if (arg0 instanceof Player)
      return super.getColumnText(arg0, arg1);
    Team team = (Teamarg0;
    return arg1 == ? team.getYear() " " + team.getName() "";
  }
}

/**
 * This class contains the data model for the PlayerTable
 */

class PlayerTableModel {
  public Team[] teams;

  /**
   * Constructs a PlayerTableModel Fills the model with data
   */
  public PlayerTableModel() {
    teams = new Team[3];

    teams[0new Team("Celtics""1985-86");
    teams[0].add(new Player("Larry""Bird"25.8f9.8f6.8f));
    teams[0].add(new Player("Kevin""McHale"21.3f8.1f2.7f));
    teams[0].add(new Player("Robert""Parish"16.1f9.5f1.8f));
    teams[0].add(new Player("Dennis""Johnson"15.6f3.4f5.8f));
    teams[0].add(new Player("Danny""Ainge"10.7f2.9f5.1f));
    teams[0].add(new Player("Scott""Wedman"8.0f2.4f1.1f));
    teams[0].add(new Player("Bill""Walton"7.6f6.8f2.1f));
    teams[0].add(new Player("Jerry""Sichting"6.5f1.3f2.3f));
    teams[0].add(new Player("David""Thirdkill"3.3f1.4f0.3f));
    teams[0].add(new Player("Sam""Vincent"3.2f0.8f1.2f));
    teams[0].add(new Player("Sly""Williams"2.8f2.5f0.3f));
    teams[0].add(new Player("Rick""Carlisle"2.6f1.0f1.4f));
    teams[0].add(new Player("Greg""Kite"1.3f2.0f1.3f));

    teams[1new Team("Bulls""1995-96");
    teams[1].add(new Player("Michael""Jordan"30.4f6.6f4.3f));
    teams[1].add(new Player("Scottie""Pippen"19.4f6.4f5.9f));
    teams[1].add(new Player("Toni""Kukoc"13.1f4.0f3.5f));
    teams[1].add(new Player("Luc""Longley"9.1f5.1f1.9f));
    teams[1].add(new Player("Steve""Kerr"8.4f1.3f2.3f));
    teams[1].add(new Player("Ron""Harper"7.4f2.7f2.6f));
    teams[1].add(new Player("Dennis""Rodman"5.5f14.9f2.5f));
    teams[1].add(new Player("Bill""Wennington"5.3f2.5f0.6f));
    teams[1].add(new Player("Jack""Haley"5.0f2.0f0.0f));
    teams[1].add(new Player("John""Salley"4.4f3.3f1.3f));
    teams[1].add(new Player("Jud""Buechler"3.8f1.5f0.8f));
    teams[1].add(new Player("Dickey""Simpkins"3.6f2.6f0.6f));
    teams[1].add(new Player("James""Edwards"3.5f1.4f0.4f));
    teams[1].add(new Player("Jason""Caffey"3.2f1.9f0.4f));
    teams[1].add(new Player("Randy""Brown"2.7f1.0f1.1f));

    teams[2new Team("Lakers""1987-1988");
    teams[2].add(new Player("Magic""Johnson"23.9f6.3f12.2f));
    teams[2].add(new Player("James""Worthy"19.4f5.7f2.8f));
    teams[2].add(new Player("Kareem""Abdul-Jabbar"17.5f6.7f2.6f));
    teams[2].add(new Player("Byron""Scott"17.0f3.5f3.4f));
    teams[2].add(new Player("A.C.""Green"10.8f7.8f1.1f));
    teams[2].add(new Player("Michael""Cooper"10.5f3.1f4.5f));
    teams[2].add(new Player("Mychal""Thompson"10.1f4.1f0.8f));
    teams[2].add(new Player("Kurt""Rambis"5.7f5.8f0.8f));
    teams[2].add(new Player("Billy""Thompson"5.6f2.9f1.0f));
    teams[2].add(new Player("Adrian""Branch"4.3f1.7f0.5f));
    teams[2].add(new Player("Wes""Matthews"4.2f0.9f2.0f));
    teams[2].add(new Player("Frank""Brickowski"4.0f2.6f0.3f));
    teams[2].add(new Player("Mike""Smrek"2.2f1.1f0.1f));
  }
}

/**
 * This class represents a player
 */

class Player {
  private Team team;

  private String lastName;

  private String firstName;

  private float points;

  private float rebounds;

  private float assists;

  /**
   * Constructs an empty Player
   */
  public Player() {
    this(null, null, 0.0f0.0f0.0f);
  }

  /**
   * Constructs a Player
   
   @param firstName
   *            the first name
   @param lastName
   *            the last name
   @param points
   *            the points
   @param rebounds
   *            the rebounds
   @param assists
   *            the assists
   */
  public Player(String firstName, String lastName, float points,
      float rebounds, float assists) {
    setFirstName(firstName);
    setLastName(lastName);
    setPoints(points);
    setRebounds(rebounds);
    setAssists(assists);
  }

  /**
   * Sets the team for theo player
   
   @param team
   *            the team
   */
  public void setTeam(Team team) {
    this.team = team;
  }

  /**
   * Gets the assists
   
   @return float
   */
  public float getAssists() {
    return assists;
  }

  /**
   * Sets the assists
   
   @param assists
   *            The assists to set.
   */
  public void setAssists(float assists) {
    this.assists = assists;
  }

  /**
   * Gets the first name
   
   @return String
   */
  public String getFirstName() {
    return firstName;
  }

  /**
   * Sets the first name
   
   @param firstName
   *            The firstName to set.
   */
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }

  /**
   * Gets the last name
   
   @return String
   */
  public String getLastName() {
    return lastName;
  }

  /**
   * Sets the last name
   
   @param lastName
   *            The lastName to set.
   */
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }

  /**
   * Gets the points
   
   @return float
   */
  public float getPoints() {
    return points;
  }

  /**
   * Sets the points
   
   @param points
   *            The points to set.
   */
  public void setPoints(float points) {
    this.points = points;
  }

  /**
   * Gets the rebounds
   
   @return float
   */
  public float getRebounds() {
    return rebounds;
  }

  /**
   * Sets the rebounds
   
   @param rebounds
   *            The rebounds to set.
   */
  public void setRebounds(float rebounds) {
    this.rebounds = rebounds;
  }

  /**
   * Gets the team
   
   @return Team
   */
  public Team getTeam() {
    return team;
  }

  /**
   * Returns whether this player led the team in the specified category
   
   @param column
   *            the column (category)
   @return boolean
   */
  public boolean ledTeam(int column) {
    return team.led(this, column);
  }
}
/**
 * This class represents a team
 */

class Team {
  private String name;

  private String year;

  private List players;

  /**
   * Constructs a Team
   
   @param name
   *            the name
   @param year
   *            the year
   */
  public Team(String name, String year) {
    this.name = name;
    this.year = year;
    players = new LinkedList();
  }

  /**
   * Gets the name
   
   @return String
   */
  public String getName() {
    return name;
  }

  /**
   * Gets the year
   
   @return String
   */
  public String getYear() {
    return year;
  }

  /**
   * Adds a player
   
   @param player
   *            the player to add
   @return boolean
   */
  public boolean add(Player player) {
    boolean added = players.add(player);
    if (added)
      player.setTeam(this);
    return added;
  }

  /**
   * Gets the players
   
   @return List
   */
  public List getPlayers() {
    return Collections.unmodifiableList(players);
  }

  /**
   * Returns whether the specified player led his team in the specified
   * category
   
   @param player
   *            the player
   @param column
   *            the category
   @return boolean
   */
  public boolean led(Player player, int column) {
    boolean led = true;

    // Go through all the players on the team, comparing the specified
    // player's
    // stats with each other player.
    for (int i = 0, n = players.size(); i < n && led; i++) {
      Player test = (Playerplayers.get(i);
      if (player == test)
        continue;
      switch (column) {
      case PlayerConst.COLUMN_POINTS:
        if (player.getPoints() < test.getPoints())
          led = false;
        break;
      case PlayerConst.COLUMN_REBOUNDS:
        if (player.getRebounds() < test.getRebounds())
          led = false;
        break;
      case PlayerConst.COLUMN_ASSISTS:
        if (player.getAssists() < test.getAssists())
          led = false;
        break;
      }
    }
    return led;
  }
}

/**
 * This class contains constants for the PlayerTable application
 */

class PlayerConst {
  // Column constants
  public static final int COLUMN_FIRST_NAME = 0;

  public static final int COLUMN_LAST_NAME = 1;

  public static final int COLUMN_POINTS = 2;

  public static final int COLUMN_REBOUNDS = 3;

  public static final int COLUMN_ASSISTS = 4;
}

/**
 * This class provides the labels for PlayerTable
 */

class PlayerLabelProvider implements ITableLabelProvider {
  // Image to display if the player led his team
  private Image ball;

  // Constructs a PlayerLabelProvider
  public PlayerLabelProvider() {
    // Create the image
    try {
      ball = new Image(null, new FileInputStream("images/ball.png"));
    catch (FileNotFoundException e) {
      // Swallow it
    }
  }

  /**
   * Gets the image for the specified column
   
   @param arg0
   *            the player
   @param arg1
   *            the column
   @return Image
   */
  public Image getColumnImage(Object arg0, int arg1) {
    Player player = (Playerarg0;
    Image image = null;
    switch (arg1) {
    // A player can't lead team in first name or last name
    case PlayerConst.COLUMN_POINTS:
    case PlayerConst.COLUMN_REBOUNDS:
    case PlayerConst.COLUMN_ASSISTS:
      if (player.ledTeam(arg1))
        // Set the image
        image = ball;
      break;
    }
    return image;
  }

  /**
   * Gets the text for the specified column
   
   @param arg0
   *            the player
   @param arg1
   *            the column
   @return String
   */
  public String getColumnText(Object arg0, int arg1) {
    Player player = (Playerarg0;
    String text = "";
    switch (arg1) {
    case PlayerConst.COLUMN_FIRST_NAME:
      text = player.getFirstName();
      break;
    case PlayerConst.COLUMN_LAST_NAME:
      text = player.getLastName();
      break;
    case PlayerConst.COLUMN_POINTS:
      text = String.valueOf(player.getPoints());
      break;
    case PlayerConst.COLUMN_REBOUNDS:
      text = String.valueOf(player.getRebounds());
      break;
    case PlayerConst.COLUMN_ASSISTS:
      text = String.valueOf(player.getAssists());
      break;
    }
    return text;
  }

  /**
   * Adds a listener
   
   @param arg0
   *            the listener
   */
  public void addListener(ILabelProviderListener arg0) {
    // Throw it away
  }

  /**
   * Dispose any created resources
   */
  public void dispose() {
    // Dispose the image
    if (ball != null)
      ball.dispose();
  }

  /**
   * Returns whether the specified property, if changed, would affect the
   * label
   
   @param arg0
   *            the player
   @param arg1
   *            the property
   @return boolean
   */
  public boolean isLabelProperty(Object arg0, String arg1) {
    return false;
  }

  /**
   * Removes the specified listener
   
   @param arg0
   *            the listener
   */
  public void removeListener(ILabelProviderListener arg0) {
    // Do nothing
  }
}

           
       
Related examples in the same category
1. SWT Table Tree
2. Demonstrates TableTree (Table Tree)Demonstrates TableTree (Table 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.