playertabletree.java
来自「java的swt图形程序」· Java 代码 · 共 710 行 · 第 1/2 页
JAVA
710 行
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 ((Team) arg0).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 ((Player) arg0).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 ((PlayerTableModel) arg0).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 = (Team) arg0;
return arg1 == 0 ? 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[0] = new Team("Celtics", "1985-86");
teams[0].add(new Player("Larry", "Bird", 25.8f, 9.8f, 6.8f));
teams[0].add(new Player("Kevin", "McHale", 21.3f, 8.1f, 2.7f));
teams[0].add(new Player("Robert", "Parish", 16.1f, 9.5f, 1.8f));
teams[0].add(new Player("Dennis", "Johnson", 15.6f, 3.4f, 5.8f));
teams[0].add(new Player("Danny", "Ainge", 10.7f, 2.9f, 5.1f));
teams[0].add(new Player("Scott", "Wedman", 8.0f, 2.4f, 1.1f));
teams[0].add(new Player("Bill", "Walton", 7.6f, 6.8f, 2.1f));
teams[0].add(new Player("Jerry", "Sichting", 6.5f, 1.3f, 2.3f));
teams[0].add(new Player("David", "Thirdkill", 3.3f, 1.4f, 0.3f));
teams[0].add(new Player("Sam", "Vincent", 3.2f, 0.8f, 1.2f));
teams[0].add(new Player("Sly", "Williams", 2.8f, 2.5f, 0.3f));
teams[0].add(new Player("Rick", "Carlisle", 2.6f, 1.0f, 1.4f));
teams[0].add(new Player("Greg", "Kite", 1.3f, 2.0f, 1.3f));
teams[1] = new Team("Bulls", "1995-96");
teams[1].add(new Player("Michael", "Jordan", 30.4f, 6.6f, 4.3f));
teams[1].add(new Player("Scottie", "Pippen", 19.4f, 6.4f, 5.9f));
teams[1].add(new Player("Toni", "Kukoc", 13.1f, 4.0f, 3.5f));
teams[1].add(new Player("Luc", "Longley", 9.1f, 5.1f, 1.9f));
teams[1].add(new Player("Steve", "Kerr", 8.4f, 1.3f, 2.3f));
teams[1].add(new Player("Ron", "Harper", 7.4f, 2.7f, 2.6f));
teams[1].add(new Player("Dennis", "Rodman", 5.5f, 14.9f, 2.5f));
teams[1].add(new Player("Bill", "Wennington", 5.3f, 2.5f, 0.6f));
teams[1].add(new Player("Jack", "Haley", 5.0f, 2.0f, 0.0f));
teams[1].add(new Player("John", "Salley", 4.4f, 3.3f, 1.3f));
teams[1].add(new Player("Jud", "Buechler", 3.8f, 1.5f, 0.8f));
teams[1].add(new Player("Dickey", "Simpkins", 3.6f, 2.6f, 0.6f));
teams[1].add(new Player("James", "Edwards", 3.5f, 1.4f, 0.4f));
teams[1].add(new Player("Jason", "Caffey", 3.2f, 1.9f, 0.4f));
teams[1].add(new Player("Randy", "Brown", 2.7f, 1.0f, 1.1f));
teams[2] = new Team("Lakers", "1987-1988");
teams[2].add(new Player("Magic", "Johnson", 23.9f, 6.3f, 12.2f));
teams[2].add(new Player("James", "Worthy", 19.4f, 5.7f, 2.8f));
teams[2].add(new Player("Kareem", "Abdul-Jabbar", 17.5f, 6.7f, 2.6f));
teams[2].add(new Player("Byron", "Scott", 17.0f, 3.5f, 3.4f));
teams[2].add(new Player("A.C.", "Green", 10.8f, 7.8f, 1.1f));
teams[2].add(new Player("Michael", "Cooper", 10.5f, 3.1f, 4.5f));
teams[2].add(new Player("Mychal", "Thompson", 10.1f, 4.1f, 0.8f));
teams[2].add(new Player("Kurt", "Rambis", 5.7f, 5.8f, 0.8f));
teams[2].add(new Player("Billy", "Thompson", 5.6f, 2.9f, 1.0f));
teams[2].add(new Player("Adrian", "Branch", 4.3f, 1.7f, 0.5f));
teams[2].add(new Player("Wes", "Matthews", 4.2f, 0.9f, 2.0f));
teams[2].add(new Player("Frank", "Brickowski", 4.0f, 2.6f, 0.3f));
teams[2].add(new Player("Mike", "Smrek", 2.2f, 1.1f, 0.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.0f, 0.0f, 0.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
*
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?