playertabletree.java

来自「java的swt图形程序」· Java 代码 · 共 710 行 · 第 1/2 页

JAVA
710
字号
   * @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 = (Player) players.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 = (Player) arg0;
    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 = (Player) arg0;
    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
  }
}

           

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?