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.IStructuredContentProvider;
import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerSorter;
import org.eclipse.jface.window.ApplicationWindow;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
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;
public class MainClass extends ApplicationWindow {
private StudentTableModel model = new StudentTableModel();
private TableViewer tv;
public MainClass() {
super(null);
setBlockOnOpen(true);
open();
Display.getCurrent().dispose();
}
protected void configureShell(Shell shell) {
super.configureShell(shell);
shell.setSize(400, 400);
}
protected Control createContents(Composite parent) {
Composite composite = new Composite(parent, SWT.NONE);
composite.setLayout(new GridLayout(1, false));
Combo combo = new Combo(composite, SWT.READ_ONLY);
combo.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
tv = new TableViewer(composite);
tv.setContentProvider(new PlayerContentProvider());
tv.setLabelProvider(new PlayerLabelProvider());
tv.setSorter(new StudentViewerSorter());
Table table = tv.getTable();
table.setLayoutData(new GridData(GridData.FILL_BOTH));
TableColumn tc = new TableColumn(table, SWT.LEFT);
tc.setText("First Name");
tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((StudentViewerSorter) tv.getSorter()).doSort(Const.COLUMN_FIRST_NAME);
tv.refresh();
}
});
tc = new TableColumn(table, SWT.LEFT);
tc.setText("Last Name");
tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((StudentViewerSorter) tv.getSorter()).doSort(Const.COLUMN_LAST_NAME);
tv.refresh();
}
});
tc = new TableColumn(table, SWT.RIGHT);
tc.setText("Math");
tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((StudentViewerSorter) tv.getSorter()).doSort(Const.COLUMN_MATH);
tv.refresh();
}
});
tc = new TableColumn(table, SWT.RIGHT);
tc.setText("CHEM");
tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((StudentViewerSorter) tv.getSorter()).doSort(Const.COLUMN_CHEM);
tv.refresh();
}
});
tc = new TableColumn(table, SWT.RIGHT);
tc.setText("AVE");
tc.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
((StudentViewerSorter) tv.getSorter()).doSort(Const.COLUMN_AVE);
tv.refresh();
}
});
for (int i = 0, n = model.teams.length; i < n; i++) {
combo.add(model.teams[i].getName());
}
combo.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
update(model.teams[((Combo) event.widget).getSelectionIndex()]);
}
});
combo.select(0);
update(model.teams[0]);
for (int i = 0, n = table.getColumnCount(); i < n; i++) {
table.getColumn(i).pack();
}
table.setHeaderVisible(true);
table.setLinesVisible(true);
return composite;
}
private void update(HighSchool team) {
getShell().setText(team.getYear() + " " + team.getName());
tv.setInput(team);
}
public static void main(String[] args) {
new MainClass();
}
}
class StudentTableModel {
public HighSchool[] teams;
public StudentTableModel() {
teams = new HighSchool[3];
teams[0] = new HighSchool("school 1", "1985-86");
teams[0].add(new Student("A", "a", 25.8f, 9.8f, 6.8f));
teams[0].add(new Student("B", "b", 21.3f, 8.1f, 2.7f));
teams[0].add(new Student("C", "c", 16.1f, 9.5f, 1.8f));
teams[1] = new HighSchool("school 2", "1995-96");
teams[1].add(new Student("Michael", "Jordan", 30.4f, 6.6f, 4.3f));
teams[1].add(new Student("Scottie", "Pippen", 19.4f, 6.4f, 5.9f));
teams[1].add(new Student("Toni", "Kukoc", 13.1f, 4.0f, 3.5f));
teams[2] = new HighSchool("scholl 3", "1987-1988");
teams[2].add(new Student("Magic", "Johnson", 23.9f, 6.3f, 12.2f));
teams[2].add(new Student("James", "Worthy", 19.4f, 5.7f, 2.8f));
}
}
class PlayerLabelProvider implements ITableLabelProvider {
private Image ball;
public PlayerLabelProvider() {
try {
ball = new Image(null, new FileInputStream("images/ball.png"));
} catch (FileNotFoundException e) {
}
}
public Image getColumnImage(Object arg0, int arg1) {
Student player = (Student) arg0;
Image image = null;
switch (arg1) {
case Const.COLUMN_MATH:
case Const.COLUMN_CHEM:
case Const.COLUMN_AVE:
if (player.ledTeam(arg1))
image = ball;
break;
}
return image;
}
public String getColumnText(Object arg0, int arg1) {
Student player = (Student) arg0;
String text = "";
switch (arg1) {
case Const.COLUMN_FIRST_NAME:
text = player.getFirstName();
break;
case Const.COLUMN_LAST_NAME:
text = player.getLastName();
break;
case Const.COLUMN_MATH:
text = String.valueOf(player.getMath());
break;
case Const.COLUMN_CHEM:
text = String.valueOf(player.getChem());
break;
case Const.COLUMN_AVE:
text = String.valueOf(player.getAve());
break;
}
return text;
}
public void addListener(ILabelProviderListener arg0) {
}
public void dispose() {
if (ball != null)
ball.dispose();
}
public boolean isLabelProperty(Object arg0, String arg1) {
return false;
}
public void removeListener(ILabelProviderListener arg0) {
}
}
class PlayerContentProvider implements IStructuredContentProvider {
public Object[] getElements(Object arg0) {
return ((HighSchool) arg0).getPlayers().toArray();
}
public void dispose() {
}
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
}
}
class Const {
public static final int COLUMN_FIRST_NAME = 0;
public static final int COLUMN_LAST_NAME = 1;
public static final int COLUMN_MATH = 2;
public static final int COLUMN_CHEM = 3;
public static final int COLUMN_AVE = 4;
}
class HighSchool {
private String name;
private String year;
private List players;
public HighSchool(String name, String year) {
this.name = name;
this.year = year;
players = new LinkedList();
}
public String getName() {
return name;
}
public String getYear() {
return year;
}
public boolean add(Student player) {
boolean added = players.add(player);
if (added)
player.setSchool(this);
return added;
}
public List getPlayers() {
return Collections.unmodifiableList(players);
}
public boolean led(Student player, int column) {
boolean led = true;
for (int i = 0, n = players.size(); i < n && led; i++) {
Student test = (Student) players.get(i);
if (player == test)
continue;
switch (column) {
case Const.COLUMN_MATH:
if (player.getMath() < test.getMath())
led = false;
break;
case Const.COLUMN_CHEM:
if (player.getChem() < test.getChem())
led = false;
break;
case Const.COLUMN_AVE:
if (player.getAve() < test.getAve())
led = false;
break;
}
}
return led;
}
}
class Student {
private HighSchool team;
private String lastName;
private String firstName;
private float math;
private float chem;
private float ave;
public Student() {
this(null, null, 0.0f, 0.0f, 0.0f);
}
public Student(String firstName, String lastName, float m, float c, float a) {
setFirstName(firstName);
setLastName(lastName);
setMath(m);
setChem(c);
setAve(a);
}
public void setSchool(HighSchool team) {
this.team = team;
}
public float getAve() {
return ave;
}
public void setAve(float assists) {
this.ave = assists;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public float getMath() {
return math;
}
public void setMath(float points) {
this.math = points;
}
public float getChem() {
return chem;
}
public void setChem(float rebounds) {
this.chem = rebounds;
}
public HighSchool getTeam() {
return team;
}
public boolean ledTeam(int column) {
return team.led(this, column);
}
}
class StudentViewerSorter extends ViewerSorter {
private static final int ASCENDING = 0;
private static final int DESCENDING = 1;
private int column;
private int direction;
public void doSort(int column) {
if (column == this.column) {
direction = 1 - direction;
} else {
this.column = column;
direction = ASCENDING;
}
}
public int compare(Viewer viewer, Object e1, Object e2) {
int rc = 0;
Student p1 = (Student) e1;
Student p2 = (Student) e2;
switch (column) {
case Const.COLUMN_FIRST_NAME:
rc = collator.compare(p1.getFirstName(), p2.getFirstName());
break;
case Const.COLUMN_LAST_NAME:
rc = collator.compare(p1.getLastName(), p2.getLastName());
break;
case Const.COLUMN_MATH:
rc = p1.getMath() > p2.getMath() ? 1 : -1;
break;
case Const.COLUMN_CHEM:
rc = p1.getChem() > p2.getChem() ? 1 : -1;
break;
case Const.COLUMN_AVE:
rc = p1.getAve() > p2.getAve() ? 1 : -1;
break;
}
if (direction == DESCENDING)
rc = -rc;
return rc;
}
}